| 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 { |
|
| 19 UNLINK_ITEM(*symlist, node, next, prev); |
|
| 20 free(node); |
|
| 21 } |
|
| 22 |
|
| 23 |
|
| 24 static Symlist *mknode(void *symbol) |
|
| 25 { |
|
| 26 Symlist *node; |
|
| 27 |
|
| 28 if(symbol==NULL) |
|
| 29 return NULL; |
|
| 30 |
|
| 31 node=ALLOC(Symlist); |
|
| 32 |
|
| 33 if(node==NULL) |
|
| 34 return FALSE; |
|
| 35 |
|
| 36 node->symbol=symbol; |
|
| 37 |
|
| 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); |
|
| 104 LINK_ITEM_FIRST(*symlist, node, next, prev); |
|
| 105 |
|
| 106 return TRUE; |
|
| 107 } |
|
| 108 |
|
| 109 |
|
| 110 void symlist_remove(Symlist **symlist, void *symbol) |
|
| 111 { |
|
| 112 Symlist *node=symlist_find_node(*symlist, symbol); |
|
| 113 |
|
| 114 if(node!=NULL) |
|
| 115 free_node(symlist, node); |
|
| 116 } |
|
| 117 |
|
| 118 |
|
| 119 void symlist_clear(Symlist **symlist) |
|
| 120 { |
|
| 121 while(*symlist!=NULL) |
|
| 122 free_node(symlist, *symlist); |
|
| 123 } |
|
| 124 |
|
| 125 |
|
| 126 SymlistIterTmp symlist_iter_tmp=NULL; |
|
| 127 |
|
| 128 |
|
| 129 void symlist_iter_init(SymlistIterTmp *state, Symlist *symlist) |
|
| 130 { |
|
| 131 *state=symlist; |
|
| 132 } |
|
| 133 |
|
| 134 |
|
| 135 void *symlist_iter(SymlistIterTmp *state) |
|
| 136 { |
|
| 137 void *symbol=NULL; |
|
| 138 |
|
| 139 if(*state!=NULL){ |
|
| 140 symbol=(*state)->symbol; |
|
| 141 (*state)=(*state)->next; |
|
| 142 } |
|
| 143 |
|
| 144 return symbol; |
|
| 145 } |
|
| 146 |
|
| 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 |
|