rbtree: fix pointer usages

This commit is contained in:
Laytan Laats
2026-01-11 20:08:42 +01:00
parent d1fa640549
commit ec040c1fcb
2 changed files with 16 additions and 26 deletions

View File

@@ -91,7 +91,7 @@ destroy :: proc(t: ^$T/Tree($Key, $Value), call_on_remove: bool = true) {
}
}
len :: proc "contextless" (t: ^$T/Tree($Key, $Value)) -> (node_count: int) {
len :: proc "contextless" (t: $T/Tree($Key, $Value)) -> (node_count: int) {
return t._size
}
@@ -108,7 +108,7 @@ last :: proc "contextless" (t: ^$T/Tree($Key, $Value)) -> ^Node(Key, Value) {
}
// find finds the key in the tree, and returns the corresponding node, or nil iff the value is not present.
find :: proc(t: ^$T/Tree($Key, $Value), key: Key) -> (node: ^Node(Key, Value)) {
find :: proc(t: $T/Tree($Key, $Value), key: Key) -> (node: ^Node(Key, Value)) {
node = t._root
for node != nil {
switch t._cmp_fn(key, node.key) {
@@ -121,7 +121,7 @@ find :: proc(t: ^$T/Tree($Key, $Value), key: Key) -> (node: ^Node(Key, Value)) {
}
// find_value finds the key in the tree, and returns the corresponding value, or nil iff the value is not present.
find_value :: proc(t: ^$T/Tree($Key, $Value), key: Key) -> (value: Value, ok: bool) #optional_ok {
find_value :: proc(t: $T/Tree($Key, $Value), key: Key) -> (value: Value, ok: bool) #optional_ok {
if n := find(t, key); n != nil {
return n.value, true
}
@@ -166,7 +166,7 @@ remove :: proc {
// removal was successful. While the node's key + value will be left intact,
// the node itself will be freed via the tree's node allocator.
remove_key :: proc(t: ^$T/Tree($Key, $Value), key: Key, call_on_remove := true) -> bool {
n := find(t, key)
n := find(t^, key)
if n == nil {
return false // Key not found, nothing to do
}

View File

@@ -4,22 +4,15 @@ import rb "core:container/rbtree"
import "core:math/rand"
import "core:testing"
import "base:intrinsics"
import "core:mem"
import "core:slice"
import "core:log"
test_rbtree_integer :: proc(t: ^testing.T, $Key: typeid, $Value: typeid) {
track: mem.Tracking_Allocator
mem.tracking_allocator_init(&track, context.allocator)
track.bad_free_callback = mem.tracking_allocator_bad_free_callback_add_to_array
defer mem.tracking_allocator_destroy(&track)
context.allocator = mem.tracking_allocator(&track)
log.infof("Testing Red-Black Tree($Key=%v,$Value=%v) using random seed %v.", type_info_of(Key), type_info_of(Value), t.seed)
tree: rb.Tree(Key, Value)
rb.init(&tree)
testing.expect(t, rb.len(&tree) == 0, "empty: len should be 0")
testing.expect(t, rb.len(tree) == 0, "empty: len should be 0")
testing.expect(t, rb.first(&tree) == nil, "empty: first should be nil")
testing.expect(t, rb.last(&tree) == nil, "empty: last should be nil")
iter := rb.iterator(&tree, .Forward)
@@ -48,7 +41,7 @@ test_rbtree_integer :: proc(t: ^testing.T, $Key: typeid, $Value: typeid) {
}
entry_count := len(inserted_map)
testing.expect(t, rb.len(&tree) == entry_count, "insert: len after")
testing.expect(t, rb.len(tree) == entry_count, "insert: len after")
validate_rbtree(t, &tree)
first := rb.first(&tree)
@@ -58,8 +51,8 @@ test_rbtree_integer :: proc(t: ^testing.T, $Key: typeid, $Value: typeid) {
// Ensure that all entries can be found.
for k, v in inserted_map {
testing.expect(t, v == rb.find(&tree, k), "Find(): Node")
testing.expect(t, k == v.key, "Find(): Node key")
testing.expect(t, v == rb.find(tree, k), "Find(): Node")
testing.expect(t, k == v.key, "Find(): Node key")
}
// Test the forward/backward iterators.
@@ -97,17 +90,17 @@ test_rbtree_integer :: proc(t: ^testing.T, $Key: typeid, $Value: typeid) {
(^int)(user_data)^ -= 1
}
for k, i in inserted_keys {
node := rb.find(&tree, k)
node := rb.find(tree, k)
testing.expect(t, node != nil, "remove: find (pre)")
ok := rb.remove(&tree, k)
testing.expect(t, ok, "remove: succeeds")
testing.expect(t, entry_count - (i + 1) == rb.len(&tree), "remove: len (post)")
testing.expect(t, entry_count - (i + 1) == rb.len(tree), "remove: len (post)")
validate_rbtree(t, &tree)
testing.expect(t, nil == rb.find(&tree, k), "remove: find (post")
testing.expect(t, nil == rb.find(tree, k), "remove: find (post")
}
testing.expect(t, rb.len(&tree) == 0, "remove: len should be 0")
testing.expect(t, rb.len(tree) == 0, "remove: len should be 0")
testing.expectf(t, callback_count == 0, "remove: on_remove should've been called %v times, it was %v", entry_count, callback_count)
testing.expect(t, rb.first(&tree) == nil, "remove: first should be nil")
testing.expect(t, rb.last(&tree) == nil, "remove: last should be nil")
@@ -129,28 +122,25 @@ test_rbtree_integer :: proc(t: ^testing.T, $Key: typeid, $Value: typeid) {
ok = rb.iterator_remove(&iter)
testing.expect(t, !ok, "iterator/remove: redundant removes should fail")
testing.expect(t, rb.find(&tree, k) == nil, "iterator/remove: node should be gone")
testing.expect(t, rb.find(tree, k) == nil, "iterator/remove: node should be gone")
testing.expect(t, rb.iterator_get(&iter) == nil, "iterator/remove: get should return nil")
// Ensure that iterator_next still works.
node, ok = rb.iterator_next(&iter)
testing.expect(t, ok == (rb.len(&tree) > 0), "iterator/remove: next should return false")
testing.expect(t, ok == (rb.len(tree) > 0), "iterator/remove: next should return false")
testing.expect(t, node == rb.first(&tree), "iterator/remove: next should return first")
validate_rbtree(t, &tree)
}
testing.expect(t, rb.len(&tree) == entry_count - 1, "iterator/remove: len should drop by 1")
testing.expect(t, rb.len(tree) == entry_count - 1, "iterator/remove: len should drop by 1")
rb.destroy(&tree)
testing.expect(t, rb.len(&tree) == 0, "destroy: len should be 0")
testing.expect(t, rb.len(tree) == 0, "destroy: len should be 0")
testing.expectf(t, callback_count == 0, "remove: on_remove should've been called %v times, it was %v", entry_count, callback_count)
// print_tree_node(tree._root)
delete(inserted_map)
delete(inserted_keys)
testing.expectf(t, len(track.allocation_map) == 0, "Expected 0 leaks, have %v", len(track.allocation_map))
testing.expectf(t, len(track.bad_free_array) == 0, "Expected 0 bad frees, have %v", len(track.bad_free_array))
return
}
@(test)