Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<h2>Ordered</h2>

[![Tests](https://img.shields.io/github/actions/workflow/status/CogitatorTech/ordered/tests.yml?label=tests&style=flat&labelColor=282c34&logo=github)](https://github.com/CogitatorTech/ordered/actions/workflows/tests.yml)
[![Benchmarks](https://img.shields.io/github/actions/workflow/status/CogitatorTech/ordered/benches.yml?label=benchmarks&style=flat&labelColor=282c34&logo=github)](https://github.com/CogitatorTech/ordered/actions/workflows/benches.yml)
[![Zig](https://img.shields.io/badge/zig-0.17.0-F7A41D?style=flat&labelColor=282c34&logo=zig)](https://ziglang.org/download/)
[![Release](https://img.shields.io/github/release/CogitatorTech/ordered.svg?label=release&style=flat&labelColor=282c34&logo=github)](https://github.com/CogitatorTech/ordered/releases/latest)
[![Docs](https://img.shields.io/badge/docs-read-507ec6?style=flat&labelColor=282c34&logo=read-the-docs)](https://CogitatorTech.github.io/ordered/)
[![Examples](https://img.shields.io/badge/examples-view-507ec6?style=flat&labelColor=282c34&logo=zig)](https://github.com/CogitatorTech/ordered/tree/main/examples)
[![Benchmark Results](https://img.shields.io/badge/benchmark%20results-view-507ec6?style=flat&labelColor=282c34&logo=github)](https://cogitatortech.github.io/benchmarks-ordered)
[![License](https://img.shields.io/badge/license-MIT-007ec6?label=license&style=flat&labelColor=282c34&logo=open-source-initiative)](https://github.com/CogitatorTech/ordered/blob/main/LICENSE)

A sorted collection library for Zig
Expand Down Expand Up @@ -176,7 +176,8 @@ Check out the [examples](examples) directory for example usages of Ordered.

### Benchmarks

Check out the [benchmarks](benches) directory for local benchmarks.
Check out the [benchmarks](https://cogitatortech.github.io/benchmarks-ordered) directory for benchmark results comparing implementations in
Ordered with implementations in other languages like C, C++, Rust, and Java.

---

Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.{
.name = .ordered,
.version = "0.3.1",
.version = "0.3.2",
.fingerprint = 0xc3121f99b0352e1b, // Changing this has security and trust implications.
.minimum_zig_version = "0.17.0",
.paths = .{
Expand Down
186 changes: 93 additions & 93 deletions src/ordered/btree_map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,12 @@ pub fn BTreeMap(
pub fn get(self: *const Self, key: K) ?*const V {
var current = self.root;
while (current) |node| {
const res = std.sort.binarySearch(K, node.keys[0..node.len], key, compareFn);
if (res) |index| return &node.values[index];
const index = std.sort.lowerBound(K, node.keys[0..node.len], key, compareFn);
if (index < node.len and compare(key, node.keys[index]) == .eq) return &node.values[index];

if (node.is_leaf) return null;

const insertion_point = std.sort.lowerBound(K, node.keys[0..node.len], key, compareFn);
current = node.children[insertion_point];
current = node.children[index];
}
return null;
}
Expand All @@ -176,13 +175,12 @@ pub fn BTreeMap(
pub fn getPtr(self: *Self, key: K) ?*V {
var current = self.root;
while (current) |node| {
const res = std.sort.binarySearch(K, node.keys[0..node.len], key, compareFn);
if (res) |index| return &node.values[index];
const index = std.sort.lowerBound(K, node.keys[0..node.len], key, compareFn);
if (index < node.len and compare(key, node.keys[index]) == .eq) return &node.values[index];

if (node.is_leaf) return null;

const insertion_point = std.sort.lowerBound(K, node.keys[0..node.len], key, compareFn);
current = node.children[insertion_point];
current = node.children[index];
}
return null;
}
Expand Down Expand Up @@ -279,38 +277,22 @@ pub fn BTreeMap(

fn insertNonFull(self: *Self, node: *Node, key: K, value: V) !bool {
var i = node.len;
while (i > 0 and compare(key, node.keys[i - 1]) == .lt) : (i -= 1) {}
if (i > 0 and compare(key, node.keys[i - 1]) == .eq) {
node.values[i - 1] = value;
return false;
}
if (node.is_leaf) {
// Check if key already exists
var j: u16 = 0;
while (j < node.len) : (j += 1) {
if (compare(key, node.keys[j]) == .eq) {
// Update existing value
node.values[j] = value;
return false; // Not a new insertion
}
}

// Insert new key
while (i > 0 and compare(key, node.keys[i - 1]) == .lt) : (i -= 1) {
node.keys[i] = node.keys[i - 1];
node.values[i] = node.values[i - 1];
var j = node.len;
while (j > i) : (j -= 1) {
node.keys[j] = node.keys[j - 1];
node.values[j] = node.values[j - 1];
}
node.keys[i] = key;
node.values[i] = value;
node.len += 1;
return true; // New insertion
return true;
} else {
// Check if key exists in current node
var j: u16 = 0;
while (j < node.len) : (j += 1) {
if (compare(key, node.keys[j]) == .eq) {
// Update existing value
node.values[j] = value;
return false; // Not a new insertion
}
}

while (i > 0 and compare(key, node.keys[i - 1]) == .lt) : (i -= 1) {}
if (node.children[i].?.len == BRANCHING_FACTOR - 1) {
try self.splitChild(node, i);
// The split promotes a median key into this node at index `i`.
Expand Down Expand Up @@ -356,11 +338,11 @@ pub fn BTreeMap(
}

fn deleteFromNode(self: *Self, node: *Node, key: K) ?V {
const res = std.sort.binarySearch(K, node.keys[0..node.len], key, compareFn);
const position = std.sort.lowerBound(K, node.keys[0..node.len], key, compareFn);
var val: ?V = null;

if (res) |index_usize| {
const index = @as(u16, @intCast(index_usize));
if (position < node.len and compare(key, node.keys[position]) == .eq) {
const index: u16 = @intCast(position);
val = node.values[index];
self.len -= 1;
if (node.is_leaf) {
Expand All @@ -369,7 +351,7 @@ pub fn BTreeMap(
self.removeFromInternal(node, index);
}
} else if (!node.is_leaf) {
const insertion_point = std.sort.lowerBound(K, node.keys[0..node.len], key, compareFn);
const insertion_point = position;
const key_exists_in_child = self.ensureChildHasEnoughKeys(node, @as(u16, @intCast(insertion_point)));
if (key_exists_in_child) {
return self.deleteFromNode(node, key);
Expand Down Expand Up @@ -537,6 +519,8 @@ pub fn BTreeMap(
pub const Iterator = struct {
stack: std.ArrayList(StackFrame),
allocator: std.mem.Allocator,
leaf: ?*Node = null,
leaf_index: u16 = 0,

const StackFrame = struct {
node: *Node,
Expand All @@ -549,73 +533,46 @@ pub fn BTreeMap(
};

fn init(allocator: std.mem.Allocator, root: ?*Node) !Iterator {
var stack: std.ArrayList(StackFrame) = .empty;
// If any append below fails after the first, the local
// `stack` goes out of scope without being moved into the
// returned Iterator; its heap buffer would leak without
// this errdefer.
errdefer stack.deinit(allocator);

if (root) |r| {
try stack.append(allocator, StackFrame{ .node = r, .index = 0 });
// Descend to leftmost leaf
var current = r;
while (!current.is_leaf) {
if (current.children[0]) |child| {
try stack.append(allocator, StackFrame{ .node = child, .index = 0 });
current = child;
} else break;
}
}
var it = Iterator{ .stack = .empty, .allocator = allocator };
errdefer it.deinit();
if (root) |node| try it.pushLeft(node);
return it;
}

return Iterator{
.stack = stack,
.allocator = allocator,
};
fn pushLeft(self: *Iterator, root: *Node) !void {
var node = root;
while (!node.is_leaf) {
try self.stack.append(self.allocator, .{ .node = node, .index = 0 });
node = node.children[0].?;
}
self.leaf = node;
self.leaf_index = 0;
}

pub fn deinit(self: *Iterator) void {
self.stack.deinit(self.allocator);
}

pub fn next(self: *Iterator) !?Entry {
if (self.leaf) |leaf| {
if (self.leaf_index < leaf.len) {
const index = self.leaf_index;
self.leaf_index += 1;
return .{ .key = leaf.keys[index], .value = leaf.values[index] };
}
self.leaf = null;
}
while (self.stack.items.len > 0) {
var frame = &self.stack.items[self.stack.items.len - 1];

const frame = &self.stack.items[self.stack.items.len - 1];
if (frame.index < frame.node.len) {
const result = Entry{
.key = frame.node.keys[frame.index],
.value = frame.node.values[frame.index],
};

// Move to next position
if (!frame.node.is_leaf) {
// Go to right child of current key
if (frame.node.children[frame.index + 1]) |child| {
frame.index += 1;
try self.stack.append(self.allocator, StackFrame{ .node = child, .index = 0 });

// Descend to leftmost leaf
var current = child;
while (!current.is_leaf) {
if (current.children[0]) |left_child| {
try self.stack.append(self.allocator, StackFrame{ .node = left_child, .index = 0 });
current = left_child;
} else break;
}
} else {
frame.index += 1;
}
} else {
frame.index += 1;
}

return result;
} else {
_ = self.stack.pop();
const node = frame.node;
const index = frame.index;
frame.index += 1;
try self.pushLeft(node.children[index + 1].?);
return .{ .key = node.keys[index], .value = node.values[index] };
}
_ = self.stack.pop();
}

return null;
}
};
Expand Down Expand Up @@ -967,3 +924,46 @@ test "regression: BTreeMap put of a split median does not duplicate the key" {
}
try std.testing.expectEqual(@as(usize, 7), n);
}

test "BTreeMap: search boundaries and leaf iteration after removal" {
var map = BTreeMap(i32, i32, i32Compare, 16).init(std.testing.allocator);
defer map.deinit();
var key: i32 = 510;
while (key >= 0) : (key -= 2) try map.put(key, key);
key = 0;
while (key < 512) : (key += 2) {
try std.testing.expectEqual(key, map.get(key).?.*);
map.getPtr(key).?.* += 1;
try std.testing.expect(map.get(key + 1) == null);
try std.testing.expect(map.getPtr(key + 1) == null);
try std.testing.expect(map.remove(key + 1) == null);
}
try std.testing.expect(map.get(-1) == null);
try std.testing.expect(map.get(512) == null);
key = 0;
while (key < 512) : (key += 4) try std.testing.expectEqual(key + 1, map.remove(key).?);
var it = try map.iterator();
defer it.deinit();
key = 2;
while (try it.next()) |entry| : (key += 4) {
try std.testing.expectEqual(key, entry.key);
try std.testing.expectEqual(key + 1, entry.value);
}
try std.testing.expectEqual(@as(i32, 514), key);
try std.testing.expect(try it.next() == null);
}

fn iteratorAllocationCheck(allocator: std.mem.Allocator) !void {
var map = BTreeMap(i32, i32, i32Compare, 4).init(std.testing.allocator);
defer map.deinit();
for (0..1024) |key| try map.put(@intCast(key), @intCast(key));
var it = try @TypeOf(map).Iterator.init(allocator, map.root);
defer it.deinit();
var count: usize = 0;
while (try it.next()) |entry| : (count += 1) try std.testing.expectEqual(@as(i32, @intCast(count)), entry.key);
try std.testing.expectEqual(@as(usize, 1024), count);
}

test "BTreeMap: iterator allocation failures release the stack" {
try std.testing.checkAllAllocationFailures(std.testing.allocator, iteratorAllocationCheck, .{});
}
Loading
Loading