00001 // See the end of this file for license information. 00002 00003 #ifndef TORSION_LIST_H 00004 #define TORSION_LIST_H 00005 00006 #include "screen.h" 00007 00010 00011 class List_node { 00012 public: 00013 List_node* next; 00014 List_node* previous; 00015 }; 00016 00018 00019 template <class Type> 00020 class List_iterator { 00021 public: 00022 List_node* node; 00023 00024 List_iterator() { } 00025 List_iterator(List_node* node); 00026 List_iterator(Type* node); 00027 00028 List_iterator& 00029 operator++(); // prefix ++ 00030 00031 List_iterator& 00032 operator--(); // prefix -- 00033 00034 List_iterator& 00035 operator++(int); // postfix ++ 00036 00037 List_iterator& 00038 operator--(int); // postfix -- 00039 00040 Type& 00041 operator*() const; 00042 00043 Type* 00044 operator->() const; 00045 00046 bool 00047 operator==(const List_iterator& iterator) const; 00048 00049 bool 00050 operator!=(const List_iterator& iterator) const; 00051 }; 00052 00053 template <class Type> 00054 inline 00055 List_iterator<Type>::List_iterator(List_node* node) : node(node) { } 00056 00057 template <class Type> 00058 inline 00059 List_iterator<Type>::List_iterator(Type* node) : node((List_node*)node) { } 00060 00061 template <class Type> 00062 inline List_iterator<Type>& 00063 List_iterator<Type>::operator++() { 00064 node = node->next; 00065 return *this; 00066 } 00067 00068 template <class Type> 00069 inline List_iterator<Type>& 00070 List_iterator<Type>::operator--() { 00071 node = node->previous; 00072 return *this; 00073 } 00074 00075 template <class Type> 00076 inline List_iterator<Type>& 00077 List_iterator<Type>::operator++(int) { 00078 node = node->next; 00079 return *this; 00080 } 00081 00082 template <class Type> 00083 inline List_iterator<Type>& 00084 List_iterator<Type>::operator--(int) { 00085 node = node->previous; 00086 return *this; 00087 } 00088 00089 template <class Type> 00090 inline Type& 00091 List_iterator<Type>::operator*() const { 00092 return *(Type*)node; 00093 } 00094 00095 template <class Type> 00096 inline Type* 00097 List_iterator<Type>::operator->() const { 00098 return (Type*)node; 00099 } 00100 00101 template <class Type> 00102 inline bool 00103 List_iterator<Type>::operator==(const List_iterator& iterator) const { 00104 return (node == iterator.node); 00105 } 00106 00107 template <class Type> 00108 inline bool 00109 List_iterator<Type>::operator!=(const List_iterator& iterator) const { 00110 return (node != iterator.node); 00111 } 00112 00115 00116 template <class Type> 00117 class List { 00118 protected: 00119 List_node sentinel; 00120 00121 public: 00122 typedef List_iterator<Type> Iterator; 00123 00124 List(); 00125 00127 void 00128 clear(); 00129 00133 void 00134 free_and_clear(); 00135 00136 Type* 00137 front(); 00138 00139 Type* 00140 back(); 00141 00142 Iterator 00143 begin(); 00144 00145 Iterator 00146 end(); 00147 00148 bool 00149 is_empty() const; 00150 00151 unsigned 00152 size() const; 00153 00154 Iterator 00155 insert(Iterator position, Type* element); 00156 00157 Iterator 00158 insert_front(Type* element); 00159 00160 Iterator 00161 insert_back(Type* element); 00162 00163 Type* 00164 remove(Iterator position); 00165 00166 Type* 00167 remove(Type* element); 00168 00169 Type* 00170 remove_front(); 00171 00172 Type* 00173 remove_back(); 00174 00176 void 00177 print(); 00178 }; 00179 00180 template <class Type> 00181 inline 00182 List<Type>::List() { 00183 clear(); 00184 } 00185 00186 template <class Type> 00187 inline void 00188 List<Type>::clear() { 00189 sentinel.next = &sentinel; 00190 sentinel.previous = &sentinel; 00191 } 00192 00193 template <class Type> 00194 inline void 00195 List<Type>::free_and_clear() { 00196 // free each node in this list 00197 for (const List_node* node = sentinel.next; node != &sentinel; node = node->next) 00198 delete node; 00199 00200 // clear the list itself 00201 clear(); 00202 } 00203 00204 template <class Type> 00205 inline Type* 00206 List<Type>::front() { 00207 return (Type*)sentinel.next; 00208 } 00209 00210 template <class Type> 00211 inline Type* 00212 List<Type>::back() { 00213 return (Type*)sentinel.previous; 00214 } 00215 00216 template <class Type> 00217 inline List_iterator<Type> 00218 List<Type>::begin() { 00219 return Iterator(sentinel.next); 00220 } 00221 00222 template <class Type> 00223 inline List_iterator<Type> 00224 List<Type>::end() { 00225 return Iterator(&sentinel); 00226 } 00227 00228 template <class Type> 00229 inline bool 00230 List<Type>::is_empty() const { 00231 return sentinel.next == &sentinel; 00232 } 00233 00234 template <class Type> 00235 unsigned 00236 List<Type>::size() const { 00237 unsigned size = 0; 00238 00239 for (const List_node* node = sentinel.next; node != &sentinel; node = node->next) 00240 size++; 00241 00242 return size; 00243 } 00244 00245 template <class Type> 00246 inline List_iterator<Type> 00247 List <Type>::insert(Iterator position, Type* element) { 00248 List_node* node = (List_node*)(element); 00249 00250 if (node != position.node && node != position.node->previous) { 00251 node->next = position.node; 00252 node->previous = position.node->previous; 00253 position.node->previous = node; 00254 node->previous->next = node; 00255 } 00256 00257 return Iterator(node); 00258 } 00259 00260 template <class Type> 00261 inline List_iterator<Type> 00262 List<Type>::insert_front(Type* element) { 00263 return insert(begin(), element); 00264 } 00265 00266 template <class Type> 00267 inline List_iterator<Type> 00268 List<Type>::insert_back(Type* element) { 00269 return insert(end(), element); 00270 } 00271 00272 template <class Type> 00273 inline Type* 00274 List<Type>::remove(Iterator position) { 00275 remove((Type*)position.node); 00276 return (Type*)position.node; 00277 } 00278 00279 template <class Type> 00280 inline Type* 00281 List<Type>::remove(Type* element) { 00282 if (element->next != NULL && element->previous != NULL) { 00283 element->previous->next = element->next; 00284 element->next->previous = element->previous; 00285 element->next = NULL; 00286 element->previous = NULL; 00287 } 00288 00289 return element; 00290 } 00291 00292 template <class Type> 00293 inline Type* 00294 List<Type>::remove_front() { 00295 return remove(Iterator(front())); 00296 } 00297 00298 template <class Type> 00299 inline Type* 00300 List<Type>::remove_back() { 00301 return remove(Iterator(back())); 00302 } 00303 00304 template <class Type> 00305 void 00306 List<Type>::print() { 00307 screen.print(this, false); 00308 screen.print(": ", false); 00309 00310 unsigned i = 0; 00311 for (Iterator position = begin(); position != end() && i < 6; position++) { 00312 screen.print(&*position, false); 00313 screen.print(" ", false); 00314 i++; 00315 } 00316 if (i == 6) 00317 screen.print("..."); 00318 screen.print(); 00319 } 00320 00321 #endif 00322 00323 /* Torsion Operating System, Copyright (C) 2000-2004 Dan Helfman 00324 * 00325 * This program is free software; you can redistribute it and/or modify it 00326 * under the terms of the GNU General Public License as published by the 00327 * Free Software Foundation; either version 2 of the License, or (at your 00328 * option) any later version. 00329 * 00330 * This program is distributed in the hope that it will be useful, but 00331 * WITHOUT ANY WARRANTY; without even the implied warranty of 00332 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00333 * General Public License for more details (in the COPYING file). 00334 * 00335 * You should have received a copy of the GNU General Public License along 00336 * with this program; if not, write to the Free Software Foundation, Inc., 00337 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00338 */
Torsion Operating System, Copyright (C) 2000-2004 Dan Helfman