Skip to content

Project Documentation

src/zig_collections.zig

pub fn Counter

pub fn Counter(comptime T: type) type

Generic counter collection for counting occurrences of elements. For string keys ([]const u8), uses StringHashMap internally. For other types, uses general-purpose AutoHashMap.

return struct

Field Description
map: CounterMapType(T)
pub inline fn init
pub inline fn init(allocator: Allocator) Self

Creates new counter with given allocator

pub inline fn deinit
pub inline fn deinit(self: *Self) void

Frees all allocated memory

pub inline fn inc
pub inline fn inc(self: *Self, item: T) !void

Increments count for item by 1

pub inline fn addFromSlice
pub inline fn addFromSlice(self: *Self, slice: []const T) !void

Counts all items in slice

pub inline fn addFromIterator
pub inline fn addFromIterator(self: *Self, iterator: anytype) !void

Counts all items from iterator

pub inline fn get
pub inline fn get(self: Self, key: T) usize

Returns current count for key (0 if not present)

pub fn DefaultHashMap

pub fn DefaultHashMap(
    comptime K: type,
    comptime V: type,
    comptime context: anytype,
    comptime defaultFactory: fn (@TypeOf(context)) V,
) type

Auto-initializing map with default value factory.

return struct

Field Description
map: AutoHashMap(K, V)
pub inline fn init
pub inline fn init(allocator: Allocator) Self

Creates new map with given allocator

inline fn deinitValues
inline fn deinitValues(self: *Self) void

Deinitializes values of the map. Do not use directly.

pub inline fn deinit
pub inline fn deinit(self: *Self) void

Properly deinitializes map and all values

pub inline fn get
pub inline fn get(self: *Self, key: K) *V

Returns pointer to value (creates default if missing)