ladybird/Userland/Libraries/LibC/search.cpp

127 lines
3.0 KiB
C++
Raw Normal View History

2021-09-27 01:50:51 +03:00
/*
* Copyright (c) 2021, the SerenityOS developers.
* Copyright (c) 2021, Tim Schumacher <timschumi@gmx.de>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Format.h>
#include <bits/search.h>
#include <search.h>
2022-04-01 20:58:27 +03:00
struct search_tree_node* new_tree_node(void const* key)
2021-09-27 01:50:51 +03:00
{
auto* node = static_cast<struct search_tree_node*>(malloc(sizeof(struct search_tree_node)));
if (!node)
return nullptr;
node->key = key;
node->left = nullptr;
node->right = nullptr;
return node;
}
void delete_node_recursive(struct search_tree_node* node)
{
if (!node)
return;
delete_node_recursive(node->left);
delete_node_recursive(node->right);
free(node);
}
extern "C" {
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/tsearch.html
2022-04-01 20:58:27 +03:00
void* tsearch(void const* key, void** rootp, int (*comparator)(void const*, void const*))
2021-09-27 01:50:51 +03:00
{
if (!rootp)
return nullptr;
if (!*rootp) {
*rootp = new_tree_node(key);
return *rootp;
}
auto node = static_cast<struct search_tree_node*>(*rootp);
while (node != nullptr) {
auto comp = comparator(key, node->key);
if (comp < 0 && node->left) {
node = node->left;
} else if (comp < 0 && !node->left) {
node->left = new_tree_node(key);
return node->left;
} else if (comp > 0 && node->right) {
node = node->right;
} else if (comp > 0 && !node->right) {
node->right = new_tree_node(key);
return node->right;
} else {
return node;
}
}
VERIFY_NOT_REACHED();
}
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/tfind.html
2022-04-01 20:58:27 +03:00
void* tfind(void const* key, void* const* rootp, int (*comparator)(void const*, void const*))
2021-09-27 01:50:51 +03:00
{
if (!rootp)
return nullptr;
auto node = static_cast<struct search_tree_node*>(*rootp);
while (node != nullptr) {
auto comp = comparator(key, node->key);
if (comp < 0)
node = node->left;
else if (comp > 0)
node = node->right;
else
return node;
}
return nullptr;
}
2021-10-14 21:46:26 +03:00
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/tdelete.html
2022-04-01 20:58:27 +03:00
void* tdelete(void const*, void**, int (*)(void const*, void const*))
2021-10-14 22:21:25 +03:00
{
dbgln("FIXME: Implement tdelete()");
return nullptr;
2021-10-14 22:21:25 +03:00
}
2022-04-01 20:58:27 +03:00
static void twalk_internal(const struct search_tree_node* node, void (*action)(void const*, VISIT, int), int depth)
2021-10-14 21:46:26 +03:00
{
if (!node)
return;
if (!node->right && !node->left) {
action(node, leaf, depth);
return;
}
action(node, preorder, depth);
twalk_internal(node->left, action, depth + 1);
action(node, postorder, depth);
twalk_internal(node->right, action, depth + 1);
action(node, endorder, depth);
}
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/twalk.html
2022-04-01 20:58:27 +03:00
void twalk(void const* rootp, void (*action)(void const*, VISIT, int))
2021-10-14 21:46:26 +03:00
{
auto node = static_cast<const struct search_tree_node*>(rootp);
twalk_internal(node, action, 0);
}
2021-09-27 01:50:51 +03:00
}