/* ScummVM - Graphic Adventure Engine * * ScummVM is the legal property of its developers, whose names * are too numerous to list here. Please refer to the COPYRIGHT * file distributed with this source distribution. * * Additional copyright for this file: * Copyright (C) 1999-2000 Revolution Software Ltd. * This code is based on source code created by Revolution Software, * used with permission. * * 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, either version 3 of the License, or * (at your option) any later version. * * 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, see . * */ #ifndef ICB_PX_LIBRARY_LIST_TEMPLATE #define ICB_PX_LIBRARY_LIST_TEMPLATE namespace ICB { template class pxList { protected: Type m_item; // The item at this node pxList *m_tail; // The tail of the list public: pxList() { m_tail = NULL; // Construct an empty list } ~pxList() { if (!IsEmpty()) delete Tail(); // Destruct the array } Type &Head() { return m_item; // The lists' head } pxList *Tail() { return m_tail; // The lists' tail } bool IsEmpty() { return (m_tail == NULL); // Is the list empty ? } void Append(const Type &); // Append item to the end of the list void Insert(const Type &); // Inserts an item after the current item pxList *Find(const Type &); // Returns the list whose head == entry (NULL if not found) int32 Length(); // Returns the length of the list }; template class rcSortedList { protected: Type m_item; // The item at this node rcSortedList *m_tail; // The tail of the list public: rcSortedList() { m_tail = NULL; // Construct an empty list } ~rcSortedList() { if (!IsEmpty()) delete Tail(); // Destruct the array } public: Type &Head(); // The lists' head rcSortedList *Tail(); // The lists' tail bool IsEmpty(); // Is the list empty ? int32 Length(); // Returns the length of the list public: rcSortedList *Insert(const Type &); // Inserts an item in the correct place rcSortedList *Find(const Type &); // Returns the list whose head == entry (NULL if not found) }; template void pxList::Append(const Type &entry) { if (IsEmpty()) { m_item = entry; m_tail = new pxList; } else { Tail()->Append(entry); } } template void pxList::Insert(const Type &entry) { pxList *newNode = new pxList; if (IsEmpty()) { // Is the list is empty, insert the item at the start of the list m_item = entry; } else { // Else put the item before this node newNode->m_item = m_item; m_item = entry; newNode->m_tail = Tail(); } m_tail = newNode; } template pxList *pxList::Find(const Type &entry) { // If this is the end of list marker we haven't found it if (IsEmpty()) return NULL; if (Head() == entry) return this; return (Tail()->Find(entry)); } template int32 pxList::Length() { if (IsEmpty()) return 0; return (1 + Tail()->Length()); } template Type &rcSortedList::Head() { // The lists' head return m_item; } template rcSortedList *rcSortedList::Tail() { // The lists' tail return m_tail; } template bool rcSortedList::IsEmpty() { // Is the list empty ? return (m_tail == NULL); } template rcSortedList *rcSortedList::Insert(const Type &entry) { if (IsEmpty()) { // End of the list so add the entry here m_item = entry; m_tail = new rcSortedList; return this; } // The class being listed must have a '>' Operator defined else if (m_item > entry) { // The new item comes before the current one rcSortedList *newNode = new rcSortedList; newNode->m_tail = m_tail; newNode->m_item = m_item; m_item = entry; m_tail = newNode; return this; } else { // Keep going return Tail()->Insert(entry); } } template rcSortedList *rcSortedList::Find(const Type &entry) { // If this is the end of list marker we haven't found it if (IsEmpty()) return NULL; // this list is sorted, so we can stop when we have a higher value than entry if (Head() > entry) return NULL; if (Head() == entry) return this; return (Tail()->Find(entry)); } template int32 rcSortedList::Length() { if (IsEmpty()) return 0; return (1 + Tail()->Length()); } } // End of namespace ICB #endif // #ifndef _PX_LIBRARY_LIST_TEMPLATE