示例#1
0
 protected PoolingQueue()
 {
     Count         = 0;
     _enqueueIndex = 0;
     _dequeueIndex = 0;
     _enqueueTo    = _dequeueFrom = null;
 }
        public T Pop()
        {
            if (IsEmpty)
            {
                throw new IndexOutOfRangeException();
            }

            _topIndex--;

            if (_topIndex < 0)
            {
                _topIndex = PoolsDefaults.DefaultPoolBucketSize - 1;
                var oldTop = _top;
                _top = _top.Next;
                oldTop.Dispose();
            }

            var obj = _top[_topIndex];

            _top[_topIndex] = default;

            Count--;

            return(obj);
        }
 public void Clear()
 {
     while (_top != null)
     {
         var next = _top.Next;
         _top.Dispose();
         _top = next;
     }
 }
示例#4
0
        public void Clear()
        {
            while (_enqueueTo != null)
            {
                var next = _enqueueTo.Next;
                _enqueueTo.Dispose();
                _enqueueTo = next;
            }

            _dequeueFrom = null;
        }
示例#5
0
        public void Enqueue(T obj)
        {
            if (Count == 0 && _enqueueTo == null)
            {
                _enqueueTo = _dequeueFrom = CreateNodeHolder();
            }

            _enqueueTo[_enqueueIndex] = obj;
            _enqueueIndex++;
            Count++;

            if (_enqueueIndex == PoolsDefaults.DefaultPoolBucketSize)
            {
                var enqueue = _enqueueTo;
                _enqueueTo    = CreateNodeHolder();
                enqueue.Next  = _enqueueTo;
                _enqueueIndex = 0;
            }
        }
        public void Push(T obj)
        {
            if (Count == 0 && _top == null)
            {
                _top = CreateNodeHolder();
            }

            _top[_topIndex] = obj;
            _topIndex++;
            Count++;

            if (_topIndex == PoolsDefaults.DefaultPoolBucketSize)
            {
                var top = _top;
                _top      = CreateNodeHolder();
                _top.Next = top;
                _topIndex = 0;
            }
        }
示例#7
0
        public T Dequeue()
        {
            if (IsEmpty)
            {
                throw new IndexOutOfRangeException();
            }

            var obj = _dequeueFrom[_dequeueIndex];

            _dequeueFrom[_dequeueIndex] = default;

            _dequeueIndex++;
            Count--;

            if (_dequeueIndex == PoolsDefaults.DefaultPoolBucketSize)
            {
                var dequeue = _dequeueFrom;
                _dequeueFrom  = _dequeueFrom.Next;
                _dequeueIndex = 0;
                dequeue.Dispose();
            }

            if (Count == 0)
            {
                // return back to pool
                if (_enqueueTo != _dequeueFrom)
                {
                    var empty = _dequeueFrom;
                    _dequeueFrom  = _dequeueFrom.Next;
                    _dequeueIndex = 0;
                    empty.Dispose();
                }
                else
                // reset to pool start
                {
                    _enqueueIndex = 0;
                    _dequeueIndex = 0;
                }
            }

            return(obj);
        }
 protected PoolingStackBase()
 {
     Count     = 0;
     _topIndex = 0;
     _top      = null;
 }