An Entry represents a slot in the treap associated with a given key.
key: KeyThe associated key for this entry.
treap: *SelfA reference to the treap this entry is apart of.
node: ?*NodeThe current node at this entry.
context: union(enum) {
/// A find() was called for this entry and the position in the treap is known.
inserted_under: ?*Node,
/// The entry's node was removed from the treap and a lookup must occur again for modification.
removed,
}The current state of the entry.
Update's the Node at this Entry in the treap with the new node (null for deleting). new_node
can have undefind content because the value will be initialized internally.
pub fn set(self: *Entry, new_node: ?*Node) void {
// Update the entry's node reference after updating the treap below.
defer self.node = new_node;
if (self.node) |old| {
if (new_node) |new| {
self.treap.replace(old, new);
return;
}
self.treap.remove(old);
self.context = .removed;
return;
}
if (new_node) |new| {
// A previous treap.remove() could have rebalanced the nodes
// so when inserting after a removal, we have to re-lookup the parent again.
// This lookup shouldn't find a node because we're yet to insert it..
var parent: ?*Node = undefined;
switch (self.context) {
.inserted_under => |p| parent = p,
.removed => assert(self.treap.find(self.key, &parent) == null),
}
self.treap.insert(self.key, parent, new);
self.context = .{ .inserted_under = parent };
}
}pub const Entry = struct {
/// The associated key for this entry.
key: Key,
/// A reference to the treap this entry is apart of.
treap: *Self,
/// The current node at this entry.
node: ?*Node,
/// The current state of the entry.
context: union(enum) {
/// A find() was called for this entry and the position in the treap is known.
inserted_under: ?*Node,
/// The entry's node was removed from the treap and a lookup must occur again for modification.
removed,
},
/// Update's the Node at this Entry in the treap with the new node (null for deleting). `new_node`
/// can have `undefind` content because the value will be initialized internally.
pub fn set(self: *Entry, new_node: ?*Node) void {
// Update the entry's node reference after updating the treap below.
defer self.node = new_node;
if (self.node) |old| {
if (new_node) |new| {
self.treap.replace(old, new);
return;
}
self.treap.remove(old);
self.context = .removed;
return;
}
if (new_node) |new| {
// A previous treap.remove() could have rebalanced the nodes
// so when inserting after a removal, we have to re-lookup the parent again.
// This lookup shouldn't find a node because we're yet to insert it..
var parent: ?*Node = undefined;
switch (self.context) {
.inserted_under => |p| parent = p,
.removed => assert(self.treap.find(self.key, &parent) == null),
}
self.treap.insert(self.key, parent, new);
self.context = .{ .inserted_under = parent };
}
}
}