| 1 | /* |
| 2 | * Copyright 2010 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * "Daniel Kopecek" <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <string.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <errno.h> |
| 30 | |
| 31 | #include "rbt_common.h" |
| 32 | #include "rbt_str.h" |
| 33 | |
| 34 | static struct rbt_node *rbt_str_node_alloc(void) |
| 35 | { |
| 36 | struct rbt_node *n = NULL; |
| 37 | |
| 38 | #ifndef OS_WINDOWS |
| 39 | if (posix_memalign((void **)(void *)(&n), sizeof(void *), |
| 40 | sizeof (struct rbt_node) + sizeof (struct rbt_str_node)) != 0) |
| 41 | { |
| 42 | abort (); |
| 43 | } |
| 44 | #else |
| 45 | // https://msdn.microsoft.com/en-us/library/8z34s9c6.aspx |
| 46 | n = _aligned_malloc(sizeof (struct rbt_node) + sizeof (struct rbt_str_node), sizeof(void *)); |
| 47 | #endif |
| 48 | |
| 49 | n->_chld[0] = NULL; |
| 50 | n->_chld[1] = NULL; |
| 51 | |
| 52 | return(n); |
| 53 | } |
| 54 | |
| 55 | static void rbt_str_node_free(struct rbt_node *n) |
| 56 | { |
| 57 | if (n != NULL) |
| 58 | free(rbt_node_ptr(n)); |
| 59 | } |
| 60 | |
| 61 | rbt_t *rbt_str_new (void) |
| 62 | { |
| 63 | return rbt_new (RBT_STRKEY); |
| 64 | } |
| 65 | |
| 66 | static void rbt_str_free_callback(struct rbt_str_node *n) |
| 67 | { |
| 68 | free(n->key); |
| 69 | /* node memory is freed by rbt_free */ |
| 70 | } |
| 71 | |
| 72 | void rbt_str_free (rbt_t *rbt) |
| 73 | { |
| 74 | rbt_free(rbt, (void(*)(void *))&rbt_str_free_callback); |
| 75 | } |
| 76 | |
| 77 | void rbt_str_free_cb (rbt_t *rbt, void (*callback)(struct rbt_str_node *)) |
| 78 | { |
| 79 | rbt_free(rbt, (void(*)(void *))callback); |
| 80 | } |
| 81 | |
| 82 | void rbt_str_free_cb2 (rbt_t *rbt, void (*callback)(struct rbt_str_node *, void *user), void *user) |
| 83 | { |
| 84 | rbt_free2(rbt, (void(*)(void *, void *))callback, user); |
| 85 | } |
| 86 | |
| 87 | int rbt_str_add(rbt_t *rbt, char *key, void *data) |
| 88 | { |
| 89 | struct rbt_node fake; |
| 90 | struct rbt_node *h[4]; |
| 91 | register uint8_t dvec; |
| 92 | register char *n_key, *u_key; |
| 93 | register int cmp; |
| 94 | |
| 95 | u_key = key; |
| 96 | rbt_wlock(rbt); |
| 97 | |
| 98 | /* |
| 99 | * Fake node |
| 100 | */ |
| 101 | fake._chld[RBT_NODE_SL] = NULL; |
| 102 | fake._chld[RBT_NODE_SR] = rbt->root; |
| 103 | rbt_node_setcolor(&fake, RBT_NODE_CB); |
| 104 | |
| 105 | /* |
| 106 | * Prepare node history stack & direction history vector |
| 107 | */ |
| 108 | h[3] = NULL; |
| 109 | h[2] = NULL; |
| 110 | h[1] = &fake; |
| 111 | h[0] = rbt->root; |
| 112 | dvec = RBT_NODE_SR; |
| 113 | |
| 114 | for (;;) { |
| 115 | if (rbt_node_ptr(h[0]) == NULL) { |
| 116 | /* |
| 117 | * Allocate new node aligned to sizeof(void *) bytes. |
| 118 | * On most systems, malloc already returns aligned |
| 119 | * memory but we want to ensure that its aligned using |
| 120 | * posix_memalign(3). |
| 121 | */ |
| 122 | h[0] = rbt_str_node_alloc (); |
| 123 | |
| 124 | rbt_str_node(h[0])->key = key; |
| 125 | rbt_str_node(h[0])->data = data; |
| 126 | |
| 127 | /* |
| 128 | * Set the new node as the child of the last node we've |
| 129 | * visited. The last direction is the lowest bit of the |
| 130 | * direction history vector. |
| 131 | */ |
| 132 | rbt_node_setptr(rbt_node_ptr(h[1])->_chld[dvec & 1], h[0]); |
| 133 | rbt_node_setcolor(h[0], RBT_NODE_CR); |
| 134 | |
| 135 | /* |
| 136 | * Since we are inserting a new red node, we need to fix |
| 137 | * a red violation if the parent of the new node is red. |
| 138 | */ |
| 139 | if (rbt_node_getcolor(h[1]) == RBT_NODE_CR) { |
| 140 | rbt_redfix(h, dvec, rbt_node_ptr(h[3])->_chld[(dvec >> 2) & 1]); |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Update root node and color it black |
| 145 | */ |
| 146 | rbt->root = fake._chld[RBT_NODE_SR]; |
| 147 | rbt_node_setcolor(rbt->root, RBT_NODE_CB); |
| 148 | |
| 149 | /* |
| 150 | * Update node counter |
| 151 | */ |
| 152 | ++(rbt->size); |
| 153 | |
| 154 | break; |
| 155 | } else if (rbt_node_getcolor(rbt_node_ptr(h[0])->_chld[0]) == RBT_NODE_CR && |
| 156 | rbt_node_getcolor(rbt_node_ptr(h[0])->_chld[1]) == RBT_NODE_CR) |
| 157 | { |
| 158 | /* |
| 159 | * Color switch |
| 160 | */ |
| 161 | rbt_node_setcolor(h[0], RBT_NODE_CR); |
| 162 | rbt_node_setcolor(rbt_node_ptr(h[0])->_chld[0], RBT_NODE_CB); |
| 163 | rbt_node_setcolor(rbt_node_ptr(h[0])->_chld[1], RBT_NODE_CB); |
| 164 | |
| 165 | /* |
| 166 | * Fix red violation |
| 167 | */ |
| 168 | if (rbt_node_getcolor(h[1]) == RBT_NODE_CR) { |
| 169 | rbt_redfix(h, dvec, rbt_node_ptr(h[3])->_chld[(dvec >> 2) & 1]); |
| 170 | } |
| 171 | } else if (rbt_node_getcolor(h[0]) == RBT_NODE_CR && |
| 172 | rbt_node_getcolor(h[1]) == RBT_NODE_CR) |
| 173 | { |
| 174 | /* |
| 175 | * Fix red violation |
| 176 | */ |
| 177 | rbt_redfix(h, dvec, rbt_node_ptr(h[3])->_chld[(dvec >> 2)&1]); |
| 178 | } |
| 179 | |
| 180 | n_key = rbt_str_node(h[0])->key; |
| 181 | cmp = strcmp(u_key, n_key); |
| 182 | |
| 183 | if (cmp < 0) { |
| 184 | rbt_hpush4(h, rbt_node_ptr(h[0])->_chld[RBT_NODE_SL]); |
| 185 | dvec = (dvec << 1) | RBT_NODE_SL; |
| 186 | } else if (cmp > 0) { |
| 187 | rbt_hpush4(h, rbt_node_ptr(h[0])->_chld[RBT_NODE_SR]); |
| 188 | dvec = (dvec << 1) | RBT_NODE_SR; |
| 189 | } else { |
| 190 | /* |
| 191 | * Update root node and color it black |
| 192 | */ |
| 193 | rbt->root = fake._chld[RBT_NODE_SR]; |
| 194 | rbt_node_setcolor(rbt->root, RBT_NODE_CB); |
| 195 | |
| 196 | rbt_wunlock(rbt); |
| 197 | return (1); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | rbt_wunlock(rbt); |
| 202 | return (0); |
| 203 | } |
| 204 | |
| 205 | void *rbt_str_rep(rbt_t *rbt, const char *key, void *data) |
| 206 | { |
| 207 | return (NULL); |
| 208 | } |
| 209 | |
| 210 | int rbt_str_del(rbt_t *rbt, const char *key, void **n) |
| 211 | { |
| 212 | struct rbt_node fake, *save; |
| 213 | struct rbt_node *h[4]; |
| 214 | register uint8_t dvec; |
| 215 | register char *n_key, *u_key; |
| 216 | register int cmp; |
| 217 | |
| 218 | save = NULL; |
| 219 | u_key = (char *)key; |
| 220 | |
| 221 | rbt_wlock(rbt); |
| 222 | |
| 223 | if (rbt->size == 0) { |
| 224 | rbt_wunlock(rbt); |
| 225 | return (1); |
| 226 | } |
| 227 | |
| 228 | if (rbt_node_ptr(rbt->root) == NULL) { |
| 229 | return -1; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Fake node |
| 234 | */ |
| 235 | fake._chld[RBT_NODE_SL] = NULL; |
| 236 | fake._chld[RBT_NODE_SR] = rbt->root; |
| 237 | rbt_node_setcolor(&fake, RBT_NODE_CB); |
| 238 | |
| 239 | /* |
| 240 | * Prepare node history stack & direction history vector |
| 241 | */ |
| 242 | h[2] = NULL; |
| 243 | h[1] = NULL; |
| 244 | h[0] = &fake; |
| 245 | dvec = RBT_NODE_SR; |
| 246 | |
| 247 | for (;;) { |
| 248 | if (rbt_node_ptr(rbt_node_ptr(h[0])->_chld[dvec & 1]) == NULL) |
| 249 | break; |
| 250 | |
| 251 | rbt_hpush4(h, rbt_node_ptr(h[0])->_chld[dvec & 1]); |
| 252 | n_key = rbt_str_node(h[0])->key; |
| 253 | cmp = strcmp(u_key, n_key); |
| 254 | |
| 255 | if (cmp < 0) { |
| 256 | dvec = (dvec << 1) | RBT_NODE_SL; |
| 257 | } else if (cmp > 0) { |
| 258 | dvec = (dvec << 1) | RBT_NODE_SR; |
| 259 | } else { |
| 260 | save = rbt_node_ptr(h[0]); |
| 261 | dvec = (dvec << 1) | RBT_NODE_SL; |
| 262 | } |
| 263 | |
| 264 | if (rbt_node_getcolor(h[0]) == RBT_NODE_CB && |
| 265 | rbt_node_getcolor(rbt_node_ptr(h[0])->_chld[dvec & 1]) == RBT_NODE_CB) |
| 266 | { |
| 267 | if (rbt_node_getcolor(rbt_node_ptr(h[0])->_chld[!(dvec & 1)]) == RBT_NODE_CR) { |
| 268 | switch(dvec & 1) { |
| 269 | case RBT_NODE_SR: |
| 270 | rbt_node_setptr(rbt_node_ptr(h[1])->_chld[(dvec >> 1) & 1], |
| 271 | rbt_node_rotate_R(h[0])); |
| 272 | break; |
| 273 | case RBT_NODE_SL: |
| 274 | rbt_node_setptr(rbt_node_ptr(h[1])->_chld[(dvec >> 1) & 1], |
| 275 | rbt_node_rotate_L(h[0])); |
| 276 | break; |
| 277 | } |
| 278 | |
| 279 | h[1] = rbt_node_ptr(h[1])->_chld[(dvec >> 1) & 1]; |
| 280 | } else { |
| 281 | register struct rbt_node *s; |
| 282 | |
| 283 | s = rbt_node_ptr(rbt_node_ptr(h[1])->_chld[!((dvec >> 1) & 1)]); |
| 284 | |
| 285 | if (s != NULL) { |
| 286 | if (rbt_node_getcolor(s->_chld[0]) == RBT_NODE_CB && |
| 287 | rbt_node_getcolor(s->_chld[1]) == RBT_NODE_CB) |
| 288 | { |
| 289 | rbt_node_setcolor(h[0], RBT_NODE_CR); |
| 290 | rbt_node_setcolor(h[1], RBT_NODE_CB); |
| 291 | rbt_node_setcolor(s, RBT_NODE_CR); |
| 292 | } else { |
| 293 | register uint8_t d = rbt_node_ptr(rbt_node_ptr(h[2])->_chld[RBT_NODE_SR]) == rbt_node_ptr(h[1]); |
| 294 | |
| 295 | if (rbt_node_getcolor(s->_chld[(dvec >> 1) & 1]) == RBT_NODE_CR) { |
| 296 | switch((dvec >> 1) & 1) { |
| 297 | case RBT_NODE_SR: |
| 298 | rbt_node_setptr(rbt_node_ptr(h[2])->_chld[d], |
| 299 | rbt_node_rotate_RL(h[1])); |
| 300 | break; |
| 301 | case RBT_NODE_SL: |
| 302 | rbt_node_setptr(rbt_node_ptr(h[2])->_chld[d], |
| 303 | rbt_node_rotate_LR(h[1])); |
| 304 | break; |
| 305 | } |
| 306 | } else if (rbt_node_getcolor(s->_chld[!((dvec >> 1) & 1)]) == RBT_NODE_CR) { |
| 307 | switch((dvec >> 1) & 1) { |
| 308 | case RBT_NODE_SR: |
| 309 | rbt_node_setptr(rbt_node_ptr(h[2])->_chld[d], |
| 310 | rbt_node_rotate_R(h[1])); |
| 311 | break; |
| 312 | case RBT_NODE_SL: |
| 313 | rbt_node_setptr(rbt_node_ptr(h[2])->_chld[d], |
| 314 | rbt_node_rotate_L(h[1])); |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | s = rbt_node_ptr(rbt_node_ptr(h[2])->_chld[d]); |
| 320 | |
| 321 | rbt_node_setcolor(h[0], RBT_NODE_CR); |
| 322 | rbt_node_setcolor(s, RBT_NODE_CR); |
| 323 | rbt_node_setcolor(s->_chld[0], RBT_NODE_CB); |
| 324 | rbt_node_setcolor(s->_chld[1], RBT_NODE_CB); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | if (save != NULL) { |
| 332 | h[0] = rbt_node_ptr(h[0]); |
| 333 | /* |
| 334 | * The node color of the node that will be delete is always |
| 335 | * red in case the node is not the root node. |
| 336 | */ |
| 337 | if (rbt_node_ptr(fake._chld[RBT_NODE_SR]) != h[0] |
| 338 | && rbt_node_getcolor(h[0]) != RBT_NODE_CR) { |
| 339 | return -1; |
| 340 | } |
| 341 | if (n != NULL) |
| 342 | *n = rbt_str_node(save)->data; |
| 343 | |
| 344 | rbt_str_node(save)->data = rbt_str_node(h[0])->data; |
| 345 | rbt_str_node(save)->key = rbt_str_node(h[0])->key; |
| 346 | |
| 347 | dvec = rbt_node_ptr(rbt_node_ptr(h[1])->_chld[1]) == h[0]; |
| 348 | |
| 349 | if (rbt_node_ptr(h[0]->_chld[RBT_NODE_SR]) == NULL) { |
| 350 | rbt_node_setptr(rbt_node_ptr(h[1])->_chld[dvec], h[0]->_chld[RBT_NODE_SL]); |
| 351 | } else { |
| 352 | rbt_node_setptr(rbt_node_ptr(h[1])->_chld[dvec], h[0]->_chld[RBT_NODE_SR]); |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Free memory used by the deleted node and decrement |
| 357 | * the node counter. |
| 358 | */ |
| 359 | rbt_str_node_free(h[0]); |
| 360 | --(rbt->size); |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Update root node and color it black and unlock the tree. |
| 365 | */ |
| 366 | rbt->root = fake._chld[RBT_NODE_SR]; |
| 367 | rbt_node_setcolor(rbt->root, RBT_NODE_CB); |
| 368 | rbt_wunlock(rbt); |
| 369 | |
| 370 | return (save == NULL ? 1 : 0); |
| 371 | } |
| 372 | |
| 373 | int rbt_str_getnode(rbt_t *rbt, const char *key, struct rbt_str_node **node) |
| 374 | { |
| 375 | int r; |
| 376 | register struct rbt_node *n; |
| 377 | register char *u_key, *n_key; |
| 378 | register int cmp; |
| 379 | |
| 380 | u_key = (char *)key; |
| 381 | r = -1; |
| 382 | rbt_rlock(rbt); |
| 383 | n = rbt_node_ptr(rbt->root); |
| 384 | |
| 385 | while (n != NULL) { |
| 386 | n_key = rbt_str_node(n)->key; |
| 387 | cmp = strcmp(u_key, n_key); |
| 388 | |
| 389 | if (cmp < 0) |
| 390 | n = rbt_node_ptr(n->_chld[RBT_NODE_SL]); |
| 391 | else if (cmp > 0) |
| 392 | n = rbt_node_ptr(n->_chld[RBT_NODE_SR]); |
| 393 | else { |
| 394 | r = 0; |
| 395 | *node = (struct rbt_str_node *)(n->_node); |
| 396 | break; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | rbt_runlock(rbt); |
| 401 | return (r); |
| 402 | } |
| 403 | |
| 404 | int rbt_str_get(rbt_t *rbt, const char *key, void **data) |
| 405 | { |
| 406 | int r; |
| 407 | struct rbt_str_node *n = NULL; |
| 408 | |
| 409 | r = rbt_str_getnode(rbt, key, &n); |
| 410 | |
| 411 | if (r == 0) |
| 412 | *data = n->data; |
| 413 | |
| 414 | return(r); |
| 415 | } |
| 416 | |
| 417 | int rbt_str_walk_preorder(rbt_t *rbt, int (*callback)(struct rbt_str_node *), rbt_walk_t flags) |
| 418 | { |
| 419 | errno = ENOSYS; |
| 420 | return (-1); |
| 421 | } |
| 422 | |
| 423 | int rbt_str_walk_inorder(rbt_t *rbt, int (*callback)(struct rbt_str_node *), rbt_walk_t flags) |
| 424 | { |
| 425 | return rbt_walk_inorder(rbt, (int(*)(void *))callback, flags); |
| 426 | } |
| 427 | |
| 428 | int rbt_str_walk_inorder2(rbt_t *rbt, int (*callback)(struct rbt_str_node *, void *), void *user, rbt_walk_t flags) |
| 429 | { |
| 430 | return rbt_walk_inorder2(rbt, (int(*)(void *, void *))callback, user, flags); |
| 431 | } |
| 432 | |
| 433 | int rbt_str_walk_postorder(rbt_t *rbt, int (*callback)(struct rbt_str_node *), rbt_walk_t flags) |
| 434 | { |
| 435 | errno = ENOSYS; |
| 436 | return (-1); |
| 437 | } |
| 438 | |
| 439 | int rbt_str_walk_levelorder(rbt_t *rbt, int (*callback)(struct rbt_str_node *), rbt_walk_t flags) |
| 440 | { |
| 441 | errno = ENOSYS; |
| 442 | return (-1); |
| 443 | } |
| 444 | |
| 445 | int rbt_str_walk(rbt_t *rbt, rbt_walk_t type, int (*callback)(struct rbt_str_node *)) |
| 446 | { |
| 447 | if (rbt == NULL || callback == NULL) { |
| 448 | errno = EINVAL; |
| 449 | return -1; |
| 450 | } |
| 451 | |
| 452 | switch (type & RBT_WALK_TYPEMASK) { |
| 453 | case RBT_WALK_PREORDER: return rbt_str_walk_preorder(rbt, callback, type); |
| 454 | case RBT_WALK_INORDER: return rbt_str_walk_inorder(rbt, callback, type); |
| 455 | case RBT_WALK_POSTORDER: return rbt_str_walk_postorder(rbt, callback, type); |
| 456 | case RBT_WALK_LEVELORDER: return rbt_str_walk_levelorder(rbt, callback, type); |
| 457 | } |
| 458 | |
| 459 | errno = EINVAL; |
| 460 | return (-1); |
| 461 | } |
| 462 | |
| 463 | size_t rbt_str_size(rbt_t *rbt) |
| 464 | { |
| 465 | return rbt_size(rbt); |
| 466 | } |