Exemple #1
0
        /// <summary>
        /// Appends a node to the graph
        /// </summary>
        public void AddNode(Node newObject)
        {
            if (!Valid)
            {
                throw new InvalidOperationException("Graph is declared invalid");
            }

            if (Final)
            {
                throw new InvalidOperationException("Graph can not be modified after set to immutable");
            }

            if (newObject.Parent != null)
            {
                throw new InvalidOperationException("Only root nodes can be added to the graph");
            }

            if (Multitrees.Count == MAX_AMOUNT_NODES)
            {
                throw new InvalidOperationException($"Graph can only store up to {MAX_AMOUNT_NODES} root nodes");
            }

            if (!newObject.Unique)
            {
                Multitrees.Add(newObject.CustomHashcode, newObject);
            }
            else if (Multitrees.TryGetValue(newObject.GetHashCode(), out Node existingNode))
            {
                existingNode.Merge(newObject);
            }
            else
            {
                Multitrees.Add(newObject.GetHashCode(), newObject);
            }
        }
Exemple #2
0
        /// <summary>
        /// Merges two graphs together.
        /// The function takes care of detecting duplicates and adjusting the related edges. Note that this method is kept internal
        /// because users of this library shall use the merge processor class for graph merging operations only. The second graph
        /// should not be used after merged into this graph which is why its declared invalid at the end of the method
        /// </summary>
        /// <param name="secondGraph">The graph from which all nodes and edges should be inherited. Note that this graph will become invalid
        /// after this operation</param>
        internal void Merge(Graph secondGraph)
        {
            if (secondGraph == null)
            {
                throw new ArgumentNullException("Second graph may not be null");
            }

            if (!Valid || !secondGraph.Valid)
            {
                throw new InvalidOperationException("Unable to merge graphs that are declared as invalid");
            }

            if (Final)
            {
                throw new InvalidOperationException("Graph can not be modified after set to immutable");
            }

            foreach (var nodeToAdd in secondGraph.Multitrees)
            {
                if (Multitrees.TryGetValue(nodeToAdd.Key, out Node existingNode))
                {
                    existingNode.Merge(nodeToAdd.Value);
                }
                else
                {
                    AddNode(nodeToAdd.Value.CreateReplacement());
                }
            }

            foreach (var adjacentGroup in secondGraph.AdjacencyList.Groups)
            {
                foreach (var edge in adjacentGroup.Value.Edges)
                {
                    AddEdge(edge.Value);
                }
            }

            secondGraph.Invalidate();
        }