00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef _Matrix_list_h_
00027 #define _Matrix_list_h_
00028
00029 #include "specialType.h"
00030
00037 template<class T>
00038 struct BasicNode{
00039 public:
00040 BasicNode() { prev = next = 0 ; data = 0 ;}
00041 BasicNode(T *a) : data(a) { prev = next = 0; }
00042 ~BasicNode() { if(data) delete data ; }
00043
00044 T* data ;
00045 BasicNode<T> *prev, *next ;
00046 };
00047
00054 template<class T>
00055 class BasicList: public BasicNode<T>
00056 {
00057 public:
00058 BasicList() ;
00059 BasicList(BasicList<T>& a) ;
00060 ~BasicList() { reset() ; }
00061
00062
00063 BasicNode<T>* first() { return first_ ; }
00064 BasicNode<T>* last() { return last_ ; }
00065
00066 BasicNode<T> *current ;
00067
00068 void reset() ;
00069 void add(BasicNode<T>* obj) ;
00070 void add(const T& data) ;
00071 void addElements(BasicList<T>& list) ;
00072 BasicNode<T>* remove(BasicNode<T>* obj) ;
00073 void erase(BasicNode<T>* obj) ;
00074
00075 BasicList<T>& operator=(const BasicList<T>& a) ;
00076
00077 BasicNode<T>* goToFirst() { return (current = first_) ; }
00078 BasicNode<T>* goToNext() { if(current) return (current = current->next) ; return 0 ;}
00079 BasicNode<T>* goToPrevious() { if(current) return (current = current->prev) ; return 0 ;}
00080
00081 int size() const { return n ; }
00082
00083
00084 BasicNode<T>* operator[](int i) ;
00085
00086
00087 enum ListResetMode { delete_at_reset, keep_at_reset } ;
00088
00089 int resetMode() const { return reset_mode ; }
00090 void setResetMode(ListResetMode a ) { reset_mode = a ; }
00091
00092 protected:
00093 BasicNode<T> *first_,*last_ ;
00094 int n ;
00095 int nc ;
00096 ListResetMode reset_mode ;
00097 };
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 template <class T>
00110 BasicList<T>::BasicList(): BasicNode<T>() {
00111 first_ = last_ = 0 ;
00112 current = first_ ;
00113 reset_mode = delete_at_reset ;
00114 n = 0 ;
00115 nc = 0 ;
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 template <class T>
00128 BasicList<T>::BasicList(BasicList<T>& a): BasicNode<T>() {
00129 first_ = last_ = 0 ;
00130 current = first_ ;
00131 *this = a ;
00132 nc = 0 ;
00133 n = 0 ;
00134 }
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 template <class T>
00146 void BasicList<T>::add(BasicNode<T>* obj){
00147 if(obj){
00148 if(!first_){
00149 first_ = obj ;
00150 }
00151 else{
00152 last_->next = obj ;
00153 obj->prev = last_ ;
00154 }
00155 last_ = obj ;
00156 obj->next = 0 ;
00157 ++n ;
00158 }
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 template <class T>
00171 void BasicList<T>::add(const T& data){
00172 T *p = new T(data) ;
00173 BasicNode<T> *node = new BasicNode<T>(p) ;
00174 add(node) ;
00175 }
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 template <class T>
00188 BasicNode<T>* BasicList<T>::remove(BasicNode<T>* obj){
00189 BasicNode<T>* t ;
00190
00191 if(!obj)
00192 return 0 ;
00193
00194 if(current == obj){
00195 t = obj ;
00196 current = 0 ;
00197 if(t->prev){
00198 t->prev->next = t->next ;
00199 current = t->prev ;
00200 }
00201 if(t->next){
00202 t->next->prev = t->prev ;
00203 current = t->next ;
00204 }
00205 --n ;
00206 --nc ;
00207 if(first_==t)
00208 first_ = t->next ;
00209 if(last_==t)
00210 last_ = t->prev ;
00211 return t;
00212 }
00213
00214 t = first_ ;
00215 while(t){
00216 if(t==obj){
00217 if(t->prev)
00218 t->prev->next = t->next ;
00219 if(t->next)
00220 t->next->prev = t->prev ;
00221 --n ;
00222 if(first_==t)
00223 first_ = t->next ;
00224 if(last_==t)
00225 last_ = t->prev ;
00226 return t;
00227 }
00228 else
00229 t = t->next ;
00230 }
00231 return 0 ;
00232 }
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 template <class T>
00247 void BasicList<T>::reset(){
00248 if(reset_mode==delete_at_reset){
00249 BasicNode<T> *c ;
00250 c = first_ ;
00251 while(c){
00252 current = c ;
00253 c = current->next ;
00254 delete current ;
00255 }
00256 }
00257 else{
00258 BasicNode<T> *c ;
00259 c = first_ ;
00260 while(c){
00261 current = c ;
00262 c = current->next ;
00263 current->next = current->prev = 0 ;
00264 }
00265 }
00266 first_ = current = last_ = 0 ;
00267 n = 0 ;
00268 nc = 0 ;
00269 }
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 template <class T>
00281 BasicList<T>& BasicList<T>::operator=(const BasicList<T> &a){
00282 BasicNode<T> *t,*t2 ;
00283 T* c;
00284
00285 reset() ;
00286
00287 t = a.first_ ;
00288 while(t){
00289 c = new T(*t->data) ;
00290 t2 = new BasicNode<T>(c) ;
00291 add(t2) ;
00292
00293 if(a.current == t){
00294 current = t2 ;
00295 nc = a.nc ;
00296 }
00297
00298 t = t->next ;
00299 }
00300
00301 if(!current){
00302 current = first_ ;
00303 nc = 0 ;
00304 }
00305
00306 reset_mode = a.reset_mode ;
00307
00308 return *this ;
00309 }
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 template <class T>
00321 void BasicList<T>::addElements(BasicList<T> &list){
00322 BasicNode<T> *t,*t2 ;
00323 T* c;
00324
00325 t = list.first_ ;
00326 while(t){
00327 c = new T(*t->data) ;
00328 t2 = new BasicNode<T>(c) ;
00329 add(t2) ;
00330 t = t->next ;
00331 }
00332 }
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345 template <class T>
00346 void BasicList<T>::erase(BasicNode<T> *obj){
00347 BasicNode<T> *o ;
00348 o = remove(obj) ;
00349 if(o)
00350 delete o ;
00351 }
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363 template <class T>
00364 BasicNode<T>* BasicList<T>::operator[](int i){
00365 if(i==nc)
00366 return current ;
00367 if(i<0 || i>=n) return 0 ;
00368 if(i<nc)
00369 while(nc!=i){
00370 goToPrevious() ;
00371 --nc ;
00372 }
00373 else
00374 while(nc!=i){
00375 goToNext() ;
00376 ++nc ;
00377 }
00378 return current ;
00379 }
00380
00381 #endif