Updated source code from upstream SVN
[svmtool++.git] / include / list.h
blobb097ab329624e5b9ba5209c85b0d8925045bbf42
1 /*
2 * Copyright (C) 2004 Jesus Gimenez, Lluis Marquez and Senen Moya
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef SIMPLELIST_H
22 template <typename T>
23 struct listNode
25 int ord;
26 T data;
27 listNode<T>* next;
28 listNode<T>* previous;
31 template <typename T>
32 class simpleList
35 private:
36 //List Control
37 listNode<T>* first;
38 listNode<T>* last;
39 listNode<T>* index;
40 int numObj;
42 public:
43 // ~simpleList();
44 // simpleList();
45 // void deleteList();
46 // int next();
47 // int previous();
48 // void setFirst();
49 // void *get(int position);
50 // void *getIndex();
51 // void *getFirst();
52 // void *getLast();
53 // int show();
54 // int add(void *object);
55 // int delIndex();
56 // int isEmpty();
57 // int numElements();
59 /****************************************************************************/
60 /* Simple List */
61 /****************************************************************************/
62 void deleteList()
64 int cont = numObj;
66 if (first==0) return;
67 listNode<T>* aux=first;
69 while (first->next!=0 && cont >= 1)
71 aux = first;
72 first = first->next;
73 cont = cont - 1;
74 delete aux;
77 delete last;
78 numObj = 0;
79 first = 0;
80 last = 0;
81 index = 0;
84 /****************************************************************************/
86 ~simpleList()
88 deleteList();
91 /****************************************************************************/
93 simpleList()
95 numObj = 0;
96 first = 0;
97 last = 0;
98 index = 0;
101 /****************************************************************************/
103 /*Move Interest Point to next element */
104 bool next()
106 if ((index == 0) || (index->next == 0)) return false;
107 index = index->next;
108 return true;
111 /****************************************************************************/
113 /* Move Interest Point to previous element */
114 bool previous()
116 if ((index==0) || (index->previous==0)) return false;
117 index = index->previous;
118 return true;
121 /****************************************************************************/
123 /* Get Interest Point */
124 T* getIndex()
126 if ( index == 0 ) return 0;
127 else return &index->data;
130 /****************************************************************************/
132 /* Get Interest Point */
133 T* getFirst()
135 return &first->data;
138 /****************************************************************************/
140 T* getLast()
142 return &last->data;
145 /****************************************************************************/
147 void setFirst()
149 index = first;
152 /****************************************************************************/
154 T* get(int position)
156 listNode<T>* aux;
157 int i;
159 if (numObj == 0 || position >= numObj)
160 return 0;
162 aux = first;
164 for(i=0; i<position; i++)
166 if(aux->next != 0) aux = aux->next;
167 else return 0;
169 return aux->data;
172 /****************************************************************************/
174 /* Show list elements */
175 int show()
177 if (first==0) return 0;
179 listNode<T>* actual=first;
181 while (actual->next!=0)
183 actual=actual->next;
186 return 0;
189 /****************************************************************************/
191 int add(T object)
193 listNode<T>* aux = new listNode<T>();
195 if(numObj == 0)
197 aux->previous=0;
198 first = aux;
199 last = aux;
200 index = aux;
202 else
204 aux->previous = last;
205 last->next = aux;
206 last = aux;
209 aux->ord = numObj;
210 aux->data = object;
211 aux->next=0;
212 numObj++;
213 return numObj;
216 /****************************************************************************/
218 int delIndex()
220 listNode<T>* aux = index;
222 if(numObj == 0) return -1;
224 if (index==last && index==first)
226 /* first = aux->next;
227 aux->previous = 0;
228 index = first;
229 last = aux->previous;
230 last->next = 0;
231 index = last;*/
232 first = 0;
233 index = 0;
234 last = 0;
235 aux->previous = 0;
236 aux->next = 0;
238 else if (index==first)
241 first = aux->next;
242 first->previous = 0;
243 index = first;
245 else if (index==last)
247 last = aux->previous;
248 last->next = 0;
249 index = last;
251 else
253 index = index->previous;
254 aux->previous->next = aux->next;
255 aux->next->previous = aux->previous;
258 numObj--;
259 delete aux;
260 return numObj;
263 /****************************************************************************/
265 bool isEmpty()
267 return (numObj == 0 || first == 0);
270 /****************************************************************************/
272 int numElements()
274 return numObj;
281 #define SIMPLELIST_H
282 #endif