search.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. * Copyright (c) 2021, Tim Schumacher <timschumi@gmx.de>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Format.h>
  8. #include <bits/search.h>
  9. #include <search.h>
  10. struct search_tree_node* new_tree_node(const void* key)
  11. {
  12. auto* node = static_cast<struct search_tree_node*>(malloc(sizeof(struct search_tree_node)));
  13. if (!node)
  14. return nullptr;
  15. node->key = key;
  16. node->left = nullptr;
  17. node->right = nullptr;
  18. return node;
  19. }
  20. void delete_node_recursive(struct search_tree_node* node)
  21. {
  22. if (!node)
  23. return;
  24. delete_node_recursive(node->left);
  25. delete_node_recursive(node->right);
  26. free(node);
  27. }
  28. extern "C" {
  29. void* tsearch(const void* key, void** rootp, int (*comparator)(const void*, const void*))
  30. {
  31. if (!rootp)
  32. return nullptr;
  33. if (!*rootp) {
  34. *rootp = new_tree_node(key);
  35. return *rootp;
  36. }
  37. auto node = static_cast<struct search_tree_node*>(*rootp);
  38. while (node != nullptr) {
  39. auto comp = comparator(key, node->key);
  40. if (comp < 0 && node->left) {
  41. node = node->left;
  42. } else if (comp < 0 && !node->left) {
  43. node->left = new_tree_node(key);
  44. return node->left;
  45. } else if (comp > 0 && node->right) {
  46. node = node->right;
  47. } else if (comp > 0 && !node->right) {
  48. node->right = new_tree_node(key);
  49. return node->right;
  50. } else {
  51. return node;
  52. }
  53. }
  54. VERIFY_NOT_REACHED();
  55. }
  56. void* tfind(const void* key, void* const* rootp, int (*comparator)(const void*, const void*))
  57. {
  58. if (!rootp)
  59. return nullptr;
  60. auto node = static_cast<struct search_tree_node*>(*rootp);
  61. while (node != nullptr) {
  62. auto comp = comparator(key, node->key);
  63. if (comp < 0)
  64. node = node->left;
  65. else if (comp > 0)
  66. node = node->right;
  67. else
  68. return node;
  69. }
  70. return nullptr;
  71. }
  72. void* tdelete(const void*, void**, int (*)(const void*, const void*))
  73. {
  74. dbgln("FIXME: Implement tdelete()");
  75. TODO();
  76. }
  77. static void twalk_internal(const struct search_tree_node* node, void (*action)(const void*, VISIT, int), int depth)
  78. {
  79. if (!node)
  80. return;
  81. if (!node->right && !node->left) {
  82. action(node, leaf, depth);
  83. return;
  84. }
  85. action(node, preorder, depth);
  86. twalk_internal(node->left, action, depth + 1);
  87. action(node, postorder, depth);
  88. twalk_internal(node->right, action, depth + 1);
  89. action(node, endorder, depth);
  90. }
  91. void twalk(const void* rootp, void (*action)(const void*, VISIT, int))
  92. {
  93. auto node = static_cast<const struct search_tree_node*>(rootp);
  94. twalk_internal(node, action, 0);
  95. }
  96. }