ziglang / std-lib-orphanage

No-longer-maintained MIT licensed code that needs a new home
MIT License
17 stars 9 forks source link

Proposal: Make Red-Black tree in std.rb generic #2

Open joachimschmidt557 opened 3 years ago

joachimschmidt557 commented 3 years ago

At the moment, the implementation of red-black trees in the standard library are not generic and need to be embedded in other structs in order to work as intended.

https://github.com/ziglang/zig/blob/a2bb246db4c2bb88f402215d5db79a535dbff4b6/lib/std/rb.zig#L16-L27

This is in contrast to other data structures in the standard library such as the linked lists, which are generic:

https://github.com/ziglang/zig/blob/a2bb246db4c2bb88f402215d5db79a535dbff4b6/lib/std/linked_list.zig#L15

https://github.com/ziglang/zig/blob/a2bb246db4c2bb88f402215d5db79a535dbff4b6/lib/std/linked_list.zig#L164

https://github.com/ziglang/zig/blob/a2bb246db4c2bb88f402215d5db79a535dbff4b6/lib/std/atomic/queue.zig#L10

https://github.com/ziglang/zig/blob/a2bb246db4c2bb88f402215d5db79a535dbff4b6/lib/std/atomic/stack.zig#L8

Are there any downsides to making the RBTree Node and Tree data structures generic like the other pointer-oriented data structures?

daurnimator commented 3 years ago

This is in contrast to other data structures in the standard library such as the linked lists, which are generic

I've seen it proposed that the genericity of the other data structures should be removed. ziglang/zig#5584 is the only related issue I can find right now though

joachimschmidt557 commented 3 years ago

I see the point in having these pointer-oriented data structures be non-generic: If we have the item stored, we can immediately access the node "containing" the item.

I would advocate for a unified handling of generics in these pointer-oriented data structures then. Either we have all of them generic or all of them non-generic. It would help with removing confusion, I think.

tadeokondrak commented 3 years ago

std.rb is no longer in the standard library: https://github.com/ziglang/zig/commit/63685190da9415ff7d27080e0f33cbeab6a80156

data-man commented 3 years ago

@joachimschmidt557 https://github.com/pedromsilvapt/zig-stack-vm/blob/master/src/rbt.zig