Beispiel #1
0
        public void Push(T value)
        {
            FastStackNode <T> bottom;

            FastStackNode <T> entry = new FastStackNode <T>
            {
                Value = value
            };

            // Atomically replace the bottom of the stack.
            while (true)
            {
                bottom     = _bottom;
                entry.Next = bottom;

                // Try to replace the pointer.
                if (Interlocked.CompareExchange(
                        ref _bottom,
                        entry,
                        bottom
                        ) == bottom)
                {
                    // Success.
                    break;
                }
            }
        }
Beispiel #2
0
        public T Peek()
        {
            FastStackNode <T> bottom = _bottom;

            if (bottom == null)
            {
                throw new InvalidOperationException("The stack is empty.");
            }

            return(bottom.Value);
        }
Beispiel #3
0
        public IEnumerator <T> GetEnumerator()
        {
            FastStackNode <T> entry = _bottom;

            // Start the enumeration.
            while (entry != null)
            {
                yield return(entry.Value);

                entry = entry.Next;
            }
        }
Beispiel #4
0
        public T Pop()
        {
            // Atomically replace the bottom of the stack.
            while (true)
            {
                FastStackNode <T> bottom = _bottom;

                // If the bottom of the stack is null, the
                // stack is empty.
                if (bottom == null)
                {
                    throw new InvalidOperationException("The stack is empty.");
                }

                // Try to replace the pointer.
                if (Interlocked.CompareExchange(ref _bottom, bottom.Next, bottom) == bottom)
                {
                    // Success.
                    Interlocked.Decrement(ref _count);
                    return(bottom.Value);
                }
            }
        }