Пример #1
0
    public bool ExpandAll(SortedNodeList listaNodosAExplorar, Dictionary <Vector2, PathfindingNode> diccionarioNodos)
    {
        if (!diccionarioNodos.ContainsKey(position))
        {
            diccionarioNodos.Add(position, this);

            //Debug.Log("Expandiendo nodo " + position);

            // Si estamos en una posicion normal anyadimos los 4 vecinos al conjunto de nodos a explorar
            if (!Scenario.scenarioRef.isWarpPosition(position))
            {
                return(ExpandEach(listaNodosAExplorar, diccionarioNodos, new Vector2(position.x + 1, position.y)) ||
                       ExpandEach(listaNodosAExplorar, diccionarioNodos, new Vector2(position.x - 1, position.y)) ||
                       ExpandEach(listaNodosAExplorar, diccionarioNodos, new Vector2(position.x, position.y + 1)) ||
                       ExpandEach(listaNodosAExplorar, diccionarioNodos, new Vector2(position.x, position.y - 1)));
            }
            // Si nos encontramos en un posible nodo de portal (los 2 de los extremos) recalculamos el nodo resultante por el otro extremo
            else
            {
                if (position.x == 0)
                {
                    return(ExpandEach(listaNodosAExplorar, diccionarioNodos, new Vector2(Scenario.tamanyoMapaX - 1, position.y)) ||
                           ExpandEach(listaNodosAExplorar, diccionarioNodos, new Vector2(1, position.y)));
                }
                else
                {
                    return(ExpandEach(listaNodosAExplorar, diccionarioNodos, new Vector2(0, position.y)) ||
                           ExpandEach(listaNodosAExplorar, diccionarioNodos, new Vector2(Scenario.tamanyoMapaX - 2, position.y)));
                }
            }
        }
        return(false);
    }
Пример #2
0
 static void PrintList <T>(SortedNodeList <T> list) where T : IComparable
 {
     foreach (var node in list)
     {
         Console.Write(node + " ");
     }
 }
Пример #3
0
        private Node GetNextSibling(ArrayElementNode arrayElementNode)
        {
            ComplexNode           parent   = arrayElementNode.Parent as ComplexNode;
            SortedNodeList <Node> children = JsonHelpers.GetChildren(parent);

            return(parent != null?GetNextChild(arrayElementNode, children) : null);
        }
Пример #4
0
        public static Node GetNodeBeforePosition(int pos, ComplexNode parent)
        {
            Node node = null;
            SortedNodeList <Node> children = GetChildren(parent);
            int start = 0;

            if (children.Any())
            {
                start = children[0].Start;
            }

            if (start < pos)
            {
                int i = FindInsertIndex(children, pos) - 1;

                if (i >= 0)
                {
                    node = children[i];

                    if (node is ComplexNode complexNode)
                    {
                        // Recurse to find the deepest node
                        node = GetNodeBeforePosition(pos, complexNode);
                    }
                }
            }

            return(node);
        }
Пример #5
0
        [DataRow(170, 5)] // The last index
        public void JsonHelpers_FindInsertIndex_InvalidJson(int position, int expectedIndex)
        {
            Node complexNode = JsonNodeParser.Parse(InvalidJsonText).GetNodeSlot(0);
            SortedNodeList <Node> children = JsonHelpers.GetChildren(complexNode);
            int actualIndex = JsonHelpers.FindInsertIndex(children, position);

            Assert.AreEqual(expectedIndex, actualIndex);
        }
Пример #6
0
        private Node GetPreviousChild(Node child, SortedNodeList <Node> children)
        {
            int index = (child != null) ? children.IndexOf(child) : -1;

            if (index > 0)
            {
                return(children[index - 1]);
            }

            return(null);
        }
Пример #7
0
        private Node GetNextChild(Node child, SortedNodeList <Node> children)
        {
            int index = (child != null) ? children.IndexOf(child) : -1;

            if (index != -1 && index + 1 < children.Count)
            {
                return(children[index + 1]);
            }

            return(null);
        }
Пример #8
0
        public void JsonHelpers_GetChildren_ValidJson()
        {
            DocumentNode          documentNode  = JsonNodeParser.Parse(ValidJsonText);
            ObjectNode            topLevelValue = (ObjectNode)documentNode.TopLevelValue;
            SortedNodeList <Node> children      = JsonHelpers.GetChildren(topLevelValue);

            Assert.AreEqual(5, children.Count);
            Assert.AreEqual(NodeKind.OpenCurlyBrace, children[0].Kind);
            Assert.AreEqual(NodeKind.JSON_Member, children[1].Kind);
            Assert.AreEqual(NodeKind.JSON_Member, children[2].Kind);
            Assert.AreEqual(NodeKind.JSON_Member, children[3].Kind);
            Assert.AreEqual(NodeKind.CloseCurlyBrace, children[4].Kind);
        }
Пример #9
0
        static void Main()
        {
            #region Test create list

            // Создание двух односвязных сортированных списков с разной сортировкой:

            SortedNodeList <int> sorterList1 = new SortedNodeList <int>(SortedNodeList <int> .SortedNodeListType.SortAscending)
            {
                5, -15, 10, 1, 9, 4, -5, 1, -20, 50, -11, 0, 6
            };
            SortedNodeList <int> sorterList2 = new SortedNodeList <int>(SortedNodeList <int> .SortedNodeListType.SortDescending)
            {
                5, -15, 10, 1, 9, 4, -5, 1, -20, 50, -11, 0, 6
            };

            //Вывод на экран двух списков

            Console.Write("List sort Ascending: ");
            PrintList(sorterList1);
            Console.WriteLine();
            Console.Write("List sort Descending: ");
            PrintList(sorterList2);
            Console.WriteLine();

            #endregion

            #region Add and remove test

            sorterList1.Add(-101);
            sorterList2.Add(-102);

            sorterList1.RemoveElement(10);
            sorterList2.RemoveElement(10);

            Console.Write("List one: ");
            PrintList(sorterList1);
            Console.WriteLine();
            Console.Write("List two: ");
            PrintList(sorterList2);
            Console.WriteLine();



            #endregion



            Console.WriteLine("Finish");
            Console.ReadLine();
        }
Пример #10
0
        public static SortedNodeList <Node> GetChildren(Node node)
        {
            SortedNodeList <Node> children = new SortedNodeList <Node>();

            if (node != null)
            {
                for (int i = 0; i < node.SlotCount; i++)
                {
                    children.Add(node.GetNodeSlot(i));
                }
            }

            return(children);
        }
Пример #11
0
        private MemberNode GetLibraries(ITextBuffer textBuffer)
        {
            JsonEditorDocument document = JsonEditorDocument.FromTextBuffer(textBuffer);

            if (document != null)
            {
                Node topLevelNode = document.DocumentNode.TopLevelValue.FindType <ObjectNode>();
                SortedNodeList <Node>    topLevelNodeChildren = JsonHelpers.GetChildren(topLevelNode);
                IEnumerable <MemberNode> jsonMembers          = topLevelNodeChildren.OfType <MemberNode>();

                return(jsonMembers.FirstOrDefault(m => m.UnquotedNameText == ManifestConstants.Libraries));
            }

            return(null);
        }
Пример #12
0
        public static int FindInsertIndex(SortedNodeList <Node> nodes, int rangeStart)
        {
            int min = 0;
            int max = nodes.Count - 1;

            while (min <= max)
            {
                int mid   = (min + max) / 2;
                int start = nodes[mid].Start;

                if (rangeStart <= start)
                {
                    max = mid - 1;
                }
                else
                {
                    min = mid + 1;
                }
            }

            return(max + 1);
        }
Пример #13
0
        private void InsertIntoTextBuffer(IVsTextBuffer document, LibraryInstallationState libraryInstallationState, Manifest manifest)
        {
            ITextBuffer textBuffer = GetTextBuffer(document);

            if (textBuffer != null)
            {
                MemberNode            libraries = GetLibraries(textBuffer);
                SortedNodeList <Node> children  = JsonHelpers.GetChildren(libraries);

                if (children.Count > 0)
                {
                    ArrayNode arrayNode = children.OfType <ArrayNode>().First();

                    string newLibrary      = GetLibraryTextToBeInserted(libraryInstallationState, manifest);
                    bool   containsLibrary = arrayNode.BlockChildren.Any();

                    int insertionIndex = libraries.End - 1;

                    string lineBreakText = GetLineBreakTextFromPreviousLine(textBuffer, insertionIndex);

                    string insertionText;

                    if (containsLibrary)
                    {
                        insertionText = "," + lineBreakText + newLibrary + lineBreakText;
                    }
                    else
                    {
                        insertionText = newLibrary + lineBreakText;
                    }

                    if (insertionIndex > 0)
                    {
                        FormatSelection(textBuffer, insertionIndex, insertionText);
                    }
                }
            }
        }
        public override void Invoke(CancellationToken cancellationToken)
        {
            Telemetry.TrackUserTask("Invoke-UpdateSuggestedAction");

            if (_disabled)
            {
                return;
            }

            try
            {
                IDependencies   dependencies = _provider.DependenciesFactory.FromConfigFile(_provider.ConfigFilePath);
                IProvider       provider     = dependencies.GetProvider(_provider.InstallationState.ProviderId);
                ILibraryCatalog catalog      = provider?.GetCatalog();

                if (catalog == null)
                {
                    return;
                }

                SortedNodeList <Node> children = JsonHelpers.GetChildren(_provider.LibraryObject);
                MemberNode            member   = children.OfType <MemberNode>().FirstOrDefault(m => m.UnquotedNameText == ManifestConstants.Library);

                if (member != null)
                {
                    using (ITextEdit edit = TextBuffer.CreateEdit())
                    {
                        edit.Replace(new Span(member.Value.Start, member.Value.Width), "\"" + _updatedLibraryId + "\"");
                        edit.Apply();
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogEvent(ex.ToString(), LogLevel.Error);
                Telemetry.TrackException("UpdateSuggestedActionFailed", ex);
            }
        }
Пример #15
0
    public bool ExpandEach(SortedNodeList listaNodosAExplorar, Dictionary <Vector2, PathfindingNode> diccionarioNodos, Vector2 posicionAExplorar)
    {
        nodoExtraido = null;

        // Primero nos aseguramos de que estemos en una posicion dentro del mapa
        if (Scenario.scenarioRef.isWalkable(posicionAExplorar) && distance < 40)
        {
            diccionarioNodos.TryGetValue(posicionAExplorar, out nodoExtraido);

            // Si es la primera vez que alcanzamos ese nodo...
            if (nodoExtraido == null)
            {
                // Y no es el objetivo la anyadimos a la lista para explorar
                if (posicionAExplorar != targetPosition)
                {
                    nodoCreado = new PathfindingNode(posicionAExplorar, (short)(distance + 1), this);
                    nodoCreado.actualizarPesos();
                    listaNodosAExplorar.Add(nodoCreado);
                }
                // Si la posicion es la objetivo la encabezamos en la lista y detenemos la busqueda
                else
                {
                    nodoCreado = new PathfindingNode(posicionAExplorar, (short)(distance + 1), this);
                    listaNodosAExplorar.AddResultAtFirst(nodoCreado);
                    return(true);
                }
            }
            // Si hemos llegado a un caso mejor... reordenamos los punteros
            else if (distance + 1 < nodoExtraido.distance)
            {
                nodoExtraido.distance = (short)(distance + 1);
                nodoExtraido.parent   = this;
                nodoExtraido.actualizarPesos();
            }
        }

        return(false);
    }
Пример #16
0
        /// <summary>
        /// Generates all the possible edges for given nodes and returns them.
        /// </summary>
        /// <param name="listNodes">The list nodes.</param>
        /// <param name="listEdges">[out]The list edges.</param>
        /// <returns></returns>
        public static bool generateEdgeList(SortedNodeList listNodes,
                                            out SortedEdgeList listEdges)
        {
            bool bRet = false;
            SortedEdgeList l_listEdges = new SortedEdgeList();
            UInt32 nEdgeID = 0;

            for(Int32 nFrom = 0; nFrom < listNodes.Count; ++nFrom)
            {
                for (Int32 nTo = nFrom+1; nTo < listNodes.Count; ++nTo)
                {
                    // make sure it is sorted by distance.
                    GraphEdge e = new GraphEdge(++nEdgeID,
                                                listNodes[nFrom],
                                                listNodes[nTo],
                                                0);
                    l_listEdges.AddSorted(e);
                    bRet = true;
                }
            }
            listEdges = l_listEdges;
            return bRet;
        }
Пример #17
0
        public static bool TryGetInstallationState(ObjectNode parent, out ILibraryInstallationState installationState, string defaultProvider = null)
        {
            installationState = null;

            if (parent == null)
            {
                return(false);
            }

            var state = new LibraryInstallationStateOnDisk();

            SortedNodeList <Node> children = GetChildren(parent);

            string GetCanonicalizedValue(BlockChildNode m)
            {
                if (m.Value is TokenNode value)
                {
                    return(value.GetCanonicalizedText());
                }
                return(m.UnquotedValueText);
            }

            foreach (MemberNode child in children.OfType <MemberNode>())
            {
                // note: the Json parser escapes backslashes in the node's Value text,
                // so we need to unescape those so that they match the value from the manifest.
                switch (child.UnquotedNameText)
                {
                case ManifestConstants.Provider:
                    state.ProviderId = GetCanonicalizedValue(child);
                    break;

                case ManifestConstants.Library:
                    state.LibraryId = GetCanonicalizedValue(child);
                    break;

                case ManifestConstants.Destination:
                    state.DestinationPath = GetCanonicalizedValue(child);
                    break;

                case ManifestConstants.Files:
                    state.Files = (child.Value as ArrayNode)?.Elements.Select(e => GetCanonicalizedValue(e)).ToList();
                    break;
                }
            }

            children = GetChildren(parent.Parent?.FindType <ObjectNode>());
            IEnumerable <MemberNode> rootMembers = children?.OfType <MemberNode>();

            // Check for defaultProvider
            if (string.IsNullOrEmpty(state.ProviderId))
            {
                if (rootMembers != null)
                {
                    foreach (MemberNode child in rootMembers)
                    {
                        if (child.UnquotedNameText == "defaultProvider")
                        {
                            state.ProviderId = child.UnquotedValueText;
                        }
                    }
                }
            }

            // Check for defaultDestination
            if (string.IsNullOrEmpty(state.DestinationPath))
            {
                if (rootMembers != null)
                {
                    foreach (MemberNode child in rootMembers)
                    {
                        if (child.UnquotedNameText == ManifestConstants.DefaultDestination)
                        {
                            state.DestinationPath = child.UnquotedValueText;
                        }
                    }
                }
            }

            var converter = new LibraryStateToFileConverter(defaultProvider, defaultDestination: null);

            installationState = converter.ConvertToLibraryInstallationState(state);

            return(!string.IsNullOrEmpty(installationState.ProviderId));
        }
Пример #18
0
        public static bool TryGetInstallationState(ObjectNode parent, out ILibraryInstallationState installationState, string defaultProvider = null)
        {
            installationState = null;

            if (parent == null)
            {
                return(false);
            }

            var state = new LibraryInstallationStateOnDisk();

            SortedNodeList <Node> children = GetChildren(parent);

            foreach (MemberNode child in children.OfType <MemberNode>())
            {
                switch (child.UnquotedNameText)
                {
                case ManifestConstants.Provider:
                    state.ProviderId = child.UnquotedValueText;
                    break;

                case ManifestConstants.Library:
                    state.LibraryId = child.UnquotedValueText;
                    break;

                case ManifestConstants.Destination:
                    state.DestinationPath = child.UnquotedValueText;
                    break;

                case ManifestConstants.Files:
                    state.Files = (child.Value as ArrayNode)?.Elements.Select(e => e.UnquotedValueText).ToList();
                    break;
                }
            }

            children = GetChildren(parent.Parent?.FindType <ObjectNode>());
            IEnumerable <MemberNode> rootMembers = children?.OfType <MemberNode>();

            // Check for defaultProvider
            if (string.IsNullOrEmpty(state.ProviderId))
            {
                if (rootMembers != null)
                {
                    foreach (MemberNode child in rootMembers)
                    {
                        if (child.UnquotedNameText == "defaultProvider")
                        {
                            state.ProviderId = child.UnquotedValueText;
                        }
                    }
                }
            }

            // Check for defaultDestination
            if (string.IsNullOrEmpty(state.DestinationPath))
            {
                if (rootMembers != null)
                {
                    foreach (MemberNode child in rootMembers)
                    {
                        if (child.UnquotedNameText == ManifestConstants.DefaultDestination)
                        {
                            state.DestinationPath = child.UnquotedValueText;
                        }
                    }
                }
            }

            var converter = new LibraryStateToFileConverter(defaultProvider, defaultDestination: null);

            installationState = converter.ConvertToLibraryInstallationState(state);

            return(!string.IsNullOrEmpty(installationState.ProviderId));
        }
Пример #19
0
 public Graph()
 {
     m_listEdges = new SortedEdgeList();
     m_listNodes = new SortedNodeList();
 }