00001 // OMICRON ENGINE HEADER FILE 00002 // 00003 // -------------------------------------------------------------------------- 00004 // Copyright (C) 2001-2002 by Bjoern Paetzel <kolrabi@gmx.de> 00005 // 00006 // This file is part of the Omicron Engine. 00007 // 00008 // The Omicron Engine is free software; you can redistribute it and/or modify 00009 // it under the terms of the GNU General Public License as published by the 00010 // Free Software Foundation; either version 2 of the License, or (at your 00011 // option) any later version. 00012 // 00013 // The Omicron Engine is distributed in the hope that it will be useful, but 00014 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 00015 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00016 // for more details. 00017 // 00018 // You should have received a copy of the GNU General Public License along 00019 // with The Omicron Engine; if not, write to the Free Software Foundation, 00020 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 // 00022 // -------------------------------------------------------------------------- 00023 // Last modified: $Date: 2002/12/13 12:29:26 $ 00024 // By : $Author: kolrabi $ 00025 // $Id: list.h,v 1.2 2002/12/13 12:29:26 kolrabi Exp $ 00026 00027 /* 00028 00029 $Log: list.h,v $ 00030 Revision 1.2 2002/12/13 12:29:26 kolrabi 00031 fixed some bugs 00032 00033 Revision 1.1.1.1 2002/12/07 19:02:17 kolrabi 00034 initial release 00035 00036 00037 */ 00038 00046 /**************************************************************************** 00047 **************************************************************************** 00048 * TYPES ******************************************************************** 00049 **************************************************************************** 00050 ****************************************************************************/ 00051 00052 template <class T> 00053 class list_c 00059 { 00060 protected: 00061 00062 #if !defined(__GNUG__) 00063 template <class T> 00064 #endif 00065 struct entry_t 00067 { 00068 entry_t<T> *next, *prev; 00069 T *data; 00070 }; 00071 00072 entry_t<T> *first; 00073 entry_t<T> *last; 00074 entry_t<T> *current; 00075 00076 long count; 00077 00078 public: 00079 00080 list_c<T>() 00082 { 00083 count = 0; 00084 first = last = current = NULL; 00085 } 00086 00087 virtual 00088 ~list_c<T>() 00090 { 00091 AssertThis; 00092 00093 current = first; 00094 while(current) 00095 { 00096 entry_t<T> *next; 00097 00098 next = current->next; 00099 SafeDelete(current); 00100 current = next; 00101 } 00102 } 00103 00104 void 00105 add_first(T* data) 00111 { 00112 AssertThis; 00113 00114 count++; 00115 00116 current = new entry_t<T>; 00117 00118 current->data = data; 00119 00120 if (!first) 00121 { 00122 last = first = current; 00123 first->next = first->prev = NULL; 00124 return; 00125 } 00126 00127 current->data = data; 00128 current->next = first; 00129 current->prev = NULL; 00130 00131 first->prev = current; 00132 first = current; 00133 } 00134 00135 void 00136 append(T* data) 00141 { 00142 AssertThis; 00143 00144 count++; 00145 00146 current = new entry_t<T>; 00147 00148 current->data = data; 00149 00150 if (!last) 00151 { 00152 last = first = current; 00153 last->next = last->prev = NULL; 00154 return; 00155 } 00156 00157 current->data = data; 00158 current->next = NULL; 00159 current->prev = last; 00160 00161 last->next = current; 00162 last = current; 00163 } 00164 00165 void 00166 add_aftercurrent(T* data) 00172 { 00173 AssertThis; 00174 00175 count++; 00176 00177 if (current == last || !current) 00178 { 00179 append(data); 00180 return; 00181 } 00182 00183 entry_t<T> *p,*n; 00184 00185 p = current; 00186 n = current->next; 00187 00188 current = new entry_t<T>; 00189 00190 current->data = data; 00191 current->next = n; 00192 current->prev = p; 00193 00194 p->next = current; 00195 n->prev = current; 00196 } 00197 00198 void 00199 add_beforecurrent(T* data) 00205 { 00206 AssertThis; 00207 00208 count++; 00209 00210 if (current == first || !current) 00211 { 00212 add_first(data); 00213 return; 00214 } 00215 00216 entry_t<T> *p,*n; 00217 00218 p = current->prev; 00219 n = current; 00220 00221 current = new entry_t<T>; 00222 00223 current->data = data; 00224 current->next = n; 00225 current->prev = p; 00226 00227 p->next = current; 00228 n->prev = current; 00229 } 00230 00231 00232 long 00233 get_count() 00238 { 00239 AssertThisValue(INVALID_INDEX); 00240 return count; 00241 }; 00242 00243 T * 00244 get_first() 00251 { 00252 AssertThisV; 00253 return first ?(current=first)->data :NULL; 00254 }; 00255 00256 T * 00257 get_last() 00264 { 00265 AssertThisV; 00266 return last ?(current=last)->data :NULL; 00267 }; 00268 00269 T * 00270 get_current() 00276 { 00277 AssertThisV; 00278 return current?current->data :NULL; 00279 }; 00280 00281 T * 00282 get_next() 00290 { 00291 AssertThisV; 00292 return (current&¤t->next)?(current=current->next)->data: NULL; 00293 }; 00294 00295 T * 00296 get_prev() 00304 { 00305 AssertThisV; 00306 return (current&¤t->prev)?(current=current->prev)->data: NULL; 00307 }; 00308 00309 void 00310 set_current(T* ptr) 00316 { 00317 AssertThis; 00318 00319 T *e; 00320 00321 e = get_first(); 00322 while(e) 00323 { 00324 if (e==ptr) 00325 return; 00326 00327 e = get_next(); 00328 } 00329 00330 current = NULL; 00331 } 00332 00333 void 00334 set_current_num(ulong n) 00341 { 00342 AssertThis; 00343 00344 T *e; 00345 ulong i = 0; 00346 00347 e = get_first(); 00348 while(e) 00349 { 00350 if (i==n) 00351 return; 00352 00353 i++; 00354 e = get_next(); 00355 } 00356 00357 current = last; 00358 } 00359 00360 void 00361 remove(T* ptr) 00367 { 00368 AssertThis; 00369 00370 set_current(ptr); 00371 00372 if (!current) 00373 return; 00374 00375 if (current == first) 00376 first = first->next; 00377 00378 if (current == last) 00379 last = last->prev; 00380 00381 count --; 00382 00383 entry_t<T> *prev, *next; 00384 00385 prev = current->prev; 00386 next = current->next; 00387 00388 if (next) 00389 next->prev = prev; 00390 00391 if (prev) 00392 prev->next = next; 00393 00394 SafeDelete(current); 00395 } 00396 };
1.2.18