Main Page   Class Hierarchy   Compound List   File List   Compound Members  

list.h

00001 /*=============================================================================
00002         File: list.h
00003      Purpose:      
00004     Revision: $Id: list.h,v 1.2 2002/05/13 21:07:45 philosophil Exp $
00005   Created by: Philippe Lavoie          (3 Oct, 1996)
00006  Modified by: 
00007 
00008  Copyright notice:
00009           Copyright (C) 1996-1997 Philippe Lavoie
00010  
00011           This library is free software; you can redistribute it and/or
00012           modify it under the terms of the GNU Library General Public
00013           License as published by the Free Software Foundation; either
00014           version 2 of the License, or (at your option) any later version.
00015  
00016           This library is distributed in the hope that it will be useful,
00017           but WITHOUT ANY WARRANTY; without even the implied warranty of
00018           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019           Library General Public License for more details.
00020  
00021           You should have received a copy of the GNU Library General Public
00022           License along with this library; if not, write to the Free
00023           Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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   // Reset Mode
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        Member: BasicList --- basic list constructor
00102    Basic list constructor.
00103         Input: 
00104        Output:
00105  Restrictions: 
00106    author Philippe Lavoie (28 October 1997)
00107   Modified by:
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        Member: BasicList --- copy constructor
00120    The copy constructor.
00121         Input: a --> the list to copy
00122        Output:
00123  Restrictions: 
00124    author Philippe Lavoie (28 October 1997)
00125   Modified by:
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        Member: add --- adds an object to the list
00138    Adds an object to the list
00139         Input: obj --> the object to add to the list
00140        Output:
00141  Restrictions: 
00142    author Philippe Lavoie (28 October 1997)
00143   Modified by:
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        Member: add --- adds an object to the list
00163    Adds an object to the list
00164         Input: obj --> the object to add to the list
00165        Output:
00166  Restrictions: 
00167    author Philippe Lavoie (28 October 1997)
00168   Modified by:
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        Member: remove --- finds an element and remove it from the list
00179    Finds an element and delete it from the list. The element
00180                will {\em not} be deleted. This is up to the calling function.
00181         Input: obj --> the element to search
00182        Output: a pointer to obj if it was found in the list, 0 otherwise
00183  Restrictions: 
00184    author Philippe Lavoie (28 October 1997)
00185   Modified by:
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        Member: reset --- deletes all the node of the list
00236    Deletes all the nodes of the list. If the reset mode is
00237                set to delete the elements, all the elements will be 
00238                deleted. Otherwise, the elements are removed but not
00239                deleted.
00240         Input: 
00241        Output: 
00242  Restrictions: 
00243    author Philippe Lavoie (28 October 1997)
00244   Modified by:
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        Member: operator= --- copies a list
00273    Copies a list
00274         Input: a --> the list to copy
00275        Output: a reference to itself
00276  Restrictions: 
00277    author Philippe Lavoie (28 October 1997)
00278   Modified by:
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        Member: addElements --- adds the elements from a list
00313    Copies a list
00314         Input: a --> the list to copy
00315        Output: a reference to itself
00316  Restrictions: 
00317    author Philippe Lavoie (28 October 1997)
00318   Modified by:
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        Member: erase --- removes and deletes the element from the list
00337    Removes and deletes the element from the list. If the object
00338                is not in the list, nothing happens.
00339         Input: obj <-> the object to delete
00340        Output: 
00341  Restrictions: 
00342    author Philippe Lavoie (28 October 1997)
00343   Modified by:
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        Member: operator[] --- returns the nth element from the list
00355    Returns the nth element from the list or 0 if the value
00356                is out of bound.
00357         Input: i <-- the element to return
00358        Output: A pointer to the nth element or 0 if n is out of bound
00359  Restrictions: 
00360    author Philippe Lavoie (28 October 1997)
00361   Modified by:
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

Generated on Tue Jun 24 13:26:55 2003 for NURBS++ by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002