2024-07-05 12:56:03 -07:00
|
|
|
// Copyright 2024 Justine Alexandra Roberts Tunney
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for
|
|
|
|
// any purpose with or without fee is hereby granted, provided that the
|
|
|
|
// above copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
|
|
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
|
|
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
|
|
|
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
|
|
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
2024-07-05 23:13:20 -07:00
|
|
|
#include "libc/intrin/tree.h"
|
2024-07-05 12:56:03 -07:00
|
|
|
#include "libc/intrin/kprintf.h"
|
2024-07-05 23:13:20 -07:00
|
|
|
#include "libc/intrin/maps.h"
|
2024-07-05 12:56:03 -07:00
|
|
|
#include "libc/macros.internal.h"
|
2024-07-05 23:13:20 -07:00
|
|
|
#include "libc/mem/mem.h"
|
2024-07-05 12:56:03 -07:00
|
|
|
#include "libc/runtime/runtime.h"
|
|
|
|
#include "libc/stdio/rand.h"
|
|
|
|
|
2024-07-05 23:13:20 -07:00
|
|
|
#define NUMBER_CONTAINER(e) TREE_CONTAINER(struct number, elem, e)
|
2024-07-05 12:56:03 -07:00
|
|
|
|
2024-07-05 23:13:20 -07:00
|
|
|
void tree_checker(const struct Tree *node, const struct Tree *parent,
|
|
|
|
int black_count, int *black_height, tree_cmp_f *cmp) {
|
2024-07-05 12:56:03 -07:00
|
|
|
if (!node) {
|
|
|
|
// Leaf nodes are considered black
|
|
|
|
if (*black_height == -1) {
|
|
|
|
*black_height = black_count;
|
|
|
|
} else if (black_count != *black_height) {
|
|
|
|
// ILLEGAL TREE: Black height mismatch
|
|
|
|
__builtin_trap();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (node->parent != parent)
|
|
|
|
// ILLEGAL TREE: Parent link is incorrect
|
|
|
|
__builtin_trap();
|
|
|
|
if (parent) {
|
2024-07-05 23:13:20 -07:00
|
|
|
if (tree_get_left(parent) == node && cmp(parent, node) < 0)
|
2024-07-05 12:56:03 -07:00
|
|
|
// ILLEGAL TREE: Binary search property violated on left child
|
|
|
|
__builtin_trap();
|
|
|
|
if (parent->right == node && cmp(node, parent) < 0)
|
|
|
|
// ILLEGAL TREE: Binary search property violated on right child
|
|
|
|
__builtin_trap();
|
|
|
|
}
|
2024-07-05 23:13:20 -07:00
|
|
|
if (!tree_get_red(node)) {
|
2024-07-05 12:56:03 -07:00
|
|
|
black_count++;
|
2024-07-05 23:13:20 -07:00
|
|
|
} else if (parent && tree_get_red(parent)) {
|
2024-07-05 12:56:03 -07:00
|
|
|
// ILLEGAL TREE: Red node has red child
|
|
|
|
__builtin_trap();
|
|
|
|
}
|
2024-07-05 23:13:20 -07:00
|
|
|
tree_checker(tree_get_left(node), node, black_count, black_height, cmp);
|
|
|
|
tree_checker(node->right, node, black_count, black_height, cmp);
|
2024-07-05 12:56:03 -07:00
|
|
|
}
|
|
|
|
|
2024-07-05 23:13:20 -07:00
|
|
|
void tree_check(struct Tree *root, tree_cmp_f *cmp) {
|
2024-07-05 12:56:03 -07:00
|
|
|
if (root) {
|
2024-07-05 23:13:20 -07:00
|
|
|
if (tree_get_red(root))
|
2024-07-05 12:56:03 -07:00
|
|
|
// ILLEGAL TREE: root node must be black
|
|
|
|
__builtin_trap();
|
|
|
|
int black_height = -1;
|
2024-07-05 23:13:20 -07:00
|
|
|
tree_checker(root, 0, 0, &black_height, cmp);
|
2024-07-05 12:56:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct number {
|
2024-07-05 23:13:20 -07:00
|
|
|
long number;
|
|
|
|
struct Tree elem;
|
2024-07-05 12:56:03 -07:00
|
|
|
};
|
|
|
|
|
2024-07-05 23:13:20 -07:00
|
|
|
int number_search(const void *ra, const struct Tree *rb) {
|
|
|
|
long a = (long)ra;
|
|
|
|
const struct number *b = NUMBER_CONTAINER(rb);
|
|
|
|
return (a > b->number) - (a < b->number);
|
|
|
|
}
|
|
|
|
|
|
|
|
int number_compare(const struct Tree *ra, const struct Tree *rb) {
|
2024-07-05 12:56:03 -07:00
|
|
|
const struct number *a = NUMBER_CONTAINER(ra);
|
|
|
|
const struct number *b = NUMBER_CONTAINER(rb);
|
|
|
|
return (a->number > b->number) - (a->number < b->number);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct number *number_new(int number) {
|
2024-07-05 23:13:20 -07:00
|
|
|
struct number *res;
|
|
|
|
if ((res = malloc(sizeof(struct number))))
|
|
|
|
res->number = number;
|
2024-07-05 12:56:03 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2024-07-05 23:13:20 -07:00
|
|
|
struct Tree *tree = 0;
|
2024-07-05 12:56:03 -07:00
|
|
|
|
|
|
|
void print(void) {
|
2024-07-05 23:13:20 -07:00
|
|
|
for (struct Tree *e = tree_first(tree); e; e = tree_next(e))
|
2024-07-05 12:56:03 -07:00
|
|
|
kprintf("%3d", NUMBER_CONTAINER(e)->number);
|
|
|
|
kprintf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_reversed(void) {
|
2024-07-05 23:13:20 -07:00
|
|
|
for (struct Tree *e = tree_last(tree); e; e = tree_prev(e))
|
2024-07-05 12:56:03 -07:00
|
|
|
kprintf("%3d", NUMBER_CONTAINER(e)->number);
|
|
|
|
kprintf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void simple_test(void) {
|
2024-07-05 23:13:20 -07:00
|
|
|
static const long kNumba[] = {74, 53, 96, 70, 34, 95, 30, 2, 96, 46,
|
|
|
|
23, 2, 52, 0, 34, 94, 90, 95, 32, 65};
|
2024-07-05 12:56:03 -07:00
|
|
|
for (int i = 0; i < 20; ++i) {
|
|
|
|
int number = kNumba[i];
|
|
|
|
kprintf("%3d", number);
|
2024-07-05 23:13:20 -07:00
|
|
|
tree_insert(&tree, &number_new(number)->elem, number_compare);
|
|
|
|
tree_check(tree, number_compare);
|
2024-07-05 12:56:03 -07:00
|
|
|
}
|
|
|
|
kprintf("\n");
|
|
|
|
print();
|
|
|
|
print_reversed();
|
|
|
|
for (int i = 0; i < 20; ++i) {
|
2024-07-05 23:13:20 -07:00
|
|
|
tree_remove(&tree, tree_get(tree, (void *)kNumba[i], number_search));
|
|
|
|
tree_check(tree, number_compare);
|
2024-07-05 12:56:03 -07:00
|
|
|
print();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
ShowCrashReports();
|
2024-07-05 23:13:20 -07:00
|
|
|
tree_check(__maps.maps, __maps_compare);
|
|
|
|
kprintf("\n");
|
|
|
|
__print_maps(15);
|
|
|
|
kprintf("\n");
|
2024-07-05 12:56:03 -07:00
|
|
|
simple_test();
|
2024-07-05 23:13:20 -07:00
|
|
|
tree_check(__maps.maps, __maps_compare);
|
|
|
|
for (int i = 0; i < 100000; ++i)
|
|
|
|
tree_insert(&tree, &number_new(rand())->elem, number_compare);
|
|
|
|
tree_check(tree, number_compare);
|
|
|
|
tree_check(__maps.maps, __maps_compare);
|
|
|
|
__print_maps(15);
|
2024-07-05 12:56:03 -07:00
|
|
|
}
|