jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Dictionary Abstract Data Type |
| 3 | * Copyright (C) 1997 Kaz Kylheku <kaz@ashi.footprints.net> |
| 4 | * |
| 5 | * Free Software License: |
| 6 | * |
| 7 | * All rights are reserved by the author, with the following exceptions: |
| 8 | * Permission is granted to freely reproduce and distribute this software, |
| 9 | * possibly in exchange for a fee, provided that this copyright notice appears |
| 10 | * intact. Permission is also granted to adapt this software to produce |
| 11 | * derivative works, as long as the modified versions carry this copyright |
| 12 | * notice and additional notices stating that the work has been modified. |
| 13 | * This source code may be translated into executable form and incorporated |
| 14 | * into proprietary software; there is no requirement for such software to |
| 15 | * contain a copyright notice related to this source. |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
Paul Jakma | 238497f | 2007-08-07 18:49:18 +0000 | [diff] [blame] | 18 | #include "zebra.h" |
ajs | cee3df1 | 2004-11-24 17:14:49 +0000 | [diff] [blame] | 19 | #include "zassert.h" |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 20 | #include "memory.h" |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 21 | #include "dict.h" |
| 22 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 23 | /* |
| 24 | * These macros provide short convenient names for structure members, |
| 25 | * which are embellished with dict_ prefixes so that they are |
| 26 | * properly confined to the documented namespace. It's legal for a |
| 27 | * program which uses dict to define, for instance, a macro called ``parent''. |
| 28 | * Such a macro would interfere with the dnode_t struct definition. |
| 29 | * In general, highly portable and reusable C modules which expose their |
| 30 | * structures need to confine structure member names to well-defined spaces. |
| 31 | * The resulting identifiers aren't necessarily convenient to use, nor |
| 32 | * readable, in the implementation, however! |
| 33 | */ |
| 34 | |
| 35 | #define left dict_left |
| 36 | #define right dict_right |
| 37 | #define parent dict_parent |
| 38 | #define color dict_color |
| 39 | #define key dict_key |
| 40 | #define data dict_data |
| 41 | |
| 42 | #define nilnode dict_nilnode |
| 43 | #define nodecount dict_nodecount |
| 44 | #define maxcount dict_maxcount |
| 45 | #define compare dict_compare |
| 46 | #define allocnode dict_allocnode |
| 47 | #define freenode dict_freenode |
| 48 | #define context dict_context |
| 49 | #define dupes dict_dupes |
| 50 | |
| 51 | #define dictptr dict_dictptr |
| 52 | |
| 53 | #define dict_root(D) ((D)->nilnode.left) |
| 54 | #define dict_nil(D) (&(D)->nilnode) |
| 55 | #define DICT_DEPTH_MAX 64 |
| 56 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 57 | static dnode_t *dnode_alloc(void *context); |
| 58 | static void dnode_free(dnode_t *node, void *context); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | * Perform a ``left rotation'' adjustment on the tree. The given node P and |
| 62 | * its right child C are rearranged so that the P instead becomes the left |
| 63 | * child of C. The left subtree of C is inherited as the new right subtree |
| 64 | * for P. The ordering of the keys within the tree is thus preserved. |
| 65 | */ |
| 66 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 67 | static void rotate_left(dnode_t *upper) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 68 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 69 | dnode_t *lower, *lowleft, *upparent; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 70 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 71 | lower = upper->right; |
| 72 | upper->right = lowleft = lower->left; |
| 73 | lowleft->parent = upper; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 74 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 75 | lower->parent = upparent = upper->parent; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 76 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 77 | /* don't need to check for root node here because root->parent is |
| 78 | the sentinel nil node, and root->parent->left points back to root */ |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 79 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 80 | if (upper == upparent->left) { |
| 81 | upparent->left = lower; |
| 82 | } else { |
| 83 | assert (upper == upparent->right); |
| 84 | upparent->right = lower; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 85 | } |
| 86 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 87 | lower->left = upper; |
| 88 | upper->parent = lower; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
| 92 | * This operation is the ``mirror'' image of rotate_left. It is |
| 93 | * the same procedure, but with left and right interchanged. |
| 94 | */ |
| 95 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 96 | static void rotate_right(dnode_t *upper) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 97 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 98 | dnode_t *lower, *lowright, *upparent; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 99 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 100 | lower = upper->left; |
| 101 | upper->left = lowright = lower->right; |
| 102 | lowright->parent = upper; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 103 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 104 | lower->parent = upparent = upper->parent; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 105 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 106 | if (upper == upparent->right) { |
| 107 | upparent->right = lower; |
| 108 | } else { |
| 109 | assert (upper == upparent->left); |
| 110 | upparent->left = lower; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 111 | } |
| 112 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 113 | lower->right = upper; |
| 114 | upper->parent = lower; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Do a postorder traversal of the tree rooted at the specified |
| 119 | * node and free everything under it. Used by dict_free(). |
| 120 | */ |
| 121 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 122 | static void free_nodes(dict_t *dict, dnode_t *node, dnode_t *nil) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 123 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 124 | if (node == nil) |
| 125 | return; |
| 126 | free_nodes(dict, node->left, nil); |
| 127 | free_nodes(dict, node->right, nil); |
| 128 | dict->freenode(node, dict->context); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* |
| 132 | * This procedure performs a verification that the given subtree is a binary |
| 133 | * search tree. It performs an inorder traversal of the tree using the |
| 134 | * dict_next() successor function, verifying that the key of each node is |
| 135 | * strictly lower than that of its successor, if duplicates are not allowed, |
| 136 | * or lower or equal if duplicates are allowed. This function is used for |
| 137 | * debugging purposes. |
| 138 | */ |
| 139 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 140 | static int verify_bintree(dict_t *dict) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 141 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 142 | dnode_t *first, *next; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 143 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 144 | first = dict_first(dict); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 145 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 146 | if (dict->dupes) { |
| 147 | while (first && (next = dict_next(dict, first))) { |
| 148 | if (dict->compare(first->key, next->key) > 0) |
| 149 | return 0; |
| 150 | first = next; |
| 151 | } |
| 152 | } else { |
| 153 | while (first && (next = dict_next(dict, first))) { |
| 154 | if (dict->compare(first->key, next->key) >= 0) |
| 155 | return 0; |
| 156 | first = next; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 159 | return 1; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | |
| 163 | /* |
| 164 | * This function recursively verifies that the given binary subtree satisfies |
| 165 | * three of the red black properties. It checks that every red node has only |
| 166 | * black children. It makes sure that each node is either red or black. And it |
| 167 | * checks that every path has the same count of black nodes from root to leaf. |
| 168 | * It returns the blackheight of the given subtree; this allows blackheights to |
| 169 | * be computed recursively and compared for left and right siblings for |
| 170 | * mismatches. It does not check for every nil node being black, because there |
| 171 | * is only one sentinel nil node. The return value of this function is the |
| 172 | * black height of the subtree rooted at the node ``root'', or zero if the |
| 173 | * subtree is not red-black. |
| 174 | */ |
| 175 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 176 | static unsigned int verify_redblack(dnode_t *nil, dnode_t *root) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 177 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 178 | unsigned height_left, height_right; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 179 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 180 | if (root != nil) { |
| 181 | height_left = verify_redblack(nil, root->left); |
| 182 | height_right = verify_redblack(nil, root->right); |
| 183 | if (height_left == 0 || height_right == 0) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 184 | return 0; |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 185 | if (height_left != height_right) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 186 | return 0; |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 187 | if (root->color == dnode_red) { |
| 188 | if (root->left->color != dnode_black) |
| 189 | return 0; |
| 190 | if (root->right->color != dnode_black) |
| 191 | return 0; |
| 192 | return height_left; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 193 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 194 | if (root->color != dnode_black) |
| 195 | return 0; |
| 196 | return height_left + 1; |
| 197 | } |
| 198 | return 1; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Compute the actual count of nodes by traversing the tree and |
| 203 | * return it. This could be compared against the stored count to |
| 204 | * detect a mismatch. |
| 205 | */ |
| 206 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 207 | static dictcount_t verify_node_count(dnode_t *nil, dnode_t *root) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 208 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 209 | if (root == nil) |
| 210 | return 0; |
| 211 | else |
| 212 | return 1 + verify_node_count(nil, root->left) |
| 213 | + verify_node_count(nil, root->right); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Verify that the tree contains the given node. This is done by |
| 218 | * traversing all of the nodes and comparing their pointers to the |
| 219 | * given pointer. Returns 1 if the node is found, otherwise |
| 220 | * returns zero. It is intended for debugging purposes. |
| 221 | */ |
| 222 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 223 | static int verify_dict_has_node(dnode_t *nil, dnode_t *root, dnode_t *node) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 224 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 225 | if (root != nil) { |
| 226 | return root == node |
| 227 | || verify_dict_has_node(nil, root->left, node) |
| 228 | || verify_dict_has_node(nil, root->right, node); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 229 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 230 | return 0; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 231 | } |
| 232 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 233 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 234 | /* |
| 235 | * Dynamically allocate and initialize a dictionary object. |
| 236 | */ |
| 237 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 238 | dict_t *dict_create(dictcount_t maxcount, dict_comp_t comp) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 239 | { |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 240 | dict_t *new = XCALLOC(MTYPE_ISIS_DICT, sizeof(dict_t)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 241 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 242 | if (new) { |
| 243 | new->compare = comp; |
| 244 | new->allocnode = dnode_alloc; |
| 245 | new->freenode = dnode_free; |
| 246 | new->context = NULL; |
| 247 | new->nodecount = 0; |
| 248 | new->maxcount = maxcount; |
| 249 | new->nilnode.left = &new->nilnode; |
| 250 | new->nilnode.right = &new->nilnode; |
| 251 | new->nilnode.parent = &new->nilnode; |
| 252 | new->nilnode.color = dnode_black; |
| 253 | new->dupes = 0; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 254 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 255 | return new; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Select a different set of node allocator routines. |
| 260 | */ |
| 261 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 262 | void dict_set_allocator(dict_t *dict, dnode_alloc_t al, |
| 263 | dnode_free_t fr, void *context) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 264 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 265 | assert (dict_count(dict) == 0); |
| 266 | assert ((al == NULL && fr == NULL) || (al != NULL && fr != NULL)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 267 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 268 | dict->allocnode = al ? al : dnode_alloc; |
| 269 | dict->freenode = fr ? fr : dnode_free; |
| 270 | dict->context = context; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Free a dynamically allocated dictionary object. Removing the nodes |
| 275 | * from the tree before deleting it is required. |
| 276 | */ |
| 277 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 278 | void dict_destroy(dict_t *dict) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 279 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 280 | assert (dict_isempty(dict)); |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 281 | XFREE(MTYPE_ISIS_DICT, dict); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* |
| 285 | * Free all the nodes in the dictionary by using the dictionary's |
| 286 | * installed free routine. The dictionary is emptied. |
| 287 | */ |
| 288 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 289 | void dict_free_nodes(dict_t *dict) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 290 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 291 | dnode_t *nil = dict_nil(dict), *root = dict_root(dict); |
| 292 | free_nodes(dict, root, nil); |
| 293 | dict->nodecount = 0; |
| 294 | dict->nilnode.left = &dict->nilnode; |
| 295 | dict->nilnode.right = &dict->nilnode; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Obsolescent function, equivalent to dict_free_nodes |
| 300 | */ |
| 301 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 302 | void dict_free(dict_t *dict) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 303 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 304 | dict_free_nodes(dict); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Initialize a user-supplied dictionary object. |
| 309 | */ |
| 310 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 311 | dict_t *dict_init(dict_t *dict, dictcount_t maxcount, dict_comp_t comp) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 312 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 313 | dict->compare = comp; |
| 314 | dict->allocnode = dnode_alloc; |
| 315 | dict->freenode = dnode_free; |
| 316 | dict->context = NULL; |
| 317 | dict->nodecount = 0; |
| 318 | dict->maxcount = maxcount; |
| 319 | dict->nilnode.left = &dict->nilnode; |
| 320 | dict->nilnode.right = &dict->nilnode; |
| 321 | dict->nilnode.parent = &dict->nilnode; |
| 322 | dict->nilnode.color = dnode_black; |
| 323 | dict->dupes = 0; |
| 324 | return dict; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /* |
| 328 | * Initialize a dictionary in the likeness of another dictionary |
| 329 | */ |
| 330 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 331 | void dict_init_like(dict_t *dict, const dict_t *template) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 332 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 333 | dict->compare = template->compare; |
| 334 | dict->allocnode = template->allocnode; |
| 335 | dict->freenode = template->freenode; |
| 336 | dict->context = template->context; |
| 337 | dict->nodecount = 0; |
| 338 | dict->maxcount = template->maxcount; |
| 339 | dict->nilnode.left = &dict->nilnode; |
| 340 | dict->nilnode.right = &dict->nilnode; |
| 341 | dict->nilnode.parent = &dict->nilnode; |
| 342 | dict->nilnode.color = dnode_black; |
| 343 | dict->dupes = template->dupes; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 344 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 345 | assert (dict_similar(dict, template)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Remove all nodes from the dictionary (without freeing them in any way). |
| 350 | */ |
| 351 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 352 | static void dict_clear(dict_t *dict) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 353 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 354 | dict->nodecount = 0; |
| 355 | dict->nilnode.left = &dict->nilnode; |
| 356 | dict->nilnode.right = &dict->nilnode; |
| 357 | dict->nilnode.parent = &dict->nilnode; |
| 358 | assert (dict->nilnode.color == dnode_black); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 359 | } |
| 360 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 361 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 362 | /* |
| 363 | * Verify the integrity of the dictionary structure. This is provided for |
| 364 | * debugging purposes, and should be placed in assert statements. Just because |
| 365 | * this function succeeds doesn't mean that the tree is not corrupt. Certain |
| 366 | * corruptions in the tree may simply cause undefined behavior. |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 367 | */ |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 368 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 369 | int dict_verify(dict_t *dict) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 370 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 371 | dnode_t *nil = dict_nil(dict), *root = dict_root(dict); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 372 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 373 | /* check that the sentinel node and root node are black */ |
| 374 | if (root->color != dnode_black) |
| 375 | return 0; |
| 376 | if (nil->color != dnode_black) |
| 377 | return 0; |
| 378 | if (nil->right != nil) |
| 379 | return 0; |
| 380 | /* nil->left is the root node; check that its parent pointer is nil */ |
| 381 | if (nil->left->parent != nil) |
| 382 | return 0; |
| 383 | /* perform a weak test that the tree is a binary search tree */ |
| 384 | if (!verify_bintree(dict)) |
| 385 | return 0; |
| 386 | /* verify that the tree is a red-black tree */ |
| 387 | if (!verify_redblack(nil, root)) |
| 388 | return 0; |
| 389 | if (verify_node_count(nil, root) != dict_count(dict)) |
| 390 | return 0; |
| 391 | return 1; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Determine whether two dictionaries are similar: have the same comparison and |
| 396 | * allocator functions, and same status as to whether duplicates are allowed. |
| 397 | */ |
| 398 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 399 | int dict_similar(const dict_t *left, const dict_t *right) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 400 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 401 | if (left->compare != right->compare) |
| 402 | return 0; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 403 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 404 | if (left->allocnode != right->allocnode) |
| 405 | return 0; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 406 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 407 | if (left->freenode != right->freenode) |
| 408 | return 0; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 409 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 410 | if (left->context != right->context) |
| 411 | return 0; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 412 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 413 | if (left->dupes != right->dupes) |
| 414 | return 0; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 415 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 416 | return 1; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Locate a node in the dictionary having the given key. |
| 421 | * If the node is not found, a null a pointer is returned (rather than |
| 422 | * a pointer that dictionary's nil sentinel node), otherwise a pointer to the |
| 423 | * located node is returned. |
| 424 | */ |
| 425 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 426 | dnode_t *dict_lookup(dict_t *dict, const void *key) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 427 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 428 | dnode_t *root = dict_root(dict); |
| 429 | dnode_t *nil = dict_nil(dict); |
| 430 | dnode_t *saved; |
| 431 | int result; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 432 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 433 | /* simple binary search adapted for trees that contain duplicate keys */ |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 434 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 435 | while (root != nil) { |
| 436 | result = dict->compare(key, root->key); |
| 437 | if (result < 0) |
| 438 | root = root->left; |
| 439 | else if (result > 0) |
| 440 | root = root->right; |
| 441 | else { |
| 442 | if (!dict->dupes) { /* no duplicates, return match */ |
| 443 | return root; |
| 444 | } else { /* could be dupes, find leftmost one */ |
| 445 | do { |
| 446 | saved = root; |
| 447 | root = root->left; |
| 448 | while (root != nil && dict->compare(key, root->key)) |
| 449 | root = root->right; |
| 450 | } while (root != nil); |
| 451 | return saved; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 456 | return NULL; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /* |
| 460 | * Look for the node corresponding to the lowest key that is equal to or |
| 461 | * greater than the given key. If there is no such node, return null. |
| 462 | */ |
| 463 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 464 | dnode_t *dict_lower_bound(dict_t *dict, const void *key) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 465 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 466 | dnode_t *root = dict_root(dict); |
| 467 | dnode_t *nil = dict_nil(dict); |
| 468 | dnode_t *tentative = 0; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 469 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 470 | while (root != nil) { |
| 471 | int result = dict->compare(key, root->key); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 472 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 473 | if (result > 0) { |
| 474 | root = root->right; |
| 475 | } else if (result < 0) { |
| 476 | tentative = root; |
| 477 | root = root->left; |
| 478 | } else { |
| 479 | if (!dict->dupes) { |
| 480 | return root; |
| 481 | } else { |
| 482 | tentative = root; |
| 483 | root = root->left; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 484 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 485 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 486 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 487 | |
| 488 | return tentative; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | /* |
| 492 | * Look for the node corresponding to the greatest key that is equal to or |
| 493 | * lower than the given key. If there is no such node, return null. |
| 494 | */ |
| 495 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 496 | dnode_t *dict_upper_bound(dict_t *dict, const void *key) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 497 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 498 | dnode_t *root = dict_root(dict); |
| 499 | dnode_t *nil = dict_nil(dict); |
| 500 | dnode_t *tentative = 0; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 501 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 502 | while (root != nil) { |
| 503 | int result = dict->compare(key, root->key); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 504 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 505 | if (result < 0) { |
| 506 | root = root->left; |
| 507 | } else if (result > 0) { |
| 508 | tentative = root; |
| 509 | root = root->right; |
| 510 | } else { |
| 511 | if (!dict->dupes) { |
| 512 | return root; |
| 513 | } else { |
| 514 | tentative = root; |
| 515 | root = root->right; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 516 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 517 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 518 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 519 | |
| 520 | return tentative; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /* |
| 524 | * Insert a node into the dictionary. The node should have been |
| 525 | * initialized with a data field. All other fields are ignored. |
| 526 | * The behavior is undefined if the user attempts to insert into |
| 527 | * a dictionary that is already full (for which the dict_isfull() |
| 528 | * function returns true). |
| 529 | */ |
| 530 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 531 | void dict_insert(dict_t *dict, dnode_t *node, const void *key) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 532 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 533 | dnode_t *where = dict_root(dict), *nil = dict_nil(dict); |
| 534 | dnode_t *parent = nil, *uncle, *grandpa; |
| 535 | int result = -1; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 536 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 537 | node->key = key; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 538 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 539 | assert (!dict_isfull(dict)); |
| 540 | assert (!dict_contains(dict, node)); |
| 541 | assert (!dnode_is_in_a_dict(node)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 542 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 543 | /* basic binary tree insert */ |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 544 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 545 | while (where != nil) { |
| 546 | parent = where; |
| 547 | result = dict->compare(key, where->key); |
| 548 | /* trap attempts at duplicate key insertion unless it's explicitly allowed */ |
| 549 | assert (dict->dupes || result != 0); |
| 550 | if (result < 0) |
| 551 | where = where->left; |
| 552 | else |
| 553 | where = where->right; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 554 | } |
| 555 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 556 | assert (where == nil); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 557 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 558 | if (result < 0) |
| 559 | parent->left = node; |
| 560 | else |
| 561 | parent->right = node; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 562 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 563 | node->parent = parent; |
| 564 | node->left = nil; |
| 565 | node->right = nil; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 566 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 567 | dict->nodecount++; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 568 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 569 | /* red black adjustments */ |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 570 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 571 | node->color = dnode_red; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 572 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 573 | while (parent->color == dnode_red) { |
| 574 | grandpa = parent->parent; |
| 575 | if (parent == grandpa->left) { |
| 576 | uncle = grandpa->right; |
| 577 | if (uncle->color == dnode_red) { /* red parent, red uncle */ |
| 578 | parent->color = dnode_black; |
| 579 | uncle->color = dnode_black; |
| 580 | grandpa->color = dnode_red; |
| 581 | node = grandpa; |
| 582 | parent = grandpa->parent; |
| 583 | } else { /* red parent, black uncle */ |
| 584 | if (node == parent->right) { |
| 585 | rotate_left(parent); |
| 586 | parent = node; |
| 587 | assert (grandpa == parent->parent); |
| 588 | /* rotation between parent and child preserves grandpa */ |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 589 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 590 | parent->color = dnode_black; |
| 591 | grandpa->color = dnode_red; |
| 592 | rotate_right(grandpa); |
| 593 | break; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 594 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 595 | } else { /* symmetric cases: parent == parent->parent->right */ |
| 596 | uncle = grandpa->left; |
| 597 | if (uncle->color == dnode_red) { |
| 598 | parent->color = dnode_black; |
| 599 | uncle->color = dnode_black; |
| 600 | grandpa->color = dnode_red; |
| 601 | node = grandpa; |
| 602 | parent = grandpa->parent; |
| 603 | } else { |
| 604 | if (node == parent->left) { |
| 605 | rotate_right(parent); |
| 606 | parent = node; |
| 607 | assert (grandpa == parent->parent); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 608 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 609 | parent->color = dnode_black; |
| 610 | grandpa->color = dnode_red; |
| 611 | rotate_left(grandpa); |
| 612 | break; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 617 | dict_root(dict)->color = dnode_black; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 618 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 619 | assert (dict_verify(dict)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /* |
| 623 | * Delete the given node from the dictionary. If the given node does not belong |
| 624 | * to the given dictionary, undefined behavior results. A pointer to the |
| 625 | * deleted node is returned. |
| 626 | */ |
| 627 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 628 | dnode_t *dict_delete(dict_t *dict, dnode_t *delete) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 629 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 630 | dnode_t *nil = dict_nil(dict), *child, *delparent = delete->parent; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 631 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 632 | /* basic deletion */ |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 633 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 634 | assert (!dict_isempty(dict)); |
| 635 | assert (dict_contains(dict, delete)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 636 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 637 | /* |
| 638 | * If the node being deleted has two children, then we replace it with its |
| 639 | * successor (i.e. the leftmost node in the right subtree.) By doing this, |
| 640 | * we avoid the traditional algorithm under which the successor's key and |
| 641 | * value *only* move to the deleted node and the successor is spliced out |
| 642 | * from the tree. We cannot use this approach because the user may hold |
| 643 | * pointers to the successor, or nodes may be inextricably tied to some |
| 644 | * other structures by way of embedding, etc. So we must splice out the |
| 645 | * node we are given, not some other node, and must not move contents from |
| 646 | * one node to another behind the user's back. |
| 647 | */ |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 648 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 649 | if (delete->left != nil && delete->right != nil) { |
| 650 | dnode_t *next = dict_next(dict, delete); |
| 651 | dnode_t *nextparent = next->parent; |
| 652 | dnode_color_t nextcolor = next->color; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 653 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 654 | assert (next != nil); |
| 655 | assert (next->parent != nil); |
| 656 | assert (next->left == nil); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 657 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 658 | /* |
| 659 | * First, splice out the successor from the tree completely, by |
| 660 | * moving up its right child into its place. |
| 661 | */ |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 662 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 663 | child = next->right; |
| 664 | child->parent = nextparent; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 665 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 666 | if (nextparent->left == next) { |
| 667 | nextparent->left = child; |
| 668 | } else { |
| 669 | assert (nextparent->right == next); |
| 670 | nextparent->right = child; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 671 | } |
| 672 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 673 | /* |
| 674 | * Now that the successor has been extricated from the tree, install it |
| 675 | * in place of the node that we want deleted. |
| 676 | */ |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 677 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 678 | next->parent = delparent; |
| 679 | next->left = delete->left; |
| 680 | next->right = delete->right; |
| 681 | next->left->parent = next; |
| 682 | next->right->parent = next; |
| 683 | next->color = delete->color; |
| 684 | delete->color = nextcolor; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 685 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 686 | if (delparent->left == delete) { |
| 687 | delparent->left = next; |
| 688 | } else { |
| 689 | assert (delparent->right == delete); |
| 690 | delparent->right = next; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 691 | } |
| 692 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 693 | } else { |
| 694 | assert (delete != nil); |
| 695 | assert (delete->left == nil || delete->right == nil); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 696 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 697 | child = (delete->left != nil) ? delete->left : delete->right; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 698 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 699 | child->parent = delparent = delete->parent; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 700 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 701 | if (delete == delparent->left) { |
| 702 | delparent->left = child; |
| 703 | } else { |
| 704 | assert (delete == delparent->right); |
| 705 | delparent->right = child; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 709 | delete->parent = NULL; |
| 710 | delete->right = NULL; |
| 711 | delete->left = NULL; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 712 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 713 | dict->nodecount--; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 714 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 715 | assert (verify_bintree(dict)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 716 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 717 | /* red-black adjustments */ |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 718 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 719 | if (delete->color == dnode_black) { |
| 720 | dnode_t *parent, *sister; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 721 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 722 | dict_root(dict)->color = dnode_red; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 723 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 724 | while (child->color == dnode_black) { |
| 725 | parent = child->parent; |
| 726 | if (child == parent->left) { |
| 727 | sister = parent->right; |
| 728 | assert (sister != nil); |
| 729 | if (sister->color == dnode_red) { |
| 730 | sister->color = dnode_black; |
| 731 | parent->color = dnode_red; |
| 732 | rotate_left(parent); |
| 733 | sister = parent->right; |
| 734 | assert (sister != nil); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 735 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 736 | if (sister->left->color == dnode_black |
| 737 | && sister->right->color == dnode_black) { |
| 738 | sister->color = dnode_red; |
| 739 | child = parent; |
| 740 | } else { |
| 741 | if (sister->right->color == dnode_black) { |
| 742 | assert (sister->left->color == dnode_red); |
| 743 | sister->left->color = dnode_black; |
| 744 | sister->color = dnode_red; |
| 745 | rotate_right(sister); |
| 746 | sister = parent->right; |
| 747 | assert (sister != nil); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 748 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 749 | sister->color = parent->color; |
| 750 | sister->right->color = dnode_black; |
| 751 | parent->color = dnode_black; |
| 752 | rotate_left(parent); |
| 753 | break; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 754 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 755 | } else { /* symmetric case: child == child->parent->right */ |
| 756 | assert (child == parent->right); |
| 757 | sister = parent->left; |
| 758 | assert (sister != nil); |
| 759 | if (sister->color == dnode_red) { |
| 760 | sister->color = dnode_black; |
| 761 | parent->color = dnode_red; |
| 762 | rotate_right(parent); |
| 763 | sister = parent->left; |
| 764 | assert (sister != nil); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 765 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 766 | if (sister->right->color == dnode_black |
| 767 | && sister->left->color == dnode_black) { |
| 768 | sister->color = dnode_red; |
| 769 | child = parent; |
| 770 | } else { |
| 771 | if (sister->left->color == dnode_black) { |
| 772 | assert (sister->right->color == dnode_red); |
| 773 | sister->right->color = dnode_black; |
| 774 | sister->color = dnode_red; |
| 775 | rotate_left(sister); |
| 776 | sister = parent->left; |
| 777 | assert (sister != nil); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 778 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 779 | sister->color = parent->color; |
| 780 | sister->left->color = dnode_black; |
| 781 | parent->color = dnode_black; |
| 782 | rotate_right(parent); |
| 783 | break; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 788 | child->color = dnode_black; |
| 789 | dict_root(dict)->color = dnode_black; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 790 | } |
| 791 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 792 | assert (dict_verify(dict)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 793 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 794 | return delete; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | /* |
| 798 | * Allocate a node using the dictionary's allocator routine, give it |
| 799 | * the data item. |
| 800 | */ |
| 801 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 802 | int dict_alloc_insert(dict_t *dict, const void *key, void *data) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 803 | { |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 804 | dnode_t *node = dict->allocnode (dict->context); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 805 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 806 | if (node) { |
| 807 | dnode_init(node, data); |
| 808 | dict_insert(dict, node, key); |
| 809 | return 1; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 810 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 811 | return 0; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 812 | } |
| 813 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 814 | void dict_delete_free(dict_t *dict, dnode_t *node) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 815 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 816 | dict_delete(dict, node); |
| 817 | dict->freenode(node, dict->context); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /* |
| 821 | * Return the node with the lowest (leftmost) key. If the dictionary is empty |
| 822 | * (that is, dict_isempty(dict) returns 1) a null pointer is returned. |
| 823 | */ |
| 824 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 825 | dnode_t *dict_first(dict_t *dict) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 826 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 827 | dnode_t *nil = dict_nil(dict), *root = dict_root(dict), *left; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 828 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 829 | if (root != nil) |
| 830 | while ((left = root->left) != nil) |
| 831 | root = left; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 832 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 833 | return (root == nil) ? NULL : root; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /* |
| 837 | * Return the node with the highest (rightmost) key. If the dictionary is empty |
| 838 | * (that is, dict_isempty(dict) returns 1) a null pointer is returned. |
| 839 | */ |
| 840 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 841 | dnode_t *dict_last(dict_t *dict) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 842 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 843 | dnode_t *nil = dict_nil(dict), *root = dict_root(dict), *right; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 844 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 845 | if (root != nil) |
| 846 | while ((right = root->right) != nil) |
| 847 | root = right; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 848 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 849 | return (root == nil) ? NULL : root; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | /* |
| 853 | * Return the given node's successor node---the node which has the |
| 854 | * next key in the the left to right ordering. If the node has |
| 855 | * no successor, a null pointer is returned rather than a pointer to |
| 856 | * the nil node. |
| 857 | */ |
| 858 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 859 | dnode_t *dict_next(dict_t *dict, dnode_t *curr) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 860 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 861 | dnode_t *nil = dict_nil(dict), *parent, *left; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 862 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 863 | if (curr->right != nil) { |
| 864 | curr = curr->right; |
| 865 | while ((left = curr->left) != nil) |
| 866 | curr = left; |
| 867 | return curr; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 868 | } |
| 869 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 870 | parent = curr->parent; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 871 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 872 | while (parent != nil && curr == parent->right) { |
| 873 | curr = parent; |
| 874 | parent = curr->parent; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 875 | } |
| 876 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 877 | return (parent == nil) ? NULL : parent; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /* |
| 881 | * Return the given node's predecessor, in the key order. |
| 882 | * The nil sentinel node is returned if there is no predecessor. |
| 883 | */ |
| 884 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 885 | dnode_t *dict_prev(dict_t *dict, dnode_t *curr) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 886 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 887 | dnode_t *nil = dict_nil(dict), *parent, *right; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 888 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 889 | if (curr->left != nil) { |
| 890 | curr = curr->left; |
| 891 | while ((right = curr->right) != nil) |
| 892 | curr = right; |
| 893 | return curr; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 894 | } |
| 895 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 896 | parent = curr->parent; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 897 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 898 | while (parent != nil && curr == parent->left) { |
| 899 | curr = parent; |
| 900 | parent = curr->parent; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 901 | } |
| 902 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 903 | return (parent == nil) ? NULL : parent; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 904 | } |
| 905 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 906 | void dict_allow_dupes(dict_t *dict) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 907 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 908 | dict->dupes = 1; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | #undef dict_count |
| 912 | #undef dict_isempty |
| 913 | #undef dict_isfull |
| 914 | #undef dnode_get |
| 915 | #undef dnode_put |
| 916 | #undef dnode_getkey |
| 917 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 918 | dictcount_t dict_count(dict_t *dict) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 919 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 920 | return dict->nodecount; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 921 | } |
| 922 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 923 | int dict_isempty(dict_t *dict) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 924 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 925 | return dict->nodecount == 0; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 926 | } |
| 927 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 928 | int dict_isfull(dict_t *dict) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 929 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 930 | return dict->nodecount == dict->maxcount; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 931 | } |
| 932 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 933 | int dict_contains(dict_t *dict, dnode_t *node) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 934 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 935 | return verify_dict_has_node(dict_nil(dict), dict_root(dict), node); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 936 | } |
| 937 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 938 | static dnode_t *dnode_alloc(void *context) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 939 | { |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 940 | return XCALLOC(MTYPE_ISIS_DICT_NODE, sizeof(dnode_t)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 941 | } |
| 942 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 943 | static void dnode_free(dnode_t *node, void *context) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 944 | { |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 945 | XFREE(MTYPE_ISIS_DICT_NODE, node); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 946 | } |
| 947 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 948 | dnode_t *dnode_create(void *data) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 949 | { |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 950 | dnode_t *new = XCALLOC(MTYPE_ISIS_DICT_NODE, sizeof(dnode_t)); |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 951 | if (new) { |
| 952 | new->data = data; |
| 953 | new->parent = NULL; |
| 954 | new->left = NULL; |
| 955 | new->right = NULL; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 956 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 957 | return new; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 958 | } |
| 959 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 960 | dnode_t *dnode_init(dnode_t *dnode, void *data) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 961 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 962 | dnode->data = data; |
| 963 | dnode->parent = NULL; |
| 964 | dnode->left = NULL; |
| 965 | dnode->right = NULL; |
| 966 | return dnode; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 967 | } |
| 968 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 969 | void dnode_destroy(dnode_t *dnode) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 970 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 971 | assert (!dnode_is_in_a_dict(dnode)); |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 972 | XFREE(MTYPE_ISIS_DICT_NODE, dnode); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 973 | } |
| 974 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 975 | void *dnode_get(dnode_t *dnode) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 976 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 977 | return dnode->data; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 978 | } |
| 979 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 980 | const void *dnode_getkey(dnode_t *dnode) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 981 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 982 | return dnode->key; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 983 | } |
| 984 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 985 | void dnode_put(dnode_t *dnode, void *data) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 986 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 987 | dnode->data = data; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 988 | } |
| 989 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 990 | int dnode_is_in_a_dict(dnode_t *dnode) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 991 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 992 | return (dnode->parent && dnode->left && dnode->right); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 993 | } |
| 994 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 995 | void dict_process(dict_t *dict, void *context, dnode_process_t function) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 996 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 997 | dnode_t *node = dict_first(dict), *next; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 998 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 999 | while (node != NULL) { |
| 1000 | /* check for callback function deleting */ |
| 1001 | /* the next node from under us */ |
| 1002 | assert (dict_contains(dict, node)); |
| 1003 | next = dict_next(dict, node); |
| 1004 | function(dict, node, context); |
| 1005 | node = next; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1006 | } |
| 1007 | } |
| 1008 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1009 | static void load_begin_internal(dict_load_t *load, dict_t *dict) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1010 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1011 | load->dictptr = dict; |
| 1012 | load->nilnode.left = &load->nilnode; |
| 1013 | load->nilnode.right = &load->nilnode; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1016 | void dict_load_begin(dict_load_t *load, dict_t *dict) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1017 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1018 | assert (dict_isempty(dict)); |
| 1019 | load_begin_internal(load, dict); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1022 | void dict_load_next(dict_load_t *load, dnode_t *newnode, const void *key) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1023 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1024 | dict_t *dict = load->dictptr; |
| 1025 | dnode_t *nil = &load->nilnode; |
| 1026 | |
| 1027 | assert (!dnode_is_in_a_dict(newnode)); |
| 1028 | assert (dict->nodecount < dict->maxcount); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1029 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1030 | #ifndef NDEBUG |
| 1031 | if (dict->nodecount > 0) { |
| 1032 | if (dict->dupes) |
| 1033 | assert (dict->compare(nil->left->key, key) <= 0); |
| 1034 | else |
| 1035 | assert (dict->compare(nil->left->key, key) < 0); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1036 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1037 | #endif |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1038 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1039 | newnode->key = key; |
| 1040 | nil->right->left = newnode; |
| 1041 | nil->right = newnode; |
| 1042 | newnode->left = nil; |
| 1043 | dict->nodecount++; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1046 | void dict_load_end(dict_load_t *load) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1047 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1048 | dict_t *dict = load->dictptr; |
| 1049 | dnode_t *tree[DICT_DEPTH_MAX] = { 0 }; |
| 1050 | dnode_t *curr, *dictnil = dict_nil(dict), *loadnil = &load->nilnode, *next; |
| 1051 | dnode_t *complete = 0; |
| 1052 | dictcount_t fullcount = DICTCOUNT_T_MAX, nodecount = dict->nodecount; |
| 1053 | dictcount_t botrowcount; |
| 1054 | unsigned baselevel = 0, level = 0, i; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1055 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1056 | assert (dnode_red == 0 && dnode_black == 1); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1057 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1058 | while (fullcount >= nodecount && fullcount) |
| 1059 | fullcount >>= 1; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1060 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1061 | botrowcount = nodecount - fullcount; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1062 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1063 | for (curr = loadnil->left; curr != loadnil; curr = next) { |
| 1064 | next = curr->left; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1065 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1066 | if (complete == NULL && botrowcount-- == 0) { |
| 1067 | assert (baselevel == 0); |
| 1068 | assert (level == 0); |
| 1069 | baselevel = level = 1; |
| 1070 | complete = tree[0]; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1071 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1072 | if (complete != 0) { |
| 1073 | tree[0] = 0; |
| 1074 | complete->right = dictnil; |
| 1075 | while (tree[level] != 0) { |
| 1076 | tree[level]->right = complete; |
| 1077 | complete->parent = tree[level]; |
| 1078 | complete = tree[level]; |
| 1079 | tree[level++] = 0; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | } |
| 1083 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1084 | if (complete == NULL) { |
| 1085 | curr->left = dictnil; |
| 1086 | curr->right = dictnil; |
| 1087 | curr->color = level % 2; |
| 1088 | complete = curr; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1089 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1090 | assert (level == baselevel); |
| 1091 | while (tree[level] != 0) { |
| 1092 | tree[level]->right = complete; |
| 1093 | complete->parent = tree[level]; |
| 1094 | complete = tree[level]; |
| 1095 | tree[level++] = 0; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1096 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1097 | } else { |
| 1098 | curr->left = complete; |
| 1099 | curr->color = (level + 1) % 2; |
| 1100 | complete->parent = curr; |
| 1101 | tree[level] = curr; |
| 1102 | complete = 0; |
| 1103 | level = baselevel; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1107 | if (complete == NULL) |
| 1108 | complete = dictnil; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1109 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1110 | for (i = 0; i < DICT_DEPTH_MAX; i++) { |
| 1111 | if (tree[i] != 0) { |
| 1112 | tree[i]->right = complete; |
| 1113 | complete->parent = tree[i]; |
| 1114 | complete = tree[i]; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1115 | } |
| 1116 | } |
| 1117 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1118 | dictnil->color = dnode_black; |
| 1119 | dictnil->right = dictnil; |
| 1120 | complete->parent = dictnil; |
| 1121 | complete->color = dnode_black; |
| 1122 | dict_root(dict) = complete; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1123 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1124 | assert (dict_verify(dict)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1127 | void dict_merge(dict_t *dest, dict_t *source) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1128 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1129 | dict_load_t load; |
| 1130 | dnode_t *leftnode = dict_first(dest), *rightnode = dict_first(source); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1131 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1132 | assert (dict_similar(dest, source)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1133 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1134 | if (source == dest) |
| 1135 | return; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1136 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1137 | dest->nodecount = 0; |
| 1138 | load_begin_internal(&load, dest); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1139 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1140 | for (;;) { |
| 1141 | if (leftnode != NULL && rightnode != NULL) { |
| 1142 | if (dest->compare(leftnode->key, rightnode->key) < 0) |
| 1143 | goto copyleft; |
| 1144 | else |
| 1145 | goto copyright; |
| 1146 | } else if (leftnode != NULL) { |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1147 | goto copyleft; |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1148 | } else if (rightnode != NULL) { |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1149 | goto copyright; |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1150 | } else { |
| 1151 | assert (leftnode == NULL && rightnode == NULL); |
| 1152 | break; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | copyleft: |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1156 | { |
| 1157 | dnode_t *next = dict_next(dest, leftnode); |
| 1158 | #ifndef NDEBUG |
| 1159 | leftnode->left = NULL; /* suppress assertion in dict_load_next */ |
| 1160 | #endif |
| 1161 | dict_load_next(&load, leftnode, leftnode->key); |
| 1162 | leftnode = next; |
| 1163 | continue; |
| 1164 | } |
| 1165 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1166 | copyright: |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1167 | { |
| 1168 | dnode_t *next = dict_next(source, rightnode); |
| 1169 | #ifndef NDEBUG |
| 1170 | rightnode->left = NULL; |
| 1171 | #endif |
| 1172 | dict_load_next(&load, rightnode, rightnode->key); |
| 1173 | rightnode = next; |
| 1174 | continue; |
| 1175 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1178 | dict_clear(source); |
| 1179 | dict_load_end(&load); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | #ifdef KAZLIB_TEST_MAIN |
| 1183 | |
| 1184 | #include <stdio.h> |
| 1185 | #include <string.h> |
| 1186 | #include <ctype.h> |
| 1187 | #include <stdarg.h> |
| 1188 | |
| 1189 | typedef char input_t[256]; |
| 1190 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1191 | static int tokenize(char *string, ...) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1192 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1193 | char **tokptr; |
| 1194 | va_list arglist; |
| 1195 | int tokcount = 0; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1196 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1197 | va_start(arglist, string); |
| 1198 | tokptr = va_arg(arglist, char **); |
| 1199 | while (tokptr) { |
| 1200 | while (*string && isspace((unsigned char) *string)) |
| 1201 | string++; |
| 1202 | if (!*string) |
| 1203 | break; |
| 1204 | *tokptr = string; |
| 1205 | while (*string && !isspace((unsigned char) *string)) |
| 1206 | string++; |
| 1207 | tokptr = va_arg(arglist, char **); |
| 1208 | tokcount++; |
| 1209 | if (!*string) |
| 1210 | break; |
| 1211 | *string++ = 0; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1212 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1213 | va_end(arglist); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1214 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1215 | return tokcount; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1218 | static int comparef(const void *key1, const void *key2) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1219 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1220 | return strcmp(key1, key2); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1223 | static char *dupstring(char *str) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1224 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1225 | int sz = strlen(str) + 1; |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 1226 | char *new = XCALLOC(MTYPE_ISIS_TMP, sz); |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1227 | if (new) |
| 1228 | memcpy(new, str, sz); |
| 1229 | return new; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1232 | static dnode_t *new_node(void *c) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1233 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1234 | static dnode_t few[5]; |
| 1235 | static int count; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1236 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1237 | if (count < 5) |
| 1238 | return few + count++; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1239 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1240 | return NULL; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1243 | static void del_node(dnode_t *n, void *c) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1244 | { |
| 1245 | } |
| 1246 | |
| 1247 | static int prompt = 0; |
| 1248 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1249 | static void construct(dict_t *d) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1250 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1251 | input_t in; |
| 1252 | int done = 0; |
| 1253 | dict_load_t dl; |
| 1254 | dnode_t *dn; |
| 1255 | char *tok1, *tok2, *val; |
| 1256 | const char *key; |
| 1257 | char *help = |
| 1258 | "p turn prompt on\n" |
| 1259 | "q finish construction\n" |
| 1260 | "a <key> <val> add new entry\n"; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1261 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1262 | if (!dict_isempty(d)) |
| 1263 | puts("warning: dictionary not empty!"); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1264 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1265 | dict_load_begin(&dl, d); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1266 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1267 | while (!done) { |
| 1268 | if (prompt) |
| 1269 | putchar('>'); |
| 1270 | fflush(stdout); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1271 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1272 | if (!fgets(in, sizeof(input_t), stdin)) |
| 1273 | break; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1274 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1275 | switch (in[0]) { |
| 1276 | case '?': |
| 1277 | puts(help); |
| 1278 | break; |
| 1279 | case 'p': |
| 1280 | prompt = 1; |
| 1281 | break; |
| 1282 | case 'q': |
| 1283 | done = 1; |
| 1284 | break; |
| 1285 | case 'a': |
| 1286 | if (tokenize(in+1, &tok1, &tok2, (char **) 0) != 2) { |
| 1287 | puts("what?"); |
| 1288 | break; |
| 1289 | } |
| 1290 | key = dupstring(tok1); |
| 1291 | val = dupstring(tok2); |
| 1292 | dn = dnode_create(val); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1293 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1294 | if (!key || !val || !dn) { |
| 1295 | puts("out of memory"); |
| 1296 | free((void *) key); |
| 1297 | free(val); |
| 1298 | if (dn) |
| 1299 | dnode_destroy(dn); |
| 1300 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1301 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1302 | dict_load_next(&dl, dn, key); |
| 1303 | break; |
| 1304 | default: |
| 1305 | putchar('?'); |
| 1306 | putchar('\n'); |
| 1307 | break; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1308 | } |
| 1309 | } |
| 1310 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1311 | dict_load_end(&dl); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1314 | int main(void) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1315 | { |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1316 | input_t in; |
| 1317 | dict_t darray[10]; |
| 1318 | dict_t *d = &darray[0]; |
| 1319 | dnode_t *dn; |
| 1320 | int i; |
| 1321 | char *tok1, *tok2, *val; |
| 1322 | const char *key; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1323 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1324 | char *help = |
| 1325 | "a <key> <val> add value to dictionary\n" |
| 1326 | "d <key> delete value from dictionary\n" |
| 1327 | "l <key> lookup value in dictionary\n" |
| 1328 | "( <key> lookup lower bound\n" |
| 1329 | ") <key> lookup upper bound\n" |
| 1330 | "# <num> switch to alternate dictionary (0-9)\n" |
| 1331 | "j <num> <num> merge two dictionaries\n" |
| 1332 | "f free the whole dictionary\n" |
| 1333 | "k allow duplicate keys\n" |
| 1334 | "c show number of entries\n" |
| 1335 | "t dump whole dictionary in sort order\n" |
| 1336 | "m make dictionary out of sorted items\n" |
| 1337 | "p turn prompt on\n" |
| 1338 | "s switch to non-functioning allocator\n" |
| 1339 | "q quit"; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1340 | |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 1341 | for (i = 0; i < 10; i++) |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1342 | dict_init(&darray[i], DICTCOUNT_T_MAX, comparef); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1343 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1344 | for (;;) { |
| 1345 | if (prompt) |
| 1346 | putchar('>'); |
| 1347 | fflush(stdout); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1348 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1349 | if (!fgets(in, sizeof(input_t), stdin)) |
| 1350 | break; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1351 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1352 | switch(in[0]) { |
| 1353 | case '?': |
| 1354 | puts(help); |
| 1355 | break; |
| 1356 | case 'a': |
| 1357 | if (tokenize(in+1, &tok1, &tok2, (char **) 0) != 2) { |
| 1358 | puts("what?"); |
| 1359 | break; |
| 1360 | } |
| 1361 | key = dupstring(tok1); |
| 1362 | val = dupstring(tok2); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1363 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1364 | if (!key || !val) { |
| 1365 | puts("out of memory"); |
| 1366 | free((void *) key); |
| 1367 | free(val); |
| 1368 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1369 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1370 | if (!dict_alloc_insert(d, key, val)) { |
| 1371 | puts("dict_alloc_insert failed"); |
| 1372 | free((void *) key); |
| 1373 | free(val); |
| 1374 | break; |
| 1375 | } |
| 1376 | break; |
| 1377 | case 'd': |
| 1378 | if (tokenize(in+1, &tok1, (char **) 0) != 1) { |
| 1379 | puts("what?"); |
| 1380 | break; |
| 1381 | } |
| 1382 | dn = dict_lookup(d, tok1); |
| 1383 | if (!dn) { |
| 1384 | puts("dict_lookup failed"); |
| 1385 | break; |
| 1386 | } |
| 1387 | val = dnode_get(dn); |
| 1388 | key = dnode_getkey(dn); |
| 1389 | dict_delete_free(d, dn); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1390 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1391 | free(val); |
| 1392 | free((void *) key); |
| 1393 | break; |
| 1394 | case 'f': |
| 1395 | dict_free(d); |
| 1396 | break; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1397 | case 'l': |
| 1398 | case '(': |
| 1399 | case ')': |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1400 | if (tokenize(in+1, &tok1, (char **) 0) != 1) { |
| 1401 | puts("what?"); |
| 1402 | break; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1403 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1404 | dn = 0; |
| 1405 | switch (in[0]) { |
| 1406 | case 'l': |
| 1407 | dn = dict_lookup(d, tok1); |
| 1408 | break; |
| 1409 | case '(': |
| 1410 | dn = dict_lower_bound(d, tok1); |
| 1411 | break; |
| 1412 | case ')': |
| 1413 | dn = dict_upper_bound(d, tok1); |
| 1414 | break; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1415 | } |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1416 | if (!dn) { |
| 1417 | puts("lookup failed"); |
| 1418 | break; |
| 1419 | } |
| 1420 | val = dnode_get(dn); |
| 1421 | puts(val); |
| 1422 | break; |
| 1423 | case 'm': |
| 1424 | construct(d); |
| 1425 | break; |
| 1426 | case 'k': |
| 1427 | dict_allow_dupes(d); |
| 1428 | break; |
| 1429 | case 'c': |
| 1430 | printf("%lu\n", (unsigned long) dict_count(d)); |
| 1431 | break; |
| 1432 | case 't': |
| 1433 | for (dn = dict_first(d); dn; dn = dict_next(d, dn)) { |
| 1434 | printf("%s\t%s\n", (char *) dnode_getkey(dn), |
| 1435 | (char *) dnode_get(dn)); |
| 1436 | } |
| 1437 | break; |
| 1438 | case 'q': |
| 1439 | exit(0); |
| 1440 | break; |
| 1441 | case '\0': |
| 1442 | break; |
| 1443 | case 'p': |
| 1444 | prompt = 1; |
| 1445 | break; |
| 1446 | case 's': |
| 1447 | dict_set_allocator(d, new_node, del_node, NULL); |
| 1448 | break; |
| 1449 | case '#': |
| 1450 | if (tokenize(in+1, &tok1, (char **) 0) != 1) { |
| 1451 | puts("what?"); |
| 1452 | break; |
| 1453 | } else { |
| 1454 | int dictnum = atoi(tok1); |
| 1455 | if (dictnum < 0 || dictnum > 9) { |
| 1456 | puts("invalid number"); |
| 1457 | break; |
| 1458 | } |
| 1459 | d = &darray[dictnum]; |
| 1460 | } |
| 1461 | break; |
| 1462 | case 'j': |
| 1463 | if (tokenize(in+1, &tok1, &tok2, (char **) 0) != 2) { |
| 1464 | puts("what?"); |
| 1465 | break; |
| 1466 | } else { |
| 1467 | int dict1 = atoi(tok1), dict2 = atoi(tok2); |
| 1468 | if (dict1 < 0 || dict1 > 9 || dict2 < 0 || dict2 > 9) { |
| 1469 | puts("invalid number"); |
| 1470 | break; |
| 1471 | } |
| 1472 | dict_merge(&darray[dict1], &darray[dict2]); |
| 1473 | } |
| 1474 | break; |
| 1475 | default: |
| 1476 | putchar('?'); |
| 1477 | putchar('\n'); |
| 1478 | break; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1479 | } |
| 1480 | } |
| 1481 | |
hasso | ffe543a | 2005-09-25 12:04:25 +0000 | [diff] [blame] | 1482 | return 0; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | #endif |