23 #ifndef BAGEL_BOFLIB_LIST_H 24 #define BAGEL_BOFLIB_LIST_H 26 #include "common/scummsys.h" 27 #include "bagel/boflib/misc.h" 32 #define MIN_NODES 5 // Minimum # of pre-allocated nodes in node array 41 _pNext = _pPrev =
nullptr;
44 _pNext = _pPrev =
nullptr;
51 void setNodeItem(T cItem) {
63 if (_pItemList !=
nullptr) {
68 if (_nNumItems != 0) {
69 _pItemList = (
void **)bofAlloc(_nNumItems *
sizeof(
void *));
74 if (_pItemList !=
nullptr) {
80 void recalcItemList() {
82 if (_nNumItems >= _nItemsAllocated) {
83 if (_pItemList !=
nullptr) {
88 if (_nNumItems != 0) {
89 assert(_nItemsAllocated < 0x8000);
90 _nItemsAllocated *= 2;
91 if (_nItemsAllocated == 0)
92 _nItemsAllocated = MIN_NODES;
94 _pItemList = (
void **)bofAlloc(_nItemsAllocated *
sizeof(
void *));
98 if (_nNumItems != 0) {
99 assert(_pItemList !=
nullptr);
104 while (pNode !=
nullptr) {
105 *(_pItemList + i++) = pNode;
106 pNode = pNode->_pNext;
131 while (pNode !=
nullptr) {
133 pNode = pNode->_pPrev;
149 while (pNode !=
nullptr) {
151 pNode = pLast->_pNext;
159 uint32 _nItemsAllocated;
171 _nItemsAllocated = 0;
172 _pHead = _pTail =
nullptr;
173 _pItemList =
nullptr;
182 assert(_nNumItems == 0);
185 int getCount()
const {
196 while (pNode !=
nullptr) {
198 pNode = pNode->_pNext;
202 assert(_nNumItems == nCount);
212 return _pHead ==
nullptr;
223 assert(pNode !=
nullptr);
225 return pNode->getNodeItem();
228 void setNodeItem(
int nNodeIndex, T tNewItem) {
231 assert(pNode !=
nullptr);
233 pNode->setNodeItem(tNewItem);
236 T operator[](
int nIndex) {
237 return getNodeItem(nIndex);
246 assert(nNodeIndex >= 0 && nNodeIndex < getCount());
250 if (_pItemList ==
nullptr) {
253 while (pNode !=
nullptr) {
254 if (nNodeIndex-- == 0)
256 pNode = pNode->_pNext;
273 insertBefore(getNode(nNodeIndex), cNewItem);
282 assert(pNode !=
nullptr);
285 if (pNode == _pHead) {
291 pNewNode->_pPrev = pNode->_pPrev;
292 pNewNode->_pNext = pNode;
294 if (pNode->_pPrev !=
nullptr)
295 pNode->_pPrev->_pNext = pNewNode;
297 pNode->_pPrev = pNewNode;
301 assert(_nNumItems != 0xFFFF);
314 insertAfter(getNode(nNodeIndex), cNewItem);
323 assert(pNode !=
nullptr);
326 if (pNode == _pTail) {
331 pNewNode->_pPrev = pNode;
332 pNewNode->_pNext = pNode->_pNext;
334 if (pNode->_pNext !=
nullptr)
335 pNode->_pNext->_pPrev = pNewNode;
337 pNode->_pNext = pNewNode;
341 assert(_nNumItems != 0xFFFF);
353 assert(pNode !=
nullptr);
360 if (pNode !=
nullptr) {
362 T retVal = pNode->getNodeItem();
365 _pHead = _pHead->_pNext;
368 _pTail = _pTail->_pPrev;
370 if (pNode->_pPrev !=
nullptr)
371 pNode->_pPrev->_pNext = pNode->_pNext;
373 if (pNode->_pNext !=
nullptr)
374 pNode->_pNext->_pPrev = pNode->_pPrev;
390 T
remove(
int nNodeIndex) {
391 return remove(getNode(nNodeIndex));
410 assert(_pHead !=
nullptr);
412 return remove(_pHead);
420 assert(_pTail !=
nullptr);
421 return remove(_pTail);
429 assert(pNewNode !=
nullptr);
431 pNewNode->_pNext = _pHead;
432 pNewNode->_pPrev =
nullptr;
433 if (_pHead !=
nullptr)
434 _pHead->_pPrev = pNewNode;
437 if (_pTail ==
nullptr)
441 assert(_nNumItems != 0xFFFF);
452 addToHead(newNode(cItem));
460 assert(pNewNode !=
nullptr);
462 pNewNode->_pPrev = _pTail;
463 pNewNode->_pNext =
nullptr;
464 if (_pTail !=
nullptr)
465 _pTail->_pNext = pNewNode;
468 if (_pHead ==
nullptr)
472 assert(_nNumItems != 0xFFFF);
482 addToTail(newNode(cItem));
void insertBefore(CBofListNode< T > *pNode, T cNewItem)
Definition: list.h:281
T removeHead()
Definition: list.h:409
void removeAll()
Definition: list.h:398
T removeTail()
Definition: list.h:419
void insertAfter(int nNodeIndex, T cNewItem)
Definition: list.h:312
void insertAfter(CBofListNode< T > *pNode, T cNewItem)
Definition: list.h:322
void addToTail(T cItem)
Definition: list.h:481
void addToHead(CBofListNode< T > *pNewNode)
Definition: list.h:428
int getActualCount() const
Definition: list.h:193
void addToTail(CBofListNode< T > *pNewNode)
Definition: list.h:459
virtual ~CBofList()
Definition: list.h:179
bool isEmpty() const
Definition: list.h:211
void insertBefore(int nNodeIndex, T cNewItem)
Definition: list.h:271
T getNodeItem(int nNodeIndex)
Definition: list.h:220
CBofListNode< T > * getNode(int nNodeIndex)
Definition: list.h:245
void addToHead(T cItem)
Definition: list.h:451