https://github.com/insolor/zig-collections
Implementation of some useful data structures in Zig. Inspired by Python's collections module.
https://github.com/insolor/zig-collections
zig zig-library zig-package ziglang
Last synced: about 2 months ago
JSON representation
Implementation of some useful data structures in Zig. Inspired by Python's collections module.
- Host: GitHub
- URL: https://github.com/insolor/zig-collections
- Owner: insolor
- License: mit
- Created: 2024-12-19T17:00:15.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2026-04-21T17:19:17.000Z (2 months ago)
- Last Synced: 2026-04-21T19:25:24.143Z (2 months ago)
- Topics: zig, zig-library, zig-package, ziglang
- Language: Zig
- Homepage:
- Size: 360 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Zig Collections
[](https://ziglang.org/download/)
[](https://github.com/insolor/zig-collections/actions/workflows/zig-build-test.yml)
[](https://insolor.github.io/zig-collections/)
Implementation of some useful data structures in Zig. Inspired by Python's [`collections`](https://docs.python.org/3/library/collections.html) module.
## Installation
1. In the root directory of your project, run the following command to add `zig_collections` to your `build.zig.zon` file (replace 0.0.2 with the latest release number):
```bash
zig fetch --save https://github.com/insolor/zig-collections/archive/refs/tags/0.0.2.zip
```
Replace `main` in the URL with the tag you want to use.
2. Add zig_collections as a dependency module in your `build.zig` file, example:
```zig
const zig_collections = b.dependency("zig_collections", .{});
exe.root_module.addImport("zig_collections", zig_collections.module("zig_collections"));
```
After that, you'll be able to import `zig_collections` namespace from your code:
```zig
const zig_collections = @import("zig_collections");
const Counter = zig_collections.Counter;
const DefaultHashMap = zig_collections.DefaultHashMap;
```
## Usage examples
Implemented so far:
- ✅ `Counter`:
- a minimal functionality is implemented: increment of a value of a key, counting of duplicate values from a slice or an iterator
- ✅ `defaultdict` (`DefaultHashMap`)
### `Counter` usage examples
```zig
test "add from slice" {
var counter = Counter(u8).init(allocator);
defer counter.deinit();
const array = [_]u8{ 1, 2, 2, 3, 3, 3 };
try counter.addFromSlice(array[0..]);
try expectEqual(1, counter.get(1));
try expectEqual(2, counter.get(2));
try expectEqual(3, counter.get(3));
}
test "add from iterator" {
var counter = Counter([]const u8).init(allocator);
defer counter.deinit();
const text = "alice bob alice";
var iterator = std.mem.splitScalar(u8, text, ' ');
try counter.addFromIterator(&iterator);
try expectEqual(2, counter.get("alice"));
try expectEqual(1, counter.get("bob"));
}
```
### `DefaultHashMap` example
```zig
test "DefaultHashMap with list" {
const EmptyArrayListFactory = struct {
allocator: std.mem.Allocator,
fn produce(self: @This()) ArrayList(u8) {
return ArrayList(u8).init(self.allocator);
}
};
var map = collections.DefaultHashMap(
u8,
ArrayList(u8),
EmptyArrayListFactory{ .allocator = allocator },
EmptyArrayListFactory.produce,
).init(allocator);
defer map.deinit();
const array = [_]u8{ 3, 3, 1, 2, 3, 2 };
for (array, 0..) |item, i| {
try map.get(item).append(@intCast(i));
}
try expectEqualDeep(&[_]u8{2}, map.get(1).items);
try expectEqualDeep(&[_]u8{ 3, 5 }, map.get(2).items);
try expectEqualDeep(&[_]u8{ 0, 1, 4 }, map.get(3).items);
}
```
Corresponding Python code:
```python
from collections import defaultdict
dmap = defaultdict(list)
array = [3, 3, 1, 2, 3, 2]
for i, item in enumerate(array):
dmap[item].append(i)
assert [2] == dmap[1]
assert [3, 5] == dmap[2]
assert [0, 1, 4] == dmap[3]
```