Exemple #1
0
 //--- Methods ---
 public string Convert(XDoc html)
 {
     if(html == null || html.IsEmpty) {
         return "";
     }
     var state = new VisitState();
     var body = html.HasName("body") ? html : html["body[not(@target)]"];
     foreach(var node in body.VisitOnly(x => IncludeNode(x, state), x => CheckBlock(x, state))) {
         if(CheckBlock(node, state)) {
             continue;
         }
         switch(node.AsXmlNode.NodeType) {
         case XmlNodeType.Whitespace:
         case XmlNodeType.SignificantWhitespace:
         case XmlNodeType.CDATA:
         case XmlNodeType.Text:
             state.Append(node.AsText);
             break;
         }
     }
     return state.ToString().Trim();
 }
Exemple #2
0
        public static IEnumerable <SyntaxNode> Tokens(this SyntaxNode root, TextSpan span, Func <SyntaxNode, bool> descendIntoChildren = null)
        {
            VisitState currentState = new VisitState()
            {
                node = root
            };
            Stack <VisitState> stateStack = new Stack <VisitState>();

            stateStack.Push(currentState);

            bool foundFirst = false;

            while (stateStack.Count != 0)
            {
                currentState = stateStack.Pop();

                if (currentState.i == 0 && currentState.node.SlotCount == 0)
                {
                    if (currentState.node.FullSpan.OverlapsWith(span))
                    {
                        foundFirst = true;
                        yield return(currentState.node);
                    }
                }
                else if (currentState.i != 0 || (descendIntoChildren?.Invoke(currentState.node) != false))
                {
                    if (!foundFirst && currentState.i == 0)
                    {
                        int offset;
                        currentState.node.GetIndexAndOffset(span.Start - currentState.node.Start, out currentState.i, out offset);
                        if (currentState.i > 0)
                        {
                            // The element is the first element to start after the start position. We want
                            // the first element containing the start position so back track by one to ensure that is
                            // included.
                            currentState.i--;
                        }
                    }

                    while (currentState.i < currentState.node.SlotCount)
                    {
                        var child = currentState.node.GetNodeSlot(currentState.i);
                        currentState.i++;

                        if (child != null)
                        {
                            var childSpan = child.Span;
                            if (childSpan.Start > span.End)
                            {
                                break;
                            }
                            else if (childSpan.End < span.Start)
                            {
                                continue;
                            }
                            else
                            {
                                stateStack.Push(currentState);
                                stateStack.Push(new VisitState()
                                {
                                    node = child
                                });
                                break;
                            }
                        }
                    }
                }
            }
        }
Exemple #3
0
 public Coords(int rowIdx, int colIdx, VisitState visitState = VisitState.UNVISITED)
 {
     RowIdx     = rowIdx;
     ColIdx     = colIdx;
     VisitState = visitState;
 }
        public void TestConnectedMultipleSantaGraph()
        {
            const int numberOfDays   = 2;
            const int numberOfVisit  = 9;
            const int numberOfSantas = 1;

            bool[,] santas = new bool[numberOfDays, numberOfSantas] {
                { true }, { true }
            };
            int[] visitsDuration = new int[numberOfVisit] {
                1, 1, 1, 1, 1, 1, 1, 1, 1
            };
            VisitState[,] visitStates = new VisitState[numberOfDays, numberOfVisit] {
                { VisitState.Default, VisitState.Default, VisitState.Default, VisitState.Default, VisitState.Default, VisitState.Default, VisitState.Default, VisitState.Default, VisitState.Default },
                { VisitState.Default, VisitState.Default, VisitState.Default, VisitState.Default, VisitState.Default, VisitState.Default, VisitState.Default, VisitState.Default, VisitState.Default }
            };

            int[,] distances = new int[numberOfVisit, numberOfVisit]
            {
                { 0, 2, 4, 5, 5, 2, 4, 5, 5 },   // v0
                { 2, 0, 2, 3, 3, 4, 6, 7, 7 },   // v1
                { 4, 2, 0, 1, 1, 6, 8, 9, 9 },   // v2
                { 5, 3, 1, 0, 1, 7, 9, 10, 10 }, // v3
                { 5, 3, 1, 1, 0, 7, 9, 10, 10 }, // v4
                { 2, 4, 6, 7, 7, 0, 2, 3, 3 },   // v5
                { 4, 6, 8, 9, 9, 2, 0, 1, 1 },   // v6
                { 5, 7, 9, 10, 10, 3, 1, 0, 1 }, // v7
                { 5, 7, 9, 10, 10, 3, 1, 1, 0 }, // v8
            };

            //     4 - 3
            //     \_2_/
            //       |
            //       |
            //       1
            //       |
            //       |
            //       0 V0
            //       |
            //       |
            //       5
            //       |
            //       |
            //      _6_
            //     /   \
            //     8 - 7

            int[] dayDuration = new int[numberOfDays] {
                17, 17
            };

            var solverInputData = new SolverInputData(santas, visitsDuration, visitStates, distances, dayDuration, null)
            {
                VisitIds = new long[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }
            };
            var solver = new ClusteringILPSolver(solverInputData);

            solver.Solve(0, 60000);
            var result = solver.GetResult();

            Assert.IsNotNull(result);
            var possibleRoutes = new[] {
                "0 | 0;5 | 1;6 | 2;7 | 3;8 | 4",
                "0 | 0;5 | 1;6 | 2;8 | 3;7 | 4",
                "0 | 0;5 | 1;7 | 2;8 | 3;6 | 4",
                "0 | 0;5 | 1;8 | 2;7 | 3;6 | 4",


                "0 | 0;1 | 1;2 | 2;3 | 3;4 | 4",
                "0 | 0;1 | 1;2 | 2;4 | 3;3 | 4",
            };

            var route1 = string.Join(";", result.Waypoints[0, 0]);
            var route2 = string.Join(";", result.Waypoints[0, 1]);

            Assert.IsTrue(possibleRoutes.Contains(route1));
            Assert.IsTrue(possibleRoutes.Contains(route2));
            Assert.AreNotEqual(route1, route2);
        }
Exemple #5
0
 public void SetTemporary() => _mark = VisitState.Temporary;
Exemple #6
0
 public void SetPermanent() => _mark = VisitState.Permanent;
Exemple #7
0
 public void UpdateVisitState(VisitState visitState)
 {
     // no implementation for now
     _context.Entry(visitState).State = EntityState.Modified;
 }
Exemple #8
0
 public Meta()
 {
     state = VisitState.undiscovered;
 }
Exemple #9
0
 public void VisitInnerToOuter <StateType>(VisitState <StateType> aVisitor) where StateType : State
 {
     VisitStates <StateType>(aVisitor, CreateReverseIterator(mStateStack));
 }
Exemple #10
0
 public void VisitOuterToInner <StateType>(VisitState <StateType> aVisitor) where StateType : State
 {
     VisitStates <StateType>(aVisitor, mStateStack);
 }
Exemple #11
0
            void Visit(BlockInfo info)
            {
                // This method used to be recursive but to prevent stack overflows,
                // it's not recursive anymore.

                VisitState state = new VisitState(info);

recursive_call:
                if (state.Info.baseBlock == firstBlock)
                {
                    throw new ApplicationException("Can't visit firstBlock");
                }
                stack.Push(state.Info);
                state.Info.onStack   = true;
                state.Info.dfsNumber = dfsNumber;
                state.Info.low       = dfsNumber;
                dfsNumber++;

                state.Targets     = GetTargets(state.Info.baseBlock);
                state.TargetIndex = 0;
return_to_caller:
                for (; state.TargetIndex < state.Targets.Count; state.TargetIndex++)
                {
                    state.TargetInfo = GetInfo(state.Targets[state.TargetIndex]);
                    if (state.TargetInfo == null)
                    {
                        continue;
                    }
                    if (state.TargetInfo.baseBlock == firstBlock)
                    {
                        continue;
                    }

                    if (!state.TargetInfo.Visited())
                    {
                        visitStateStack.Push(state);
                        state = new VisitState(state.TargetInfo);
                        goto recursive_call;
                    }
                    else if (state.TargetInfo.onStack)
                    {
                        state.Info.low = Math.Min(state.Info.low, state.TargetInfo.dfsNumber);
                    }
                }

                if (state.Info.low != state.Info.dfsNumber)
                {
                    goto return_from_method;
                }
                var sccBlocks = new List <BaseBlock>();

                while (true)
                {
                    var poppedInfo = stack.Pop();
                    poppedInfo.onStack = false;
                    sccBlocks.Add(poppedInfo.baseBlock);
                    if (ReferenceEquals(state.Info, poppedInfo))
                    {
                        break;
                    }
                }
                if (sccBlocks.Count > 1)
                {
                    sccBlocks.Reverse();
                    var result = new Sorter(scopeBlock, sccBlocks, true).Sort();
                    SortLoopBlock(result);
                    sorted.InsertRange(0, result);
                }
                else
                {
                    sorted.Insert(0, sccBlocks[0]);
                }

return_from_method:
                if (visitStateStack.Count == 0)
                {
                    return;
                }
                state          = visitStateStack.Pop();
                state.Info.low = Math.Min(state.Info.low, state.TargetInfo.low);
                state.TargetIndex++;
                goto return_to_caller;
            }
Exemple #12
0
 /// <summary>
 /// Reset the abstract intepretation state of this block. It does this by putting the iterations to 0 and the pre and post states to null
 /// </summary>
 public void ResetAbstractInterpretationState()
 {
     //      this.currentlyTraversed = false;
       this.TraversingStatus = VisitState.ToVisit;
       this.iterations = 0;
 }
Exemple #13
0
 public Block(IToken tok, string/*!*/ label, List<Cmd>/*!*/ cmds, TransferCmd transferCmd)
     : base(tok)
 {
     Contract.Requires(label != null);
       Contract.Requires(cmds != null);
       Contract.Requires(tok != null);
       this.Label = label;
       this.Cmds = cmds;
       this.TransferCmd = transferCmd;
       this.Predecessors = new List<Block>();
       this.liveVarsBefore = null;
       this.TraversingStatus = VisitState.ToVisit;
       this.iterations = 0;
 }
Exemple #14
0
        private bool IncludeNode(XDoc node, VisitState state)
        {
            // node is not a filtered element and does not have the noindex class
            var skip = _removeElements.Contains(node.Name);
            skip = skip || node["@class"].Contents.ContainsInvariant("noindex");
            skip = skip || (node.Name.EqualsInvariantIgnoreCase("div") && node["@class"].Contents.EqualsInvariant("mt-dekiscript-error"));

            // record the node as filtered depending on the include state
            state.BeingFiltered = skip ? node.AsXmlNode : null;
            return !skip;
        }
Exemple #15
0
 private bool CheckBlock(XDoc node, VisitState state) {
     if(!(node.AsXmlNode is XmlElement)) {
         return false;
     }
     if(node.AsXmlNode != state.BeingFiltered    // if the current node is not being filtered
         && !_inlineElements.Contains(node.Name) // and it's not an inline element
     ) {
         state.Break();
     }
     return true;
 }
Exemple #16
0
			void Visit(BlockInfo info) {
				// This method used to be recursive but to prevent stack overflows,
				// it's not recursive anymore.

				VisitState state = new VisitState(info);
recursive_call:
				if (state.Info.baseBlock == firstBlock)
					throw new ApplicationException("Can't visit firstBlock");
				stack.Push(state.Info);
				state.Info.onStack = true;
				state.Info.dfsNumber = dfsNumber;
				state.Info.low = dfsNumber;
				dfsNumber++;

				state.Targets = GetTargets(state.Info.baseBlock);
				state.TargetIndex = 0;
return_to_caller:
				for (; state.TargetIndex < state.Targets.Count; state.TargetIndex++) {
					state.TargetInfo = GetInfo(state.Targets[state.TargetIndex]);
					if (state.TargetInfo == null)
						continue;
					if (state.TargetInfo.baseBlock == firstBlock)
						continue;

					if (!state.TargetInfo.Visited()) {
						visitStateStack.Push(state);
						state = new VisitState(state.TargetInfo);
						goto recursive_call;
					}
					else if (state.TargetInfo.onStack)
						state.Info.low = Math.Min(state.Info.low, state.TargetInfo.dfsNumber);
				}

				if (state.Info.low != state.Info.dfsNumber)
					goto return_from_method;
				var sccBlocks = new List<BaseBlock>();
				while (true) {
					var poppedInfo = stack.Pop();
					poppedInfo.onStack = false;
					sccBlocks.Add(poppedInfo.baseBlock);
					if (ReferenceEquals(state.Info, poppedInfo))
						break;
				}
				if (sccBlocks.Count > 1) {
					sccBlocks.Reverse();
					var result = new Sorter(scopeBlock, sccBlocks, true).Sort();
					SortLoopBlock(result);
					sorted.InsertRange(0, result);
				}
				else {
					sorted.Insert(0, sccBlocks[0]);
				}

return_from_method:
				if (visitStateStack.Count == 0)
					return;
				state = visitStateStack.Pop();
				state.Info.low = Math.Min(state.Info.low, state.TargetInfo.low);
				state.TargetIndex++;
				goto return_to_caller;
			}
Exemple #17
0
        private bool IncludeNode(XDoc node, VisitState state) {
            var include = true;
            if(node.AsXmlNode is XmlElement && _removeElements.Contains(node.Name)) {

                // node is a filtered element
                include = false;
            } else {
                var nodeClass = node["@class"].AsText;
                if(nodeClass != null) {

                    // node is an element that might have the noindex class
                    include = !nodeClass.EqualsInvariant("noindex");
                }
            }

            // record the node as filtered depending on the include state
            state.BeingFiltered = include ? null : node.AsXmlNode;
            return include;
        }
Exemple #18
0
 public NodeWrapper(T node)
 {
     Node  = node;
     _mark = VisitState.None;
 }
        public static void Visit(
            SyntaxNode node,
            int windowStart,
            int windowLength,
            Action <int, int, SyntaxNode, XmlClassificationTypes> resultCollector)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            int start     = 0;
            int windowEnd = windowStart + windowLength;

            VisitState currentState = CreateState(node);

            Stack <VisitState> stateStack = new Stack <VisitState>();

            stateStack.Push(currentState);

            while (stateStack.Count != 0)
            {
                currentState = stateStack.Pop();

AfterPopCurrentState:
                if (currentState.continueInsideForLoop)
                {
                    currentState.continueInsideForLoop = false;
                    currentState.i++;
                    goto ForLoop;
                }

                kindMap.TryGetValue(currentState.node.Kind, out currentState.childTypes);

                currentState.i = 0;

ForLoop:
                for (; currentState.i < currentState.node.SlotCount; currentState.i++)
                {
                    if (start > windowEnd)
                    {
                        return;
                    }

                    currentState.child = currentState.node.GetSlot(currentState.i);
                    if (currentState.child == null)
                    {
                        continue;
                    }

                    int currentStart  = Math.Max(start, windowStart);
                    int currentLength = Math.Min(windowEnd, start + currentState.child.FullWidth) - currentStart;
                    if (currentLength >= 0)
                    {
                        currentState.childType = currentState.childTypes == null ? XmlClassificationTypes.None : currentState.childTypes[currentState.i];

                        if (currentState.childType == XmlClassificationTypes.None)
                        {
                            if (currentState.child.Kind == SyntaxKind.XmlTextLiteralToken)
                            {
                                currentState.childType = XmlClassificationTypes.XmlText;
                            }
                            else if (currentState.child.Kind == SyntaxKind.XmlEntityLiteralToken)
                            {
                                currentState.childType = XmlClassificationTypes.XmlEntityReference;
                            }
                        }

                        if (currentState.childType == XmlClassificationTypes.None)
                        {
                            currentState.continueInsideForLoop = true;
                            stateStack.Push(currentState);

                            currentState = CreateState(
                                currentState.child);
                            goto AfterPopCurrentState;
                        }
                        else
                        {
                            if (currentLength > 0)
                            {
                                var returnNode = currentState.child;
                                if (start > windowEnd)
                                {
                                    return;
                                }

                                if (currentStart + currentLength >= windowStart)
                                {
                                    resultCollector(
                                        currentStart,
                                        currentLength,
                                        returnNode,
                                        currentState.childType);
                                }
                            }
                        }
                    }

                    start += currentState.child.FullWidth;
                }
            }
        }
Exemple #20
0
        protected void DoSearch()
        {
            while (SearchQueue.Count > 0)
            {
                NodeType node = SearchQueue[0];
                SearchQueue.RemoveAt(0);
                switch (IsVisited[node])
                {
                case VisitState.Unvisited:
                    OnDiscoverNode(node);
                    break;

                case VisitState.Discovered:
                    break;

                case VisitState.Visiting:
                    throw new Exception("BUG: start is Visiting and on the SearchQueue.");

                case VisitState.Finished:
                    // this happens if we SearchFrom a Finished node.
                    continue;
                }
                // node was previously Unvisited or Discovered
                IsVisited[node] = VisitState.Visiting;
                foreach (NodeType target in Successors(node))
                {
                    Edge <NodeType> edge = new Edge <NodeType>(node, target);
                    OnDiscoverEdge(edge);
                    VisitState targetIsVisited = IsVisited[target];
                    switch (targetIsVisited)
                    {
                    case VisitState.Unvisited:
                        IsVisited[target] = VisitState.Discovered;
                        SearchQueue.Add(target);
                        OnDiscoverNode(target);
                        // tree edge
                        OnTreeEdge(edge);
                        break;

                    case VisitState.Visiting:
                        // back edge
                        OnBackEdge(edge);
                        break;

                    case VisitState.Discovered:
                        // cross edge
                        OnCrossEdge(edge);
                        break;

                    case VisitState.Finished:
                        // cross edge
                        OnCrossEdge(edge);
                        break;
                    }
                }
                IsVisited[node] = VisitState.Finished;
                OnFinishNode(node);
                if (stopped)
                {
                    SearchQueue.Clear();
                    stopped = false;
                    break;
                }
            }
        }