예제 #1
0
        public void DeserializeStackInterface()
        {
            string json = @"[
  ""One"",
  ""II"",
  ""3""
]";

            IImmutableStack <string> data = JsonSerializer.Deserialize <IImmutableStack <string> >(json);

            Assert.False(data.IsEmpty);
            Assert.Equal("3", data.Peek());
            data = data.Pop();
            Assert.Equal("II", data.Peek());
            data = data.Pop();
            Assert.Equal("One", data.Peek());
        }
예제 #2
0
        public static IImmutableStack <T> Pop <T>(this IImmutableStack <T> stack, out T value)
        {
            Requires.NotNull(stack, "stack");
            Contract.Ensures(Contract.Result <IImmutableStack <T> >() != null);

            value = stack.Peek();
            return(stack.Pop());
        }
예제 #3
0
        public GameState Undo()
        {
            var moves = _moves.Pop();

            return(moves.IsEmpty
                ? this
                : new GameState(moves, CursorPosition));
        }
예제 #4
0
        public IEnumerator <T> GetEnumerator()
        {
            IImmutableStack <T> stack = this;

            do
            {
                yield return(stack.Peek());

                stack = stack.Pop();
            }while (stack != Empty);
        }
예제 #5
0
 public static IImmutableStack <T> Pop <T>(
     this IImmutableStack <T> stack,
     out T value)
 {
     if (stack == null)
     {
         throw new ArgumentNullException(nameof(stack));
     }
     value = stack.Peek();
     return(stack.Pop());
 }
예제 #6
0
        public static IImmutableStack <T> Reverse <T>(this IImmutableStack <T> stack)
        {
            var reverse = Empty <T>();

            while (!stack.IsEmpty)
            {
                reverse = reverse.Push(stack.Peek());
                stack   = stack.Pop();
            }
            return(reverse);
        }
예제 #7
0
 public static Tuple <IImmutableStack <T>, Option <T> > pop <T>(IImmutableStack <T> stack)
 {
     try
     {
         T   value;
         var newStack = stack.Pop(out value);
         return(tuple(newStack, Some(value)));
     }
     catch (InvalidOperationException)
     {
         return(tuple(stack, Option <T> .None));
     }
 }
예제 #8
0
            bool IEnumerator.MoveNext()
            {
                if (current == null)
                {
                    current = this.start;
                }
                else if (!current.IsEmpty)
                {
                    current = current.Pop();
                }

                return(!current.IsEmpty);
            }
예제 #9
0
        public IImmutableQueue <T> Dequeue()
        {
            IImmutableStack <T> f = forwards.Pop();

            if (!f.IsEmpty)
            {
                return(new ImmutableQueue <T>(f, backwards));
            }
            if (backwards.IsEmpty)
            {
                return(Empty);
            }
            return(new ImmutableQueue <T>((IImmutableStack <T>)backwards.Reverse(), ImmutableStack <T> .Empty));
        }
 bool IEnumerator.MoveNext()
 {
     if (frontStack == null)
     {
         frontStack = start.frontStack;
         backStack  = start.backStack;
     }
     else if (!frontStack.IsEmpty)
     {
         frontStack = frontStack.Pop();
     }
     else if (!backStack.IsEmpty)
     {
         backStack = backStack.Pop();
     }
     return(!(frontStack.IsEmpty || backStack.IsEmpty));
 }
예제 #11
0
        public void PopOutValue()
        {
            var stack = ImmutableStack <int> .Empty.Push(5).Push(6);

            int top;

            stack = stack.Pop(out top);
            Assert.Equal(6, top);
            var empty = stack.Pop(out top);

            Assert.Equal(5, top);
            Assert.True(empty.IsEmpty);

            // Try again with the interface to verify extension method behavior.
            IImmutableStack <int> stackInterface = stack;

            Assert.Same(empty, stackInterface.Pop(out top));
            Assert.Equal(5, top);
        }
예제 #12
0
        static IImmutableStack <T> GetPrevElementInHierarchyCore <T>(IImmutableStack <T> rootStack, Func <T, IList <T> > getChildren, Func <IList <T>, T, int> indedOf) where T : class
        {
            var currentElement = rootStack.Peek();

            Func <T, IEnumerable <T> > getChildrenPath = element => LinqExtensions.Unfold(element, x => getChildren(x).LastOrDefault(), x => x == null);

            var parents = rootStack.Pop();
            var parent  = parents.FirstOrDefault();

            if (parent == null)
            {
                return(ImmutableStack.Empty <T>().PushMultiple(getChildrenPath(currentElement)));
            }

            var neighbors = getChildren(parent);
            var index     = indedOf(neighbors, currentElement);

            if (index > 0)
            {
                return(parents.PushMultiple(getChildrenPath(neighbors[index - 1])));
            }

            return(parents);
        }
예제 #13
0
        private static ImmutableStack <T> Reverse(IImmutableStack <T> stack)
        {
            ImmutableStack <T> immutableStack1 = ImmutableStack <T> .Empty;

            for (IImmutableStack <T> immutableStack2 = stack; !immutableStack2.IsEmpty; immutableStack2 = immutableStack2.Pop())
            {
                immutableStack1 = immutableStack1.Push(immutableStack2.Peek());
            }
            return(immutableStack1);
        }
예제 #14
0
 public static void Deconstruct <T>(this IImmutableStack <T> src, out T head, out IImmutableStack <T> tail)
 {
     head = src.Peek();
     tail = src.Pop();
 }