Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

List.cpp

Go to the documentation of this file.
00001 /*
00002   List - just a simple list class (Object elements)
00003  
00004   Module:    $RCSfile: List.cpp,v $
00005   Date:      $Date: 2001/11/20 23:38:00 $
00006   Version:   $Revision: 1.5 $
00007   ID:        $Id: List.cpp,v 1.5 2001/11/20 23:38:00 zoran Exp $
00008   Authors:   Zoran Constantinescu <zoranc@acm.org>
00009 */
00010 
00011 
00012 #include "List.h"
00013 
00014 //*********************************************************************
00015 // List::List()
00016 //   Constructor
00017 //
00018 List::List()
00019 {
00020     head = tail = current = 0;
00021     number = 0;
00022     current_index = -1;
00023 }
00024 
00025 
00026 //*********************************************************************
00027 // List::~List()
00028 //   Destructor
00029 //
00030 List::~List()
00031 {
00032     Destroy();
00033 }
00034 
00035 
00036 //*********************************************************************
00037 // void List::Release()
00038 //   Release all the objects from our list.
00039 //
00040 void List::Release()
00041 {
00042   listnode    *node;
00043   while (head)
00044   {
00045     node = head;
00046     head = head->next;
00047     delete node;
00048   }
00049   head = tail = current = 0;
00050   number = 0;
00051   current_index = -1;
00052 }
00053 
00054 
00055 //*********************************************************************
00056 // void List::Destroy()
00057 //   Delete all the objects from our list.
00058 //
00059 void List::Destroy()
00060 {
00061     listnode    *node;
00062     while (head)
00063     {
00064   node = head;
00065   head = head->next;
00066   delete node->object;
00067   delete node;
00068     }
00069     head = tail = current = 0;
00070     number = 0;
00071     current_index = -1;
00072 }
00073 
00074 
00075 //*********************************************************************
00076 // void List::Add(Object *object)
00077 //   Add an object to the list.
00078 //
00079 void List::Add(Object *object)
00080 {
00081     listnode    *node = new listnode;
00082     node->next = 0;
00083     node->prev = tail;
00084     node->object = object;
00085     if (tail)
00086     {
00087   tail->next = node;
00088   tail = node;
00089     }
00090     else
00091     {
00092   head = tail = node;
00093     }
00094 
00095     number++;
00096 }
00097 
00098 
00099 //*********************************************************************
00100 // void List::Insert(Object *object, int position)
00101 //   Add an object to the list.
00102 //
00103 void List::Insert(Object *object, int position)
00104 {
00105     listnode    *node = new listnode;
00106     node->next = 0;
00107     node->prev = 0;
00108     node->object = object;
00109 
00110     listnode    *ln = head;
00111 
00112     for (int i = 0; i < position && ln; i++, ln = ln->next)
00113   ;
00114     if (!ln)
00115     {
00116   node->prev = tail;
00117   if (tail)
00118       tail->next = node;
00119   tail = node;
00120 
00121   //
00122   // The list is empty.  This is a simple case, then.
00123   //
00124   if (!head)
00125       head = node;
00126     }
00127     else
00128     {
00129   if (ln == head)
00130   {
00131       node->next = head;
00132       node->next->prev = node;
00133       head = node;
00134   }
00135   else
00136   {
00137       node->next = ln;
00138       node->prev = ln->prev;
00139       node->prev->next = node;
00140       node->next->prev = node;
00141   }
00142     }
00143 
00144     current_index = -1;;
00145     number++;
00146 }
00147 
00148 
00149 //*********************************************************************
00150 // void List::Assign(Object *object, int position)
00151 //   Assign a new value to an index.
00152 //
00153 void List::Assign(Object *object, int position)
00154 {
00155     //
00156     // First make sure that there is something there!
00157     //
00158     while (number < position + 1)
00159     {
00160   Add(0);
00161     }
00162 
00163     //
00164     // Now find the listnode to put the new object in
00165     //
00166     listnode  *temp = head;
00167 
00168     for (int i = 0; temp && i < position; i++)
00169     {
00170   temp = temp->next;
00171     }
00172 
00173     current_index = -1;
00174     delete temp->object;
00175     temp->object = object;
00176 }
00177 
00178 
00179 //*********************************************************************
00180 // int List::Remove(Object *object)
00181 //   Remove an object from the list.
00182 //
00183 int List::Remove(Object *object)
00184 {
00185     listnode    *node = head;
00186     while (node)
00187     {
00188   if (node->object == object)
00189   {
00190       //
00191       // Found it!
00192       //
00193       //
00194       // If we are in the middle of a Get_Next() sequence, we need to
00195       // fix up any problems with the current node.
00196       //
00197       if (current == node)
00198       {
00199     current = node->next;
00200       }
00201 
00202       if (head == tail)
00203       {
00204     head = tail = 0;
00205       }
00206       else if (head == node)
00207       {
00208     head = head->next;
00209     head->prev = 0;
00210       }
00211       else if (tail == node)
00212       {
00213     tail = tail->prev;
00214     tail->next = 0;
00215       }
00216       else
00217       {
00218     node->next->prev = node->prev;
00219     node->prev->next = node->next;
00220       }
00221 
00222       delete node;
00223       number--;
00224       current_index = -1;
00225       return 1;
00226   }
00227   node = node->next;
00228     }
00229     return 0;
00230 }
00231 
00232 
00233 //*********************************************************************
00234 // Object *List::Get_Next()
00235 //   Return the next object in the list.
00236 //
00237 Object *List::Get_Next()
00238 {
00239     listnode  *temp = current;
00240 
00241     if (current)
00242     {
00243   current = current->next;
00244   if (current_index >= 0)
00245       current_index++;
00246     }
00247     else
00248   return 0;
00249     return temp->object;
00250 }
00251 
00252 
00253 //*********************************************************************
00254 // Object *List::Get_First()
00255 //   Return the first object in the list.
00256 //
00257 Object *List::Get_First()
00258 {
00259     Start_Get(); 
00260     return Get_Next();
00261 /*    if (head)
00262   return head->object;
00263     else
00264   return 0; */
00265 }
00266 
00267 
00268 //*********************************************************************
00269 // int List::Index(Object *obj)
00270 //   Return the index of an object in the list.
00271 //
00272 int List::Index(Object *obj)
00273 {
00274     listnode  *temp = head;
00275     int     index = 0;
00276 
00277     while (temp && temp->object != obj)
00278     {
00279   temp = temp->next;
00280   index++;
00281     }
00282     if (index >= number)
00283   return -1;
00284     else
00285   return index;
00286 }
00287 
00288 
00289 //*********************************************************************
00290 // Object *List::Next(Object *prev)
00291 //   Return the next object in the list.  Using this, the list will
00292 //   appear as a circular list.
00293 //
00294 Object *List::Next(Object *prev)
00295 {
00296     listnode  *node = head;
00297     while (node)
00298     {
00299   if (node->object == prev)
00300   {
00301       node = node->next;
00302       if (!node)
00303     return head->object;
00304       else
00305     return node->object;
00306   }
00307   node = node->next;
00308     }
00309   
00310     return 0;
00311 }
00312 
00313 
00314 //*********************************************************************
00315 // Object *List::Previous(Object *prev)
00316 //   Return the previous object in the list.  Using this, the list will
00317 //   appear as a circular list.
00318 //
00319 Object *List::Previous(Object *prev)
00320 {
00321     listnode  *node = tail;
00322     while (node)
00323     {
00324   if (node->object == prev)
00325   {
00326       node = node->prev;
00327       if (!node)
00328     return tail->object;
00329       else
00330     return node->object;
00331   }
00332   node = node->prev;
00333     }
00334   
00335     return 0;
00336 }
00337 
00338 
00339 //*********************************************************************
00340 // Object *List::Nth(int n)
00341 //   Return the nth object in the list.
00342 //
00343 Object *List::Nth(int n)
00344 {
00345     if (n < 0 || n >= number)
00346         return 0;
00347 
00348     listnode  *temp = head;
00349 
00350     if (current_index == n)
00351   return current->object;
00352 
00353     if (current && current_index >= 0 && n == current_index + 1)
00354     {
00355   current = current->next;
00356   if (!current)
00357   {
00358       current_index = -1;
00359       return 0;
00360   }
00361   current_index = n;
00362   return current->object;
00363     }
00364 
00365     for (int i = 0; temp && i < n; i++)
00366     {
00367   temp = temp->next;
00368     }
00369 
00370     if (temp)
00371     {
00372   current_index = n;
00373   current = temp;
00374   return temp->object;
00375     }
00376     else
00377   return 0;
00378 }
00379 
00380 
00381 //*********************************************************************
00382 // Object *List::Last()
00383 //   Return the last object inserted.
00384 //
00385 Object *List::Last()
00386 {
00387     if (tail)
00388     {
00389   return tail->object;
00390     }
00391 
00392     return 0;
00393 }
00394 
00395 
00396 //*********************************************************************
00397 // Object *List::Copy()
00398 //   Return a deep copy of the list.
00399 //
00400 Object *List::Copy()
00401 {
00402     List  *list = new List;
00403 
00404     Start_Get();
00405     Object  *obj;
00406     while ((obj = Get_Next()))
00407     {
00408   list->Add(obj->Copy());
00409     }
00410     return list;
00411 }
00412 
00413 
00414 //*********************************************************************
00415 // List &List::operator=(List &list)
00416 //   Return a deep copy of the list.
00417 //
00418 List &List::operator=(List &list)
00419 {
00420     Destroy();
00421     list.Start_Get();
00422     Object  *obj;
00423     while ((obj = list.Get_Next()))
00424     {
00425   Add(obj->Copy());
00426     }
00427     return *this;
00428 }
00429 
00430 

Generated on Mon Nov 25 12:46:28 2002 for qadpz by doxygen1.2.18