linklist.cc

00001 /* 
00002    EyeDB Object Database Management System
00003    Copyright (C) 1994-2008 SYSRA
00004    
00005    EyeDB is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009    
00010    EyeDB is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014    
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA 
00018 */
00019 
00020 /*
00021    Author: Eric Viara <viara@sysra.com>
00022 */
00023 
00024 
00025 #include <eyedb/linklist.h>
00026 #include <stdio.h>
00027 
00028 namespace eyedb {
00029 
00030   struct Link {
00031     void *object;
00032 
00033     Link *prev, *next;
00034 
00035     Link(void *);
00036     ~Link() {}
00037   };
00038 
00039   LinkedListCursor::LinkedListCursor(const LinkedList &_list)
00040   {
00041     list = (LinkedList *)&_list;
00042     link = list->f_link;
00043   }
00044 
00045   LinkedListCursor::LinkedListCursor(const LinkedList *_list)
00046   {
00047     list = (LinkedList *)_list;
00048     link = (list ? list->f_link : 0);
00049   }
00050 
00051   void
00052   LinkedListCursor::restart()
00053   {
00054     link = (list ? list->f_link : 0);
00055   }
00056 
00057   int LinkedListCursor::getNext(void* &o)
00058   {
00059     if (!link)
00060       return 0;
00061 
00062     o = link->object;
00063     link = link->next;
00064     return 1;
00065   }
00066 
00067   Link::Link(void *o)
00068   {
00069     object = o;
00070     prev = next = 0;
00071   }
00072 
00073   LinkedList::LinkedList()
00074   {
00075     link_cnt = 0;
00076     f_link = (Link *)0;
00077     l_link = (Link *)0;
00078   }
00079 
00080   int LinkedList::insertObject(void *o)
00081   {
00082     return insertObjectLast(o);
00083   }
00084 
00085   int LinkedList::insertObjectLast(void *o)
00086   {
00087     Link *l = new Link(o);
00088 
00089     if (l_link)
00090       {
00091         l_link->next = l;
00092         l->prev = l_link;
00093       }
00094     else
00095       f_link = l;
00096 
00097     l_link = l;
00098 
00099     link_cnt++;
00100     return link_cnt - 1;
00101   }
00102 
00103   int LinkedList::insertObjectFirst(void *o)
00104   {
00105     Link *l = new Link(o);
00106 
00107     if (f_link)
00108       {
00109         f_link->prev = l;
00110         l->next = f_link;
00111       }
00112     else
00113       l_link = l;
00114 
00115     f_link = l;
00116 
00117     link_cnt++;
00118     return 0;
00119   }
00120 
00121   void *LinkedList::getObject(int pos) const
00122   {
00123     if (pos < 0 || pos >= link_cnt)
00124       return 0;
00125 
00126     Link *l = f_link;
00127 
00128     for (int j = 0; j < pos; j++)
00129       l = l->next;
00130 
00131     return l->object;
00132   }
00133 
00134   void LinkedList::delete_realize(Link *l)
00135   {
00136     if (l->prev)
00137       l->prev->next = l->next;
00138     else
00139       f_link = l->next;
00140   
00141     if (l->next)
00142       l->next->prev = l->prev;
00143     else
00144       l_link = l->prev;
00145   
00146     link_cnt--;
00147     delete l;
00148   }
00149 
00150   int LinkedList::deleteObject(void *o)
00151   {
00152     int pos = 0;
00153     Link *l = f_link;
00154 
00155     while (l)
00156       {
00157         if (l->object == o)
00158           {
00159             delete_realize(l);
00160             return pos;
00161           }
00162         l = l->next;
00163         pos++;
00164       }
00165 
00166     return -1;
00167   }
00168 
00169   int LinkedList::deleteObject(int pos)
00170   {
00171     if (pos < 0 || pos >= link_cnt)
00172       return -1;
00173 
00174     Link *l = f_link;
00175 
00176     int j;
00177     for (j = 0; j < pos; j++)
00178       l = l->next;
00179 
00180     delete_realize(l);
00181     return j;
00182   }
00183 
00184   int LinkedList::getPos(void *o) const
00185   {
00186     int pos = 0;
00187     Link *l = f_link;
00188 
00189     while (l) {
00190       if (l->object == o)
00191         return pos;
00192       l = l->next;
00193       pos++;
00194     }
00195 
00196     return -1;
00197   }
00198 
00199   int LinkedList::getCount(void) const
00200   {
00201     return link_cnt;
00202   }
00203 
00204 
00205   void* LinkedList::getFirstObject() const
00206   {
00207     return (f_link ? f_link->object : 0);
00208   }
00209 
00210   void* LinkedList::getLastObject() const
00211   {
00212     return (l_link ? l_link->object : 0);
00213   }
00214 
00215   LinkedListCursor *LinkedList::startScan() const
00216   {
00217     return new LinkedListCursor(this);
00218   }
00219 
00220   int LinkedList::getNextObject(LinkedListCursor *cursor, void* &o) const
00221   {
00222     if (!cursor->link)
00223       return 0;
00224 
00225     o = cursor->link->object;
00226     cursor->link = cursor->link->next;
00227     return 1;
00228   }
00229 
00230   void LinkedList::endScan(LinkedListCursor *cursor) const
00231   {
00232     delete cursor;
00233   }
00234 
00235   void LinkedList::applyToObjects(void (*f)(void *, void *), void *user_arg) const
00236   {
00237     Link *l;
00238 
00239     l = f_link;
00240     while (l)
00241       {
00242         (*f)(l->object, user_arg);
00243         l = l->next;
00244       }
00245   }
00246 
00247 
00248   void LinkedList::empty()
00249   {
00250     Link *l, *next;
00251 
00252     l = f_link;
00253     while (l)
00254       {
00255         next = l->next;
00256         delete l;
00257         l = next;
00258       }
00259     f_link = l_link = 0;
00260     link_cnt = 0;
00261   }
00262 
00263   LinkedList::~LinkedList()
00264   {
00265     empty();
00266   }
00267 }

Generated on Mon Dec 22 18:15:58 2008 for eyedb by  doxygen 1.5.3