/********************************************************************************
        * PRIVATE
        ********************************************************************************/

        private void Dettach(LinkedIndexPoolEntry *e)
        {
            if (e->Queue > -1)
            {
                LinkedIndexPoolQueue *eq = &_queues[e->Queue];
                if (e->Index == eq->First)
                {
                    if (e->Next != -1)
                    {
                        eq->First = e->Next;
                        _entries[eq->First].Previous = -1;
                    }
                    else
                    {
                        eq->First = eq->Last = -1;
                    }
                }
                else if (e->Index == eq->Last)
                {
                    if (e->Previous != -1)
                    {
                        eq->Last = e->Previous;
                        _entries[eq->Last].Next = -1;
                    }
                    else
                    {
                        eq->First = eq->Last = -1;
                    }
                }
                else
                {
                    if (e->Next != -1)
                    {
                        _entries[e->Next].Previous = e->Previous;
                    }
                    if (e->Previous != -1)
                    {
                        _entries[e->Previous].Next = e->Next;
                    }
                }
                e->Next = e->Previous = e->Queue = -1;
                eq->Count--;
            }
            CheckCoherency();
        }
 public LinkedIndexPool(int capacity, int queueCount, bool check = false)
 {
     if (capacity < 8)
     {
         capacity = 8;
     }
     if (queueCount < 2)
     {
         queueCount = 2;
     }
     // -------- Initialize entries
     _entries  = (LinkedIndexPoolEntry *)HeapAllocator.New(sizeof(LinkedIndexPoolEntry) * capacity);
     _capacity = capacity;
     for (int i = 0; i < capacity; i++)
     {
         _entries[i].Previous = i - 1;
         _entries[i].Next     = i + 1;
         _entries[i].Index    = i;
         _entries[i].Queue    = 0;
     }
     _entries[0].Previous        = -1;
     _entries[capacity - 1].Next = -1;
     // -------- Initialize queues
     _queues     = (LinkedIndexPoolQueue *)HeapAllocator.New(sizeof(LinkedIndexPoolQueue) * queueCount);
     _queueCount = queueCount;
     for (int i = 0; i < queueCount; i++)
     {
         _queues[i].First = _queues[i].Last = -1;
         _queues[i].Index = i;
         _queues[i].Count = 0;
     }
     _queues[0].First = 0;
     _queues[0].Last  = capacity - 1;
     _queues[0].Count = capacity;
     // -------- Initialize queues stack
     _queueStack = new StackedIndexPoolUnsafe(queueCount, 2);
     // -------- All is ok...
     _checkIt = check;
 }
        /********************************************************************************/
        // -------- Queues
        public int GetFreeQueue()
        {
            int tmp = _queueStack.Pop();

            if (_queueStack.Capacity > _queueCount)
            {
                int newQueueCount = _queueStack.Capacity;
                LinkedIndexPoolQueue *_oldQueues = _queues;
                _queues = (LinkedIndexPoolQueue *)HeapAllocator.New(sizeof(LinkedIndexPoolQueue) * newQueueCount);
                for (int i = 0; i < _queueCount; i++)
                {
                    _queues[i] = _oldQueues[i];
                }
                for (int i = _queueCount; i < newQueueCount; i++)
                {
                    _queues[i].First = _queues[i].Last = -1;
                    _queues[i].Index = i;
                    _queues[i].Count = 0;
                }
            }
            return(tmp);
        }
 private void MoveAtStart(LinkedIndexPoolEntry *e, LinkedIndexPoolQueue *tq)
 {
     // -------- Dettach e
     Dettach(e);
     // -------- Attach e
     if (tq->First == -1)
     {
         // -------- Empty queue
         tq->First = tq->Last = e->Index;
         tq->Count = 1;
         e->Queue  = tq->Index;
     }
     else
     {
         _entries[tq->First].Previous = e->Index;
         e->Next   = tq->First;
         tq->First = e->Index;
         e->Queue  = tq->Index;
         tq->Count++;
     }
     CheckCoherency();
 }