Ejemplo n.º 1
0
        private static bool TryGetEndpoints(Graph graph, Endpoints edge, out Endpoints endpoints)
        {
            bool hasTail = graph.TryGetTail(edge, out int tail);
            bool hasHead = graph.TryGetHead(edge, out int head);

            endpoints = new Endpoints(tail, head);
            return(hasTail && hasHead);
        }
        private void EnumerateVerticesCore(Graph graph, bool multipleSource)
        {
            Debug.Assert(graph != null, "graph != null");

            // Arrange

            byte[] mapBackingStore = ArrayPool <byte> .Shared.Rent(Math.Max(graph.VertexCount, 1));

            Array.Clear(mapBackingStore, 0, mapBackingStore.Length);
            IndexedColorDictionary eagerColorMap = new(mapBackingStore);
            ConcurrentQueue <int>  fringe        = new();

            byte[] setBackingStore = ArrayPool <byte> .Shared.Rent(Math.Max(graph.VertexCount, 1));

            Array.Clear(setBackingStore, 0, setBackingStore.Length);
            IndexedSet set = new(setBackingStore);

            using Rist <int> eagerSteps      = new(graph.VertexCount);
            using Rist <int> enumerableSteps = new(graph.VertexCount);
            BfsHandler <Graph, int, Endpoints> bfsHandler = CreateBfsHandler(eagerSteps);

            // Act

            if (multipleSource)
            {
                if (graph.VertexCount < 3)
                {
                    return;
                }

                int             sourceCount = graph.VertexCount / 3;
                IndexEnumerator sources     = new(sourceCount);

                EagerBfs.Traverse(graph, sources, eagerColorMap, bfsHandler);
                using IEnumerator <int> vertices = GenericSearch.EnumerateVertices(graph, sources, fringe, set);
                enumerableSteps.AddEnumerator(vertices);
            }
            else
            {
                int source = graph.VertexCount >> 1;
                EagerBfs.Traverse(graph, source, eagerColorMap, bfsHandler);
                using IEnumerator <int> vertices = GenericSearch.EnumerateVertices(graph, source, fringe, set);
                enumerableSteps.AddEnumerator(vertices);
            }

            // Assert

            int eagerStepCount      = eagerSteps.Count;
            int enumerableStepCount = enumerableSteps.Count;

            Assert.Equal(eagerStepCount, enumerableStepCount);

            int count = eagerStepCount;

            for (int i = 0; i < count; ++i)
            {
                int eagerStep      = eagerSteps[i];
                int enumerableStep = enumerableSteps[i];

                if (eagerStep == enumerableStep)
                {
                    continue;
                }

                Assert.Equal(eagerStep, enumerableStep);
            }

            // Cleanup

            ArrayPool <byte> .Shared.Return(mapBackingStore);

            ArrayPool <byte> .Shared.Return(setBackingStore);
        }