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