Exemplo n.º 1
0
        public Buffer(int capacity)
        {
            _bufferLock = new object();

            Count    = 0;
            Capacity = capacity;
            Elements = new BufferElement <TValue> [capacity];

            for (var index = 0; index < Elements.Length; index++)
            {
                Elements[index] = new BufferElement <TValue>(this);
            }
        }
Exemplo n.º 2
0
        internal void Deactivate(BufferElement <TValue> bufferElement)
        {
            lock (_bufferLock)
            {
                if (!bufferElement.Active)
                {
                    return;
                }

                bufferElement.Active = false;
                bufferElement.Dispose();
                Count--;
            }
        }
Exemplo n.º 3
0
        public BufferElement <TValue> Activate()
        {
            lock (_bufferLock)
            {
                if (ActiveCount == Capacity)
                {
                    if (ActiveCount == Count)
                    {
                        var elements = new BufferElement <TValue> [Capacity *= 2];

                        Array.Copy(Elements, elements, Count);

                        for (var index = Count; index < Capacity; index++)
                        {
                            elements[index] = new BufferElement <TValue>(this);
                        }

                        Elements = elements;
                    }
                    else
                    {
                        for (var index = ActiveCount - 1; index >= 0; index--)
                        {
                            if (!Elements[index].Active)
                            {
                                Swap(index, --ActiveCount);
                            }
                        }
                    }
                }

                var bufferElement = Elements[Count++];

                if (!bufferElement.Active)
                {
                    bufferElement.Active = true;
                    ActiveCount++;
                }

                return(bufferElement);
            }
        }