2
0

qtree.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. /*
  2. * GLIB - Library of useful routines for C programming
  3. * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  4. *
  5. * SPDX-License-Identifier: LGPL-2.1-or-later
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /*
  21. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  22. * file for a list of people on the GLib Team. See the ChangeLog
  23. * files for a list of changes. These files are distributed with
  24. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  25. */
  26. /*
  27. * MT safe
  28. */
  29. #include "qemu/osdep.h"
  30. #include "qemu/qtree.h"
  31. /**
  32. * SECTION:trees-binary
  33. * @title: Balanced Binary Trees
  34. * @short_description: a sorted collection of key/value pairs optimized
  35. * for searching and traversing in order
  36. *
  37. * The #QTree structure and its associated functions provide a sorted
  38. * collection of key/value pairs optimized for searching and traversing
  39. * in order. This means that most of the operations (access, search,
  40. * insertion, deletion, ...) on #QTree are O(log(n)) in average and O(n)
  41. * in worst case for time complexity. But, note that maintaining a
  42. * balanced sorted #QTree of n elements is done in time O(n log(n)).
  43. *
  44. * To create a new #QTree use q_tree_new().
  45. *
  46. * To insert a key/value pair into a #QTree use q_tree_insert()
  47. * (O(n log(n))).
  48. *
  49. * To remove a key/value pair use q_tree_remove() (O(n log(n))).
  50. *
  51. * To look up the value corresponding to a given key, use
  52. * q_tree_lookup() and q_tree_lookup_extended().
  53. *
  54. * To find out the number of nodes in a #QTree, use q_tree_nnodes(). To
  55. * get the height of a #QTree, use q_tree_height().
  56. *
  57. * To traverse a #QTree, calling a function for each node visited in
  58. * the traversal, use q_tree_foreach().
  59. *
  60. * To destroy a #QTree, use q_tree_destroy().
  61. **/
  62. #define MAX_GTREE_HEIGHT 40
  63. /**
  64. * QTree:
  65. *
  66. * The QTree struct is an opaque data structure representing a
  67. * [balanced binary tree][glib-Balanced-Binary-Trees]. It should be
  68. * accessed only by using the following functions.
  69. */
  70. struct _QTree {
  71. QTreeNode *root;
  72. GCompareDataFunc key_compare;
  73. GDestroyNotify key_destroy_func;
  74. GDestroyNotify value_destroy_func;
  75. gpointer key_compare_data;
  76. guint nnodes;
  77. gint ref_count;
  78. };
  79. struct _QTreeNode {
  80. gpointer key; /* key for this node */
  81. gpointer value; /* value stored at this node */
  82. QTreeNode *left; /* left subtree */
  83. QTreeNode *right; /* right subtree */
  84. gint8 balance; /* height (right) - height (left) */
  85. guint8 left_child;
  86. guint8 right_child;
  87. };
  88. static QTreeNode *q_tree_node_new(gpointer key,
  89. gpointer value);
  90. static QTreeNode *q_tree_insert_internal(QTree *tree,
  91. gpointer key,
  92. gpointer value,
  93. gboolean replace);
  94. static gboolean q_tree_remove_internal(QTree *tree,
  95. gconstpointer key,
  96. gboolean steal);
  97. static QTreeNode *q_tree_node_balance(QTreeNode *node);
  98. static QTreeNode *q_tree_find_node(QTree *tree,
  99. gconstpointer key);
  100. static QTreeNode *q_tree_node_search(QTreeNode *node,
  101. GCompareFunc search_func,
  102. gconstpointer data);
  103. static QTreeNode *q_tree_node_rotate_left(QTreeNode *node);
  104. static QTreeNode *q_tree_node_rotate_right(QTreeNode *node);
  105. #ifdef Q_TREE_DEBUG
  106. static void q_tree_node_check(QTreeNode *node);
  107. #endif
  108. static QTreeNode*
  109. q_tree_node_new(gpointer key,
  110. gpointer value)
  111. {
  112. QTreeNode *node = g_new(QTreeNode, 1);
  113. node->balance = 0;
  114. node->left = NULL;
  115. node->right = NULL;
  116. node->left_child = FALSE;
  117. node->right_child = FALSE;
  118. node->key = key;
  119. node->value = value;
  120. return node;
  121. }
  122. /**
  123. * q_tree_new:
  124. * @key_compare_func: the function used to order the nodes in the #QTree.
  125. * It should return values similar to the standard strcmp() function -
  126. * 0 if the two arguments are equal, a negative value if the first argument
  127. * comes before the second, or a positive value if the first argument comes
  128. * after the second.
  129. *
  130. * Creates a new #QTree.
  131. *
  132. * Returns: a newly allocated #QTree
  133. */
  134. QTree *
  135. q_tree_new(GCompareFunc key_compare_func)
  136. {
  137. g_return_val_if_fail(key_compare_func != NULL, NULL);
  138. return q_tree_new_full((GCompareDataFunc) key_compare_func, NULL,
  139. NULL, NULL);
  140. }
  141. /**
  142. * q_tree_new_with_data:
  143. * @key_compare_func: qsort()-style comparison function
  144. * @key_compare_data: data to pass to comparison function
  145. *
  146. * Creates a new #QTree with a comparison function that accepts user data.
  147. * See q_tree_new() for more details.
  148. *
  149. * Returns: a newly allocated #QTree
  150. */
  151. QTree *
  152. q_tree_new_with_data(GCompareDataFunc key_compare_func,
  153. gpointer key_compare_data)
  154. {
  155. g_return_val_if_fail(key_compare_func != NULL, NULL);
  156. return q_tree_new_full(key_compare_func, key_compare_data,
  157. NULL, NULL);
  158. }
  159. /**
  160. * q_tree_new_full:
  161. * @key_compare_func: qsort()-style comparison function
  162. * @key_compare_data: data to pass to comparison function
  163. * @key_destroy_func: a function to free the memory allocated for the key
  164. * used when removing the entry from the #QTree or %NULL if you don't
  165. * want to supply such a function
  166. * @value_destroy_func: a function to free the memory allocated for the
  167. * value used when removing the entry from the #QTree or %NULL if you
  168. * don't want to supply such a function
  169. *
  170. * Creates a new #QTree like q_tree_new() and allows to specify functions
  171. * to free the memory allocated for the key and value that get called when
  172. * removing the entry from the #QTree.
  173. *
  174. * Returns: a newly allocated #QTree
  175. */
  176. QTree *
  177. q_tree_new_full(GCompareDataFunc key_compare_func,
  178. gpointer key_compare_data,
  179. GDestroyNotify key_destroy_func,
  180. GDestroyNotify value_destroy_func)
  181. {
  182. QTree *tree;
  183. g_return_val_if_fail(key_compare_func != NULL, NULL);
  184. tree = g_new(QTree, 1);
  185. tree->root = NULL;
  186. tree->key_compare = key_compare_func;
  187. tree->key_destroy_func = key_destroy_func;
  188. tree->value_destroy_func = value_destroy_func;
  189. tree->key_compare_data = key_compare_data;
  190. tree->nnodes = 0;
  191. tree->ref_count = 1;
  192. return tree;
  193. }
  194. /**
  195. * q_tree_node_first:
  196. * @tree: a #QTree
  197. *
  198. * Returns the first in-order node of the tree, or %NULL
  199. * for an empty tree.
  200. *
  201. * Returns: (nullable) (transfer none): the first node in the tree
  202. *
  203. * Since: 2.68 in GLib. Internal in Qtree, i.e. not in the public API.
  204. */
  205. static QTreeNode *
  206. q_tree_node_first(QTree *tree)
  207. {
  208. QTreeNode *tmp;
  209. g_return_val_if_fail(tree != NULL, NULL);
  210. if (!tree->root) {
  211. return NULL;
  212. }
  213. tmp = tree->root;
  214. while (tmp->left_child) {
  215. tmp = tmp->left;
  216. }
  217. return tmp;
  218. }
  219. /**
  220. * q_tree_node_previous
  221. * @node: a #QTree node
  222. *
  223. * Returns the previous in-order node of the tree, or %NULL
  224. * if the passed node was already the first one.
  225. *
  226. * Returns: (nullable) (transfer none): the previous node in the tree
  227. *
  228. * Since: 2.68 in GLib. Internal in Qtree, i.e. not in the public API.
  229. */
  230. static QTreeNode *
  231. q_tree_node_previous(QTreeNode *node)
  232. {
  233. QTreeNode *tmp;
  234. g_return_val_if_fail(node != NULL, NULL);
  235. tmp = node->left;
  236. if (node->left_child) {
  237. while (tmp->right_child) {
  238. tmp = tmp->right;
  239. }
  240. }
  241. return tmp;
  242. }
  243. /**
  244. * q_tree_node_next
  245. * @node: a #QTree node
  246. *
  247. * Returns the next in-order node of the tree, or %NULL
  248. * if the passed node was already the last one.
  249. *
  250. * Returns: (nullable) (transfer none): the next node in the tree
  251. *
  252. * Since: 2.68 in GLib. Internal in Qtree, i.e. not in the public API.
  253. */
  254. static QTreeNode *
  255. q_tree_node_next(QTreeNode *node)
  256. {
  257. QTreeNode *tmp;
  258. g_return_val_if_fail(node != NULL, NULL);
  259. tmp = node->right;
  260. if (node->right_child) {
  261. while (tmp->left_child) {
  262. tmp = tmp->left;
  263. }
  264. }
  265. return tmp;
  266. }
  267. /**
  268. * q_tree_remove_all:
  269. * @tree: a #QTree
  270. *
  271. * Removes all nodes from a #QTree and destroys their keys and values,
  272. * then resets the #QTree’s root to %NULL.
  273. *
  274. * Since: 2.70 in GLib. Internal in Qtree, i.e. not in the public API.
  275. */
  276. static void QEMU_DISABLE_CFI
  277. q_tree_remove_all(QTree *tree)
  278. {
  279. QTreeNode *node;
  280. QTreeNode *next;
  281. g_return_if_fail(tree != NULL);
  282. node = q_tree_node_first(tree);
  283. while (node) {
  284. next = q_tree_node_next(node);
  285. if (tree->key_destroy_func) {
  286. tree->key_destroy_func(node->key);
  287. }
  288. if (tree->value_destroy_func) {
  289. tree->value_destroy_func(node->value);
  290. }
  291. g_free(node);
  292. #ifdef Q_TREE_DEBUG
  293. g_assert(tree->nnodes > 0);
  294. tree->nnodes--;
  295. #endif
  296. node = next;
  297. }
  298. #ifdef Q_TREE_DEBUG
  299. g_assert(tree->nnodes == 0);
  300. #endif
  301. tree->root = NULL;
  302. #ifndef Q_TREE_DEBUG
  303. tree->nnodes = 0;
  304. #endif
  305. }
  306. /**
  307. * q_tree_ref:
  308. * @tree: a #QTree
  309. *
  310. * Increments the reference count of @tree by one.
  311. *
  312. * It is safe to call this function from any thread.
  313. *
  314. * Returns: the passed in #QTree
  315. *
  316. * Since: 2.22
  317. */
  318. QTree *
  319. q_tree_ref(QTree *tree)
  320. {
  321. g_return_val_if_fail(tree != NULL, NULL);
  322. g_atomic_int_inc(&tree->ref_count);
  323. return tree;
  324. }
  325. /**
  326. * q_tree_unref:
  327. * @tree: a #QTree
  328. *
  329. * Decrements the reference count of @tree by one.
  330. * If the reference count drops to 0, all keys and values will
  331. * be destroyed (if destroy functions were specified) and all
  332. * memory allocated by @tree will be released.
  333. *
  334. * It is safe to call this function from any thread.
  335. *
  336. * Since: 2.22
  337. */
  338. void
  339. q_tree_unref(QTree *tree)
  340. {
  341. g_return_if_fail(tree != NULL);
  342. if (g_atomic_int_dec_and_test(&tree->ref_count)) {
  343. q_tree_remove_all(tree);
  344. g_free(tree);
  345. }
  346. }
  347. /**
  348. * q_tree_destroy:
  349. * @tree: a #QTree
  350. *
  351. * Removes all keys and values from the #QTree and decreases its
  352. * reference count by one. If keys and/or values are dynamically
  353. * allocated, you should either free them first or create the #QTree
  354. * using q_tree_new_full(). In the latter case the destroy functions
  355. * you supplied will be called on all keys and values before destroying
  356. * the #QTree.
  357. */
  358. void
  359. q_tree_destroy(QTree *tree)
  360. {
  361. g_return_if_fail(tree != NULL);
  362. q_tree_remove_all(tree);
  363. q_tree_unref(tree);
  364. }
  365. /**
  366. * q_tree_insert_node:
  367. * @tree: a #QTree
  368. * @key: the key to insert
  369. * @value: the value corresponding to the key
  370. *
  371. * Inserts a key/value pair into a #QTree.
  372. *
  373. * If the given key already exists in the #QTree its corresponding value
  374. * is set to the new value. If you supplied a @value_destroy_func when
  375. * creating the #QTree, the old value is freed using that function. If
  376. * you supplied a @key_destroy_func when creating the #QTree, the passed
  377. * key is freed using that function.
  378. *
  379. * The tree is automatically 'balanced' as new key/value pairs are added,
  380. * so that the distance from the root to every leaf is as small as possible.
  381. * The cost of maintaining a balanced tree while inserting new key/value
  382. * result in a O(n log(n)) operation where most of the other operations
  383. * are O(log(n)).
  384. *
  385. * Returns: (transfer none): the inserted (or set) node.
  386. *
  387. * Since: 2.68 in GLib. Internal in Qtree, i.e. not in the public API.
  388. */
  389. static QTreeNode *
  390. q_tree_insert_node(QTree *tree,
  391. gpointer key,
  392. gpointer value)
  393. {
  394. QTreeNode *node;
  395. g_return_val_if_fail(tree != NULL, NULL);
  396. node = q_tree_insert_internal(tree, key, value, FALSE);
  397. #ifdef Q_TREE_DEBUG
  398. q_tree_node_check(tree->root);
  399. #endif
  400. return node;
  401. }
  402. /**
  403. * q_tree_insert:
  404. * @tree: a #QTree
  405. * @key: the key to insert
  406. * @value: the value corresponding to the key
  407. *
  408. * Inserts a key/value pair into a #QTree.
  409. *
  410. * Inserts a new key and value into a #QTree as q_tree_insert_node() does,
  411. * only this function does not return the inserted or set node.
  412. */
  413. void
  414. q_tree_insert(QTree *tree,
  415. gpointer key,
  416. gpointer value)
  417. {
  418. q_tree_insert_node(tree, key, value);
  419. }
  420. /**
  421. * q_tree_replace_node:
  422. * @tree: a #QTree
  423. * @key: the key to insert
  424. * @value: the value corresponding to the key
  425. *
  426. * Inserts a new key and value into a #QTree similar to q_tree_insert_node().
  427. * The difference is that if the key already exists in the #QTree, it gets
  428. * replaced by the new key. If you supplied a @value_destroy_func when
  429. * creating the #QTree, the old value is freed using that function. If you
  430. * supplied a @key_destroy_func when creating the #QTree, the old key is
  431. * freed using that function.
  432. *
  433. * The tree is automatically 'balanced' as new key/value pairs are added,
  434. * so that the distance from the root to every leaf is as small as possible.
  435. *
  436. * Returns: (transfer none): the inserted (or set) node.
  437. *
  438. * Since: 2.68 in GLib. Internal in Qtree, i.e. not in the public API.
  439. */
  440. static QTreeNode *
  441. q_tree_replace_node(QTree *tree,
  442. gpointer key,
  443. gpointer value)
  444. {
  445. QTreeNode *node;
  446. g_return_val_if_fail(tree != NULL, NULL);
  447. node = q_tree_insert_internal(tree, key, value, TRUE);
  448. #ifdef Q_TREE_DEBUG
  449. q_tree_node_check(tree->root);
  450. #endif
  451. return node;
  452. }
  453. /**
  454. * q_tree_replace:
  455. * @tree: a #QTree
  456. * @key: the key to insert
  457. * @value: the value corresponding to the key
  458. *
  459. * Inserts a new key and value into a #QTree as q_tree_replace_node() does,
  460. * only this function does not return the inserted or set node.
  461. */
  462. void
  463. q_tree_replace(QTree *tree,
  464. gpointer key,
  465. gpointer value)
  466. {
  467. q_tree_replace_node(tree, key, value);
  468. }
  469. /* internal insert routine */
  470. static QTreeNode * QEMU_DISABLE_CFI
  471. q_tree_insert_internal(QTree *tree,
  472. gpointer key,
  473. gpointer value,
  474. gboolean replace)
  475. {
  476. QTreeNode *node, *retnode;
  477. QTreeNode *path[MAX_GTREE_HEIGHT];
  478. int idx;
  479. g_return_val_if_fail(tree != NULL, NULL);
  480. if (!tree->root) {
  481. tree->root = q_tree_node_new(key, value);
  482. tree->nnodes++;
  483. return tree->root;
  484. }
  485. idx = 0;
  486. path[idx++] = NULL;
  487. node = tree->root;
  488. while (1) {
  489. int cmp = tree->key_compare(key, node->key, tree->key_compare_data);
  490. if (cmp == 0) {
  491. if (tree->value_destroy_func) {
  492. tree->value_destroy_func(node->value);
  493. }
  494. node->value = value;
  495. if (replace) {
  496. if (tree->key_destroy_func) {
  497. tree->key_destroy_func(node->key);
  498. }
  499. node->key = key;
  500. } else {
  501. /* free the passed key */
  502. if (tree->key_destroy_func) {
  503. tree->key_destroy_func(key);
  504. }
  505. }
  506. return node;
  507. } else if (cmp < 0) {
  508. if (node->left_child) {
  509. path[idx++] = node;
  510. node = node->left;
  511. } else {
  512. QTreeNode *child = q_tree_node_new(key, value);
  513. child->left = node->left;
  514. child->right = node;
  515. node->left = child;
  516. node->left_child = TRUE;
  517. node->balance -= 1;
  518. tree->nnodes++;
  519. retnode = child;
  520. break;
  521. }
  522. } else {
  523. if (node->right_child) {
  524. path[idx++] = node;
  525. node = node->right;
  526. } else {
  527. QTreeNode *child = q_tree_node_new(key, value);
  528. child->right = node->right;
  529. child->left = node;
  530. node->right = child;
  531. node->right_child = TRUE;
  532. node->balance += 1;
  533. tree->nnodes++;
  534. retnode = child;
  535. break;
  536. }
  537. }
  538. }
  539. /*
  540. * Restore balance. This is the goodness of a non-recursive
  541. * implementation, when we are done with balancing we 'break'
  542. * the loop and we are done.
  543. */
  544. while (1) {
  545. QTreeNode *bparent = path[--idx];
  546. gboolean left_node = (bparent && node == bparent->left);
  547. g_assert(!bparent || bparent->left == node || bparent->right == node);
  548. if (node->balance < -1 || node->balance > 1) {
  549. node = q_tree_node_balance(node);
  550. if (bparent == NULL) {
  551. tree->root = node;
  552. } else if (left_node) {
  553. bparent->left = node;
  554. } else {
  555. bparent->right = node;
  556. }
  557. }
  558. if (node->balance == 0 || bparent == NULL) {
  559. break;
  560. }
  561. if (left_node) {
  562. bparent->balance -= 1;
  563. } else {
  564. bparent->balance += 1;
  565. }
  566. node = bparent;
  567. }
  568. return retnode;
  569. }
  570. /**
  571. * q_tree_remove:
  572. * @tree: a #QTree
  573. * @key: the key to remove
  574. *
  575. * Removes a key/value pair from a #QTree.
  576. *
  577. * If the #QTree was created using q_tree_new_full(), the key and value
  578. * are freed using the supplied destroy functions, otherwise you have to
  579. * make sure that any dynamically allocated values are freed yourself.
  580. * If the key does not exist in the #QTree, the function does nothing.
  581. *
  582. * The cost of maintaining a balanced tree while removing a key/value
  583. * result in a O(n log(n)) operation where most of the other operations
  584. * are O(log(n)).
  585. *
  586. * Returns: %TRUE if the key was found (prior to 2.8, this function
  587. * returned nothing)
  588. */
  589. gboolean
  590. q_tree_remove(QTree *tree,
  591. gconstpointer key)
  592. {
  593. gboolean removed;
  594. g_return_val_if_fail(tree != NULL, FALSE);
  595. removed = q_tree_remove_internal(tree, key, FALSE);
  596. #ifdef Q_TREE_DEBUG
  597. q_tree_node_check(tree->root);
  598. #endif
  599. return removed;
  600. }
  601. /**
  602. * q_tree_steal:
  603. * @tree: a #QTree
  604. * @key: the key to remove
  605. *
  606. * Removes a key and its associated value from a #QTree without calling
  607. * the key and value destroy functions.
  608. *
  609. * If the key does not exist in the #QTree, the function does nothing.
  610. *
  611. * Returns: %TRUE if the key was found (prior to 2.8, this function
  612. * returned nothing)
  613. */
  614. gboolean
  615. q_tree_steal(QTree *tree,
  616. gconstpointer key)
  617. {
  618. gboolean removed;
  619. g_return_val_if_fail(tree != NULL, FALSE);
  620. removed = q_tree_remove_internal(tree, key, TRUE);
  621. #ifdef Q_TREE_DEBUG
  622. q_tree_node_check(tree->root);
  623. #endif
  624. return removed;
  625. }
  626. /* internal remove routine */
  627. static gboolean QEMU_DISABLE_CFI
  628. q_tree_remove_internal(QTree *tree,
  629. gconstpointer key,
  630. gboolean steal)
  631. {
  632. QTreeNode *node, *parent, *balance;
  633. QTreeNode *path[MAX_GTREE_HEIGHT];
  634. int idx;
  635. gboolean left_node;
  636. g_return_val_if_fail(tree != NULL, FALSE);
  637. if (!tree->root) {
  638. return FALSE;
  639. }
  640. idx = 0;
  641. path[idx++] = NULL;
  642. node = tree->root;
  643. while (1) {
  644. int cmp = tree->key_compare(key, node->key, tree->key_compare_data);
  645. if (cmp == 0) {
  646. break;
  647. } else if (cmp < 0) {
  648. if (!node->left_child) {
  649. return FALSE;
  650. }
  651. path[idx++] = node;
  652. node = node->left;
  653. } else {
  654. if (!node->right_child) {
  655. return FALSE;
  656. }
  657. path[idx++] = node;
  658. node = node->right;
  659. }
  660. }
  661. /*
  662. * The following code is almost equal to q_tree_remove_node,
  663. * except that we do not have to call q_tree_node_parent.
  664. */
  665. balance = parent = path[--idx];
  666. g_assert(!parent || parent->left == node || parent->right == node);
  667. left_node = (parent && node == parent->left);
  668. if (!node->left_child) {
  669. if (!node->right_child) {
  670. if (!parent) {
  671. tree->root = NULL;
  672. } else if (left_node) {
  673. parent->left_child = FALSE;
  674. parent->left = node->left;
  675. parent->balance += 1;
  676. } else {
  677. parent->right_child = FALSE;
  678. parent->right = node->right;
  679. parent->balance -= 1;
  680. }
  681. } else {
  682. /* node has a right child */
  683. QTreeNode *tmp = q_tree_node_next(node);
  684. tmp->left = node->left;
  685. if (!parent) {
  686. tree->root = node->right;
  687. } else if (left_node) {
  688. parent->left = node->right;
  689. parent->balance += 1;
  690. } else {
  691. parent->right = node->right;
  692. parent->balance -= 1;
  693. }
  694. }
  695. } else {
  696. /* node has a left child */
  697. if (!node->right_child) {
  698. QTreeNode *tmp = q_tree_node_previous(node);
  699. tmp->right = node->right;
  700. if (parent == NULL) {
  701. tree->root = node->left;
  702. } else if (left_node) {
  703. parent->left = node->left;
  704. parent->balance += 1;
  705. } else {
  706. parent->right = node->left;
  707. parent->balance -= 1;
  708. }
  709. } else {
  710. /* node has a both children (pant, pant!) */
  711. QTreeNode *prev = node->left;
  712. QTreeNode *next = node->right;
  713. QTreeNode *nextp = node;
  714. int old_idx = idx + 1;
  715. idx++;
  716. /* path[idx] == parent */
  717. /* find the immediately next node (and its parent) */
  718. while (next->left_child) {
  719. path[++idx] = nextp = next;
  720. next = next->left;
  721. }
  722. path[old_idx] = next;
  723. balance = path[idx];
  724. /* remove 'next' from the tree */
  725. if (nextp != node) {
  726. if (next->right_child) {
  727. nextp->left = next->right;
  728. } else {
  729. nextp->left_child = FALSE;
  730. }
  731. nextp->balance += 1;
  732. next->right_child = TRUE;
  733. next->right = node->right;
  734. } else {
  735. node->balance -= 1;
  736. }
  737. /* set the prev to point to the right place */
  738. while (prev->right_child) {
  739. prev = prev->right;
  740. }
  741. prev->right = next;
  742. /* prepare 'next' to replace 'node' */
  743. next->left_child = TRUE;
  744. next->left = node->left;
  745. next->balance = node->balance;
  746. if (!parent) {
  747. tree->root = next;
  748. } else if (left_node) {
  749. parent->left = next;
  750. } else {
  751. parent->right = next;
  752. }
  753. }
  754. }
  755. /* restore balance */
  756. if (balance) {
  757. while (1) {
  758. QTreeNode *bparent = path[--idx];
  759. g_assert(!bparent ||
  760. bparent->left == balance ||
  761. bparent->right == balance);
  762. left_node = (bparent && balance == bparent->left);
  763. if (balance->balance < -1 || balance->balance > 1) {
  764. balance = q_tree_node_balance(balance);
  765. if (!bparent) {
  766. tree->root = balance;
  767. } else if (left_node) {
  768. bparent->left = balance;
  769. } else {
  770. bparent->right = balance;
  771. }
  772. }
  773. if (balance->balance != 0 || !bparent) {
  774. break;
  775. }
  776. if (left_node) {
  777. bparent->balance += 1;
  778. } else {
  779. bparent->balance -= 1;
  780. }
  781. balance = bparent;
  782. }
  783. }
  784. if (!steal) {
  785. if (tree->key_destroy_func) {
  786. tree->key_destroy_func(node->key);
  787. }
  788. if (tree->value_destroy_func) {
  789. tree->value_destroy_func(node->value);
  790. }
  791. }
  792. g_free(node);
  793. tree->nnodes--;
  794. return TRUE;
  795. }
  796. /**
  797. * q_tree_lookup_node:
  798. * @tree: a #QTree
  799. * @key: the key to look up
  800. *
  801. * Gets the tree node corresponding to the given key. Since a #QTree is
  802. * automatically balanced as key/value pairs are added, key lookup
  803. * is O(log n) (where n is the number of key/value pairs in the tree).
  804. *
  805. * Returns: (nullable) (transfer none): the tree node corresponding to
  806. * the key, or %NULL if the key was not found
  807. *
  808. * Since: 2.68 in GLib. Internal in Qtree, i.e. not in the public API.
  809. */
  810. static QTreeNode *
  811. q_tree_lookup_node(QTree *tree,
  812. gconstpointer key)
  813. {
  814. g_return_val_if_fail(tree != NULL, NULL);
  815. return q_tree_find_node(tree, key);
  816. }
  817. /**
  818. * q_tree_lookup:
  819. * @tree: a #QTree
  820. * @key: the key to look up
  821. *
  822. * Gets the value corresponding to the given key. Since a #QTree is
  823. * automatically balanced as key/value pairs are added, key lookup
  824. * is O(log n) (where n is the number of key/value pairs in the tree).
  825. *
  826. * Returns: the value corresponding to the key, or %NULL
  827. * if the key was not found
  828. */
  829. gpointer
  830. q_tree_lookup(QTree *tree,
  831. gconstpointer key)
  832. {
  833. QTreeNode *node;
  834. node = q_tree_lookup_node(tree, key);
  835. return node ? node->value : NULL;
  836. }
  837. /**
  838. * q_tree_lookup_extended:
  839. * @tree: a #QTree
  840. * @lookup_key: the key to look up
  841. * @orig_key: (out) (optional) (nullable): returns the original key
  842. * @value: (out) (optional) (nullable): returns the value associated with
  843. * the key
  844. *
  845. * Looks up a key in the #QTree, returning the original key and the
  846. * associated value. This is useful if you need to free the memory
  847. * allocated for the original key, for example before calling
  848. * q_tree_remove().
  849. *
  850. * Returns: %TRUE if the key was found in the #QTree
  851. */
  852. gboolean
  853. q_tree_lookup_extended(QTree *tree,
  854. gconstpointer lookup_key,
  855. gpointer *orig_key,
  856. gpointer *value)
  857. {
  858. QTreeNode *node;
  859. g_return_val_if_fail(tree != NULL, FALSE);
  860. node = q_tree_find_node(tree, lookup_key);
  861. if (node) {
  862. if (orig_key) {
  863. *orig_key = node->key;
  864. }
  865. if (value) {
  866. *value = node->value;
  867. }
  868. return TRUE;
  869. } else {
  870. return FALSE;
  871. }
  872. }
  873. /**
  874. * q_tree_foreach:
  875. * @tree: a #QTree
  876. * @func: the function to call for each node visited.
  877. * If this function returns %TRUE, the traversal is stopped.
  878. * @user_data: user data to pass to the function
  879. *
  880. * Calls the given function for each of the key/value pairs in the #QTree.
  881. * The function is passed the key and value of each pair, and the given
  882. * @data parameter. The tree is traversed in sorted order.
  883. *
  884. * The tree may not be modified while iterating over it (you can't
  885. * add/remove items). To remove all items matching a predicate, you need
  886. * to add each item to a list in your #GTraverseFunc as you walk over
  887. * the tree, then walk the list and remove each item.
  888. */
  889. void
  890. q_tree_foreach(QTree *tree,
  891. GTraverseFunc func,
  892. gpointer user_data)
  893. {
  894. QTreeNode *node;
  895. g_return_if_fail(tree != NULL);
  896. if (!tree->root) {
  897. return;
  898. }
  899. node = q_tree_node_first(tree);
  900. while (node) {
  901. if ((*func)(node->key, node->value, user_data)) {
  902. break;
  903. }
  904. node = q_tree_node_next(node);
  905. }
  906. }
  907. /**
  908. * q_tree_search_node:
  909. * @tree: a #QTree
  910. * @search_func: a function used to search the #QTree
  911. * @user_data: the data passed as the second argument to @search_func
  912. *
  913. * Searches a #QTree using @search_func.
  914. *
  915. * The @search_func is called with a pointer to the key of a key/value
  916. * pair in the tree, and the passed in @user_data. If @search_func returns
  917. * 0 for a key/value pair, then the corresponding node is returned as
  918. * the result of q_tree_search(). If @search_func returns -1, searching
  919. * will proceed among the key/value pairs that have a smaller key; if
  920. * @search_func returns 1, searching will proceed among the key/value
  921. * pairs that have a larger key.
  922. *
  923. * Returns: (nullable) (transfer none): the node corresponding to the
  924. * found key, or %NULL if the key was not found
  925. *
  926. * Since: 2.68 in GLib. Internal in Qtree, i.e. not in the public API.
  927. */
  928. static QTreeNode *
  929. q_tree_search_node(QTree *tree,
  930. GCompareFunc search_func,
  931. gconstpointer user_data)
  932. {
  933. g_return_val_if_fail(tree != NULL, NULL);
  934. if (!tree->root) {
  935. return NULL;
  936. }
  937. return q_tree_node_search(tree->root, search_func, user_data);
  938. }
  939. /**
  940. * q_tree_search:
  941. * @tree: a #QTree
  942. * @search_func: a function used to search the #QTree
  943. * @user_data: the data passed as the second argument to @search_func
  944. *
  945. * Searches a #QTree using @search_func.
  946. *
  947. * The @search_func is called with a pointer to the key of a key/value
  948. * pair in the tree, and the passed in @user_data. If @search_func returns
  949. * 0 for a key/value pair, then the corresponding value is returned as
  950. * the result of q_tree_search(). If @search_func returns -1, searching
  951. * will proceed among the key/value pairs that have a smaller key; if
  952. * @search_func returns 1, searching will proceed among the key/value
  953. * pairs that have a larger key.
  954. *
  955. * Returns: the value corresponding to the found key, or %NULL
  956. * if the key was not found
  957. */
  958. gpointer
  959. q_tree_search(QTree *tree,
  960. GCompareFunc search_func,
  961. gconstpointer user_data)
  962. {
  963. QTreeNode *node;
  964. node = q_tree_search_node(tree, search_func, user_data);
  965. return node ? node->value : NULL;
  966. }
  967. /**
  968. * q_tree_height:
  969. * @tree: a #QTree
  970. *
  971. * Gets the height of a #QTree.
  972. *
  973. * If the #QTree contains no nodes, the height is 0.
  974. * If the #QTree contains only one root node the height is 1.
  975. * If the root node has children the height is 2, etc.
  976. *
  977. * Returns: the height of @tree
  978. */
  979. gint
  980. q_tree_height(QTree *tree)
  981. {
  982. QTreeNode *node;
  983. gint height;
  984. g_return_val_if_fail(tree != NULL, 0);
  985. if (!tree->root) {
  986. return 0;
  987. }
  988. height = 0;
  989. node = tree->root;
  990. while (1) {
  991. height += 1 + MAX(node->balance, 0);
  992. if (!node->left_child) {
  993. return height;
  994. }
  995. node = node->left;
  996. }
  997. }
  998. /**
  999. * q_tree_nnodes:
  1000. * @tree: a #QTree
  1001. *
  1002. * Gets the number of nodes in a #QTree.
  1003. *
  1004. * Returns: the number of nodes in @tree
  1005. */
  1006. gint
  1007. q_tree_nnodes(QTree *tree)
  1008. {
  1009. g_return_val_if_fail(tree != NULL, 0);
  1010. return tree->nnodes;
  1011. }
  1012. static QTreeNode *
  1013. q_tree_node_balance(QTreeNode *node)
  1014. {
  1015. if (node->balance < -1) {
  1016. if (node->left->balance > 0) {
  1017. node->left = q_tree_node_rotate_left(node->left);
  1018. }
  1019. node = q_tree_node_rotate_right(node);
  1020. } else if (node->balance > 1) {
  1021. if (node->right->balance < 0) {
  1022. node->right = q_tree_node_rotate_right(node->right);
  1023. }
  1024. node = q_tree_node_rotate_left(node);
  1025. }
  1026. return node;
  1027. }
  1028. static QTreeNode * QEMU_DISABLE_CFI
  1029. q_tree_find_node(QTree *tree,
  1030. gconstpointer key)
  1031. {
  1032. QTreeNode *node;
  1033. gint cmp;
  1034. node = tree->root;
  1035. if (!node) {
  1036. return NULL;
  1037. }
  1038. while (1) {
  1039. cmp = tree->key_compare(key, node->key, tree->key_compare_data);
  1040. if (cmp == 0) {
  1041. return node;
  1042. } else if (cmp < 0) {
  1043. if (!node->left_child) {
  1044. return NULL;
  1045. }
  1046. node = node->left;
  1047. } else {
  1048. if (!node->right_child) {
  1049. return NULL;
  1050. }
  1051. node = node->right;
  1052. }
  1053. }
  1054. }
  1055. static QTreeNode *
  1056. q_tree_node_search(QTreeNode *node,
  1057. GCompareFunc search_func,
  1058. gconstpointer data)
  1059. {
  1060. gint dir;
  1061. if (!node) {
  1062. return NULL;
  1063. }
  1064. while (1) {
  1065. dir = (*search_func)(node->key, data);
  1066. if (dir == 0) {
  1067. return node;
  1068. } else if (dir < 0) {
  1069. if (!node->left_child) {
  1070. return NULL;
  1071. }
  1072. node = node->left;
  1073. } else {
  1074. if (!node->right_child) {
  1075. return NULL;
  1076. }
  1077. node = node->right;
  1078. }
  1079. }
  1080. }
  1081. static QTreeNode *
  1082. q_tree_node_rotate_left(QTreeNode *node)
  1083. {
  1084. QTreeNode *right;
  1085. gint a_bal;
  1086. gint b_bal;
  1087. right = node->right;
  1088. if (right->left_child) {
  1089. node->right = right->left;
  1090. } else {
  1091. node->right_child = FALSE;
  1092. right->left_child = TRUE;
  1093. }
  1094. right->left = node;
  1095. a_bal = node->balance;
  1096. b_bal = right->balance;
  1097. if (b_bal <= 0) {
  1098. if (a_bal >= 1) {
  1099. right->balance = b_bal - 1;
  1100. } else {
  1101. right->balance = a_bal + b_bal - 2;
  1102. }
  1103. node->balance = a_bal - 1;
  1104. } else {
  1105. if (a_bal <= b_bal) {
  1106. right->balance = a_bal - 2;
  1107. } else {
  1108. right->balance = b_bal - 1;
  1109. }
  1110. node->balance = a_bal - b_bal - 1;
  1111. }
  1112. return right;
  1113. }
  1114. static QTreeNode *
  1115. q_tree_node_rotate_right(QTreeNode *node)
  1116. {
  1117. QTreeNode *left;
  1118. gint a_bal;
  1119. gint b_bal;
  1120. left = node->left;
  1121. if (left->right_child) {
  1122. node->left = left->right;
  1123. } else {
  1124. node->left_child = FALSE;
  1125. left->right_child = TRUE;
  1126. }
  1127. left->right = node;
  1128. a_bal = node->balance;
  1129. b_bal = left->balance;
  1130. if (b_bal <= 0) {
  1131. if (b_bal > a_bal) {
  1132. left->balance = b_bal + 1;
  1133. } else {
  1134. left->balance = a_bal + 2;
  1135. }
  1136. node->balance = a_bal - b_bal + 1;
  1137. } else {
  1138. if (a_bal <= -1) {
  1139. left->balance = b_bal + 1;
  1140. } else {
  1141. left->balance = a_bal + b_bal + 2;
  1142. }
  1143. node->balance = a_bal + 1;
  1144. }
  1145. return left;
  1146. }
  1147. #ifdef Q_TREE_DEBUG
  1148. static gint
  1149. q_tree_node_height(QTreeNode *node)
  1150. {
  1151. gint left_height;
  1152. gint right_height;
  1153. if (node) {
  1154. left_height = 0;
  1155. right_height = 0;
  1156. if (node->left_child) {
  1157. left_height = q_tree_node_height(node->left);
  1158. }
  1159. if (node->right_child) {
  1160. right_height = q_tree_node_height(node->right);
  1161. }
  1162. return MAX(left_height, right_height) + 1;
  1163. }
  1164. return 0;
  1165. }
  1166. static void q_tree_node_check(QTreeNode *node)
  1167. {
  1168. gint left_height;
  1169. gint right_height;
  1170. gint balance;
  1171. QTreeNode *tmp;
  1172. if (node) {
  1173. if (node->left_child) {
  1174. tmp = q_tree_node_previous(node);
  1175. g_assert(tmp->right == node);
  1176. }
  1177. if (node->right_child) {
  1178. tmp = q_tree_node_next(node);
  1179. g_assert(tmp->left == node);
  1180. }
  1181. left_height = 0;
  1182. right_height = 0;
  1183. if (node->left_child) {
  1184. left_height = q_tree_node_height(node->left);
  1185. }
  1186. if (node->right_child) {
  1187. right_height = q_tree_node_height(node->right);
  1188. }
  1189. balance = right_height - left_height;
  1190. g_assert(balance == node->balance);
  1191. if (node->left_child) {
  1192. q_tree_node_check(node->left);
  1193. }
  1194. if (node->right_child) {
  1195. q_tree_node_check(node->right);
  1196. }
  1197. }
  1198. }
  1199. #endif