init
Returns a map backed by memory allocated with allocator.
Handles kvs_list the same way as initComptime().
Function parameters
Parameters
- kvs_list:anytype
- allocator:mem.Allocator
Static string map optimized for small sets of disparate string keys.
Functions
- StaticStringMap
- Static string map optimized for small sets of disparate string keys.
- defaultEql
- Like `std.mem.eql`, but takes advantage of the fact that the lengths
- eqlAsciiIgnoreCase
- Like `std.ascii.eqlIgnoreCase` but takes advantage of the fact that
- StaticStringMapWithEql
- StaticStringMap, but accepts an equality function (`eql`).
Source
Implementation
pub fn init(kvs_list: anytype, allocator: mem.Allocator) !Self {
var self = Self{};
if (kvs_list.len == 0)
return self;
const sorted_keys = try allocator.alloc([]const u8, kvs_list.len);
errdefer allocator.free(sorted_keys);
const sorted_vals = try allocator.alloc(V, kvs_list.len);
errdefer allocator.free(sorted_vals);
const kvs = try allocator.create(KVs);
errdefer allocator.destroy(kvs);
self.initSortedKVs(kvs_list, sorted_keys, sorted_vals);
kvs.* = .{
.keys = sorted_keys.ptr,
.values = sorted_vals.ptr,
.len = @intCast(kvs_list.len),
};
self.kvs = kvs;
const len_indexes = try allocator.alloc(u32, self.max_len + 1);
self.initLenIndexes(len_indexes);
self.len_indexes = len_indexes.ptr;
self.len_indexes_len = @intCast(len_indexes.len);
return self;
}