range_tree.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /*
  2. * Copyright (c) 2016, Campbell Barton.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "Apache License")
  5. * with the following modification; you may not use this file except in
  6. * compliance with the Apache License and the following modification to it:
  7. * Section 6. Trademarks. is deleted and replaced with:
  8. *
  9. * 6. Trademarks. This License does not grant permission to use the trade
  10. * names, trademarks, service marks, or product names of the Licensor
  11. * and its affiliates, except as required to comply with Section 4(c) of
  12. * the License and to reproduce the content of the NOTICE file.
  13. *
  14. * You may obtain a copy of the Apache License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the Apache License with the above modification is
  20. * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  21. * KIND, either express or implied. See the Apache License for the specific
  22. * language governing permissions and limitations under the Apache License.
  23. */
  24. #include <stdlib.h>
  25. #include <stdbool.h>
  26. #include <string.h>
  27. #include <assert.h>
  28. #include "range_tree.h"
  29. typedef unsigned int uint;
  30. /* Use binary-tree for lookups, else fallback to full search */
  31. #define USE_BTREE
  32. /* Use memory pool for nodes, else do individual allocations */
  33. #define USE_TPOOL
  34. /* Node representing a range in the RangeTreeUInt. */
  35. typedef struct Node {
  36. struct Node *next, *prev;
  37. /* range (inclusive) */
  38. uint min, max;
  39. #ifdef USE_BTREE
  40. /* Left leaning red-black tree, for reference implementation see:
  41. * https://gitlab.com/ideasman42/btree-mini-py */
  42. struct Node *left, *right;
  43. /* RED/BLACK */
  44. bool color;
  45. #endif
  46. } Node;
  47. #ifdef USE_TPOOL
  48. /* rt_pool_* pool allocator */
  49. #define TPOOL_IMPL_PREFIX rt_node
  50. #define TPOOL_ALLOC_TYPE Node
  51. #define TPOOL_STRUCT ElemPool_Node
  52. #include "generic_alloc_impl.h"
  53. #undef TPOOL_IMPL_PREFIX
  54. #undef TPOOL_ALLOC_TYPE
  55. #undef TPOOL_STRUCT
  56. #endif /* USE_TPOOL */
  57. typedef struct LinkedList {
  58. Node *first, *last;
  59. } LinkedList;
  60. typedef struct RangeTreeUInt {
  61. uint range[2];
  62. LinkedList list;
  63. #ifdef USE_BTREE
  64. Node *root;
  65. #endif
  66. #ifdef USE_TPOOL
  67. struct ElemPool_Node epool;
  68. #endif
  69. } RangeTreeUInt;
  70. /* ------------------------------------------------------------------------- */
  71. /* List API */
  72. static void list_push_front(LinkedList *list, Node *node)
  73. {
  74. if (list->first != NULL) {
  75. node->next = list->first;
  76. node->next->prev = node;
  77. node->prev = NULL;
  78. }
  79. else {
  80. list->last = node;
  81. }
  82. list->first = node;
  83. }
  84. static void list_push_back(LinkedList *list, Node *node)
  85. {
  86. if (list->first != NULL) {
  87. node->prev = list->last;
  88. node->prev->next = node;
  89. node->next = NULL;
  90. }
  91. else {
  92. list->first = node;
  93. }
  94. list->last = node;
  95. }
  96. static void list_push_after(LinkedList *list, Node *node_prev, Node *node_new)
  97. {
  98. /* node_new before node_next */
  99. /* empty list */
  100. if (list->first == NULL) {
  101. list->first = node_new;
  102. list->last = node_new;
  103. return;
  104. }
  105. /* insert at head of list */
  106. if (node_prev == NULL) {
  107. node_new->prev = NULL;
  108. node_new->next = list->first;
  109. node_new->next->prev = node_new;
  110. list->first = node_new;
  111. return;
  112. }
  113. /* at end of list */
  114. if (list->last == node_prev) {
  115. list->last = node_new;
  116. }
  117. node_new->next = node_prev->next;
  118. node_new->prev = node_prev;
  119. node_prev->next = node_new;
  120. if (node_new->next) {
  121. node_new->next->prev = node_new;
  122. }
  123. }
  124. static void list_push_before(LinkedList *list, Node *node_next, Node *node_new)
  125. {
  126. /* node_new before node_next */
  127. /* empty list */
  128. if (list->first == NULL) {
  129. list->first = node_new;
  130. list->last = node_new;
  131. return;
  132. }
  133. /* insert at end of list */
  134. if (node_next == NULL) {
  135. node_new->prev = list->last;
  136. node_new->next = NULL;
  137. list->last->next = node_new;
  138. list->last = node_new;
  139. return;
  140. }
  141. /* at beginning of list */
  142. if (list->first == node_next) {
  143. list->first = node_new;
  144. }
  145. node_new->next = node_next;
  146. node_new->prev = node_next->prev;
  147. node_next->prev = node_new;
  148. if (node_new->prev) {
  149. node_new->prev->next = node_new;
  150. }
  151. }
  152. static void list_remove(LinkedList *list, Node *node)
  153. {
  154. if (node->next != NULL) {
  155. node->next->prev = node->prev;
  156. }
  157. if (node->prev != NULL) {
  158. node->prev->next = node->next;
  159. }
  160. if (list->last == node) {
  161. list->last = node->prev;
  162. }
  163. if (list->first == node) {
  164. list->first = node->next;
  165. }
  166. }
  167. static void list_clear(LinkedList *list)
  168. {
  169. list->first = NULL;
  170. list->last = NULL;
  171. }
  172. /* end list API */
  173. /* forward declarations */
  174. static void rt_node_free(RangeTreeUInt *rt, Node *node);
  175. #ifdef USE_BTREE
  176. #ifdef DEBUG
  177. static bool rb_is_balanced_root(const Node *root);
  178. #endif
  179. /* ------------------------------------------------------------------------- */
  180. /* Internal BTree API
  181. *
  182. * Left-leaning red-black tree.
  183. */
  184. /* use minimum, could use max too since nodes never overlap */
  185. #define KEY(n) ((n)->min)
  186. enum {
  187. RED = 0,
  188. BLACK = 1,
  189. };
  190. static bool is_red(const Node *node)
  191. {
  192. return (node && (node->color == RED));
  193. }
  194. static int key_cmp(uint key1, uint key2)
  195. {
  196. return (key1 == key2) ? 0 : ((key1 < key2) ? -1 : 1);
  197. }
  198. /* removed from the tree */
  199. static void rb_node_invalidate(Node *node)
  200. {
  201. #ifdef DEBUG
  202. node->left = NULL;
  203. node->right = NULL;
  204. node->color = false;
  205. #else
  206. (void)node;
  207. #endif
  208. }
  209. static void rb_flip_color(Node *node)
  210. {
  211. node->color ^= 1;
  212. node->left->color ^= 1;
  213. node->right->color ^= 1;
  214. }
  215. static Node *rb_rotate_left(Node *left)
  216. {
  217. /* Make a right-leaning 3-node lean to the left. */
  218. Node *right = left->right;
  219. left->right = right->left;
  220. right->left = left;
  221. right->color = left->color;
  222. left->color = RED;
  223. return right;
  224. }
  225. static Node *rb_rotate_right(Node *right)
  226. {
  227. /* Make a left-leaning 3-node lean to the right. */
  228. Node *left = right->left;
  229. right->left = left->right;
  230. left->right = right;
  231. left->color = right->color;
  232. right->color = RED;
  233. return left;
  234. }
  235. /* Fixup colors when insert happened */
  236. static Node *rb_fixup_insert(Node *node)
  237. {
  238. if (is_red(node->right) && !is_red(node->left)) {
  239. node = rb_rotate_left(node);
  240. }
  241. if (is_red(node->left) && is_red(node->left->left)) {
  242. node = rb_rotate_right(node);
  243. }
  244. if (is_red(node->left) && is_red(node->right)) {
  245. rb_flip_color(node);
  246. }
  247. return node;
  248. }
  249. static Node *rb_insert_recursive(Node *node, Node *node_to_insert)
  250. {
  251. if (node == NULL) {
  252. return node_to_insert;
  253. }
  254. const int cmp = key_cmp(KEY(node_to_insert), KEY(node));
  255. if (cmp == 0) {
  256. /* caller ensures no collisions */
  257. assert(0);
  258. }
  259. else if (cmp == -1) {
  260. node->left = rb_insert_recursive(node->left, node_to_insert);
  261. }
  262. else {
  263. node->right = rb_insert_recursive(node->right, node_to_insert);
  264. }
  265. return rb_fixup_insert(node);
  266. }
  267. static Node *rb_insert_root(Node *root, Node *node_to_insert)
  268. {
  269. root = rb_insert_recursive(root, node_to_insert);
  270. root->color = BLACK;
  271. return root;
  272. }
  273. static Node *rb_move_red_to_left(Node *node)
  274. {
  275. /* Assuming that h is red and both h->left and h->left->left
  276. * are black, make h->left or one of its children red.
  277. */
  278. rb_flip_color(node);
  279. if (node->right && is_red(node->right->left)) {
  280. node->right = rb_rotate_right(node->right);
  281. node = rb_rotate_left(node);
  282. rb_flip_color(node);
  283. }
  284. return node;
  285. }
  286. static Node *rb_move_red_to_right(Node *node)
  287. {
  288. /* Assuming that h is red and both h->right and h->right->left
  289. * are black, make h->right or one of its children red.
  290. */
  291. rb_flip_color(node);
  292. if (node->left && is_red(node->left->left)) {
  293. node = rb_rotate_right(node);
  294. rb_flip_color(node);
  295. }
  296. return node;
  297. }
  298. /* Fixup colors when remove happened */
  299. static Node *rb_fixup_remove(Node *node)
  300. {
  301. if (is_red(node->right)) {
  302. node = rb_rotate_left(node);
  303. }
  304. if (is_red(node->left) && is_red(node->left->left)) {
  305. node = rb_rotate_right(node);
  306. }
  307. if (is_red(node->left) && is_red(node->right)) {
  308. rb_flip_color(node);
  309. }
  310. return node;
  311. }
  312. static Node *rb_pop_min_recursive(Node *node, Node **r_node_pop)
  313. {
  314. if (node == NULL) {
  315. return NULL;
  316. }
  317. if (node->left == NULL) {
  318. rb_node_invalidate(node);
  319. *r_node_pop = node;
  320. return NULL;
  321. }
  322. if ((!is_red(node->left)) && (!is_red(node->left->left))) {
  323. node = rb_move_red_to_left(node);
  324. }
  325. node->left = rb_pop_min_recursive(node->left, r_node_pop);
  326. return rb_fixup_remove(node);
  327. }
  328. static Node *rb_remove_recursive(Node *node, const Node *node_to_remove)
  329. {
  330. if (node == NULL) {
  331. return NULL;
  332. }
  333. if (key_cmp(KEY(node_to_remove), KEY(node)) == -1) {
  334. if (node->left != NULL) {
  335. if ((!is_red(node->left)) && (!is_red(node->left->left))) {
  336. node = rb_move_red_to_left(node);
  337. }
  338. }
  339. node->left = rb_remove_recursive(node->left, node_to_remove);
  340. }
  341. else {
  342. if (is_red(node->left)) {
  343. node = rb_rotate_right(node);
  344. }
  345. if ((node == node_to_remove) && (node->right == NULL)) {
  346. rb_node_invalidate(node);
  347. return NULL;
  348. }
  349. assert(node->right != NULL);
  350. if ((!is_red(node->right)) && (!is_red(node->right->left))) {
  351. node = rb_move_red_to_right(node);
  352. }
  353. if (node == node_to_remove) {
  354. /* minor improvement over original method:
  355. * no need to double lookup min */
  356. Node *node_free; /* will always be set */
  357. node->right = rb_pop_min_recursive(node->right, &node_free);
  358. node_free->left = node->left;
  359. node_free->right = node->right;
  360. node_free->color = node->color;
  361. rb_node_invalidate(node);
  362. node = node_free;
  363. }
  364. else {
  365. node->right = rb_remove_recursive(node->right, node_to_remove);
  366. }
  367. }
  368. return rb_fixup_remove(node);
  369. }
  370. static Node *rb_btree_remove(Node *root, const Node *node_to_remove)
  371. {
  372. root = rb_remove_recursive(root, node_to_remove);
  373. if (root != NULL) {
  374. root->color = BLACK;
  375. }
  376. return root;
  377. }
  378. /*
  379. * Returns the node closest to and including 'key',
  380. * excluding anything below.
  381. */
  382. static Node *rb_get_or_upper_recursive(Node *n, const uint key)
  383. {
  384. if (n == NULL) {
  385. return NULL;
  386. }
  387. const int cmp_upper = key_cmp(KEY(n), key);
  388. if (cmp_upper == 0) {
  389. return n; // exact match
  390. }
  391. else if (cmp_upper == 1) {
  392. assert(KEY(n) >= key);
  393. Node *n_test = rb_get_or_upper_recursive(n->left, key);
  394. return n_test ? n_test : n;
  395. }
  396. else { // cmp_upper == -1
  397. return rb_get_or_upper_recursive(n->right, key);
  398. }
  399. }
  400. /*
  401. * Returns the node closest to and including 'key',
  402. * excluding anything above.
  403. */
  404. static Node *rb_get_or_lower_recursive(Node *n, const uint key)
  405. {
  406. if (n == NULL) {
  407. return NULL;
  408. }
  409. const int cmp_lower = key_cmp(KEY(n), key);
  410. if (cmp_lower == 0) {
  411. return n; // exact match
  412. }
  413. else if (cmp_lower == -1) {
  414. assert(KEY(n) <= key);
  415. Node *n_test = rb_get_or_lower_recursive(n->right, key);
  416. return n_test ? n_test : n;
  417. }
  418. else { // cmp_lower == 1
  419. return rb_get_or_lower_recursive(n->left, key);
  420. }
  421. }
  422. #ifdef DEBUG
  423. static bool rb_is_balanced_recursive(const Node *node, int black)
  424. {
  425. // Does every path from the root to a leaf have the given number
  426. // of black links?
  427. if (node == NULL) {
  428. return black == 0;
  429. }
  430. if (!is_red(node)) {
  431. black--;
  432. }
  433. return rb_is_balanced_recursive(node->left, black) &&
  434. rb_is_balanced_recursive(node->right, black);
  435. }
  436. static bool rb_is_balanced_root(const Node *root)
  437. {
  438. // Do all paths from root to leaf have same number of black edges?
  439. int black = 0; // number of black links on path from root to min
  440. const Node *node = root;
  441. while (node != NULL) {
  442. if (!is_red(node)) {
  443. black++;
  444. }
  445. node = node->left;
  446. }
  447. return rb_is_balanced_recursive(root, black);
  448. }
  449. #endif // DEBUG
  450. /* End BTree API */
  451. #endif // USE_BTREE
  452. /* ------------------------------------------------------------------------- */
  453. /* Internal RangeTreeUInt API */
  454. #ifdef _WIN32
  455. #define inline __inline
  456. #endif
  457. static inline Node *rt_node_alloc(RangeTreeUInt *rt)
  458. {
  459. #ifdef USE_TPOOL
  460. return rt_node_pool_elem_alloc(&rt->epool);
  461. #else
  462. (void)rt;
  463. return malloc(sizeof(Node));
  464. #endif
  465. }
  466. static Node *rt_node_new(RangeTreeUInt *rt, uint min, uint max)
  467. {
  468. Node *node = rt_node_alloc(rt);
  469. assert(min <= max);
  470. node->prev = NULL;
  471. node->next = NULL;
  472. node->min = min;
  473. node->max = max;
  474. #ifdef USE_BTREE
  475. node->left = NULL;
  476. node->right = NULL;
  477. #endif
  478. return node;
  479. }
  480. static void rt_node_free(RangeTreeUInt *rt, Node *node)
  481. {
  482. #ifdef USE_TPOOL
  483. rt_node_pool_elem_free(&rt->epool, node);
  484. #else
  485. (void)rt;
  486. free(node);
  487. #endif
  488. }
  489. #ifdef USE_BTREE
  490. static void rt_btree_insert(RangeTreeUInt *rt, Node *node)
  491. {
  492. node->color = RED;
  493. node->left = NULL;
  494. node->right = NULL;
  495. rt->root = rb_insert_root(rt->root, node);
  496. }
  497. #endif
  498. static void rt_node_add_back(RangeTreeUInt *rt, Node *node)
  499. {
  500. list_push_back(&rt->list, node);
  501. #ifdef USE_BTREE
  502. rt_btree_insert(rt, node);
  503. #endif
  504. }
  505. static void rt_node_add_front(RangeTreeUInt *rt, Node *node)
  506. {
  507. list_push_front(&rt->list, node);
  508. #ifdef USE_BTREE
  509. rt_btree_insert(rt, node);
  510. #endif
  511. }
  512. static void rt_node_add_before(RangeTreeUInt *rt, Node *node_next, Node *node)
  513. {
  514. list_push_before(&rt->list, node_next, node);
  515. #ifdef USE_BTREE
  516. rt_btree_insert(rt, node);
  517. #endif
  518. }
  519. static void rt_node_add_after(RangeTreeUInt *rt, Node *node_prev, Node *node)
  520. {
  521. list_push_after(&rt->list, node_prev, node);
  522. #ifdef USE_BTREE
  523. rt_btree_insert(rt, node);
  524. #endif
  525. }
  526. static void rt_node_remove(RangeTreeUInt *rt, Node *node)
  527. {
  528. list_remove(&rt->list, node);
  529. #ifdef USE_BTREE
  530. rt->root = rb_btree_remove(rt->root, node);
  531. #endif
  532. rt_node_free(rt, node);
  533. }
  534. static Node *rt_find_node_from_value(RangeTreeUInt *rt, const uint value)
  535. {
  536. #ifdef USE_BTREE
  537. Node *node = rb_get_or_lower_recursive(rt->root, value);
  538. if (node != NULL) {
  539. if ((value >= node->min) && (value <= node->max)) {
  540. return node;
  541. }
  542. }
  543. return NULL;
  544. #else
  545. for (Node *node = rt->list.first; node; node = node->next) {
  546. if ((value >= node->min) && (value <= node->max)) {
  547. return node;
  548. }
  549. }
  550. return NULL;
  551. #endif // USE_BTREE
  552. }
  553. static void rt_find_node_pair_around_value(RangeTreeUInt *rt, const uint value,
  554. Node **r_node_prev, Node **r_node_next)
  555. {
  556. if (value < rt->list.first->min) {
  557. *r_node_prev = NULL;
  558. *r_node_next = rt->list.first;
  559. return;
  560. }
  561. else if (value > rt->list.last->max) {
  562. *r_node_prev = rt->list.last;
  563. *r_node_next = NULL;
  564. return;
  565. }
  566. else {
  567. #ifdef USE_BTREE
  568. Node *node_next = rb_get_or_upper_recursive(rt->root, value);
  569. if (node_next != NULL) {
  570. Node *node_prev = node_next->prev;
  571. if ((node_prev->max < value) && (value < node_next->min)) {
  572. *r_node_prev = node_prev;
  573. *r_node_next = node_next;
  574. return;
  575. }
  576. }
  577. #else
  578. Node *node_prev = rt->list.first;
  579. Node *node_next;
  580. while ((node_next = node_prev->next)) {
  581. if ((node_prev->max < value) && (value < node_next->min)) {
  582. *r_node_prev = node_prev;
  583. *r_node_next = node_next;
  584. return;
  585. }
  586. node_prev = node_next;
  587. }
  588. #endif // USE_BTREE
  589. }
  590. *r_node_prev = NULL;
  591. *r_node_next = NULL;
  592. }
  593. /* ------------------------------------------------------------------------- */
  594. /* Public API */
  595. static RangeTreeUInt *rt_create_empty(uint min, uint max)
  596. {
  597. RangeTreeUInt *rt = malloc(sizeof(*rt));
  598. rt->range[0] = min;
  599. rt->range[1] = max;
  600. list_clear(&rt->list);
  601. #ifdef USE_BTREE
  602. rt->root = NULL;
  603. #endif
  604. #ifdef USE_TPOOL
  605. rt_node_pool_create(&rt->epool, 512);
  606. #endif
  607. return rt;
  608. }
  609. RangeTreeUInt *range_tree_uint_alloc(uint min, uint max)
  610. {
  611. RangeTreeUInt *rt = rt_create_empty(min, max);
  612. Node *node = rt_node_new(rt, min, max);
  613. rt_node_add_front(rt, node);
  614. return rt;
  615. }
  616. void range_tree_uint_free(RangeTreeUInt *rt)
  617. {
  618. #ifdef DEBUG
  619. #ifdef USE_BTREE
  620. assert(rb_is_balanced_root(rt->root));
  621. #endif
  622. #endif
  623. #ifdef USE_TPOOL
  624. rt_node_pool_destroy(&rt->epool);
  625. #else
  626. for (Node *node = rt->list.first, *node_next; node; node = node_next) {
  627. node_next = node->next;
  628. rt_node_free(rt, node);
  629. }
  630. #endif
  631. free(rt);
  632. }
  633. #ifdef USE_BTREE
  634. static Node *rt_copy_recursive(RangeTreeUInt *rt_dst, const Node *node_src)
  635. {
  636. if (node_src == NULL) {
  637. return NULL;
  638. }
  639. Node *node_dst = rt_node_alloc(rt_dst);
  640. *node_dst = *node_src;
  641. node_dst->left = rt_copy_recursive(rt_dst, node_dst->left);
  642. list_push_back(&rt_dst->list, node_dst);
  643. node_dst->right = rt_copy_recursive(rt_dst, node_dst->right);
  644. return node_dst;
  645. }
  646. #endif // USE_BTREE
  647. RangeTreeUInt *range_tree_uint_copy(const RangeTreeUInt *rt_src)
  648. {
  649. RangeTreeUInt *rt_dst = rt_create_empty(rt_src->range[0], rt_src->range[1]);
  650. #ifdef USE_BTREE
  651. rt_dst->root = rt_copy_recursive(rt_dst, rt_src->root);
  652. #else
  653. for (Node *node_src = rt_src->list.first; node_src; node_src = node_src->next) {
  654. Node *node_dst = rt_node_alloc(rt_dst);
  655. *node_dst = *node_src;
  656. list_push_back(&rt_dst->list, node_dst);
  657. }
  658. #endif
  659. return rt_dst;
  660. }
  661. /**
  662. * Return true if the tree has the value (not taken).
  663. */
  664. bool range_tree_uint_has(RangeTreeUInt *rt, const uint value)
  665. {
  666. assert(value >= rt->range[0] && value <= rt->range[1]);
  667. Node *node = rt_find_node_from_value(rt, value);
  668. return (node != NULL);
  669. }
  670. static void range_tree_uint_take_impl(RangeTreeUInt *rt, const uint value, Node *node)
  671. {
  672. assert(node == rt_find_node_from_value(rt, value));
  673. if (node->min == value) {
  674. if (node->max != value) {
  675. node->min += 1;
  676. }
  677. else {
  678. assert(node->min == node->max);
  679. rt_node_remove(rt, node);
  680. }
  681. }
  682. else if (node->max == value) {
  683. node->max -= 1;
  684. }
  685. else {
  686. Node *node_next = rt_node_new(rt, value + 1, node->max);
  687. node->max = value - 1;
  688. rt_node_add_after(rt, node, node_next);
  689. }
  690. }
  691. void range_tree_uint_take(RangeTreeUInt *rt, const uint value)
  692. {
  693. Node *node = rt_find_node_from_value(rt, value);
  694. assert(node != NULL);
  695. range_tree_uint_take_impl(rt, value, node);
  696. }
  697. bool range_tree_uint_retake(RangeTreeUInt *rt, const uint value)
  698. {
  699. Node *node = rt_find_node_from_value(rt, value);
  700. if (node != NULL) {
  701. range_tree_uint_take_impl(rt, value, node);
  702. return true;
  703. }
  704. else {
  705. return false;
  706. }
  707. }
  708. uint range_tree_uint_take_any(RangeTreeUInt *rt)
  709. {
  710. Node *node = rt->list.first;
  711. uint value = node->min;
  712. if (value == node->max) {
  713. rt_node_remove(rt, node);
  714. }
  715. else {
  716. node->min += 1;
  717. }
  718. return value;
  719. }
  720. void range_tree_uint_release(RangeTreeUInt *rt, const uint value)
  721. {
  722. bool touch_prev, touch_next;
  723. Node *node_prev, *node_next;
  724. if (rt->list.first != NULL) {
  725. rt_find_node_pair_around_value(rt, value, &node_prev, &node_next);
  726. /* the value must have been already taken */
  727. assert(node_prev || node_next);
  728. /* Cases:
  729. * 1) fill the gap between prev & next (two spans into one span).
  730. * 2) touching prev, (grow node_prev->max up one).
  731. * 3) touching next, (grow node_next->min down one).
  732. * 4) touching neither, add a new segment. */
  733. touch_prev = (node_prev != NULL && node_prev->max + 1 == value);
  734. touch_next = (node_next != NULL && node_next->min - 1 == value);
  735. }
  736. else {
  737. // we could handle this case (4) inline,
  738. // since its not a common case - use regular logic.
  739. node_prev = node_next = NULL;
  740. touch_prev = false;
  741. touch_next = false;
  742. }
  743. if (touch_prev && touch_next) { // 1)
  744. node_prev->max = node_next->max;
  745. rt_node_remove(rt, node_next);
  746. }
  747. else if (touch_prev) { // 2)
  748. assert(node_prev->max + 1 == value);
  749. node_prev->max = value;
  750. }
  751. else if (touch_next) { // 3)
  752. assert(node_next->min - 1 == value);
  753. node_next->min = value;
  754. }
  755. else { // 4)
  756. Node *node_new = rt_node_new(rt, value, value);
  757. if (node_prev != NULL) {
  758. rt_node_add_after(rt, node_prev, node_new);
  759. }
  760. else if (node_next != NULL) {
  761. rt_node_add_before(rt, node_next, node_new);
  762. }
  763. else {
  764. assert(rt->list.first == NULL);
  765. rt_node_add_back(rt, node_new);
  766. }
  767. }
  768. }