csutil/invasivelist.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 2012 by Frank Richter 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public 00015 License along with this library; if not, write to the Free 00016 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00017 */ 00018 00019 #ifndef __CS_UTIL_INVASIVELIST_H__ 00020 #define __CS_UTIL_INVASIVELIST_H__ 00021 00026 namespace CS 00027 { 00028 namespace Container 00029 { 00039 template<typename Tag> 00040 class InvasiveList 00041 { 00042 public: 00043 class Element 00044 { 00045 friend class InvasiveList<Tag>; 00046 00047 Element* next; 00048 Element* prev; 00049 public: 00050 Element() : next (nullptr), prev (nullptr) {} 00051 }; 00052 00053 InvasiveList () : head (nullptr), tail (nullptr) {} 00054 00056 void PushBack (Element* item) 00057 { 00058 item->prev = tail; 00059 if (tail) tail->next = item; 00060 tail = item; 00061 if (!head) head = item; 00062 } 00063 00068 Element* PopFront() 00069 { 00070 Element* front (head); 00071 if (front) Delete (front); 00072 return front; 00073 } 00074 00076 void Delete (Element* item) 00077 { 00078 if (head == item) head = item->next; 00079 if (tail == item) tail = item->prev; 00080 if (item->prev) item->prev->next = item->next; 00081 if (item->next) item->next->prev = item->prev; 00082 item->prev = nullptr; 00083 item->next = nullptr; 00084 } 00085 00087 bool IsEmpty() const 00088 { 00089 return !head; 00090 } 00091 private: 00092 Element* head; 00093 Element* tail; 00094 }; 00095 } // namespace Container 00096 } // namespace CS 00097 00098 #endif // __CSUTIL_INVASIVELIST_H__
Generated for Crystal Space 2.1 by doxygen 1.6.1
