1616[ ![ 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 )
1717[ ![ 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 )
1818
19- Pure Zig implementations of high-performance, memory-safe ordered data structures
19+ A sorted collection library for Zig
2020
2121</div >
2222
2323---
2424
25- Ordered is a Zig library that provides efficient implementations of various popular data structures including
26- B-tree, skip list, trie, and red-black tree for Zig programming language.
25+ Ordered is a Zig library that provides fast and efficient implementations of various data structures that keep elements
26+ sorted (AKA sorted collections).
27+ It is written in pure Zig and has no external dependencies.
28+ Ordered is inspired by [ Java collections] ( https://en.wikipedia.org/wiki/Java_collections_framework ) and sorted
29+ containers in the [ C++ standard library] ( https://en.cppreference.com/w/cpp/container ) , and aims to provide a similar
30+ experience in Zig.
2731
2832### Features
2933
30- To be added.
34+ - Simple and uniform API for all data structures
35+ - Pure Zig implementations with no external dependencies
36+ - Fast and memory-efficient implementations (see [ benches] ( benches ) )
3137
3238### Data Structures
3339
34- | Data Structure | Build Complexity | Memory Complexity | Search Complexity |
35- | ------------------------------------------------------------------------| ------------------| -------------------| -------------------|
36- | [ B-tree] ( https://en.wikipedia.org/wiki/B-tree ) | $O(\log n)$ | $O(n)$ | $O(\log n)$ |
37- | [ Cartesian tree] ( https://en.wikipedia.org/wiki/Cartesian_tree ) | $O(\log n)$\* | $O(n)$ | $O(\log n)$\* |
38- | [ Red-black tree] ( https://en.wikipedia.org/wiki/Red%E2%80%93black_tree ) | $O(\log n)$ | $O(n)$ | $O(\log n)$ |
39- | [ Skip list] ( https://en.wikipedia.org/wiki/Skip_list ) | $O(\log n)$\* | $O(n)$ | $O(\log n)$\* |
40- | Sorted set | $O(n)$ | $O(n)$ | $O(\log n)$ |
41- | [ Trie] ( https://en.wikipedia.org/wiki/Trie ) | $O(m)$ | $O(n \cdot m)$ | $O(m)$ |
40+ Ordered provides two main interfaces for working with sorted collections: sorted maps and sorted sets.
41+ At the moment, Ordered supports the following implementations of these interfaces:
4242
43- - $n$: number of stored elements
44- - $m$: maximum length of a key
45- - \* : average case complexity
43+ #### Maps (Key-value)
44+
45+ | Type | Data Structure | Insert | Search | Delete | Space |
46+ | --------------------| ------------------------------------------------------| --------------| --------------| --------------| ----------------|
47+ | ` BTreeMap ` | [ B-tree] ( https://en.wikipedia.org/wiki/B-tree ) | $O(\log n)$ | $O(\log n)$ | $O(\log n)$ | $O(n)$ |
48+ | ` SkipListMap ` | [ Skip list] ( https://en.wikipedia.org/wiki/Skip_list ) | $O(\log n)$† | $O(\log n)$† | $O(\log n)$† | $O(n)$ |
49+ | ` TrieMap ` | [ Trie] ( https://en.wikipedia.org/wiki/Trie ) | $O(m)$ | $O(m)$ | $O(m)$ | $O(n \cdot m)$ |
50+ | ` CartesianTreeMap ` | [ Treap] ( https://en.wikipedia.org/wiki/Treap ) | $O(\log n)$† | $O(\log n)$† | $O(\log n)$† | $O(n)$ |
51+
52+ #### Sets (Value-only)
53+
54+ | Type | Data Structure | Insert | Search | Delete | Space |
55+ | -------------------| ----------------------------------------------------------------| -------------| -------------| -------------| --------|
56+ | ` SortedSet ` | [ Sorted array] ( https://en.wikipedia.org/wiki/Sorted_array ) | $O(n)$ | $O(\log n)$ | $O(n)$ | $O(n)$ |
57+ | ` RedBlackTreeSet ` | [ Red-black tree] ( https://en.wikipedia.org/wiki/Red-black_tree ) | $O(\log n)$ | $O(\log n)$ | $O(\log n)$ | $O(n)$ |
58+
59+ - $n$ = number of elements stored
60+ - $m$ = length of the key (for string-based keys)
61+ - † = average case complexity (the worst case is $O(n)$)
62+
63+ See the [ ROADMAP.md] ( ROADMAP.md ) for the list of implemented and planned features.
4664
4765> [ !IMPORTANT]
4866> Ordered is in early development, so bugs and breaking API changes are expected.
@@ -52,7 +70,91 @@ To be added.
5270
5371### Getting Started
5472
55- To be added.
73+ You can add Ordered to your project and start using it by following the steps below.
74+
75+ #### Installation
76+
77+ Run the following command in the root directory of your project to download Ordered:
78+
79+ ``` sh
80+ zig fetch --save=ordered " https://github.com/CogitatorTech/ordered/archive/<branch_or_tag>.tar.gz"
81+ ```
82+
83+ Replace ` <branch_or_tag> ` with the desired branch or release tag, like ` main ` (for the development version) or ` v0.1.0 ` .
84+ This command will download Ordered and add it to Zig's global cache and update your project's ` build.zig.zon ` file.
85+
86+ #### Adding to Build Script
87+
88+ Next, modify your ` build.zig ` file to make Ordered available to your build target as a module.
89+
90+ ``` zig
91+ const std = @import("std");
92+
93+ pub fn build(b: *std.Build) void {
94+ const target = b.standardTargetOptions(.{});
95+ const optimize = b.standardOptimizeOption(.{});
96+
97+ // 1. Get the dependency object from the builder
98+ const ordered_dep = b.dependency("ordered", .{});
99+
100+ // 2. Create a module for the dependency
101+ const ordered_module = ordered_dep.module("ordered");
102+
103+ // 3. Create your executable module and add ordered as import
104+ const exe_module = b.createModule(.{
105+ .root_source_file = b.path("src/main.zig"),
106+ .target = target,
107+ .optimize = optimize,
108+ });
109+ exe_module.addImport("ordered", ordered_module);
110+
111+ // 4. Create executable with the module
112+ const exe = b.addExecutable(.{
113+ .name = "your-application",
114+ .root_module = exe_module,
115+ });
116+
117+ b.installArtifact(exe);
118+ }
119+ ```
120+
121+ #### Using Ordered in Your Project
122+
123+ Finally, you can ` @import("ordered") ` and start using it in your Zig code.
124+
125+ ``` zig
126+ const std = @import("std");
127+ const ordered = @import("ordered");
128+
129+ // Define a comparison function for the keys.
130+ // The function must return a `std.math.Order` value based on the comparison of the two keys
131+ fn strCompare(lhs: []const u8, rhs: []const u8) std.math.Order {
132+ return std.mem.order(u8, lhs, rhs);
133+ }
134+
135+ pub fn main() !void {
136+ const allocator = std.heap.page_allocator;
137+
138+ std.debug.print("## BTreeMap Example ##\n", .{});
139+ const B = 4; // Branching Factor for B-tree
140+ var map = ordered.BTreeMap([]const u8, u32, strCompare, B).init(allocator);
141+ defer map.deinit();
142+
143+ try map.put("banana", 150);
144+ try map.put("apple", 100);
145+ try map.put("cherry", 200);
146+
147+ const key_to_find = "apple";
148+ if (map.get(key_to_find)) |value_ptr| {
149+ std.debug.print("Found key '{s}': value is {d}\n", .{ key_to_find, value_ptr.* });
150+ }
151+
152+ const removed = map.remove("banana");
153+ std.debug.print("Removed 'banana' with value: {?d}\n", .{if (removed) |v| v else null});
154+ std.debug.print("Contains 'banana' after remove? {any}\n", .{map.contains("banana")});
155+ std.debug.print("Map count: {d}\n\n", .{map.count()});
156+ }
157+ ```
56158
57159---
58160
@@ -70,7 +172,7 @@ Check out the [examples](examples) directory for example usages of Ordered.
70172
71173### Benchmarks
72174
73- To be added .
175+ Check out the [ benchmarks ] ( benches ) directory for local benchmarks .
74176
75177---
76178
0 commit comments