src/Stack.cs

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;

namespace Rapidity.Json
{
    internal class Stack<T>
    {
        private const int InitialCapacity = 8;
        private T[] _array;
        private int _index;
        public int Count => _index + 1;

        public Stack() : this(InitialCapacity)
        {
        }

        public Stack(int capacity)
        {
            if (capacity < 0) throw new ArgumentOutOfRangeException(nameof(capacity));
            _array = new T[capacity];
            _index = -1;
        }

        public void Push(T data)
        {
            if (_index + 1 >= _array.Length)
            {
                Array.Resize(ref _array, _array.Length == 0 ? InitialCapacity : _array.Length * 2);
            }
            _array[++_index] = data;
        }

        public T Pop()
        {
            if (Count <= 0) throw new InvalidOperationException("当前栈为空");
            var value = _array[_index];
            _array[_index] = default;
            _index--;
            return value;
        }

        public bool TryPop(out T value)
        {
            if (Count <= 0)
            {
                value = default;
                return false;
            }
            value = _array[_index];
            _array[_index] = default;
            _index--;
            return true;
        }

        public T Peek()
        {
            if (Count <= 0) throw new InvalidOperationException("当前栈为空");
            return _array[_index];
        }

        public bool TryPeek(out T value)
后面还有 34 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。