00001 /* 00002 Copyright (C) 1999 Carsten Winkelholz 00003 00004 Address: FGAN Forschungsgesellschaft fr Angewandte Naturwissenschaften e. V. 00005 Neuenahrer Str. 20 00006 D - 53343 Wachtberg 00007 00008 Email: winkelholz@fgan.de 00009 00010 This program is free software; you can redistribute it and/or 00011 modify it under the terms of the GNU General Public License 00012 as published by the Free Software Foundation; either version 2 00013 of the License, or (at your option) any later version. 00014 00015 This program is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 GNU General Public License for more details. 00019 00020 You should have received a copy of the GNU General Public License 00021 along with this program; if not, write to the Free Software 00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00023 */ 00024 00025 00026 #ifndef KETTE_HC 00027 #define KETTE_HC 00028 00029 #include <assert.h> 00030 00031 #include "../oawconfig.h" 00032 00033 OAW_BEGIN_NAMESPACE 00034 00035 template <class Typ> class KetteC; 00036 00037 template <class Typ> 00038 class KettenElementC 00039 { 00040 friend class KetteC<Typ>; 00041 public: 00042 KettenElementC(const Typ& t){v=0;r=0;element=t;} 00043 inline KettenElementC* forward(){return v;} 00044 inline KettenElementC* back(){return r;} 00045 public: 00046 Typ element; 00047 protected: 00048 KettenElementC* v; 00049 KettenElementC* r; 00050 }; 00051 00052 00053 template <class Typ> 00054 class KetteC 00055 { 00056 public: 00057 00058 class Iter{ 00059 public: 00060 Iter(){p=0;} 00061 Iter(KettenElementC<Typ>*pp){p=pp;} 00062 const Iter& operator ++(int){if(p) p=p->forward(); return *this;} 00063 const Iter& operator --(int){if(p) p=p->back(); return *this;} 00064 int operator != (const Iter& i) const{ 00065 return p!=i.p; 00066 } 00067 int operator == (const Iter& i) const{ 00068 return p==i.p; 00069 } 00070 const Typ& operator * (){ 00071 return p->element; 00072 } 00073 protected: 00074 KettenElementC<Typ>* p; 00075 }; 00076 public: 00077 long size(){return m_laenge;} 00078 Iter begin(){return m_zanfang;} 00079 Iter end(){return 0;} 00080 KettenElementC<Typ>* pBegin() const {return m_zanfang;}; 00081 KettenElementC<Typ>* pEnd(){return m_zende;} 00082 KetteC(){m_zanfang=m_zende=0;m_laenge=0;m_loop=0;} 00083 KetteC<Typ>(const KetteC<Typ>& k) 00084 { 00085 m_zanfang=m_zende=0; 00086 m_laenge=0; 00087 m_loop=0; 00088 KettenElementC<Typ> *z; 00089 z=k.m_zanfang; 00090 if(z==0) return; 00091 while(z!=k.m_zende) 00092 { 00093 push_back(z->element); 00094 z=z->v; 00095 } 00096 push_back(z->element); 00097 if(k.m_loop){CloseLoop();} 00098 } 00099 00100 ~KetteC() 00101 { 00102 if(m_laenge==0) {return;} 00103 m_zanfang->r=0; m_zende->v=0; 00104 KettenElementC<Typ> *z=m_zanfang; 00105 while ((z!=m_zende)&&(z!=0)){z=z->v;delete (z->r);} 00106 if(z!=0){delete z;} 00107 } 00108 00109 void RemoveAll(const Typ& t) 00110 { 00111 if(m_zanfang==0) return; 00112 KettenElementC<Typ> *z,*tmpz; 00113 z=m_zanfang; 00114 if ((z==0)||(m_laenge==0)){return;} 00115 while(z!=0){ 00116 if(z->element==t){ 00117 tmpz =z; 00118 z=z->v; 00119 Remove(tmpz); 00120 }else{ 00121 z=z->v; 00122 } 00123 } 00124 } 00125 00126 void RemoveSingle(const Typ& t) 00127 { 00128 if(m_zanfang==0) return; 00129 KettenElementC<Typ> *z; 00130 z=m_zanfang; 00131 if ((z==0)||(m_laenge==0)){return;} 00132 while(z!=0){ 00133 if(z->element==t){Remove(z);return;} 00134 z=z->v; 00135 } 00136 } 00137 00138 void Remove(KettenElementC<Typ>* z) 00139 { 00140 assert(m_zanfang!=0); 00141 if(m_laenge==1){ 00142 assert(z==m_zanfang);delete z;m_laenge=0;m_zanfang=0;m_zende=0;return;} 00143 if(z==m_zanfang){m_zanfang=z->v;m_zanfang->r=0;m_laenge--; delete z;return;} 00144 if(z==m_zende){m_zende=z->r;m_zende->v=0;m_laenge--; delete z;return;} 00145 z->r->v=z->v; 00146 z->v->r=z->r; 00147 delete z; 00148 m_laenge--; 00149 } 00150 00151 short RemoveFirst() 00152 { 00153 KettenElementC<Typ> *z; 00154 z=m_zanfang; 00155 if(z==0) return 0; 00156 if(m_loop){CutLoop();m_loop=1;} 00157 m_zanfang=z->v; 00158 delete z; 00159 m_laenge--; 00160 if(m_laenge==0){ 00161 m_zanfang=m_zende=0; 00162 }else{ 00163 m_zanfang->r=0; 00164 } 00165 if(m_loop){CloseLoop();} 00166 return 1; 00167 } 00168 00169 short RemoveLast() 00170 { 00171 KettenElementC<Typ> *z; 00172 z=m_zende; 00173 if(z==0) return 0; 00174 if(m_loop){CutLoop();m_loop=1;} 00175 m_zende=z->r; 00176 delete z; 00177 m_laenge--; 00178 if(m_laenge==0){ 00179 m_zanfang=m_zende=0; 00180 }else{ 00181 m_zende->v=0; 00182 } 00183 if(m_loop){CloseLoop();} 00184 return 1; 00185 } 00186 00187 short RemoveFirst(Typ& t) 00188 { 00189 if(m_zanfang==0){return 0;} 00190 t=m_zanfang->element; 00191 return RemoveFirst(); 00192 } 00193 00194 short RemoveLast(Typ& t) 00195 { 00196 if(m_zende==0){return 0;} 00197 t=m_zende->element; 00198 return removeLast(); 00199 } 00200 00201 void push_back(const Typ& t) 00202 { 00203 KettenElementC<Typ> *z = new KettenElementC<Typ>(t); 00204 if(z==0) return; 00205 if(m_loop){CutLoop();m_loop=1;} 00206 if(m_zanfang==0){m_zanfang=m_zende=z;} 00207 else{z->r=m_zende;m_zende->v=z;m_zende=z;} 00208 if(m_loop){CloseLoop();} 00209 m_laenge++; 00210 } 00211 00212 void Empty() 00213 { 00214 KettenElementC<Typ> *z; 00215 if(m_zanfang==0){return;} 00216 if(m_zanfang!=0){m_zanfang->r=0;m_zende->v=0;} 00217 CutLoop(); 00218 z=m_zanfang; 00219 while (z!=m_zende){z=z->v;delete(z->r);} 00220 if(z!=0){delete z;} 00221 m_zanfang=0; m_zende=0; 00222 m_laenge=0; 00223 } 00224 00225 00226 KetteC<Typ>& operator = (const KetteC<Typ>& k) 00227 { 00228 KettenElementC<Typ> *z; 00229 Empty(); 00230 z=k.m_zanfang; 00231 if(z==0) return *this; 00232 while(z!=k.m_zende) 00233 { 00234 push_back(z->element); 00235 z=z->v; 00236 } 00237 push_back(z->element); 00238 if(k.m_loop){CloseLoop();} 00239 return *this; 00240 } 00241 00242 KettenElementC<Typ>* GetElement(const Typ& t){ 00243 KettenElementC<Typ> *z=m_zanfang; 00244 if(m_zanfang==0){return 0;} 00245 while(z!=m_zende){ 00246 if(z->element == t){return 1;} 00247 z=z->v; 00248 } 00249 if(z->element == t){return z;} 00250 return 0; 00251 } 00252 00253 short Contain(const Typ& t) 00254 { 00255 KettenElementC<Typ> *z=m_zanfang; 00256 if(m_zanfang==0){return 0;} 00257 while(z!=m_zende){ 00258 if(z->element == t){return 1;} 00259 z=z->v; 00260 } 00261 if(z->element == t){return 1;} 00262 return 0; 00263 } 00264 00265 long Count(const Typ& t) 00266 { 00267 long i=0; 00268 KettenElementC<Typ> *z=m_zanfang; 00269 if(m_zanfang==0){return 0;} 00270 while(z!=0){ 00271 if(z->element == t){i++;} 00272 z=z->v; 00273 } 00274 return i; 00275 } 00276 00277 protected: 00278 void CloseLoop() 00279 { 00280 if(m_zanfang==0){return;} 00281 m_loop=1; 00282 m_zende->v=m_zanfang; 00283 m_zanfang->r=m_zende; 00284 } 00285 00286 void CutLoop() 00287 { 00288 if(m_zanfang==0){return;} 00289 m_loop=0; 00290 m_zende->v=0; 00291 m_zanfang->r=0; 00292 } 00293 00294 00295 protected: 00296 long m_laenge; 00297 int m_loop; 00298 KettenElementC<Typ> *m_zanfang; 00299 KettenElementC<Typ> *m_zende; 00300 }; 00301 00302 OAW_END_NAMESPACE 00303 00304 #endif 00305 00306 00307 00308 00309 00310
1.3-rc2