Exemplo n.º 1
0
        /// <summary>
        /// Find the <see cref="VertexNode"/> for a given edge, if it exists.
        /// </summary>
        /// <param name="graph">Graph to look in</param>
        /// <param name="fromVtx">Start Vertex</param>
        /// <param name="ToVtx">End Vertex, or null if we don't care</param>
        /// <returns>Pointer to the vertex node, if found</returns>
        /// <!-- Based off 3.1.1 -->
        public static VertexNode findNodeForEdge(
            ref VertexGraph graph,
            GeoCoord fromVtx,
            GeoCoord toVtx)
        {
            uint index         = _hashVertex(fromVtx, graph.res, graph.numBuckets);
            var  currentBucket = graph.buckets[(int)index];

            var nodeIndex = currentBucket.FindIndex(
                t => GeoCoord.geoAlmostEqual(t.from, fromVtx) &&
                (toVtx == null || GeoCoord.geoAlmostEqual(t.to, toVtx))
                );

            return(nodeIndex < 0
                ? null
                : currentBucket[nodeIndex]);
        }
Exemplo n.º 2
0
        /// <summary>Add an edge to the graph</summary>
        /// <param name="graph">Graph to add node to</param>
        /// <param name="fromVtx">Start vertex</param>
        /// <param name="toVtx">End vertex</param>
        /// <returns>new node</returns>
        /// <!-- Based off 3.1.1 -->
        public static VertexNode addVertexNode(ref VertexGraph graph, GeoCoord fromVtx,
                                               GeoCoord toVtx)
        {
            // Make the new node
            VertexNode node = _initVertexNode(fromVtx, toVtx);
            // Determine location
            var index = _hashVertex(fromVtx, graph.res, graph.numBuckets);
            // Check whether there's an existing node in that spot
            List <VertexNode> currentNode = graph.buckets[(int)index];

            if (currentNode.Count == 0)
            {
                // Set bucket to the new node
                graph.buckets[(int)index].Add(node);
            }
            else
            {
                //  Go through the list to make sure the
                //  edge doesn't already exist
                //
                //  NOTE: Later, use a Hashset
                foreach (var vertexNode in graph.buckets[(int)index])
                {
                    if (GeoCoord.geoAlmostEqual(vertexNode.from, fromVtx) &&
                        GeoCoord.geoAlmostEqual(vertexNode.to, toVtx))
                    {
                        //  already exists, bail.
                        return(vertexNode);
                    }
                }
                // Add the new node to the end of the list
                graph.buckets[(int)index].Add(node);
            }
            graph.size++;
            return(node);
        }