ESyS-Particle  4.0.1
t_list.hpp
00001 
00002 //                                                         //
00003 // Copyright (c) 2003-2011 by The University of Queensland //
00004 // Earth Systems Science Computational Centre (ESSCC)      //
00005 // http://www.uq.edu.au/esscc                              //
00006 //                                                         //
00007 // Primary Business: Brisbane, Queensland, Australia       //
00008 // Licensed under the Open Software License version 3.0    //
00009 // http://www.opensource.org/licenses/osl-3.0.php          //
00010 //                                                         //
00012 
00013 template <class T>
00014 void Stack<T>::Push(T* V) {
00015   L.InsertAtStart(V) ;
00016 } 
00017 
00018 template <class T>
00019 T* Stack<T>::Pop() {
00020   L.First() ;
00021   T* t= L.Get() ;
00022   L.Clear() ;
00023   return t ;
00024 }
00025 
00026 template <class T>
00027 List<T>::List()
00028    {
00029    Start = End = Current = NULL ;
00030    }
00031 
00032 template <class T>
00033 List<T>::List(const List<T> &L)
00034    {
00035    Node<T> *Lcur ;
00036    Start = End = Current = NULL ;
00037    Lcur = L.Start ;
00038    while (Lcur)
00039       {
00040       Append(Lcur->Val) ;
00041       Lcur = Lcur->Next ;
00042       }
00043    }
00044 
00045 template <class T>
00046 List<T>::~List()
00047    {
00048    Destroy() ;
00049    }
00050 
00054 template <class T>
00055 int List<T>::SizeList()
00056    {
00057     int i = 0;
00058     First() ;
00059     while (!IsEnd()) {
00060         i++ ;
00061         Next() ;
00062     }
00063     return i ;
00064    }
00065 
00070 template <class T>
00071 void List<T>::InsertAtStart(T *V)
00072    {
00073    if (Start==NULL)
00074       {
00075       Start = new Node<T> ;
00076       Start->Next = NULL ;
00077       Start->Prev = NULL ;
00078       Current = Start ;
00079       End = Start ;
00080       Current->Val = V ;
00081       }
00082    else
00083       {
00084       Node<T> *Inter ;
00085       Inter = new Node<T> ;
00086       Inter->Prev = NULL ;
00087       Inter->Next = Start ;
00088       Inter->Val = V ;
00089       Start->Prev = Inter ;
00090       Start = Inter ;
00091       }
00092    }
00093 
00094 template <class T>
00095 void List<T>::Append(T *V)
00096    {
00097    if (Start==NULL)
00098       {
00099       Start = new Node<T> ;
00100       Start->Next = NULL ;
00101       Start->Prev = NULL ;
00102       Current = Start ;
00103       End = Start ;
00104       Current->Val = V ;
00105       }
00106    else
00107       {
00108       Node<T> *Inter ;
00109       Inter = new Node<T> ;
00110       Inter->Prev = End ;
00111       Inter->Next = NULL ;
00112       Inter->Val = V ;
00113       End->Next = Inter ;
00114       End = Inter ;
00115       }
00116    }
00117 
00118 template <class T>
00119 void List<T>::InsertAfter(T *V)
00120    {
00121    if ((Start==NULL)||(Current==NULL)||(Current->Next==NULL))
00122       {
00123       Append(V) ;
00124       }
00125    else
00126       {
00127       Node<T> *Inter ;
00128       Inter = new Node<T> ;
00129       Inter->Prev = Current ;
00130       Inter->Next = Current->Next ;
00131       Inter->Val = V ;
00132       Current->Next->Prev = Inter ;
00133       Current->Next = Inter ;
00134       }
00135    }
00136 
00137 template <class T>
00138 void List<T>::InsertBefore(T *V)
00139    {
00140    if ((Start==NULL)||(Current==NULL))
00141       {
00142       Append(V) ;
00143       }
00144    else if (Start==Current)
00145       {
00146       InsertAtStart(V) ;
00147       }
00148    else
00149       {
00150       Node<T> *Inter ;
00151       Inter = new Node<T> ;
00152       Inter->Prev = Current->Prev ;
00153       Inter->Next = Current ;
00154       Inter->Val = V ;
00155       Current->Prev->Next = Inter ;
00156       Current->Prev = Inter ;
00157       }
00158    }
00159 
00160 template <class T>
00161 T* List<T>::Get()
00162    {
00163 #ifdef _DEBUG
00164      if (!Current) {
00165        console.Critical() << "Invalid access in List via null pointer\n" ;
00166      }
00167      if (!Current->Val) {
00168        console.Warning() << "List just returned a null pointer\n" ;
00169      }
00170 #endif
00171    return Current->Val ;
00172    }
00173 
00174 template <class T>
00175 void List<T>::Put(T *V)
00176    {
00177    if (Current) Current->Val = V ;
00178    }
00179 
00180 template <class T>
00181 void List<T>::Clear()
00182    {
00183    if (Current)
00184       {
00185       if (Current==Start)
00186          {
00187          Start = Current->Next ;
00188          if (Start) Start->Prev = NULL ;
00189          else End=NULL ;
00190          delete Current ;
00191          Current = Start ;
00192          }
00193       else if (Current==End)
00194          {
00195          End = Current->Prev ;
00196          End->Next = NULL ;
00197          delete Current ;
00198          Current = NULL ;
00199          }
00200       else
00201          {
00202          Node<T> *Inter ;
00203 
00204          Inter = Current->Next ;
00205          Current->Prev->Next = Current->Next ;
00206          Current->Next->Prev = Current->Prev ;
00207          delete Current ;
00208          Current = Inter ;
00209          }
00210       }
00211    }
00212 
00213 template <class T>
00214 void List<T>::Destroy()
00215    {
00216    Current=Start ;
00217    while (Current) Clear() ;
00218    }
00219 
00220 template <class T>
00221 List<T>& List<T>::operator << (T *V)
00222   {
00223   InsertAfter(V) ;
00224   Next() ;
00225   return *this ;
00226   }
00227 
00228 template <class T>
00229 List<T>& List<T>::operator >> (T *V)
00230   {
00231   if (Current) V = Current->Val ;
00232   Next() ;
00233   return *this ;
00234   }
00235 
00236 template <class T>
00237 void List<T>::Next()
00238   {
00239   if (Current) Current=Current->Next ;
00240   }
00241 
00242 template <class T>
00243 void List<T>::Prev()
00244   {
00245   if (Current && Current->Prev) Current=Current->Prev ;
00246   }
00247 
00248 template <class T>
00249 void List<T>::First()
00250    {
00251    Current = Start ;
00252    }
00253 
00254 template <class T>
00255 void List<T>::Last()
00256    {
00257    Current = End ;
00258    }
00259 
00260 template <class T>
00261 int List<T>::IsEnd()
00262    {
00263    return (Current==NULL) ;
00264    }
00265 
00266 template <class T>
00267 int List<T>::IsStart()
00268    {
00269    return (Current==Start) ;
00270    }
00271 
00272 template <class T>
00273 void List<T>::Swap() // echange current avec le suivant
00274    {
00275    Node<T> *CS, *CP, *CSS, *C ;
00276 
00277    C = Current ;
00278    CS = C->Next ;
00279    CP = C->Prev ;
00280    CSS = CS->Next ;
00281    if (CP) CP->Next = CS ;
00282    CS->Prev = CP ;
00283    CS->Next = C ;
00284    C->Prev = CS ;
00285    C->Next = CSS ;
00286    if (CSS) CSS->Prev = C ;
00287    if (!CP) Start = CS ;
00288    if (!CSS) End = C ;
00289    }
00290 
00291 template <class T>
00292 List<T> List<T>::operator + (const List<T>& L)
00293    {
00294    List<T> This=*this;
00295    This+=L ;
00296    return This ;
00297    }
00298 
00299 template <class T>
00300 List<T>& List<T>::operator += (const List<T>& L)
00301    {
00302    Node<T> *Lcur ;
00303    Lcur = L.Start ;
00304    while (Lcur)
00305       {
00306       Append(Lcur->Val) ;
00307       Lcur = Lcur->Next ;
00308       }
00309    return *this ;
00310    }
00311 
00312 template <class T>
00313 List<T>& List<T>::operator = (const List<T>& L)
00314    {
00315    Node<T> *Lcur ;
00316    Destroy() ;
00317    Lcur = L.Start ;
00318    while (Lcur)
00319       {
00320       Append(Lcur->Val) ;
00321       Lcur = Lcur->Next ;
00322       }
00323    return *this ;
00324    }
00325 
00326 #if 0
00327 template <class T>
00328 void ListWS<T>::PushPos() 
00329 { 
00330   stack.Push(Current) ;
00331 }
00332  
00333 template <class T>
00334 void ListWS<T>::PopPos() 
00335 { 
00336   Current = stack.Pop() ;
00337 } 
00338 #endif