/* Copyright (C) 2003 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef DLFIFOLIST_HPP #define DLFIFOLIST_HPP #include "ArrayPool.hpp" #include /** * Template class used for implementing an * list of object retreived from a pool */ template class DLFifoList { public: /** * List head */ struct Head { Head(); Uint32 firstItem; Uint32 lastItem; inline bool isEmpty() const { return firstItem == RNIL;} }; DLFifoList(ArrayPool & thePool); /** * Allocate an object from pool - update Ptr * * Return i */ bool seize(Ptr &); /** * Allocate object i from pool - update Ptr * * Return i */ bool seizeId(Ptr &, Uint32 i); /** * Add object to list * * @NOTE MUST be seized from correct pool */ void add(Ptr &); /** * Remove from list */ void remove(Ptr &); /** * Return an object to pool */ void release(Uint32 i); /** * Return an object to pool */ void release(Ptr &); /** * Return all objects to the pool */ void release(); /** * Update i & p value according to i */ void getPtr(Ptr &, Uint32 i) const; /** * Update p value for ptr according to i value */ void getPtr(Ptr &) const ; /** * Get pointer for i value */ T * getPtr(Uint32 i) const ; /** * Update ptr to first element in list * * Return i */ bool first(Ptr &) const ; /** * Get next element * * NOTE ptr must be both p & i */ bool next(Ptr &) const ; /** * Check if next exists * * NOTE ptr must be both p & i */ bool hasNext(const Ptr &) const; /** * Check if prev exists i.e. this is not first * * NOTE ptr must be both p & i */ bool hasPrev(const Ptr &) const; Uint32 noOfElements() const { Uint32 c = 0; Uint32 i = head.firstItem; while(i != RNIL){ c++; const T * t = thePool.getPtr(i); i = t->nextList; } return c; } /** * Print * (Run operator NdbOut<< on every element) */ void print(NdbOut & out) { Uint32 i = head.firstItem; while(i != RNIL){ T * t = thePool.getPtr(i); out << (unsigned int) t << "[" << i << "]:"; t->print(out); out << " "; i = t->nextList; } } inline bool isEmpty() const { return head.firstItem == RNIL;} protected: Head head; ArrayPool & thePool; }; template class LocalDLFifoList : public DLFifoList { public: LocalDLFifoList(ArrayPool & thePool, typename DLFifoList::Head & _src) : DLFifoList(thePool), src(_src) { this->head = src; } ~LocalDLFifoList(){ src = this->head; } private: typename DLFifoList::Head & src; }; template inline DLFifoList::DLFifoList(ArrayPool & _pool): thePool(_pool){ } template inline DLFifoList::Head::Head(){ firstItem = RNIL; lastItem = RNIL; } /** * Allocate an object from pool - update Ptr * * Return i */ template inline bool DLFifoList::seize(Ptr & p){ thePool.seize(p); if (p.i != RNIL) { add(p); return true; } p.p = NULL; return false; } /** * Allocate an object from pool - update Ptr * * Return i */ template inline bool DLFifoList::seizeId(Ptr & p, Uint32 ir){ thePool.seizeId(p, ir); if(p.i != RNIL){ add(p); return true; } p.p = NULL; return false; } template inline void DLFifoList::add(Ptr & p){ T * t = p.p; Uint32 last = head.lastItem; if(p.i == RNIL) ErrorReporter::handleAssert("DLFifoList::add", __FILE__, __LINE__); t->nextList = RNIL; t->prevList = last; if (head.firstItem == RNIL) head.firstItem = p.i; head.lastItem = p.i; if(last != RNIL){ T * t2 = thePool.getPtr(last); t2->nextList = p.i; } } /** * Return an object to pool */ template inline void DLFifoList::release(Uint32 i){ Ptr p; p.i = i; p.p = thePool.getPtr(i); release(p); } template inline void DLFifoList::remove(Ptr & p){ T * t = p.p; Uint32 ni = t->nextList; Uint32 pi = t->prevList; if(ni != RNIL){ T * t = thePool.getPtr(ni); t->prevList = pi; } else { // We are releasing last head.lastItem = pi; } if(pi != RNIL){ T * t = thePool.getPtr(pi); t->nextList = ni; } else { // We are releasing first head.firstItem = ni; } } /** * Return an object to pool */ template inline void DLFifoList::release(Ptr & p){ remove(p); thePool.release(p.i); } template inline void DLFifoList::release(){ Ptr p; while(head.firstItem != RNIL){ p.i = head.firstItem; p.p = thePool.getPtr(head.firstItem); T * t = p.p; head.firstItem = t->nextList; release(p); } } template inline void DLFifoList::getPtr(Ptr & p, Uint32 i) const { p.i = i; p.p = thePool.getPtr(i); } template inline void DLFifoList::getPtr(Ptr & p) const { thePool.getPtr(p); } template inline T * DLFifoList::getPtr(Uint32 i) const { return thePool.getPtr(i); } /** * Update ptr to first element in list * * Return i */ template inline bool DLFifoList::first(Ptr & p) const { p.i = head.firstItem; if(p.i != RNIL){ p.p = thePool.getPtr(p.i); return true; } p.p = NULL; return false; } template inline bool DLFifoList::next(Ptr & p) const { p.i = p.p->nextList; if(p.i != RNIL){ p.p = thePool.getPtr(p.i); return true; } p.p = NULL; return false; } template inline bool DLFifoList::hasNext(const Ptr & p) const { return p.p->nextList != RNIL; } template inline bool DLFifoList::hasPrev(const Ptr & p) const { return p.p->prevList != RNIL; } #endif