Sat, 26 Feb 2005 10:37:20 +0100
Added struct field address macros.
60 | 1 | /* |
2 | * libtu/symlist.c | |
3 | * | |
4 | * Copyright (c) Tuomo Valkonen 1999-2004. | |
5 | * | |
6 | * You may distribute and modify this library under the terms of either | |
7 | * the Clarified Artistic License or the GNU LGPL, version 2.1 or later. | |
8 | */ | |
9 | ||
10 | #include "obj.h" | |
11 | #include "symlist.h" | |
12 | #include "types.h" | |
13 | #include "dlist.h" | |
14 | #include "misc.h" | |
15 | ||
16 | ||
17 | static void free_node(Symlist **symlist, Symlist *node) | |
18 | { | |
62 | 19 | UNLINK_ITEM(*symlist, node, next, prev); |
20 | free(node); | |
60 | 21 | } |
22 | ||
23 | ||
88 | 24 | static Symlist *mknode(void *symbol) |
60 | 25 | { |
62 | 26 | Symlist *node; |
27 | ||
28 | if(symbol==NULL) | |
88 | 29 | return NULL; |
62 | 30 | |
31 | node=ALLOC(Symlist); | |
32 | ||
33 | if(node==NULL) | |
34 | return FALSE; | |
35 | ||
36 | node->symbol=symbol; | |
37 | ||
88 | 38 | return node; |
39 | } | |
40 | ||
41 | ||
42 | static Symlist *symlist_find_node(Symlist *symlist, void *symbol) | |
43 | { | |
44 | Symlist *node=symlist; | |
45 | ||
46 | while(node!=NULL){ | |
47 | if(node->symbol==symbol) | |
48 | break; | |
49 | node=node->next; | |
50 | } | |
51 | ||
52 | return node; | |
53 | } | |
54 | ||
55 | ||
56 | bool symlist_insert_last(Symlist **symlist, void *symbol) | |
57 | { | |
58 | Symlist *node=mknode(symbol); | |
59 | ||
60 | if(node==NULL) | |
61 | return FALSE; | |
62 | ||
63 | LINK_ITEM_LAST(*symlist, node, next, prev); | |
64 | ||
65 | return TRUE; | |
66 | } | |
67 | ||
68 | ||
69 | bool symlist_insert_first(Symlist **symlist, void *symbol) | |
70 | { | |
71 | Symlist *node=mknode(symbol); | |
72 | ||
73 | if(node==NULL) | |
74 | return FALSE; | |
75 | ||
76 | LINK_ITEM_FIRST(*symlist, node, next, prev); | |
77 | ||
78 | return TRUE; | |
79 | } | |
80 | ||
81 | ||
82 | bool symlist_reinsert_last(Symlist **symlist, void *symbol) | |
83 | { | |
84 | Symlist *node=symlist_find_node(*symlist, symbol); | |
85 | ||
86 | if(node==NULL) | |
87 | return FALSE; | |
88 | ||
89 | UNLINK_ITEM(*symlist, node, next, prev); | |
90 | LINK_ITEM_LAST(*symlist, node, next, prev); | |
91 | ||
92 | return TRUE; | |
93 | } | |
94 | ||
95 | ||
96 | bool symlist_reinsert_first(Symlist **symlist, void *symbol) | |
97 | { | |
98 | Symlist *node=symlist_find_node(*symlist, symbol); | |
99 | ||
100 | if(node==NULL) | |
101 | return FALSE; | |
102 | ||
103 | UNLINK_ITEM(*symlist, node, next, prev); | |
62 | 104 | LINK_ITEM_FIRST(*symlist, node, next, prev); |
105 | ||
106 | return TRUE; | |
60 | 107 | } |
108 | ||
109 | ||
110 | void symlist_remove(Symlist **symlist, void *symbol) | |
111 | { | |
88 | 112 | Symlist *node=symlist_find_node(*symlist, symbol); |
62 | 113 | |
88 | 114 | if(node!=NULL) |
115 | free_node(symlist, node); | |
60 | 116 | } |
117 | ||
118 | ||
119 | void symlist_clear(Symlist **symlist) | |
120 | { | |
62 | 121 | while(*symlist!=NULL) |
122 | free_node(symlist, *symlist); | |
60 | 123 | } |
124 | ||
125 | ||
88 | 126 | SymlistIterTmp symlist_iter_tmp=NULL; |
60 | 127 | |
128 | ||
88 | 129 | void symlist_iter_init(SymlistIterTmp *state, Symlist *symlist) |
60 | 130 | { |
88 | 131 | *state=symlist; |
60 | 132 | } |
133 | ||
134 | ||
88 | 135 | void *symlist_iter(SymlistIterTmp *state) |
60 | 136 | { |
88 | 137 | void *symbol=NULL; |
62 | 138 | |
88 | 139 | if(*state!=NULL){ |
140 | symbol=(*state)->symbol; | |
141 | (*state)=(*state)->next; | |
142 | } | |
62 | 143 | |
88 | 144 | return symbol; |
60 | 145 | } |
146 | ||
88 | 147 | |
148 | void symlist_iter_rev_init(SymlistIterTmp *state, Symlist *symlist) | |
149 | { | |
150 | *state=(symlist==NULL ? NULL : symlist->prev); | |
151 | } | |
152 | ||
153 | ||
154 | void *symlist_iter_rev(SymlistIterTmp *state) | |
155 | { | |
156 | void *symbol=NULL; | |
157 | ||
158 | if(*state!=NULL){ | |
159 | symbol=(*state)->symbol; | |
160 | *state=(*state)->prev; | |
161 | if((*state)->next==NULL) | |
162 | *state=NULL; | |
163 | } | |
164 | ||
165 | return symbol; | |
166 | } | |
167 |