/*************************************************************************** * Copyright (C) 2001-2023 by Tom Hicks * * headhunter3@gmail.com * * * * 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 2 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, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #ifndef OpenArena__list_h__ #define OpenArena__list_h__ #if defined HAVE_CONFIG_H #include "config.h" #endif #include #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunknown-pragmas" #pragma warning(disable : 4715) namespace OpenArena { typedef unsigned int LIST_ARRAY_INDEX_TYPE; /*! * \brief * Write brief comment for listNode here. * * \param ItemType * Description of parameter ItemType. * * Write detailed description for listNode here. * * \remarks * Write remarks for listNode here. * * \see * Separate items with the '|' character. */ template class listNode { public: /*! * \brief * Write brief comment for listNode here. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for listNode here. * * \remarks * Write remarks for listNode here. * * \see * Separate items with the '|' character. */ listNode(); ItemType data; listNode* next; }; /*! * \brief * Write brief comment for list here. * * \param ItemType * Description of parameter ItemType. * * Write detailed description for list here. * * \remarks * Write remarks for list here. * * \see * Separate items with the '|' character. */ template class list { public: /*! * \brief * Write brief comment for list here. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for list here. * * \remarks * Write remarks for list here. * * \see * Separate items with the '|' character. */ list(); /*! * \brief * Write brief comment for ~list here. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for ~list here. * * \remarks * Write remarks for ~list here. * * \see * Separate items with the '|' character. */ ~list(); /*! * \brief * Write brief comment for list here. * * \param * Description of parameter . * * \throws * Description of criteria for throwing this exception. * * Write detailed description for list here. * * \remarks * Write remarks for list here. * * \see * Separate items with the '|' character. */ list(const list&); /*! * \brief * Write brief comment for IsEmpty here. * * \returns * Write description of return value here. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for IsEmpty here. * * \remarks * Write remarks for IsEmpty here. * * \see * Separate items with the '|' character. */ bool IsEmpty() const; /*! * \brief * Write brief comment for IsFull here. * * \returns * Write description of return value here. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for IsFull here. * * \remarks * Write remarks for IsFull here. * * \see * Separate items with the '|' character. */ bool IsFull() const; /*! * \brief * Write brief comment for Insert here. * * \param newItem * Description of parameter newItem. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for Insert here. * * \remarks * Write remarks for Insert here. * * \see * Separate items with the '|' character. */ void Insert(ItemType newItem); /*! * \brief * Write brief comment for Remove here. * * \param target * Description of parameter target. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for Remove here. * * \remarks * Write remarks for Remove here. * * \see * Separate items with the '|' character. */ void Remove(ItemType target); /*! * \brief * Write brief comment for Remove here. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for Remove here. * * \remarks * Write remarks for Remove here. * * \see * Separate items with the '|' character. */ void Remove(); /*! * \brief * Write brief comment for Retrieve here. * * \returns * Write description of return value here. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for Retrieve here. * * \remarks * Write remarks for Retrieve here. * * \see * Separate items with the '|' character. */ ItemType Retrieve() const; /*! * \brief * Write brief comment for PrevPosition here. * * \returns * Write description of return value here. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for PrevPosition here. * * \remarks * Write remarks for PrevPosition here. * * \see * Separate items with the '|' character. */ bool PrevPosition(); /*! * \brief * Write brief comment for NextPosition here. * * \returns * Write description of return value here. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for NextPosition here. * * \remarks * Write remarks for NextPosition here. * * \see * Separate items with the '|' character. */ bool NextPosition(); /*! * \brief * Write brief comment for IsFirstPosition here. * * \returns * Write description of return value here. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for IsFirstPosition here. * * \remarks * Write remarks for IsFirstPosition here. * * \see * Separate items with the '|' character. */ bool IsFirstPosition() const; /*! * \brief * Write brief comment for IsLastPosition here. * * \returns * Write description of return value here. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for IsLastPosition here. * * \remarks * Write remarks for IsLastPosition here. * * \see * Separate items with the '|' character. */ bool IsLastPosition() const; /*! * \brief * Write brief comment for FirstPosition here. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for FirstPosition here. * * \remarks * Write remarks for FirstPosition here. * * \see * Separate items with the '|' character. */ void FirstPosition(); /*! * \brief * Write brief comment for LastPosition here. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for LastPosition here. * * \remarks * Write remarks for LastPosition here. * * \see * Separate items with the '|' character. */ void LastPosition(); /*! * \brief * Write brief comment for Clear here. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for Clear here. * * \remarks * Write remarks for Clear here. * * \see * Separate items with the '|' character. */ void Clear(); /*! * \brief * Write brief comment for Contains here. * * \param * Description of parameter . * * \returns * Write description of return value here. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for Contains here. * * \remarks * Write remarks for Contains here. * * \see * Separate items with the '|' character. */ bool Contains(ItemType) const; /*! * \brief * Write brief comment for Length here. * * \returns * Write description of return value here. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for Length here. * * \remarks * Write remarks for Length here. * * \see * Separate items with the '|' character. */ unsigned int Length(); /*! * \brief * Write brief comment for operator = here. * * \param * Description of parameter . * * \throws * Description of criteria for throwing this exception. * * Write detailed description for operator = here. * * \remarks * Write remarks for operator = here. * * \see * Separate items with the '|' character. */ void operator=(const list&); /*! * \brief * Write brief comment for operator [] here. * * \param * Description of parameter . * * \returns * Write description of return value here. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for operator [] here. * * \remarks * Write remarks for operator [] here. * * \see * Separate items with the '|' character. */ ItemType operator[](LIST_ARRAY_INDEX_TYPE) const; /*! * \brief * Write brief comment for operator [] here. * * \param * Description of parameter . * * \returns * Write description of return value here. * * \throws * Description of criteria for throwing this exception. * * Write detailed description for operator [] here. * * \remarks * Write remarks for operator [] here. * * \see * Separate items with the '|' character. */ ItemType& operator[](LIST_ARRAY_INDEX_TYPE); private: listNode* head; listNode* tail; listNode* currPos; }; template listNode::listNode() { next = NULL; } template list::list() { head = NULL; tail = NULL; currPos = NULL; } template list::~list() { currPos = NULL; while (head != NULL) { tail = head; head = head->next; delete tail; tail = NULL; } } template list::list(const list& rtOp) { head = NULL; tail = NULL; currPos = NULL; operator=(rtOp); } template void list::Clear() { while (head) Remove(head->data); } template void list::operator=(const list& rtOp) { Clear(); if (!rtOp.IsEmpty()) { listNode* temp = rtOp.head->next; listNode* temp2 = NULL; if (temp != NULL) { head = new listNode; head->data = rtOp.head->data; tail = head; temp2 = head; } while (temp != NULL) { temp2->next = new listNode; temp2 = temp2->next; temp2->data = temp->data; temp = temp->next; } tail = temp2; } } template bool list::IsEmpty() const { return head == NULL; } template bool list::IsFull() const { return false; } template void list::Insert(ItemType newItem) { listNode* temp = head; listNode* temp2 = NULL; if (head == NULL) { temp = tail = head = new listNode; } else { if (newItem < head->data) { temp2 = head; temp = head = new listNode; head->next = temp2; temp2 = NULL; } else { // temp = head; while (temp->next != NULL && newItem > temp->next->data) { temp = temp->next; } temp2 = temp->next; temp = temp->next = new listNode; temp->next = temp2; temp2 = NULL; } } temp->data = newItem; if (temp->next == NULL) { tail = temp; } } template void list::Remove(ItemType target) { if (head != NULL) { listNode* temp = head; listNode* temp2 = head; while (temp->next != NULL && temp->data != target) { temp2 = temp; temp = temp->next; } if (temp->data == target) { if (temp != head) { temp2->next = temp->next; delete temp; } else { if (tail == head) tail = NULL; head = head->next; delete temp2; } } } } template bool list::PrevPosition() { if (currPos != head) { tail->next = currPos; currPos = head; if (currPos != tail->next) { while (currPos->next != tail->next) { currPos = currPos->next; } } tail->next = NULL; return true; } else { return false; } } template bool list::NextPosition() { if (currPos != tail) { currPos = currPos->next; return true; } else { return false; } } template void list::Remove() { if (currPos != NULL) { tail->next = currPos; currPos = currPos->next; delete tail->next; tail->next = NULL; } } template ItemType list::Retrieve() const { if (currPos != NULL) return currPos->data; } template bool list::IsFirstPosition() const { return currPos == head; } template bool list::IsLastPosition() const { return currPos == tail; } template void list::FirstPosition() { currPos = head; } template void list::LastPosition() { currPos = tail; } template ItemType list::operator[](LIST_ARRAY_INDEX_TYPE index) const { if (head == NULL) { // We should throw an exception here but instead I'll just return shit guess // for now if somebody does this they're just fucked. } else { listNode* temp = head; LIST_ARRAY_INDEX_TYPE current; for (current = 0; current < index; current++) { if (temp->next == NULL) { temp->next = new listNode; } temp = temp->next; } return temp->data; } } template ItemType& list::operator[](LIST_ARRAY_INDEX_TYPE index) { if (head == NULL) { tail = currPos = head = new listNode; return (ItemType&)(head->data); } else { listNode* temp = head; LIST_ARRAY_INDEX_TYPE current; for (current = 0; current < index; current++) { if (temp->next == NULL) { temp->next = new listNode; } temp = temp->next; } return (ItemType&)(temp->data); } } template unsigned int list::Length() { if (head == NULL) { return 0; } else { int len = 1; listNode* temp = head->next; while (temp != NULL) { temp = temp->next; len++; } return len; } } template bool list::Contains(ItemType value) const { listNode* temp = head; while (temp != NULL) { if (temp->data == value) { return true; } temp = temp->next; } return false; } }; // namespace OpenArena #pragma clang diagnostic pop #endif // End !defined(OpenArena__list_h__)