File: list.h

package info (click to toggle)
gnutls28 3.6.7-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 67,368 kB
  • sloc: ansic: 295,691; asm: 70,637; sh: 15,067; makefile: 6,288; yacc: 1,840; lisp: 1,371; cpp: 1,358; perl: 1,030; sed: 16
file content (447 lines) | stat: -rw-r--r-- 12,694 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2001,2002 Paul Sheer
 *
 * This file is part of GnuTLS.
 *
 * GnuTLS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * GnuTLS 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 General 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

/*
    SOAP:
    
    Academics always want to implement hash tables (i.e. dictionaries),
    singly or doubly linked lists, and queues, because ...  well ... they
    know how.
    
    These datatypes are nonsense for the following reasons:
	hash tables: Hash tables are a mapping of some
	    string to some data, where that data is going to
	    be accessed COMPLETELY RANDOMLY. This is what it
	    is for. However it is extremely rare to have a
	    large number of data elements which really are
	    being accessed in a completely random way.

	lists: appending and searching through lists is always
	    slow because these operations search all the way
	    through the list.

	queues: whats the difference between a queue and a list?
	    very little really.

    The system implemented here is a doubly linked list with previous
    search index that can be appended or prepended with no overhead,
    implemented entirely in macros. It is hence as versatile as a
    doubly/singly linked list or queue and blazingly fast. Hence doing
    sequential searches where the next search result is likely to be
    closely indexed to the previous (usual case), is efficient.

    Of course this doesn't mean you should use this as a hash table
    where you REALLY need a hash table.

*/

/********** example usage **********/
/*

#include "list.h"

extern void free (void *x);
extern char *strdup (char *s);

// consider a list of elements containing an `int' and a `char *'
LIST_TYPE_DECLARE (names, char *s; int i;);

// for sorting, to compare elements
static int cm (names **a, names **b)
{
    return strcmp ((*a)->s, (*b)->s);
}

// to free the contents of an element
static void free_item (names *a)
{
    free (a->s);
    a->s = 0;	// say
    a->i = 0;	// say
}

int main (int argc, char **argv)
{
// you can separate these into LIST_TYPE_DECLARE(), LIST_DECLARE() and linit() if needed.
    LIST_DECLARE_INIT (l, names, free_item);
    names *j;

    lappend (l);
    l.tail->s = strdup ("hello");
    l.tail->i = 1;
    lappend (l);
    l.tail->s = strdup ("there");
    l.tail->i = 2;
    lappend (l);
    l.tail->s = strdup ("my");
    l.tail->i = 3;
    lappend (l);
    l.tail->s = strdup ("name");
    l.tail->i = 4;
    lappend (l);
    l.tail->s = strdup ("is");
    l.tail->i = 5;
    lappend (l);
    l.tail->s = strdup ("fred");
    l.tail->i = 6;

    printf ("%ld\n\n", lcount (l));
    lloopforward (l, j, printf ("%d %s\n", j->i, j->s));
    printf ("\n");

    lsort (l, cm);
    lloopforward (l, j, printf ("%d %s\n", j->i, j->s));

    lloopreverse (l, j, if (j->i <= 3) ldeleteinc (l, j););

    printf ("\n");
    lloopforward (l, j, printf ("%d %s\n", j->i, j->s));

    ldeleteall (l);

    printf ("\n");
    lloopforward (l, j, printf ("%d %s\n", j->i, j->s));
    return 0;
}

*/


#ifndef _LIST_H
#define _LIST_H

/* the `search' member points to the last found.
   this speeds up repeated searches on the same list-item,
   the consecutive list-item, or the pre-consecutive list-item.
   this obviates the need for a hash table for 99% of
   cercumstances the time */
struct list {
	long length;
	long item_size;
	struct list_item {
		struct list_item *next;
		struct list_item *prev;
		char data[1];
	} *head, *tail, *search;
	void (*free_func) (struct list_item *);
};

/* declare a list of type `x', also called `x' having members `typelist' */

#define LIST_TYPE_DECLARE(type,typelist)						\
    typedef struct type {								\
	struct type *next;								\
	struct type *prev;								\
	typelist									\
    } type

#define LIST_DECLARE(l,type)								\
    struct {										\
	long length;									\
	long item_size;									\
	type *head, *tail, *search;							\
	void (*free_func) (type *);							\
    } l

#define LIST_DECLARE_INIT(l,type,free)							\
    struct {										\
	long length;									\
	long item_size;									\
	type *head, *tail, *search;							\
	void (*free_func) (type *);							\
    } l = {0, sizeof (type), 0, 0, 0, (void (*) (type *)) free}

#define linit(l,type,free)								\
    {											\
	memset (&(l), 0, sizeof (l));							\
	(l).item_size = sizeof (type);							\
	(l).free_func = free;								\
    }

/* returns a pointer to the data of an item */
#define ldata(i)	((void *) &((i)->data[0]))

/* returns a pointer to the list head */
#define lhead(l)	((l).head)

/* returns a pointer to the list tail */
#define ltail(l)	((l).tail)

#define lnewsearch(l)									\
	(l).search = 0;

/* adds to the beginning of the list */
#define lprepend(l)									\
    {											\
	struct list_item *__t;								\
	__t = (struct list_item *) malloc ((l).item_size);				\
	memset (__t, 0, (l).item_size);							\
	__t->next = (l).head;								\
	if (__t->next)									\
	    __t->next->prev = __t;							\
	__t->prev = 0;									\
	if (!(l).tail)									\
	    (l).tail = __t;								\
	(l).head = __t;									\
	length++;									\
    }

/* adds to the end of the list */
#define lappend(l)									\
    {											\
	struct list_item *__t;								\
	__t = (struct list_item *) malloc ((l).item_size);				\
	memset (__t, 0, (l).item_size);							\
	__t->prev = (struct list_item *) (l).tail;					\
	if (__t->prev)									\
	    __t->prev->next = __t;							\
	__t->next = 0;									\
	if (!(l).head)									\
	    (l).head = (void *) __t;							\
	(l).tail = (void *) __t;							\
	(l).length++;									\
    }

/* you should free these manually */
#define lunlink(l,e)									\
    {											\
	struct list_item *__t;								\
	(l).search = 0;									\
	__t = (void *) e;								\
	if ((void *) (l).head == (void *) __t)						\
	    (l).head = (l).head->next;							\
	if ((void *) (l).tail == (void *) __t)						\
	    (l).tail = (l).tail->prev;							\
	if (__t->next)									\
	    __t->next->prev = __t->prev;						\
	if (__t->prev)									\
	    __t->prev->next = __t->next;						\
	(l).length--;									\
    }

/* deletes list entry at point e, and increments e to the following list entry */
#define ldeleteinc(l,e)									\
    {											\
	struct list_item *__t;								\
	(l).search = 0;									\
	__t = (void *) e;								\
	if ((void *) (l).head == (void *) __t)						\
	    (l).head = (l).head->next;							\
	if ((void *) (l).tail == (void *) __t)						\
	    (l).tail = (l).tail->prev;							\
	if (__t->next)									\
	    __t->next->prev = __t->prev;						\
	if (__t->prev)									\
	    __t->prev->next = __t->next;						\
	__t = __t->next;								\
	if ((l).free_func)								\
	    (*(l).free_func) ((void *) e);						\
	free (e);									\
	e = (void *) __t;								\
	(l).length--;									\
    }

/* deletes list entry at point e, and deccrements e to the preceding list emtry */
#define ldeletedec(l,e)									\
    {											\
	struct list_item *__t;								\
	(l).search = 0;									\
	__t = (void *) e;								\
	if ((void *) (l).head == (void *) __t)						\
	    (l).head = (l).head->next;							\
	if ((void *) (l).tail == (void *) __t)						\
	    (l).tail = (l).tail->prev;							\
	if (__t->next)									\
	    __t->next->prev = __t->prev;						\
	if (__t->prev)									\
	    __t->prev->next = __t->next;						\
	__t = __t->prev;								\
	if ((l).free_func)								\
	    (*(l).free_func) ((void *) e);						\
	free (e);									\
	e = (void *) __t;								\
	(l).length--;									\
    }

/* p and q must be consecutive and neither must be zero */
#define linsert(l,p,q)									\
    {											\
	struct list_item *__t;								\
	__t = (struct list_item *) malloc ((l).item_size);				\
	memset (__t, 0, (l).item_size);							\
	__t->prev = (void *) p;								\
	__t->next = (void *) q;								\
	q->prev = (void *) __t;								\
	p->next = (void *) __t;								\
	(l).length++;									\
    }

/* p and q must be consecutive and neither must be zero */
#define ldeleteall(l)									\
    {											\
	(l).search = 0;									\
	while ((l).head) {								\
	    struct list_item *__p;							\
	    __p = (struct list_item *) (l).head;					\
	    lunlink(l, __p);								\
	    if ((l).free_func)								\
		(*(l).free_func) ((void *) __p);					\
	    free (__p);									\
	}										\
    }

#define lloopstart(l,i)									\
	for (i = (void *) (l).head; i;) {						\
	    struct list_item *__tl;							\
	    __tl = (void *) i->next;							\

#define lloopend(l,i)									\
	    i = (void *) __tl;								\
	}										\

#define lloopforward(l,i,op)								\
    {											\
	for (i = (void *) (l).head; i;) {						\
	    struct list_item *__t;							\
	    __t = (void *) i->next;							\
	    op;										\
	    i = (void *) __t;								\
	}										\
    }

#define lloopreverse(l,i,op)								\
    {											\
	for (i = (void *) (l).tail; i;) {						\
	    struct list_item *__t;							\
	    __t = (void *) i->prev;							\
	    op;										\
	    i = (void *) __t;								\
	}										\
    }

#define lindex(l,i,n)									\
    {											\
	int __k;									\
	for (__k = 0, i = (void *) (l).head; i && __k != n; i = i->next, __k++);	\
    }

#define lsearchforward(l,i,op)								\
    {											\
	int __found = 0;								\
	if (!__found)									\
	    if ((i = (void *) (l).search)) {						\
		if (op) {								\
		    __found = 1;							\
		    (l).search = (void *) i;						\
		}									\
		if (!__found)								\
		    if ((i = (void *) (l).search->next))				\
			if (op) {							\
			    __found = 1;						\
			    (l).search = (void *) i;					\
			}								\
		if (!__found)								\
		    if ((i = (void *) (l).search->prev))				\
			if (op) {							\
			    __found = 1;						\
			    (l).search = (void *) i;					\
			}								\
	    }										\
	if (!__found)									\
	    for (i = (void *) (l).head; i; i = i->next)					\
		if (op) {								\
		    __found = 1;							\
		    (l).search = (void *) i;						\
		    break;								\
		}									\
    }

#define lsearchreverse(l,i,op)								\
    {											\
	int __found = 0;								\
	if (!__found)									\
	    if ((i = (void *) (l).search)) {						\
		if (op) {								\
		    __found = 1;							\
		    (l).search = (void *) i;						\
		}									\
		if (!__found)								\
		    if ((i = (void *) (l).search->prev))				\
			if (op) {							\
			    __found = 1;						\
			    (l).search = (void *) i;					\
			}								\
		if (!__found)								\
		    if ((i = (void *) (l).search->next))				\
			if (op) {							\
			    __found = 1;						\
			    (l).search = (void *) i;					\
			}								\
	    }										\
	if (!__found)									\
	    for (i = (void *) (l).tail; i; i = i->prev)					\
		if (op) {								\
		    __found = 1;							\
		    (l).search = (void *) i;						\
		    break;								\
		}									\
    }

#define lcount(l)	((l).length)

/* sort with comparison function see qsort(3) */
#define larray(l,a)									\
    {											\
	long __i;									\
	struct list_item *__p;								\
	a = (void *) malloc (((l).length + 1) * sizeof (void *));			\
	for (__i = 0, __p = (void *) (l).head; __p; __p = __p->next, __i++)		\
	    a[__i] = (void *) __p;							\
	a[__i] = 0;									\
    }											\

/* sort with comparison function see qsort(3) */
#define llist(l,a)									\
    {											\
	struct list_item *__p;								\
	(l).head = (void *) a[0];							\
	(l).search = 0;									\
	__p = (void *) a[0];								\
	__p->prev = 0;									\
	for (__j = 1; a[__j]; __j++, __p = __p->next) {					\
	    __p->next = (void *) a[__j];						\
	    __p->next->prev = __p;							\
	}										\
	(l).tail = (void *) __p;							\
	__p->next = 0;									\
    }											\

/* sort with comparison function see qsort(3) */
#define lsort(l,compare)								\
    {											\
	void **__t;									\
	long __j;									\
	larray (l,__t);									\
	qsort (__t, (l).length, sizeof (void *), (int (*) (const void *, const void *)) compare);	\
	llist (l,__t);									\
	free (__t);									\
    }											\

#endif				/* _LIST_H */