| 1 | /* |
| 2 | * Copyright 2011 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 | #ifdef HAVE_CONFIG_H |
| 23 | #include <config.h> |
| 24 | #endif |
| 25 | |
| 26 | #include <errno.h> |
| 27 | #include <stdlib.h> |
| 28 | #include "rbt_common.h" |
| 29 | #include "rbt_i64.h" |
| 30 | |
| 31 | static struct rbt_node *rbt_i64_node_alloc(void) |
| 32 | { |
| 33 | struct rbt_node *n = NULL; |
| 34 | #ifndef OS_WINDOWS |
| 35 | if (posix_memalign((void **)(void *)(&n), sizeof(void *), |
| 36 | sizeof (struct rbt_node) + sizeof (struct rbt_i64_node)) != 0) |
| 37 | { |
| 38 | abort (); |
| 39 | } |
| 40 | #else |
| 41 | // https://msdn.microsoft.com/en-us/library/8z34s9c6.aspx |
| 42 | n = _aligned_malloc(sizeof (struct rbt_node) + sizeof (struct rbt_i64_node), sizeof(void *)); |
| 43 | #endif |
| 44 | n->_chld[0] = NULL; |
| 45 | n->_chld[1] = NULL; |
| 46 | |
| 47 | return(n); |
| 48 | } |
| 49 | |
| 50 | static void rbt_i64_node_free(struct rbt_node *n) |
| 51 | { |
| 52 | if (n != NULL) |
| 53 | { |
| 54 | #ifndef OS_WINDOWS |
| 55 | free(rbt_node_ptr(n)); |
| 56 | #else |
| 57 | // using free for memory allocated through _aligned_malloc is illegal |
| 58 | // rbt_i64.c -> rbt_i64_node_alloc |
| 59 | // https://msdn.microsoft.com/en-us/library/8z34s9c6.aspx |
| 60 | _aligned_free(rbt_node_ptr(n)); |
| 61 | #endif |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | rbt_t *rbt_i64_new (void) |
| 66 | { |
| 67 | return rbt_new (RBT_I64KEY); |
| 68 | } |
| 69 | |
| 70 | void rbt_i64_free (rbt_t *rbt) |
| 71 | { |
| 72 | rbt_free(rbt, NULL); |
| 73 | } |
| 74 | |
| 75 | void rbt_i64_free_cb (rbt_t *rbt, void (*callback)(rbt_i64_node_t *)) |
| 76 | { |
| 77 | rbt_free(rbt, (void(*)(void *))callback); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Add new entry to the tree. If `coll' is not NULL and there is |
| 82 | * already an entry with the same key (i.e. a collision), then |
| 83 | * place the old data pointer at `*coll' and update it to the |
| 84 | * new data pointer. Otherwise, if `coll' is NULL and there is a |
| 85 | * collision, then an error is returned and no modification of |
| 86 | * the tree is made. |
| 87 | * |
| 88 | * @param rbt pointer to the tree |
| 89 | * @param key key |
| 90 | * @param data data pointer |
| 91 | * @param coll where to store the data pointer from an colliding entry |
| 92 | */ |
| 93 | int rbt_i64_add(rbt_t *rbt, int64_t key, void *data, void **coll) |
| 94 | { |
| 95 | struct rbt_node fake; |
| 96 | struct rbt_node *h[4]; |
| 97 | register uint8_t dvec; |
| 98 | register int64_t n_key, u_key; |
| 99 | |
| 100 | u_key = key; |
| 101 | rbt_wlock(rbt); |
| 102 | |
| 103 | /* |
| 104 | * Fake node |
| 105 | */ |
| 106 | fake._chld[RBT_NODE_SL] = NULL; |
| 107 | fake._chld[RBT_NODE_SR] = rbt->root; |
| 108 | rbt_node_setcolor(&fake, RBT_NODE_CB); |
| 109 | |
| 110 | /* |
| 111 | * Prepare node history stack & direction history vector |
| 112 | */ |
| 113 | h[3] = NULL; |
| 114 | h[2] = NULL; |
| 115 | h[1] = &fake; |
| 116 | h[0] = rbt->root; |
| 117 | dvec = RBT_NODE_SR; |
| 118 | |
| 119 | for (;;) { |
| 120 | if (rbt_node_ptr(h[0]) == NULL) { |
| 121 | /* |
| 122 | * Allocate new node aligned to sizeof(void *) bytes. |
| 123 | * On most systems, malloc already returns aligned |
| 124 | * memory but we want to ensure that its aligned using |
| 125 | * posix_memalign(3). |
| 126 | */ |
| 127 | h[0] = rbt_i64_node_alloc (); |
| 128 | |
| 129 | rbt_i64_node(h[0])->key = key; |
| 130 | rbt_i64_node(h[0])->data = data; |
| 131 | |
| 132 | /* |
| 133 | * Set the new node as the child of the last node we've |
| 134 | * visited. The last direction is the lowest bit of the |
| 135 | * direction history vector. |
| 136 | */ |
| 137 | rbt_node_setptr(rbt_node_ptr(h[1])->_chld[dvec & 1], h[0]); |
| 138 | rbt_node_setcolor(h[0], RBT_NODE_CR); |
| 139 | |
| 140 | /* |
| 141 | * Since we are inserting a new red node, we need to fix |
| 142 | * a red violation if the parent of the new node is red. |
| 143 | */ |
| 144 | if (rbt_node_getcolor(h[1]) == RBT_NODE_CR) { |
| 145 | rbt_redfix(h, dvec, rbt_node_ptr(h[3])->_chld[(dvec >> 2) & 1]); |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Update root node and color it black |
| 150 | */ |
| 151 | rbt->root = fake._chld[RBT_NODE_SR]; |
| 152 | rbt_node_setcolor(rbt->root, RBT_NODE_CB); |
| 153 | |
| 154 | /* |
| 155 | * Update node counter |
| 156 | */ |
| 157 | ++(rbt->size); |
| 158 | |
| 159 | break; |
| 160 | } else if (rbt_node_getcolor(rbt_node_ptr(h[0])->_chld[0]) == RBT_NODE_CR && |
| 161 | rbt_node_getcolor(rbt_node_ptr(h[0])->_chld[1]) == RBT_NODE_CR) |
| 162 | { |
| 163 | /* |
| 164 | * Color switch |
| 165 | */ |
| 166 | rbt_node_setcolor(h[0], RBT_NODE_CR); |
| 167 | rbt_node_setcolor(rbt_node_ptr(h[0])->_chld[0], RBT_NODE_CB); |
| 168 | rbt_node_setcolor(rbt_node_ptr(h[0])->_chld[1], RBT_NODE_CB); |
| 169 | |
| 170 | /* |
| 171 | * Fix red violation |
| 172 | */ |
| 173 | if (rbt_node_getcolor(h[1]) == RBT_NODE_CR) { |
| 174 | rbt_redfix(h, dvec, rbt_node_ptr(h[3])->_chld[(dvec >> 2) & 1]); |
| 175 | } |
| 176 | } else if (rbt_node_getcolor(h[0]) == RBT_NODE_CR && |
| 177 | rbt_node_getcolor(h[1]) == RBT_NODE_CR) |
| 178 | { |
| 179 | /* |
| 180 | * Fix red violation |
| 181 | */ |
| 182 | rbt_redfix(h, dvec, rbt_node_ptr(h[3])->_chld[(dvec >> 2)&1]); |
| 183 | } |
| 184 | |
| 185 | n_key = rbt_i64_node(h[0])->key; |
| 186 | |
| 187 | if (u_key < n_key) { |
| 188 | rbt_hpush4(h, rbt_node_ptr(h[0])->_chld[RBT_NODE_SL]); |
| 189 | dvec = (dvec << 1) | RBT_NODE_SL; |
| 190 | } else if (u_key > n_key) { |
| 191 | rbt_hpush4(h, rbt_node_ptr(h[0])->_chld[RBT_NODE_SR]); |
| 192 | dvec = (dvec << 1) | RBT_NODE_SR; |
| 193 | } else { |
| 194 | int r; |
| 195 | |
| 196 | if (coll != NULL) { |
| 197 | *coll = rbt_i64_node(h[0])->data; |
| 198 | rbt_i64_node(h[0])->data = data; |
| 199 | r = 0; |
| 200 | } else |
| 201 | r = 1; |
| 202 | |
| 203 | /* |
| 204 | * Update root node and color it black |
| 205 | */ |
| 206 | rbt->root = fake._chld[RBT_NODE_SR]; |
| 207 | rbt_node_setcolor(rbt->root, RBT_NODE_CB); |
| 208 | |
| 209 | rbt_wunlock(rbt); |
| 210 | return (r); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | rbt_wunlock(rbt); |
| 215 | return (0); |
| 216 | } |
| 217 | |
| 218 | void *rbt_i64_rep(rbt_t *rbt, int64_t key, void *data) |
| 219 | { |
| 220 | return (NULL); |
| 221 | } |
| 222 | |
| 223 | int rbt_i64_del(rbt_t *rbt, int64_t key, void **n) |
| 224 | { |
| 225 | struct rbt_node fake, *save; |
| 226 | struct rbt_node *h[4]; |
| 227 | register uint8_t dvec; |
| 228 | register int64_t n_key, u_key; |
| 229 | |
| 230 | save = NULL; |
| 231 | u_key = key; |
| 232 | |
| 233 | rbt_wlock(rbt); |
| 234 | |
| 235 | if (rbt->size == 0) { |
| 236 | rbt_wunlock(rbt); |
| 237 | return (1); |
| 238 | } |
| 239 | |
| 240 | if (rbt_node_ptr(rbt->root) == NULL) { |
| 241 | return -1; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * Fake node |
| 246 | */ |
| 247 | fake._chld[RBT_NODE_SL] = NULL; |
| 248 | fake._chld[RBT_NODE_SR] = rbt->root; |
| 249 | rbt_node_setcolor(&fake, RBT_NODE_CB); |
| 250 | |
| 251 | /* |
| 252 | * Prepare node history stack & direction history vector |
| 253 | */ |
| 254 | h[2] = NULL; |
| 255 | h[1] = NULL; |
| 256 | h[0] = &fake; |
| 257 | dvec = RBT_NODE_SR; |
| 258 | |
| 259 | for (;;) { |
| 260 | if (rbt_node_ptr(rbt_node_ptr(h[0])->_chld[dvec & 1]) == NULL) |
| 261 | break; |
| 262 | |
| 263 | rbt_hpush4(h, rbt_node_ptr(h[0])->_chld[dvec & 1]); |
| 264 | n_key = rbt_i64_node(h[0])->key; |
| 265 | |
| 266 | if (u_key < n_key) { |
| 267 | dvec = (dvec << 1) | RBT_NODE_SL; |
| 268 | } else if (u_key > n_key) { |
| 269 | dvec = (dvec << 1) | RBT_NODE_SR; |
| 270 | } else { |
| 271 | save = rbt_node_ptr(h[0]); |
| 272 | dvec = (dvec << 1) | RBT_NODE_SL; |
| 273 | } |
| 274 | |
| 275 | if (rbt_node_getcolor(h[0]) == RBT_NODE_CB && |
| 276 | rbt_node_getcolor(rbt_node_ptr(h[0])->_chld[dvec & 1]) == RBT_NODE_CB) |
| 277 | { |
| 278 | if (rbt_node_getcolor(rbt_node_ptr(h[0])->_chld[!(dvec & 1)]) == RBT_NODE_CR) { |
| 279 | switch(dvec & 1) { |
| 280 | case RBT_NODE_SR: |
| 281 | rbt_node_setptr(rbt_node_ptr(h[1])->_chld[(dvec >> 1) & 1], |
| 282 | rbt_node_rotate_R(h[0])); |
| 283 | break; |
| 284 | case RBT_NODE_SL: |
| 285 | rbt_node_setptr(rbt_node_ptr(h[1])->_chld[(dvec >> 1) & 1], |
| 286 | rbt_node_rotate_L(h[0])); |
| 287 | break; |
| 288 | } |
| 289 | |
| 290 | h[1] = rbt_node_ptr(h[1])->_chld[(dvec >> 1) & 1]; |
| 291 | } else { |
| 292 | register struct rbt_node *s; |
| 293 | |
| 294 | s = rbt_node_ptr(rbt_node_ptr(h[1])->_chld[!((dvec >> 1) & 1)]); |
| 295 | |
| 296 | if (s != NULL) { |
| 297 | if (rbt_node_getcolor(s->_chld[0]) == RBT_NODE_CB && |
| 298 | rbt_node_getcolor(s->_chld[1]) == RBT_NODE_CB) |
| 299 | { |
| 300 | rbt_node_setcolor(h[0], RBT_NODE_CR); |
| 301 | rbt_node_setcolor(h[1], RBT_NODE_CB); |
| 302 | rbt_node_setcolor(s, RBT_NODE_CR); |
| 303 | } else { |
| 304 | register uint8_t d = rbt_node_ptr(rbt_node_ptr(h[2])->_chld[RBT_NODE_SR]) == rbt_node_ptr(h[1]); |
| 305 | |
| 306 | 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_RL(h[1])); |
| 311 | break; |
| 312 | case RBT_NODE_SL: |
| 313 | rbt_node_setptr(rbt_node_ptr(h[2])->_chld[d], |
| 314 | rbt_node_rotate_LR(h[1])); |
| 315 | break; |
| 316 | } |
| 317 | } else if (rbt_node_getcolor(s->_chld[!((dvec >> 1) & 1)]) == RBT_NODE_CR) { |
| 318 | switch((dvec >> 1) & 1) { |
| 319 | case RBT_NODE_SR: |
| 320 | rbt_node_setptr(rbt_node_ptr(h[2])->_chld[d], |
| 321 | rbt_node_rotate_R(h[1])); |
| 322 | break; |
| 323 | case RBT_NODE_SL: |
| 324 | rbt_node_setptr(rbt_node_ptr(h[2])->_chld[d], |
| 325 | rbt_node_rotate_L(h[1])); |
| 326 | break; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | s = rbt_node_ptr(rbt_node_ptr(h[2])->_chld[d]); |
| 331 | |
| 332 | rbt_node_setcolor(h[0], RBT_NODE_CR); |
| 333 | rbt_node_setcolor(s, RBT_NODE_CR); |
| 334 | rbt_node_setcolor(s->_chld[0], RBT_NODE_CB); |
| 335 | rbt_node_setcolor(s->_chld[1], RBT_NODE_CB); |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | if (save != NULL) { |
| 343 | h[0] = rbt_node_ptr(h[0]); |
| 344 | /* |
| 345 | * The node color of the node that will be delete is always |
| 346 | * red in case the node is not the root node. |
| 347 | */ |
| 348 | if (rbt_node_ptr(fake._chld[RBT_NODE_SR]) != h[0] && rbt_node_getcolor(h[0]) != RBT_NODE_CR) { |
| 349 | return -1; |
| 350 | } |
| 351 | if (n != NULL) |
| 352 | *n = rbt_i64_node(save)->data; |
| 353 | |
| 354 | rbt_i64_node(save)->data = rbt_i64_node(h[0])->data; |
| 355 | rbt_i64_node(save)->key = rbt_i64_node(h[0])->key; |
| 356 | |
| 357 | dvec = rbt_node_ptr(rbt_node_ptr(h[1])->_chld[1]) == h[0]; |
| 358 | |
| 359 | if (rbt_node_ptr(h[0]->_chld[RBT_NODE_SR]) == NULL) { |
| 360 | rbt_node_setptr(rbt_node_ptr(h[1])->_chld[dvec], h[0]->_chld[RBT_NODE_SL]); |
| 361 | } else { |
| 362 | rbt_node_setptr(rbt_node_ptr(h[1])->_chld[dvec], h[0]->_chld[RBT_NODE_SR]); |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Free memory used by the deleted node and decrement |
| 367 | * the node counter. |
| 368 | */ |
| 369 | rbt_i64_node_free(h[0]); |
| 370 | --(rbt->size); |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * Update root node and color it black and unlock the tree. |
| 375 | */ |
| 376 | rbt->root = fake._chld[RBT_NODE_SR]; |
| 377 | rbt_node_setcolor(rbt->root, RBT_NODE_CB); |
| 378 | rbt_wunlock(rbt); |
| 379 | |
| 380 | return (save == NULL ? 1 : 0); |
| 381 | } |
| 382 | |
| 383 | int rbt_i64_get(rbt_t *rbt, int64_t key, void **data) |
| 384 | { |
| 385 | int r; |
| 386 | register struct rbt_node *n; |
| 387 | register int64_t u_key, n_key; |
| 388 | |
| 389 | u_key = key; |
| 390 | r = -1; |
| 391 | rbt_rlock(rbt); |
| 392 | n = rbt_node_ptr(rbt->root); |
| 393 | |
| 394 | while (n != NULL) { |
| 395 | n_key = rbt_i64_node(n)->key; |
| 396 | |
| 397 | if (u_key < n_key) |
| 398 | n = rbt_node_ptr(n->_chld[RBT_NODE_SL]); |
| 399 | else if (u_key > n_key) |
| 400 | n = rbt_node_ptr(n->_chld[RBT_NODE_SR]); |
| 401 | else { |
| 402 | r = 0; |
| 403 | *data = rbt_i64_node(n)->data; |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | rbt_runlock(rbt); |
| 409 | return (r); |
| 410 | } |
| 411 | |
| 412 | int rbt_i64_walk_preorder(rbt_t *rbt, int (*callback)(rbt_i64_node_t *), rbt_walk_t flags) |
| 413 | { |
| 414 | errno = ENOSYS; |
| 415 | return (-1); |
| 416 | } |
| 417 | |
| 418 | int rbt_i64_walk_inorder(rbt_t *rbt, int (*callback)(rbt_i64_node_t *), rbt_walk_t flags) |
| 419 | { |
| 420 | return rbt_walk_inorder(rbt, (int(*)(void *))callback, flags); |
| 421 | } |
| 422 | |
| 423 | int rbt_i64_walk_postorder(rbt_t *rbt, int (*callback)(rbt_i64_node_t *), rbt_walk_t flags) |
| 424 | { |
| 425 | errno = ENOSYS; |
| 426 | return (-1); |
| 427 | } |
| 428 | |
| 429 | int rbt_i64_walk_levelorder(rbt_t *rbt, int (*callback)(rbt_i64_node_t *), rbt_walk_t flags) |
| 430 | { |
| 431 | errno = ENOSYS; |
| 432 | return (-1); |
| 433 | } |
| 434 | |
| 435 | int rbt_i64_walk(rbt_t *rbt, rbt_walk_t type, int (*callback)(rbt_i64_node_t *)) |
| 436 | { |
| 437 | if (rbt == NULL || callback == NULL) { |
| 438 | errno = EINVAL; |
| 439 | return -1; |
| 440 | } |
| 441 | |
| 442 | switch (type & RBT_WALK_TYPEMASK) { |
| 443 | case RBT_WALK_PREORDER: return rbt_i64_walk_preorder(rbt, callback, type); |
| 444 | case RBT_WALK_INORDER: return rbt_i64_walk_inorder(rbt, callback, type); |
| 445 | case RBT_WALK_POSTORDER: return rbt_i64_walk_postorder(rbt, callback, type); |
| 446 | case RBT_WALK_LEVELORDER: return rbt_i64_walk_levelorder(rbt, callback, type); |
| 447 | } |
| 448 | |
| 449 | errno = EINVAL; |
| 450 | return (-1); |
| 451 | } |
| 452 | |
| 453 | size_t rbt_i64_size(rbt_t *rbt) |
| 454 | { |
| 455 | return rbt_size(rbt); |
| 456 | } |