Skip to content

Added Generic, ordered map (rbt) #24339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

wyattgill9
Copy link

^

lib/std/map.zig Outdated
parent: ?*Node,

fn init(allocator: Allocator, key: K, value: V) !*Node {
const node = try allocator.create(Node);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This style of allocation would be a perfect fit for the MemoryPool allocator.

lib/std/map.zig Outdated
};

/// Generic Map implementation (Red Black Tree similar to std::map)
pub fn Map(comptime K: type, comptime V: type) type {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Map is vague. It is going to be the first thing users find when searching for something mappish in the standard library but they definitely want to find the hashmaps first instead. I would suggest RBTree or maybe OrderedMap, to reflect more on the use case of this datastructure.

lib/std/map.zig Outdated
try float_map.insert("e", 2.71828);

try testing.expectApproxEqRel(@as(f64, 3.14159), float_map.get("pi").?, 0.00001);
try testing.expectApproxEqRel(@as(f64, 2.71828), float_map.get("e").?, 0.00001);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these approximate equals necessary? I think they should be used when doing computation on floats, but in this case i would expect to get the byte-for-byte same output as my input.

lib/std/map.zig Outdated
return Iterator{ .current = self.minimum(self.root) };
}

fn compare(self: *const Self, a: K, b: K) i32 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This compare function should probably be provided as an optional argument to the map, to allow custom comparisons. Similar to how the hash maps do it.

@squeek502
Copy link
Collaborator

Prior art:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants