[go: up one dir, main page]

Menu

[r990]: / mia2 / vistaio / list.c  Maximize  Restore  History

Download this file

380 lines (294 with data), 8.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
*
* Copyrigh (C) 2004 Max-Planck-Institute of Cognitive Neurosience
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*$Id: list.c 52 2004-03-02 15:53:19Z tittge $ */
/*! \file list.c
* \brief routines for manipulating VList instances
* \author Arthur Pope, UBC Laboratory for Computational Intelligentce
*/
#include "vistaio/vistaio.h"
/* Later in this file: */
static VNodePtrType MakeNode (VPointer item, VNodePtrType prev,
VNodePtrType next);
/*
* MakeNode
*
* Make a node
*/
static VNodePtrType MakeNode (VPointer item, VNodePtrType prev,
VNodePtrType next)
{
VNodePtrType result = VMalloc (sizeof (struct V_Node));
result->item = item;
result->prev = prev;
result->next = next;
return result;
}
/*! \brief Make a new, empty list, and returns its reference.
*
* \return VList
*/
VList VListCreate (void)
{
VList vlist = VMalloc (sizeof (struct V_List));
VNodePtrType dummy_head, dummy_tail;
dummy_head = VMalloc (sizeof (struct V_Node));
dummy_tail = VMalloc (sizeof (struct V_Node));
dummy_head->item = NULL;
dummy_head->prev = NULL;
dummy_head->next = dummy_tail;
dummy_tail->item = NULL;
dummy_tail->prev = dummy_head;
dummy_tail->next = NULL;
vlist->head = dummy_head;
vlist->tail = dummy_tail;
vlist->current = dummy_head;
vlist->count = 0;
return vlist;
}
/*! \brief Return a pointer to the first item in vlist,
* and make the first item the current item.
*
* \param vlist
* \return VPointer
*/
VPointer VListFirst (VList vlist)
{
if (vlist->count == 0) /* empty vist, move beyond beginning */
vlist->current = vlist->head;
else /* vlist not empty, move to beginning */
vlist->current = vlist->head->next;
return vlist->current->item;
}
/*! \brief Return a pointer to the last item in vlist,
* and make the last item the current item.
*
* \param vlist
* \return VPointer
*/
VPointer VListLast (VList vlist)
{
if (vlist->count == 0) /* empty vlist, move beyond end */
vlist->current = vlist->tail;
else /* vlist not empty, move to end */
vlist->current = vlist->tail->prev;
return vlist->current->item;
}
/*! \brief Advance vlist's current item by one, return the
* new current item. Return NULL if the new current
* item is beyond the end of vlist.
*
* \param vlist
* \return VPointer
*/
VPointer VListNext (VList vlist)
{
if (vlist->current == vlist->tail)
/* already beyond end, no action */
;
else /* move to next node */
vlist->current = vlist->current->next;
return vlist->current->item;
}
/*! \brief Back up vlist's current item by one, return the
* new current item. Return NULL if the new current
* item is before the beginning of vlist.
*
* \param vlist
* \return VPointer
*/
VPointer VListPrev (VList vlist)
{
if (vlist->current == vlist->head)
/* already before beginning, no action */
;
else /* move to previous node */
vlist->current = vlist->current->prev;
return vlist->current->item;
}
/*! \brief Add item to vlist immediately after the current item,
*
* and make item the current item. If the
* current pointer is before the beginning of vlist,
* item is added at the beginning. If the current
* pointer is beyond the end of vlist, item is
* added at the end.
*
* \param vlist
* \param item
*/
void VListAdd (VList vlist, VPointer item)
{
VNodePtrType add_me;
if (vlist->current == vlist->tail)
/* current pointer beyond end, add to end */
vlist->current = vlist->tail->prev;
add_me = MakeNode (item, vlist->current, vlist->current->next);
add_me->prev->next = add_me;
add_me->next->prev = add_me;
vlist->current = add_me;
vlist->count++;
}
/*! \brief Add item to vlist immediately before the current item,
*
* and make item the current item. If the current pointer is before
* the beginning of vlist, item is added at the beginning. If the current
* pointer is beyond the end of vlist, item is added at the end.
*
* \param vlist
* \param item
*/
void VListInsert (VList vlist, VPointer item)
{
VNodePtrType add_me;
if (vlist->current == vlist->head)
/* current pointer before beginning, add to beginning */
vlist->current = vlist->head->next;
add_me = MakeNode (item, vlist->current->prev, vlist->current);
add_me->prev->next = add_me;
add_me->next->prev = add_me;
vlist->current = add_me;
vlist->count++;
}
/*! \brief Add item to the end of vlist, and make item the current item.
*
* \param vlist
* \param item
*/
void VListAppend (VList vlist, VPointer item)
{
vlist->current = vlist->tail; /* move beyond end */
VListAdd (vlist, item);
}
/*! \brief Add item to the beginning of vlist, and make
* item the current item.
*
* \param vlist
* \param item
*/
void VListPrepend (VList vlist, VPointer item)
{
vlist->current = vlist->head; /* move before beginning */
VListAdd (vlist, item);
}
/*! \brief Return current item and take it out of vlist.
*
* Make the next item the current one.
*
* \param vlist
* \return VPointer
*/
VPointer VListRemove (VList vlist)
{
VPointer return_me;
VNodePtrType free_me;
return_me = vlist->current->item;
if ((vlist->current == vlist->tail)
|| (vlist->current == vlist->head))
/* current pointer before beginning or beyond end, no action */
;
else { /* free current node */
vlist->current->prev->next = vlist->current->next;
vlist->current->next->prev = vlist->current->prev;
free_me = vlist->current;
vlist->current = vlist->current->next;
VFree (free_me);
vlist->count--;
}
return return_me;
}
/*! \brief Add vlist2 to the end of vlist1.
*
* The current pointer is set to the current pointer of vlist1.
* vlist2 no longer exists after the operation.
*
* \param vlist1
* \param vlist2
*/
void VListConcat (VList vlist1, VList vlist2)
{
VNodePtrType free_me, free_me_too;
free_me = vlist1->tail;
free_me_too = vlist2->head;
vlist1->tail->prev->next = vlist2->head->next;
vlist2->head->next->prev = vlist1->tail->prev;
if (vlist1->current == vlist1->tail)
/* current pointer of vlist1 points beyond end,
set it to first node of vlist2 */
vlist1->current = vlist2->head->next;
vlist1->tail = vlist2->tail;
vlist1->count += vlist2->count;
VFree (free_me);
VFree (free_me_too);
VFree (vlist2);
}
/*! \brief Delete vlist.
*
* \param vlist
* \param item_free A pointer to a routine that frees an item.
*/
void VListDestroy (VList vlist, void (*item_free) ())
{
VPointer free_me;
vlist->current = vlist->head->next;
while (vlist->current != vlist->tail) {
free_me = VListRemove (vlist);
(*item_free) (free_me);
}
VFree (vlist->head);
VFree (vlist->tail);
VFree (vlist);
}
/*! \brief Return last item and take it out of vlist.
*
* Make the new last item the current one.
*
* \param vlist
* \return VPointer
*/
VPointer VListTrim (VList vlist)
{
VPointer return_me;
return_me = VListLast (vlist);
VListRemove (vlist);
VListLast (vlist);
return return_me;
}
/*! \brief Searche vlist starting at the current item until the end is
* reached or a match is found.
*
* \param vlist
* \param comp
* \param comp_arg
* \return VPointer
*/
VPointer VListSearch (VList vlist, int (*comp) (), VPointer comp_arg)
{
if (vlist->current == vlist->head)
/* before beginning, go to next node */
vlist->current = vlist->current->next;
while (vlist->current != vlist->tail) {
if ((*comp) (vlist->current->item, comp_arg))
/* a match is found */
return (vlist->current->item);
else
vlist->current = vlist->current->next;
}
/* no match */
return vlist->current->item;
}