summaryrefslogtreecommitdiff
path: root/lib/dlist.c
blob: c4ee32457322799f46af50c1285a949fc608d52a (plain)
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/*
 * dlist.c
 *
 * Copyright (C) 2003 Eric J Bohm
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021110307 USA
 *
 */


/* Double linked list implementation.

 * You allocate the data and give dlist the pointer.
 * If your data is complex set the dlist->del_func to a an appropriate
 * delete function.  Otherwise dlist will just use free.

*/
#include "dlist.h"

/*
 * Return pointer to node at marker.
 * else null if no nodes.
 */

inline void *dlist_mark(Dlist *list)
{
  if(list->marker!=NULL)
    return(list->marker->data);
  else
    return(NULL);
}

/* 
 * Set marker to start.
 */

inline void dlist_start(Dlist *list)
{
  list->marker=list->head;
}

/* 
 * Set marker to end.
 */

inline void dlist_end(Dlist *list)
{
  list->marker=list->head;
}

/* internal use function
 * quickie inline to consolidate the marker movement logic
 * in one place
 *
 * when direction true it moves marker after 
 * when direction false it moves marker before.
 * return pointer to data at new marker
 * if nowhere to move the marker in desired direction return null 
 */
inline void *_dlist_mark_move(Dlist *list,int direction)
{
  if(direction)
    {
      if( list->marker && list->marker->next!=NULL)
	list->marker=list->marker->next;
      else
	return(NULL);
    }
  else
    {
      if( list->marker && list->marker->prev!=NULL)
	list->marker=list->marker->prev;
      else
	return(NULL);
    }
  if(list->marker!=list->head)
    return(list->marker->data);
  else
    return(NULL);
}

/*
 * Create new linked list to store nodes of datasize.
 * return null if list cannot be created.
 */
Dlist *dlist_new(size_t datasize)
{
  Dlist *list=NULL;
  if((list=malloc(sizeof(Dlist))))
    {
      list->marker=NULL;
      list->count=0L;
      list->data_size=datasize;
      list->del_func=free;
      list->head=&(list->headnode);
      list->head->prev=NULL;
      list->head->next=NULL;
      list->head->data=NULL;
    }
  return(list);
}

/*
 * Create new linked list to store nodes of datasize set list
 * data node delete function to the passed in del_func
 * return null if list cannot be created.
 */
Dlist *dlist_new_with_delete(size_t datasize,void (*del_func)(void*))
{
  Dlist *list=NULL;
  list=dlist_new(datasize);
  if(list!=NULL)
    list->del_func=del_func;
  return(list);
}


/*
 * remove marker node from list
 * call data_delete function on data if registered.
 * otherwise call free.
 * when direction true it moves marker after 
 * when direction false it moves marker before.
 * free marker node
 * return nothing.
 */
void dlist_delete(Dlist *list,int direction)
{
  if((list->marker != list->head)&&(list->marker!=NULL)) 
    {
      DL_node *corpse;
      corpse=list->marker;
      _dlist_mark_move(list,direction);
      if(list->head->next==corpse)
	list->head->next=corpse->next;
      if(list->head->prev==corpse)
	list->head->prev=corpse->prev;
      if(corpse->prev!=NULL) //should be impossible
	corpse->prev->next=corpse->next;
      if(corpse->next!=NULL) //should be impossible
	corpse->next->prev=corpse->prev;
      list->del_func(corpse->data);
      list->count--;
      free(corpse);
    }
}

/*
 * Insert node containing data at marker.
 * If direction true it inserts after.
 * If direction false it inserts before.
 * move marker to inserted node
 * return pointer to inserted node
 */
void *dlist_insert(Dlist *list,void *data,int direction)
{
  DL_node *new_node=NULL;
  if(list==NULL || data==NULL)
    return(NULL);
  if(list->marker==NULL) //in case the marker ends up unset
    list->marker=list->head;
  if((new_node=malloc(sizeof(DL_node))))
    {
      new_node->data=data;
      new_node->prev=NULL;
      new_node->next=NULL;
      list->count++;
      if(list->head->next==NULL) //no l
	{
	  list->head->next=list->head->prev=new_node;
	  new_node->prev=list->head;
	  new_node->next=list->head;
	}
      else if(direction)
	{
	  new_node->next=list->marker->next;
	  new_node->prev=list->marker;
	  list->marker->next->prev=new_node;
	  list->marker->next=new_node;
	}
      else
	{
	  new_node->prev=list->marker->prev;
	  new_node->next=list->marker;
	  list->marker->prev->next=new_node;
	  list->marker->prev=new_node;
	}
	list->marker=new_node;
    }
  else
    {
      return(NULL);
    }
  return(list->marker->data);
}

/* internal use only
 * Insert dl_node  at marker.
 * If direction true it inserts after.
 * If direction false it inserts before.
 * move marker to inserted node
 * return pointer to inserted node
 */
void *_dlist_insert_dlnode(struct dlist *list,struct dl_node *new_node,int direction)
{
  if(list==NULL || new_node==NULL)
    return(NULL);
  if(list->marker==NULL) //in case the marker ends up unset
    list->marker=list->head;
  list->count++;
  if(list->head->next==NULL) 
    {
      list->head->next=list->head->prev=new_node;
      new_node->prev=list->head;
      new_node->next=list->head;
    }
  else if(direction)
    {
      new_node->next=list->marker->next;
      new_node->prev=list->marker;
      list->marker->next->prev=new_node;
      list->marker->next=new_node;
    }
  else
    {
      new_node->prev=list->marker->prev;
      new_node->next=list->marker;
      list->marker->prev->next=new_node;
      list->marker->prev=new_node;
    }
  list->marker=new_node;
  return(list->marker);
}



/* 
 * Remove DL_node from list without deallocating data.
 * if marker == killme .
 *  when direction true it moves marker after 
 *  when direction false it moves marker before.
 * to previous if there is no next.
 */
void *_dlist_remove(Dlist *list,DL_node *killme,int direction)
{
  if(killme!=NULL)
    {
      void *killer_data=killme->data;
      // take care of head and marker pointers.
      if(list->marker==killme)
	_dlist_mark_move(list,direction);
      if(killme ==list->head->next)
	list->head->next=killme->next;
      if(killme==list->head->prev)  
	list->head->prev=killme->prev;
      // remove from list
      if(killme->prev !=NULL)
	killme->prev->next=killme->next;
      if(killme->next !=NULL)
	killme->next->prev=killme->prev;
      list->count--;
      free(killme);
      return(killer_data);
    }
  else
    return (NULL);
}

/* 
 * move dl_node from source to dest
 * if marker == target .
 *  when direction true it moves marker after 
 *  when direction false it moves marker before.
 * to previous if there is no next.
 */
void dlist_move(struct dlist *source, struct dlist *dest, struct dl_node *target,int direction)
{

  if(target!=NULL)
    {
      if(target==source->head)
	{
	  //not even going to try
	}
      else
	{
	  // take care of head and marker pointers.
	  if(source->marker==target)
	    _dlist_mark_move(source,direction);
	  if(target ==source->head->next)
	    source->head->next=target->next;
	  if(target==source->head->prev)  
	    source->head->prev=target->prev;
	  // remove from list
	  if(source->count==1)
	    {
	      target->prev=NULL;
	      target->next=NULL;
	      source->head->next=NULL;
	      source->head->prev=NULL;
	    }
	  else
	    {
	      if(target->prev !=NULL)
		target->prev->next=target->next;
	      if(target->next !=NULL)
		target->next->prev=target->prev;
	      target->prev=NULL;
	      target->next=NULL;
	    }
	  source->count--;
	  _dlist_insert_dlnode(dest,target,direction);
	}
    }
}


/*
 * Insert node containing data after end.
 */
void dlist_push(Dlist *list,void *data)
{
  list->marker=list->head->prev;
  dlist_insert(list,data,1);
}

/*
 * Insert node containing data at start.
 */

void dlist_unshift(Dlist *list,void *data)

{
  list->marker=list->head->next;
  dlist_insert(list,data,0);
}

void dlist_unshift_sorted(Dlist *list, void *data, 
			int (*sorter)(void *new_elem, void *old_elem))
{
	if (list->count == 0)
		dlist_unshift(list, data);
	else {
		list->marker=list->head->next;
		dlist_insert_sorted(list, data, sorter);
	}
}

/* 
 * Remove end node from list.
 * Return pointer to data in removed node.
 * Null if no nodes.
 */

void *dlist_pop(Dlist *list)
{
  return(_dlist_remove(list,list->head->prev,0));
}

/* 
 * Remove start node from list.
 * Return pointer to data in removed node.
 * Null if no nodes.
 */

void *dlist_shift(Dlist *list)
{
  return(_dlist_remove(list,list->head->next,1));
}


/* 
 * destroy the list freeing all memory
 */


void dlist_destroy(Dlist *list)
{
  if(list !=NULL)
    {
      dlist_start(list);
      dlist_next(list);
      while (dlist_mark(list)) {
	      dlist_delete(list,1);
      }
      free(list);
    }
}

/**
 *  Return void pointer to list_data element matching comp function criteria
 *  else null
 *  Does not move the marker.
 */

void *dlist_find_custom(struct dlist *list, void *target, int (*comp)(void *, void *))
{
	/* test the comp function on each node */
	struct dl_node *nodepointer;
	dlist_for_each_nomark(list,nodepointer)
		if(comp(target,nodepointer->data))
			return(nodepointer->data);
	return(NULL);
}

/**
 * Apply the node_operation function to each data node in the list
 */
void dlist_transform(struct dlist *list, void (*node_operation)(void *))
{
	struct dl_node *nodepointer;
	dlist_for_each_nomark(list,nodepointer)
		node_operation(nodepointer->data);
}

/**
 * insert new into list in sorted order
 * sorter function in form int sorter(new,ith)
 *       must return 1 for when new should go before ith
 *       else 0
 * return pointer to inserted node
 * NOTE: assumes list is already sorted
 */
void *dlist_insert_sorted(struct dlist *list, void *new, int (*sorter)(void *, void *))
{
	for(dlist_start(list),dlist_next(list); \
		list->marker!=list->head && !sorter(new,list->marker->data);dlist_next(list));
	return(dlist_insert_before(list,new));
}

/*
 * NOTE: internal use only
 */
int _dlist_merge(struct dlist *listsource, struct dlist *listdest, unsigned int passcount, int (*compare)(void *, void *))
{

  struct dl_node *l1head;
  struct dl_node *l2head;
  struct dl_node *target;
  unsigned int l1count=0;
  unsigned int l2count=0;
  unsigned int mergecount=0;
  while(listsource->count>0)
    {
      l1head=listsource->head->next;
      l2head=l1head;
      while((l1count<passcount)&&(l2head!=listsource->head))
	{
	  l2head=l2head->next;
	  l1count++;
	}
      // so now we have two lists to merge
      
      if(l2head==listsource->head)
	{// l2count
	  l2count=0;
	}
      else
	{
	  l2count=passcount;
	}
      while(l1count>0 || l2count>0)
	{
	  mergecount++;
	  if((l2count>0)&&(l1count>0))
	    {
	      // we have things to merge
	      int result=compare(l1head->data,l2head->data);
	      if(result>0)
		{
		  // move from l2
		  target=l2head;
		  l2head=l2head->next;
		  dlist_move(listsource,listdest,target,1);
		  l2count--;
		  if(l2head==listsource->head)
		    l2count=0;
		}
	      else
		{
		  // move from l1
		  target=l1head;
		  l1head=l1head->next;
		  dlist_move(listsource,listdest,target,1);
		  l1count--;
		}
	    }
	  else if(l1count>0)
	    {
	      // only have l1 to work with
	      while(l1count>0)
		{
		  target=l1head;
		  l1head=l1head->next;
		  dlist_move(listsource,listdest,target,1);
		  l1count--;
		}
	    }
	  else if(l2count>0)
	    {
	      // only have l2 to work with
	      while(l2count>0)
		{
		  if(l2head==listsource->head)
		    {
		      l2count=0;
		    }
		  else
		    {
		      target=l2head;
		      l2head=l2head->next;
		      dlist_move(listsource,listdest,target,1);
		      l2count--;
		    }
		}
	    }
	  else
	    { //nothing left and this should be unreachable
	    }
	}  
    }
  return(mergecount);
}

/**
 * mergesort the list based on compare
 * compare function in form int sorter(void * a,void * b)
 *       must return >0 for a after b
 *       must return <0 for a before b
 *       else 0

 * NOTE: mergesort changes the mark pointer
 */
void dlist_sort_custom(struct dlist *list, int (*compare)(void *, void *))
{

  struct dlist *listsource, *listdest, *swap;
  struct dlist *templist;
  unsigned int passcount = 1;
  unsigned int mergecount = 1;

  dlist_start(list);
  templist = dlist_new(list->data_size);

  // do nothing if there isn't anything to sort
  listsource = list;
  listdest = templist;
  if(listsource->count<2)
    { //nothing to do
      return;
    }
  else
    {
      while(mergecount>0)
	{
	  mergecount=_dlist_merge(listsource, listdest, passcount, compare);
	  if(mergecount>1)
	    {
	      passcount=passcount*2;
	      //start new pass
	      swap=listsource;
	      listsource=listdest;
	      listdest=swap;
	    }
	}
    }
  // now put the input list pointers right
  // list pointers = newlist pointers
  // including the forward and next nodes prev and back pointers
  if(list->count==0)
    {//copy
      list->marker = listdest->marker;
      list->count = listdest->count;
      list->data_size = listdest->data_size;
      list->del_func = listdest->del_func;
      list->head->prev = listdest->head->prev;
      list->head->next = listdest->head->next;
      list->head->data = listdest->head->data;
      list->head->next->prev=list->head;
      list->head->prev->next=list->head;
      templist->head->next=NULL;
      templist->head->prev=NULL;
      templist->count=0;
    }
  else
    {// no need to copy
      
    }

  dlist_destroy(templist);
}



/* internal use function
   swaps elements a and b
   No sense in juggling node pointers when we can just swap the data pointers
*/

void _dlist_swap(struct dlist *list, struct dl_node *a, struct dl_node *b)
{

  void *swap=a->data;
  a->data=b->data;
  b->data=swap;
  
}