Ordered
[](https://github.com/CogitatorTech/ordered/actions/workflows/tests.yml)
[](https://github.com/CogitatorTech/ordered/actions/workflows/benches.yml)
[](https://ziglang.org/download/)
[](https://github.com/CogitatorTech/ordered/releases/latest)
[](https://CogitatorTech.github.io/ordered/)
[](https://github.com/CogitatorTech/ordered/tree/main/examples)
[](https://github.com/CogitatorTech/ordered/blob/main/LICENSE)
A sorted collection library for Zig
---
Ordered is a Zig library that provides fast and efficient implementations of various data structures that keep elements
sorted (AKA sorted collections).
It is written in pure Zig and has no external dependencies.
Ordered is inspired by [Java collections](https://en.wikipedia.org/wiki/Java_collections_framework) and sorted
containers in the [C++ standard library](https://en.cppreference.com/w/cpp/container), and aims to provide a similar
experience in Zig.
### Features
- Simple and uniform API for all data structures
- Pure Zig implementations with no external dependencies
- Fast, cache-friendly, and memory-efficient implementations (see [benches](benches))
### Data Structures
Ordered provides two main interfaces for working with sorted collections: *sorted maps* and *sorted sets*.
At the moment, Ordered supports the following implementations of these interfaces:
#### Maps (key-value)
| Type | Data Structure | Insert | Search | Delete | Space |
|--------------------|------------------------------------------------------|--------------|--------------|--------------|----------------|
| `BTreeMap` | [B-tree](https://en.wikipedia.org/wiki/B-tree) | $O(\log n)$ | $O(\log n)$ | $O(\log n)$ | $O(n)$ |
| `SkipListMap` | [Skip list](https://en.wikipedia.org/wiki/Skip_list) | $O(\log n)$† | $O(\log n)$† | $O(\log n)$† | $O(n)$ |
| `TrieMap` | [Trie](https://en.wikipedia.org/wiki/Trie) | $O(m)$ | $O(m)$ | $O(m)$ | $O(n \cdot m)$ |
| `CartesianTreeMap` | [Treap](https://en.wikipedia.org/wiki/Treap) | $O(\log n)$† | $O(\log n)$† | $O(\log n)$† | $O(n)$ |
#### Sets (value-only)
| Type | Data Structure | Insert | Search | Delete | Space |
|-------------------|----------------------------------------------------------------|-------------|-------------|-------------|--------|
| `SortedSet` | [Sorted array](https://en.wikipedia.org/wiki/Sorted_array) | $O(n)$ | $O(\log n)$ | $O(n)$ | $O(n)$ |
| `RedBlackTreeSet` | [Red-black tree](https://en.wikipedia.org/wiki/Red-black_tree) | $O(\log n)$ | $O(\log n)$ | $O(\log n)$ | $O(n)$ |
- $n$ = number of elements stored
- $m$ = length of the key (for string-based keys)
- † = average case complexity (the worst case is $O(n)$)
> [!IMPORTANT]
> Ordered is in early development, so bugs and breaking API changes are expected.
> Please use the [issues page](https://github.com/CogitatorTech/ordered/issues) to report bugs or request features.
---
### Getting Started
You can add Ordered to your project and start using it by following the steps below.
#### Installation
Run the following command in the root directory of your project to download Ordered:
```sh
zig fetch --save=ordered "https://github.com/CogitatorTech/ordered/archive/