Ejemplo n.º 1
0
    public void TestAllocateThenPopTwice()
    {
        var queue = new PooledQueue <int>();


        var span1 = queue.AllocateRight(2);

        span1[0] = 0;
        span1[1] = 1;

        Assert.Equal(0, queue.PopLeft());
        Assert.Equal(1, queue.PopLeft());


        var span2 = queue.AllocateRight(2);

        span2[0] = 2;
        span2[1] = 3;

        Assert.Equal(2, queue.PopLeft());
        Assert.Equal(3, queue.PopLeft());


        queue.Dispose();
    }
Ejemplo n.º 2
0
    public void TestAllocateThenPop()
    {
        var queue = new PooledQueue <int>();

        var span = queue.AllocateRight(3);

        span[0] = 0;
        span[1] = 1;
        span[2] = 2;

        Assert.Equal(0, queue.PopLeft());
        Assert.Equal(1, queue.PopLeft());
        Assert.Equal(2, queue.PopLeft());

        queue.Dispose();
    }
    /// <summary>
    /// Yields all of the nodes in the tree represented by <paramref name="value"/> in a breadth-first traversal order.
    ///
    /// <para>
    /// This is a breadth-first pre-order traversal.
    /// </para>
    ///
    /// </summary>
    /// <typeparam name="T">The rewritable tree type</typeparam>
    /// <param name="rewriter">The rewriter</param>
    /// <param name="value">The value to traverse</param>
    /// <returns>An enumerable containing all of the nodes in the tree represented by <paramref name="value"/> in a breadth-first traversal order.</returns>
    public static IEnumerable <T> SelfAndDescendantsBreadthFirst <T>(this IRewriter <T> rewriter, T value)
    {
        if (rewriter == null)
        {
            throw new ArgumentNullException(nameof(rewriter));
        }

        IEnumerable <T> Iterator()
        {
            var queue = new PooledQueue <T>();

            queue.AllocateRight(1)[0] = value;

            try
            {
                while (queue.Count != 0)
                {
                    var x = queue.PopLeft();

                    yield return(x);

                    var count = rewriter.CountChildren(x);
                    var span  = queue.AllocateRight(count);
                    rewriter.GetChildren(span, x);
                }
            }
            finally
            {
                queue.Dispose();
            }
        }

        return(Iterator());
    }
Ejemplo n.º 4
0
    public void TestFillBufferThenRefill()
    {
        var queue = new PooledQueue <int>();

        var span1 = queue.AllocateRight(512);

        span1.Fill(1);
        for (var i = 0; i < 512; i++)
        {
            Assert.Equal(1, queue.PopLeft());
        }

        var span2 = queue.AllocateRight(512);

        span2.Fill(2);
        for (var i = 0; i < 512; i++)
        {
            Assert.Equal(2, queue.PopLeft());
        }

        queue.Dispose();
    }
Ejemplo n.º 5
0
    public void TestOverfillBuffer()
    {
        var queue = new PooledQueue <int>();

        var span1 = queue.AllocateRight(513);

        span1.Fill(1);
        for (var i = 0; i < 513; i++)
        {
            Assert.Equal(1, queue.PopLeft());
        }

        queue.Dispose();
    }