aboutsummaryrefslogtreecommitdiff
path: root/lib/std/treap.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-10-03 10:58:48 -0700
committerGitHub <noreply@github.com>2023-10-03 10:58:48 -0700
commit47f08605bd3ce01fb38e0d41408f4e09268fce09 (patch)
treeb087e2571002b40a8d150841bf8b3d53cdd9fde4 /lib/std/treap.zig
parent6734d2117e3520c1ef011609d0e2b2dd131a80aa (diff)
parent95f4c1532aa4fe7f0ec8f733f7e79c85d2c09b52 (diff)
downloadzig-47f08605bd3ce01fb38e0d41408f4e09268fce09.tar.gz
zig-47f08605bd3ce01fb38e0d41408f4e09268fce09.zip
Merge pull request #17383 from squeek502/gpa-optim-treap
GeneralPurposeAllocator: Considerably improve worst case performance
Diffstat (limited to 'lib/std/treap.zig')
-rw-r--r--lib/std/treap.zig53
1 files changed, 52 insertions, 1 deletions
diff --git a/lib/std/treap.zig b/lib/std/treap.zig
index 1c4541f96f..383dc1802a 100644
--- a/lib/std/treap.zig
+++ b/lib/std/treap.zig
@@ -225,7 +225,6 @@ pub fn Treap(comptime Key: type, comptime compareFn: anytype) type {
link.* = null;
// clean up after ourselves
- node.key = undefined;
node.priority = 0;
node.parent = null;
node.children = [_]?*Node{ null, null };
@@ -257,6 +256,48 @@ pub fn Treap(comptime Key: type, comptime compareFn: anytype) type {
assert(link.* == node);
link.* = target;
}
+
+ pub const InorderIterator = struct {
+ current: ?*Node,
+ previous: ?*Node = null,
+
+ pub fn next(it: *InorderIterator) ?*Node {
+ while (true) {
+ if (it.current) |current| {
+ const previous = it.previous;
+ it.previous = current;
+ if (previous == current.parent) {
+ if (current.children[0]) |left_child| {
+ it.current = left_child;
+ } else {
+ if (current.children[1]) |right_child| {
+ it.current = right_child;
+ } else {
+ it.current = current.parent;
+ }
+ return current;
+ }
+ } else if (previous == current.children[0]) {
+ if (current.children[1]) |right_child| {
+ it.current = right_child;
+ } else {
+ it.current = current.parent;
+ }
+ return current;
+ } else {
+ std.debug.assert(previous == current.children[1]);
+ it.current = current.parent;
+ }
+ } else {
+ return null;
+ }
+ }
+ }
+ };
+
+ pub fn inorderIterator(self: *Self) InorderIterator {
+ return .{ .current = self.root };
+ }
};
}
@@ -344,6 +385,16 @@ test "std.Treap: insert, find, replace, remove" {
try testing.expectEqual(entry.node, treap.getEntryForExisting(node).node);
}
+ // in-order iterator check
+ {
+ var it = treap.inorderIterator();
+ var last_key: u64 = 0;
+ while (it.next()) |node| {
+ try std.testing.expect(node.key >= last_key);
+ last_key = node.key;
+ }
+ }
+
// replace check
iter.reset();
while (iter.next()) |node| {