1 /*	$Id: alist.h,v 1.1 2001/07/13 19:09:55 sandro Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 Sandro Sigala.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef ALIST_H
28 #define ALIST_H
29 
30 #include <stddef.h>
31 
32 typedef struct alist_s *alist;
33 
34 extern alist	alist_new(void);
35 extern alist	alist_copy(alist al);
36 extern void	alist_delete(alist al);
37 extern void	alist_clear(alist al);
38 extern int	alist_count(alist al);
39 extern int	alist_isempty(alist al);
40 extern void *	alist_first(alist al);
41 extern void *	alist_last(alist al);
42 extern void *	alist_prev(alist al);
43 extern void *	alist_next(alist al);
44 extern void *	alist_current(alist al);
45 extern int	alist_current_idx(alist al);
46 extern void	alist_insert(alist al, unsigned int i, void *p);
47 extern void	alist_prepend(alist al, void *p);
48 extern void	alist_append(alist al, void *p);
49 extern int	alist_remove(alist al);
50 extern int	alist_remove_ptr(alist al, void *p);
51 extern int	alist_remove_idx(alist al, unsigned int i);
52 extern void *	alist_take(alist al);
53 extern void *	alist_take_idx(alist al, unsigned int i);
54 extern void	alist_sort(alist al, int (*cmp)(const void *p1, const void *p2));
55 extern void *	alist_at(alist al, unsigned int i);
56 extern int	alist_find(alist al, void *p);
57 
58 /*
59  * Internal data structures
60  *
61  * You should never access directly to the following data.
62  */
63 
64 typedef struct aentry_s *aentry;
65 
66 struct aentry_s {
67 	void *	p;
68 	aentry	prev;
69 	aentry	next;
70 };
71 
72 struct alist_s {
73 	aentry	head;
74 	aentry	tail;
75 	aentry	current;
76 	int	idx;
77 	size_t	size;
78 };
79 
80 #ifndef ALIST_NO_MACRO_DEFS
81 
82 /*
83  * Fast macro definitions.
84  */
85 
86 #define alist_isempty(al)	(al->size == 0)
87 #define alist_count(al)		(al->size)
88 #define alist_first(al)		(al->idx = 0, (al->current = al->head) != NULL ? al->current->p : NULL)
89 #define alist_last(al)		((al->current = al->tail) != NULL ? (al->idx = al->size - 1, a->current->p) : NULL)
90 #define alist_prev(al)		((al->current != NULL) ? (al->current = al->current->prev, (al->current != NULL) ? --al->idx, al->current->p : NULL) : NULL)
91 #define alist_next(al)		((al->current != NULL) ? (al->current = al->current->next, (al->current != NULL) ? ++al->idx, al->current->p : NULL) : NULL)
92 #define alist_current(al)	((al->current != NULL) ? al->current->p : NULL)
93 #define alist_current_idx(al)	((al->current != NULL) ? al->idx : -1)
94 
95 #endif /* !ALIST_NO_MACRO_DEFS */
96 
97 #endif /* !ALIST_H */
98