2020-01-19 18:05:38 +00:00
|
|
|
const std = @import("std");
|
2018-08-09 02:52:46 +01:00
|
|
|
const assert = std.debug.assert;
|
2019-02-08 23:18:47 +00:00
|
|
|
const testing = std.testing;
|
2020-01-01 23:08:40 +00:00
|
|
|
const Order = std.math.Order;
|
2018-08-05 23:11:29 +01:00
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const Color = enum(u1) {
|
2018-08-05 23:11:29 +01:00
|
|
|
Black,
|
|
|
|
Red,
|
|
|
|
};
|
|
|
|
const Red = Color.Red;
|
|
|
|
const Black = Color.Black;
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const ReplaceError = error{NotEqual};
|
2020-01-19 19:35:56 +00:00
|
|
|
const SortError = error{NotUnique}; // The new comparison function results in duplicates.
|
2018-08-05 23:11:29 +01:00
|
|
|
|
|
|
|
/// Insert this into your struct that you want to add to a red-black tree.
|
|
|
|
/// Do not use a pointer. Turn the *rb.Node results of the functions in rb
|
|
|
|
/// (after resolving optionals) to your structure using @fieldParentPtr(). Example:
|
|
|
|
///
|
|
|
|
/// const Number = struct {
|
|
|
|
/// node: rb.Node,
|
|
|
|
/// value: i32,
|
|
|
|
/// };
|
2018-09-02 06:23:34 +01:00
|
|
|
/// fn number(node: *rb.Node) Number {
|
2018-08-05 23:11:29 +01:00
|
|
|
/// return @fieldParentPtr(Number, "node", node);
|
|
|
|
/// }
|
2018-11-13 13:08:37 +00:00
|
|
|
pub const Node = struct {
|
2018-08-05 23:11:29 +01:00
|
|
|
left: ?*Node,
|
|
|
|
right: ?*Node,
|
2018-08-28 00:25:18 +01:00
|
|
|
|
|
|
|
/// parent | color
|
|
|
|
parent_and_color: usize,
|
2018-08-05 23:11:29 +01:00
|
|
|
|
|
|
|
pub fn next(constnode: *Node) ?*Node {
|
|
|
|
var node = constnode;
|
|
|
|
|
|
|
|
if (node.right) |right| {
|
|
|
|
var n = right;
|
|
|
|
while (n.left) |left|
|
|
|
|
n = left;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
2018-11-16 04:09:21 +00:00
|
|
|
var parent = node.getParent();
|
2018-08-05 23:11:29 +01:00
|
|
|
if (parent) |p| {
|
|
|
|
if (node != p.right)
|
|
|
|
return p;
|
|
|
|
node = p;
|
|
|
|
} else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn prev(constnode: *Node) ?*Node {
|
|
|
|
var node = constnode;
|
|
|
|
|
|
|
|
if (node.left) |left| {
|
|
|
|
var n = left;
|
|
|
|
while (n.right) |right|
|
|
|
|
n = right;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
2018-11-16 04:09:21 +00:00
|
|
|
var parent = node.getParent();
|
2018-08-05 23:11:29 +01:00
|
|
|
if (parent) |p| {
|
|
|
|
if (node != p.left)
|
|
|
|
return p;
|
|
|
|
node = p;
|
|
|
|
} else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
pub fn isRoot(node: *Node) bool {
|
|
|
|
return node.getParent() == null;
|
2018-08-05 23:11:29 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
fn isRed(node: *Node) bool {
|
|
|
|
return node.getColor() == Red;
|
2018-08-05 23:11:29 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
fn isBlack(node: *Node) bool {
|
|
|
|
return node.getColor() == Black;
|
2018-08-05 23:11:29 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
fn setParent(node: *Node, parent: ?*Node) void {
|
2018-08-05 23:11:29 +01:00
|
|
|
node.parent_and_color = @ptrToInt(parent) | (node.parent_and_color & 1);
|
|
|
|
}
|
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
fn getParent(node: *Node) ?*Node {
|
2018-08-05 23:11:29 +01:00
|
|
|
const mask: usize = 1;
|
|
|
|
comptime {
|
|
|
|
assert(@alignOf(*Node) >= 2);
|
|
|
|
}
|
2019-07-28 13:38:11 +01:00
|
|
|
const maybe_ptr = node.parent_and_color & ~mask;
|
|
|
|
return if (maybe_ptr == 0) null else @intToPtr(*Node, maybe_ptr);
|
2018-08-05 23:11:29 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
fn setColor(node: *Node, color: Color) void {
|
2018-08-05 23:11:29 +01:00
|
|
|
const mask: usize = 1;
|
|
|
|
node.parent_and_color = (node.parent_and_color & ~mask) | @enumToInt(color);
|
|
|
|
}
|
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
fn getColor(node: *Node) Color {
|
2018-08-05 23:11:29 +01:00
|
|
|
return @intToEnum(Color, @intCast(u1, node.parent_and_color & 1));
|
|
|
|
}
|
|
|
|
|
2018-11-16 15:23:41 +00:00
|
|
|
fn setChild(node: *Node, child: ?*Node, is_left: bool) void {
|
2018-08-05 23:11:29 +01:00
|
|
|
if (is_left) {
|
|
|
|
node.left = child;
|
|
|
|
} else {
|
|
|
|
node.right = child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
fn getFirst(nodeconst: *Node) *Node {
|
2018-08-05 23:11:29 +01:00
|
|
|
var node = nodeconst;
|
|
|
|
while (node.left) |left| {
|
|
|
|
node = left;
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-01-30 17:01:51 +00:00
|
|
|
fn getLast(nodeconst: *Node) *Node {
|
|
|
|
var node = nodeconst;
|
2018-08-05 23:11:29 +01:00
|
|
|
while (node.right) |right| {
|
|
|
|
node = right;
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
pub const Tree = struct {
|
2018-08-05 23:11:29 +01:00
|
|
|
root: ?*Node,
|
2020-01-19 18:08:05 +00:00
|
|
|
compareFn: fn (*Node, *Node, *Tree) Order,
|
2018-08-05 23:11:29 +01:00
|
|
|
|
2020-01-19 18:10:21 +00:00
|
|
|
/// Re-sorts a tree with a new compare function
|
2020-01-19 19:35:56 +00:00
|
|
|
pub fn sort(tree: *Tree, newCompareFn: fn (*Node, *Node, *Tree) Order) SortError!void {
|
2020-01-19 18:10:21 +00:00
|
|
|
var newTree = Tree.init(newCompareFn);
|
|
|
|
var node: *Node = undefined;
|
|
|
|
while (true) {
|
|
|
|
node = tree.first() orelse break;
|
|
|
|
tree.remove(node);
|
|
|
|
if (newTree.insert(node) != null) {
|
|
|
|
return error.NotUnique; // EEXISTS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tree.* = newTree;
|
|
|
|
}
|
|
|
|
|
2018-08-09 02:52:46 +01:00
|
|
|
/// If you have a need for a version that caches this, please file a bug.
|
2018-08-05 23:11:29 +01:00
|
|
|
pub fn first(tree: *Tree) ?*Node {
|
|
|
|
var node: *Node = tree.root orelse return null;
|
|
|
|
|
|
|
|
while (node.left) |left| {
|
|
|
|
node = left;
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn last(tree: *Tree) ?*Node {
|
|
|
|
var node: *Node = tree.root orelse return null;
|
|
|
|
|
|
|
|
while (node.right) |right| {
|
|
|
|
node = right;
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Duplicate keys are not allowed. The item with the same key already in the
|
|
|
|
/// tree will be returned, and the item will not be inserted.
|
|
|
|
pub fn insert(tree: *Tree, node_const: *Node) ?*Node {
|
|
|
|
var node = node_const;
|
|
|
|
var maybe_key: ?*Node = undefined;
|
|
|
|
var maybe_parent: ?*Node = undefined;
|
|
|
|
var is_left: bool = undefined;
|
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
maybe_key = doLookup(node, tree, &maybe_parent, &is_left);
|
2018-08-05 23:11:29 +01:00
|
|
|
if (maybe_key) |key| {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
node.left = null;
|
|
|
|
node.right = null;
|
2018-11-16 04:09:21 +00:00
|
|
|
node.setColor(Red);
|
|
|
|
node.setParent(maybe_parent);
|
2018-08-05 23:11:29 +01:00
|
|
|
|
|
|
|
if (maybe_parent) |parent| {
|
2018-11-16 15:23:41 +00:00
|
|
|
parent.setChild(node, is_left);
|
2018-08-05 23:11:29 +01:00
|
|
|
} else {
|
|
|
|
tree.root = node;
|
|
|
|
}
|
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
while (node.getParent()) |*parent| {
|
|
|
|
if (parent.*.isBlack())
|
2018-08-05 23:11:29 +01:00
|
|
|
break;
|
2018-08-28 00:25:18 +01:00
|
|
|
// the root is always black
|
2018-11-16 04:09:21 +00:00
|
|
|
var grandpa = parent.*.getParent() orelse unreachable;
|
2018-08-05 23:11:29 +01:00
|
|
|
|
|
|
|
if (parent.* == grandpa.left) {
|
|
|
|
var maybe_uncle = grandpa.right;
|
|
|
|
|
|
|
|
if (maybe_uncle) |uncle| {
|
2018-11-16 04:09:21 +00:00
|
|
|
if (uncle.isBlack())
|
2018-08-05 23:11:29 +01:00
|
|
|
break;
|
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
parent.*.setColor(Black);
|
|
|
|
uncle.setColor(Black);
|
|
|
|
grandpa.setColor(Red);
|
2018-08-05 23:11:29 +01:00
|
|
|
node = grandpa;
|
|
|
|
} else {
|
|
|
|
if (node == parent.*.right) {
|
2018-11-16 04:09:21 +00:00
|
|
|
rotateLeft(parent.*, tree);
|
2018-08-05 23:11:29 +01:00
|
|
|
node = parent.*;
|
2018-11-16 04:09:21 +00:00
|
|
|
parent.* = node.getParent().?; // Just rotated
|
2018-08-05 23:11:29 +01:00
|
|
|
}
|
2018-11-16 04:09:21 +00:00
|
|
|
parent.*.setColor(Black);
|
|
|
|
grandpa.setColor(Red);
|
|
|
|
rotateRight(grandpa, tree);
|
2018-08-05 23:11:29 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var maybe_uncle = grandpa.left;
|
2018-08-28 00:25:18 +01:00
|
|
|
|
2018-08-05 23:11:29 +01:00
|
|
|
if (maybe_uncle) |uncle| {
|
2018-11-16 04:09:21 +00:00
|
|
|
if (uncle.isBlack())
|
2018-08-05 23:11:29 +01:00
|
|
|
break;
|
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
parent.*.setColor(Black);
|
|
|
|
uncle.setColor(Black);
|
|
|
|
grandpa.setColor(Red);
|
2018-08-05 23:11:29 +01:00
|
|
|
node = grandpa;
|
|
|
|
} else {
|
|
|
|
if (node == parent.*.left) {
|
2018-11-16 04:09:21 +00:00
|
|
|
rotateRight(parent.*, tree);
|
2018-08-05 23:11:29 +01:00
|
|
|
node = parent.*;
|
2018-11-16 04:09:21 +00:00
|
|
|
parent.* = node.getParent().?; // Just rotated
|
2018-08-05 23:11:29 +01:00
|
|
|
}
|
2018-11-16 04:09:21 +00:00
|
|
|
parent.*.setColor(Black);
|
|
|
|
grandpa.setColor(Red);
|
|
|
|
rotateLeft(grandpa, tree);
|
2018-08-05 23:11:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// This was an insert, there is at least one node.
|
2018-11-16 04:09:21 +00:00
|
|
|
tree.root.?.setColor(Black);
|
2018-08-05 23:11:29 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-07-31 14:17:09 +01:00
|
|
|
/// lookup searches for the value of key, using binary search. It will
|
|
|
|
/// return a pointer to the node if it is there, otherwise it will return null.
|
|
|
|
/// Complexity guaranteed O(log n), where n is the number of nodes book-kept
|
|
|
|
/// by tree.
|
2018-08-05 23:11:29 +01:00
|
|
|
pub fn lookup(tree: *Tree, key: *Node) ?*Node {
|
2019-07-31 14:17:09 +01:00
|
|
|
var parent: ?*Node = undefined;
|
2018-08-05 23:11:29 +01:00
|
|
|
var is_left: bool = undefined;
|
2018-11-16 04:09:21 +00:00
|
|
|
return doLookup(key, tree, &parent, &is_left);
|
2018-08-05 23:11:29 +01:00
|
|
|
}
|
|
|
|
|
2020-01-19 18:10:21 +00:00
|
|
|
/// If node is not part of tree, behavior is undefined.
|
2018-08-05 23:11:29 +01:00
|
|
|
pub fn remove(tree: *Tree, nodeconst: *Node) void {
|
|
|
|
var node = nodeconst;
|
|
|
|
// as this has the same value as node, it is unsafe to access node after newnode
|
|
|
|
var newnode: ?*Node = nodeconst;
|
2018-11-16 04:09:21 +00:00
|
|
|
var maybe_parent: ?*Node = node.getParent();
|
2018-08-05 23:11:29 +01:00
|
|
|
var color: Color = undefined;
|
|
|
|
var next: *Node = undefined;
|
|
|
|
|
|
|
|
// This clause is to avoid optionals
|
|
|
|
if (node.left == null and node.right == null) {
|
|
|
|
if (maybe_parent) |parent| {
|
2018-11-16 15:23:41 +00:00
|
|
|
parent.setChild(null, parent.left == node);
|
2018-08-05 23:11:29 +01:00
|
|
|
} else
|
|
|
|
tree.root = null;
|
2018-11-16 04:09:21 +00:00
|
|
|
color = node.getColor();
|
2018-08-05 23:11:29 +01:00
|
|
|
newnode = null;
|
|
|
|
} else {
|
|
|
|
if (node.left == null) {
|
|
|
|
next = node.right.?; // Not both null as per above
|
|
|
|
} else if (node.right == null) {
|
2018-08-28 00:25:18 +01:00
|
|
|
next = node.left.?; // Not both null as per above
|
2018-08-05 23:11:29 +01:00
|
|
|
} else
|
2018-11-16 04:09:21 +00:00
|
|
|
next = node.right.?.getFirst(); // Just checked for null above
|
2018-08-05 23:11:29 +01:00
|
|
|
|
|
|
|
if (maybe_parent) |parent| {
|
2018-11-16 15:23:41 +00:00
|
|
|
parent.setChild(next, parent.left == node);
|
2018-08-05 23:11:29 +01:00
|
|
|
} else
|
|
|
|
tree.root = next;
|
|
|
|
|
|
|
|
if (node.left != null and node.right != null) {
|
|
|
|
const left = node.left.?;
|
|
|
|
const right = node.right.?;
|
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
color = next.getColor();
|
|
|
|
next.setColor(node.getColor());
|
2018-08-05 23:11:29 +01:00
|
|
|
|
|
|
|
next.left = left;
|
2018-11-16 04:09:21 +00:00
|
|
|
left.setParent(next);
|
2018-08-05 23:11:29 +01:00
|
|
|
|
|
|
|
if (next != right) {
|
2018-11-16 04:09:21 +00:00
|
|
|
var parent = next.getParent().?; // Was traversed via child node (right/left)
|
|
|
|
next.setParent(node.getParent());
|
2018-08-05 23:11:29 +01:00
|
|
|
|
|
|
|
newnode = next.right;
|
|
|
|
parent.left = node;
|
|
|
|
|
|
|
|
next.right = right;
|
2018-11-16 04:09:21 +00:00
|
|
|
right.setParent(next);
|
2018-08-05 23:11:29 +01:00
|
|
|
} else {
|
2018-11-16 04:09:21 +00:00
|
|
|
next.setParent(maybe_parent);
|
2018-08-05 23:11:29 +01:00
|
|
|
maybe_parent = next;
|
|
|
|
newnode = next.right;
|
|
|
|
}
|
|
|
|
} else {
|
2018-11-16 04:09:21 +00:00
|
|
|
color = node.getColor();
|
2018-08-05 23:11:29 +01:00
|
|
|
newnode = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newnode) |n|
|
2018-11-16 04:09:21 +00:00
|
|
|
n.setParent(maybe_parent);
|
2018-08-05 23:11:29 +01:00
|
|
|
|
|
|
|
if (color == Red)
|
|
|
|
return;
|
|
|
|
if (newnode) |n| {
|
2018-11-16 04:09:21 +00:00
|
|
|
n.setColor(Black);
|
2018-08-05 23:11:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (node == tree.root) {
|
|
|
|
// If not root, there must be parent
|
|
|
|
var parent = maybe_parent.?;
|
|
|
|
if (node == parent.left) {
|
|
|
|
var sibling = parent.right.?; // Same number of black nodes.
|
2018-08-28 00:25:18 +01:00
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
if (sibling.isRed()) {
|
|
|
|
sibling.setColor(Black);
|
|
|
|
parent.setColor(Red);
|
|
|
|
rotateLeft(parent, tree);
|
2018-08-05 23:11:29 +01:00
|
|
|
sibling = parent.right.?; // Just rotated
|
|
|
|
}
|
2018-11-16 04:09:21 +00:00
|
|
|
if ((if (sibling.left) |n| n.isBlack() else true) and
|
|
|
|
(if (sibling.right) |n| n.isBlack() else true))
|
2018-08-28 00:25:18 +01:00
|
|
|
{
|
2018-11-16 04:09:21 +00:00
|
|
|
sibling.setColor(Red);
|
2018-08-05 23:11:29 +01:00
|
|
|
node = parent;
|
2018-11-16 04:09:21 +00:00
|
|
|
maybe_parent = parent.getParent();
|
2018-08-05 23:11:29 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-11-16 04:09:21 +00:00
|
|
|
if (if (sibling.right) |n| n.isBlack() else true) {
|
|
|
|
sibling.left.?.setColor(Black); // Same number of black nodes.
|
|
|
|
sibling.setColor(Red);
|
|
|
|
rotateRight(sibling, tree);
|
2018-08-05 23:11:29 +01:00
|
|
|
sibling = parent.right.?; // Just rotated
|
|
|
|
}
|
2018-11-16 04:09:21 +00:00
|
|
|
sibling.setColor(parent.getColor());
|
|
|
|
parent.setColor(Black);
|
|
|
|
sibling.right.?.setColor(Black); // Same number of black nodes.
|
|
|
|
rotateLeft(parent, tree);
|
2018-08-05 23:11:29 +01:00
|
|
|
newnode = tree.root;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
var sibling = parent.left.?; // Same number of black nodes.
|
2018-08-28 00:25:18 +01:00
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
if (sibling.isRed()) {
|
|
|
|
sibling.setColor(Black);
|
|
|
|
parent.setColor(Red);
|
|
|
|
rotateRight(parent, tree);
|
2018-08-05 23:11:29 +01:00
|
|
|
sibling = parent.left.?; // Just rotated
|
|
|
|
}
|
2018-11-16 04:09:21 +00:00
|
|
|
if ((if (sibling.left) |n| n.isBlack() else true) and
|
|
|
|
(if (sibling.right) |n| n.isBlack() else true))
|
2018-08-28 00:25:18 +01:00
|
|
|
{
|
2018-11-16 04:09:21 +00:00
|
|
|
sibling.setColor(Red);
|
2018-08-05 23:11:29 +01:00
|
|
|
node = parent;
|
2018-11-16 04:09:21 +00:00
|
|
|
maybe_parent = parent.getParent();
|
2018-08-05 23:11:29 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-11-16 04:09:21 +00:00
|
|
|
if (if (sibling.left) |n| n.isBlack() else true) {
|
|
|
|
sibling.right.?.setColor(Black); // Same number of black nodes
|
|
|
|
sibling.setColor(Red);
|
|
|
|
rotateLeft(sibling, tree);
|
2018-08-05 23:11:29 +01:00
|
|
|
sibling = parent.left.?; // Just rotated
|
|
|
|
}
|
2018-11-16 04:09:21 +00:00
|
|
|
sibling.setColor(parent.getColor());
|
|
|
|
parent.setColor(Black);
|
|
|
|
sibling.left.?.setColor(Black); // Same number of black nodes
|
|
|
|
rotateRight(parent, tree);
|
2018-08-05 23:11:29 +01:00
|
|
|
newnode = tree.root;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
if (node.isRed())
|
2018-08-05 23:11:29 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newnode) |n|
|
2018-11-16 04:09:21 +00:00
|
|
|
n.setColor(Black);
|
2018-08-05 23:11:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This is a shortcut to avoid removing and re-inserting an item with the same key.
|
|
|
|
pub fn replace(tree: *Tree, old: *Node, newconst: *Node) !void {
|
|
|
|
var new = newconst;
|
|
|
|
|
|
|
|
// I assume this can get optimized out if the caller already knows.
|
2020-01-19 18:08:05 +00:00
|
|
|
if (tree.compareFn(old, new, tree) != .eq) return ReplaceError.NotEqual;
|
2018-08-05 23:11:29 +01:00
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
if (old.getParent()) |parent| {
|
2018-11-16 15:23:41 +00:00
|
|
|
parent.setChild(new, parent.left == old);
|
2018-08-05 23:11:29 +01:00
|
|
|
} else
|
|
|
|
tree.root = new;
|
|
|
|
|
|
|
|
if (old.left) |left|
|
2018-11-16 04:09:21 +00:00
|
|
|
left.setParent(new);
|
2018-08-05 23:11:29 +01:00
|
|
|
if (old.right) |right|
|
2018-11-16 04:09:21 +00:00
|
|
|
right.setParent(new);
|
2018-08-05 23:11:29 +01:00
|
|
|
|
|
|
|
new.* = old.*;
|
|
|
|
}
|
|
|
|
|
2020-01-19 18:08:05 +00:00
|
|
|
pub fn init(f: fn (*Node, *Node, *Tree) Order) Tree {
|
|
|
|
return Tree{
|
|
|
|
.root = null,
|
|
|
|
.compareFn = f,
|
|
|
|
};
|
2018-08-05 23:11:29 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
fn rotateLeft(node: *Node, tree: *Tree) void {
|
2018-08-05 23:11:29 +01:00
|
|
|
var p: *Node = node;
|
|
|
|
var q: *Node = node.right orelse unreachable;
|
|
|
|
var parent: *Node = undefined;
|
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
if (!p.isRoot()) {
|
|
|
|
parent = p.getParent().?;
|
2018-08-05 23:11:29 +01:00
|
|
|
if (parent.left == p) {
|
|
|
|
parent.left = q;
|
|
|
|
} else {
|
|
|
|
parent.right = q;
|
|
|
|
}
|
2018-11-16 04:09:21 +00:00
|
|
|
q.setParent(parent);
|
2018-08-05 23:11:29 +01:00
|
|
|
} else {
|
|
|
|
tree.root = q;
|
2018-11-16 04:09:21 +00:00
|
|
|
q.setParent(null);
|
2018-08-05 23:11:29 +01:00
|
|
|
}
|
2018-11-16 04:09:21 +00:00
|
|
|
p.setParent(q);
|
2018-08-05 23:11:29 +01:00
|
|
|
|
|
|
|
p.right = q.left;
|
|
|
|
if (p.right) |right| {
|
2018-11-16 04:09:21 +00:00
|
|
|
right.setParent(p);
|
2018-08-05 23:11:29 +01:00
|
|
|
}
|
|
|
|
q.left = p;
|
|
|
|
}
|
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
fn rotateRight(node: *Node, tree: *Tree) void {
|
2018-08-05 23:11:29 +01:00
|
|
|
var p: *Node = node;
|
|
|
|
var q: *Node = node.left orelse unreachable;
|
|
|
|
var parent: *Node = undefined;
|
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
if (!p.isRoot()) {
|
|
|
|
parent = p.getParent().?;
|
2018-08-05 23:11:29 +01:00
|
|
|
if (parent.left == p) {
|
|
|
|
parent.left = q;
|
|
|
|
} else {
|
|
|
|
parent.right = q;
|
|
|
|
}
|
2018-11-16 04:09:21 +00:00
|
|
|
q.setParent(parent);
|
2018-08-05 23:11:29 +01:00
|
|
|
} else {
|
|
|
|
tree.root = q;
|
2018-11-16 04:09:21 +00:00
|
|
|
q.setParent(null);
|
2018-08-05 23:11:29 +01:00
|
|
|
}
|
2018-11-16 04:09:21 +00:00
|
|
|
p.setParent(q);
|
2018-08-05 23:11:29 +01:00
|
|
|
|
|
|
|
p.left = q.right;
|
|
|
|
if (p.left) |left| {
|
2018-11-16 04:09:21 +00:00
|
|
|
left.setParent(p);
|
2018-08-05 23:11:29 +01:00
|
|
|
}
|
|
|
|
q.right = p;
|
|
|
|
}
|
|
|
|
|
2018-11-16 04:09:21 +00:00
|
|
|
fn doLookup(key: *Node, tree: *Tree, pparent: *?*Node, is_left: *bool) ?*Node {
|
2018-08-05 23:11:29 +01:00
|
|
|
var maybe_node: ?*Node = tree.root;
|
|
|
|
|
|
|
|
pparent.* = null;
|
|
|
|
is_left.* = false;
|
|
|
|
|
|
|
|
while (maybe_node) |node| {
|
2020-01-19 18:08:05 +00:00
|
|
|
const res = tree.compareFn(node, key, tree);
|
2020-01-01 23:08:40 +00:00
|
|
|
if (res == .eq) {
|
2018-08-05 23:11:29 +01:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
pparent.* = node;
|
2020-01-01 23:08:40 +00:00
|
|
|
switch (res) {
|
|
|
|
.gt => {
|
|
|
|
is_left.* = true;
|
|
|
|
maybe_node = node.left;
|
|
|
|
},
|
|
|
|
.lt => {
|
|
|
|
is_left.* = false;
|
|
|
|
maybe_node = node.right;
|
|
|
|
},
|
|
|
|
.eq => unreachable, // handled above
|
2018-08-05 23:11:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const testNumber = struct {
|
2018-08-05 23:11:29 +01:00
|
|
|
node: Node,
|
|
|
|
value: usize,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn testGetNumber(node: *Node) *testNumber {
|
|
|
|
return @fieldParentPtr(testNumber, "node", node);
|
|
|
|
}
|
|
|
|
|
2020-01-19 18:08:05 +00:00
|
|
|
fn testCompare(l: *Node, r: *Node, contextIgnored: *Tree) Order {
|
2018-08-05 23:11:29 +01:00
|
|
|
var left = testGetNumber(l);
|
|
|
|
var right = testGetNumber(r);
|
|
|
|
|
|
|
|
if (left.value < right.value) {
|
2020-01-01 23:08:40 +00:00
|
|
|
return .lt;
|
2018-08-05 23:11:29 +01:00
|
|
|
} else if (left.value == right.value) {
|
2020-01-01 23:08:40 +00:00
|
|
|
return .eq;
|
2018-08-05 23:11:29 +01:00
|
|
|
} else if (left.value > right.value) {
|
2020-01-01 23:08:40 +00:00
|
|
|
return .gt;
|
2018-08-05 23:11:29 +01:00
|
|
|
}
|
|
|
|
unreachable;
|
|
|
|
}
|
|
|
|
|
2020-01-19 18:10:21 +00:00
|
|
|
fn testCompareReverse(l: *Node, r: *Node, contextIgnored: *Tree) Order {
|
|
|
|
return testCompare(r, l, contextIgnored);
|
|
|
|
}
|
|
|
|
|
2018-08-09 02:52:46 +01:00
|
|
|
test "rb" {
|
2019-09-22 04:55:56 +01:00
|
|
|
if (@import("builtin").arch == .aarch64) {
|
|
|
|
// TODO https://github.com/ziglang/zig/issues/3288
|
|
|
|
return error.SkipZigTest;
|
|
|
|
}
|
|
|
|
|
2020-01-19 18:08:05 +00:00
|
|
|
var tree = Tree.init(testCompare);
|
2018-08-05 23:11:29 +01:00
|
|
|
var ns: [10]testNumber = undefined;
|
|
|
|
ns[0].value = 42;
|
|
|
|
ns[1].value = 41;
|
|
|
|
ns[2].value = 40;
|
|
|
|
ns[3].value = 39;
|
|
|
|
ns[4].value = 38;
|
|
|
|
ns[5].value = 39;
|
|
|
|
ns[6].value = 3453;
|
|
|
|
ns[7].value = 32345;
|
|
|
|
ns[8].value = 392345;
|
|
|
|
ns[9].value = 4;
|
|
|
|
|
|
|
|
var dup: testNumber = undefined;
|
|
|
|
dup.value = 32345;
|
|
|
|
|
|
|
|
_ = tree.insert(&ns[1].node);
|
|
|
|
_ = tree.insert(&ns[2].node);
|
|
|
|
_ = tree.insert(&ns[3].node);
|
|
|
|
_ = tree.insert(&ns[4].node);
|
|
|
|
_ = tree.insert(&ns[5].node);
|
|
|
|
_ = tree.insert(&ns[6].node);
|
|
|
|
_ = tree.insert(&ns[7].node);
|
|
|
|
_ = tree.insert(&ns[8].node);
|
|
|
|
_ = tree.insert(&ns[9].node);
|
|
|
|
tree.remove(&ns[3].node);
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(tree.insert(&dup.node) == &ns[7].node);
|
2018-08-05 23:11:29 +01:00
|
|
|
try tree.replace(&ns[7].node, &dup.node);
|
|
|
|
|
|
|
|
var num: *testNumber = undefined;
|
|
|
|
num = testGetNumber(tree.first().?);
|
|
|
|
while (num.node.next() != null) {
|
2019-02-08 23:18:47 +00:00
|
|
|
testing.expect(testGetNumber(num.node.next().?).value > num.value);
|
2018-08-05 23:11:29 +01:00
|
|
|
num = testGetNumber(num.node.next().?);
|
|
|
|
}
|
|
|
|
}
|
2019-07-31 14:17:09 +01:00
|
|
|
|
|
|
|
test "inserting and looking up" {
|
2020-01-19 18:08:05 +00:00
|
|
|
var tree = Tree.init(testCompare);
|
2019-07-31 14:17:09 +01:00
|
|
|
var number: testNumber = undefined;
|
|
|
|
number.value = 1000;
|
|
|
|
_ = tree.insert(&number.node);
|
|
|
|
var dup: testNumber = undefined;
|
|
|
|
//Assert that tuples with identical value fields finds the same pointer
|
|
|
|
dup.value = 1000;
|
|
|
|
assert(tree.lookup(&dup.node) == &number.node);
|
|
|
|
//Assert that tuples with identical values do not clobber when inserted.
|
|
|
|
_ = tree.insert(&dup.node);
|
|
|
|
assert(tree.lookup(&dup.node) == &number.node);
|
|
|
|
assert(tree.lookup(&number.node) != &dup.node);
|
|
|
|
assert(testGetNumber(tree.lookup(&dup.node).?).value == testGetNumber(&dup.node).value);
|
|
|
|
//Assert that if looking for a non-existing value, return null.
|
|
|
|
var non_existing_value: testNumber = undefined;
|
|
|
|
non_existing_value.value = 1234;
|
|
|
|
assert(tree.lookup(&non_existing_value.node) == null);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "multiple inserts, followed by calling first and last" {
|
2019-09-22 04:55:56 +01:00
|
|
|
if (@import("builtin").arch == .aarch64) {
|
|
|
|
// TODO https://github.com/ziglang/zig/issues/3288
|
|
|
|
return error.SkipZigTest;
|
|
|
|
}
|
2020-01-19 18:08:05 +00:00
|
|
|
var tree = Tree.init(testCompare);
|
2019-07-31 14:17:09 +01:00
|
|
|
var zeroth: testNumber = undefined;
|
|
|
|
zeroth.value = 0;
|
|
|
|
var first: testNumber = undefined;
|
|
|
|
first.value = 1;
|
|
|
|
var second: testNumber = undefined;
|
|
|
|
second.value = 2;
|
|
|
|
var third: testNumber = undefined;
|
|
|
|
third.value = 3;
|
|
|
|
_ = tree.insert(&zeroth.node);
|
|
|
|
_ = tree.insert(&first.node);
|
|
|
|
_ = tree.insert(&second.node);
|
|
|
|
_ = tree.insert(&third.node);
|
|
|
|
assert(testGetNumber(tree.first().?).value == 0);
|
|
|
|
assert(testGetNumber(tree.last().?).value == 3);
|
|
|
|
var lookupNode: testNumber = undefined;
|
|
|
|
lookupNode.value = 3;
|
|
|
|
assert(tree.lookup(&lookupNode.node) == &third.node);
|
2020-01-19 18:10:21 +00:00
|
|
|
tree.sort(testCompareReverse) catch unreachable;
|
|
|
|
assert(testGetNumber(tree.first().?).value == 3);
|
|
|
|
assert(testGetNumber(tree.last().?).value == 0);
|
|
|
|
assert(tree.lookup(&lookupNode.node) == &third.node);
|
2019-07-31 14:17:09 +01:00
|
|
|
}
|