fn ArrayHashMapUnmanaged(comptime K: type, comptime V: type, comptime Context: type, comptime store_hash: bool) type

General purpose hash table. Insertion order is preserved. Deletions perform a “swap removal” on the entries list. Modifying the hash map while iterating is allowed, however, one must understand the (well defined) behavior when mixing insertions and deletions with iteration. This type does not store an Allocator field - the Allocator must be passed in with each function call that requires it. See ArrayHashMap for a type that stores an Allocator field for convenience. Can be initialized directly using the default field values. This type is designed to have low overhead for small numbers of entries. When store_hash is false and the number of entries in the map is less than 9, the overhead cost of using ArrayHashMapUnmanaged rather than std.ArrayList is only a single pointer-sized integer. When store_hash is false, this data structure is biased towards cheap eql functions. It does not store each item’s hash in the table. Setting store_hash to true incurs slightly more memory cost by storing each key’s hash in the table but guarantees only one call to eql per insertion/deletion. Context must be a struct type with two member functions: hash(self, K) u32 eql(self, K, K) bool Adapted variants of many functions are provided. These variants take a pseudo key instead of a key. Their context must have the functions: hash(self, PseudoKey) u32 eql(self, PseudoKey, K) bool

Parameters

K: type,
V: type,
Context: type,
store_hash: bool,

Fields

entries: DataList = .{ },

It is permitted to access this field directly.

index_header: ?*IndexHeader = null,

When entries length is less than linear_scan_max, this remains null. Once entries length grows big enough, this field is allocated. There is an IndexHeader followed by an array of Index(I) structs, where I is defined by how many total indexes there are.

Functions

fn capacity(self: Self) usize

Returns the number of total elements which may be present before it is no longe…

Returns the number of total elements which may be present before it is no longer guaranteed that no allocations will be performed.

fn clearAndFree(self: *Self, allocator: Allocator) void

Clears the map and releases the backing allocation

fn clearRetainingCapacity(self: *Self) void

Clears the map but retains the backing allocation for future use.

fn clone(self: Self, allocator: Allocator) !Self

Create a copy of the hash map which can be modified separately. The copy uses t…

Create a copy of the hash map which can be modified separately. The copy uses the same context as this instance, but is allocated with the provided allocator.

fn cloneContext(self: Self, allocator: Allocator, ctx: Context) !Self

No documentation provided.

fn contains(self: Self, key: K) bool

Check whether a key is stored in the map

fn containsAdapted(self: Self, key: anytype, ctx: anytype) bool

No documentation provided.

fn containsContext(self: Self, key: K, ctx: Context) bool

No documentation provided.

fn count(self: Self) usize

Returns the number of KV pairs stored in this map.

fn deinit(self: *Self, allocator: Allocator) void

Frees the backing allocation and leaves the map in an undefined state. Note tha…

Frees the backing allocation and leaves the map in an undefined state. Note that this does not free keys or values. You must take care of that before calling this function, if it is needed.

fn ensureTotalCapacity(self: *Self, allocator: Allocator, new_capacity: usize) !void

Increases capacity, guaranteeing that insertions up until the expected_count

Increases capacity, guaranteeing that insertions up until the expected_count will not cause an allocation, and therefore cannot fail.

fn ensureTotalCapacityContext(self: *Self, allocator: Allocator, new_capacity: usize, ctx: Context) !void

No documentation provided.

fn ensureUnusedCapacity(self: *Self, allocator: Allocator, additional_capacity: usize) !void

Increases capacity, guaranteeing that insertions up until additional_count **…

Increases capacity, guaranteeing that insertions up until additional_count more items will not cause an allocation, and therefore cannot fail.

fn ensureUnusedCapacityContext(self: *Self, allocator: Allocator, additional_capacity: usize, ctx: Context) !void

No documentation provided.

fn fetchOrderedRemove(self: *Self, key: K) ?KV

If there is an Entry with a matching key, it is deleted from the hash map, an…

If there is an Entry with a matching key, it is deleted from the hash map, and then returned from this function. The entry is removed from the underlying array by shifting all elements forward thereby maintaining the current ordering.

fn fetchOrderedRemoveAdapted(self: *Self, key: anytype, ctx: anytype) ?KV

No documentation provided.

fn fetchOrderedRemoveContext(self: *Self, key: K, ctx: Context) ?KV

No documentation provided.

fn fetchOrderedRemoveContextAdapted(self: *Self, key: anytype, key_ctx: anytype, ctx: Context) ?KV

No documentation provided.

fn fetchPut(self: *Self, allocator: Allocator, key: K, value: V) !?KV

Inserts a new Entry into the hash map, returning the previous one, if any.

fn fetchPutAssumeCapacity(self: *Self, key: K, value: V) ?KV

Inserts a new Entry into the hash map, returning the previous one, if any. If…

Inserts a new Entry into the hash map, returning the previous one, if any. If insertion happens, asserts there is enough capacity without allocating.

fn fetchPutAssumeCapacityContext(self: *Self, key: K, value: V, ctx: Context) ?KV

No documentation provided.

fn fetchPutContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) !?KV

No documentation provided.

fn fetchSwapRemove(self: *Self, key: K) ?KV

If there is an Entry with a matching key, it is deleted from the hash map, an…

If there is an Entry with a matching key, it is deleted from the hash map, and then returned from this function. The entry is removed from the underlying array by swapping it with the last element.

fn fetchSwapRemoveAdapted(self: *Self, key: anytype, ctx: anytype) ?KV

No documentation provided.

fn fetchSwapRemoveContext(self: *Self, key: K, ctx: Context) ?KV

No documentation provided.

fn fetchSwapRemoveContextAdapted(self: *Self, key: anytype, key_ctx: anytype, ctx: Context) ?KV

No documentation provided.

fn get(self: Self, key: K) ?V

Find the value associated with a key

fn getAdapted(self: Self, key: anytype, ctx: anytype) ?V

No documentation provided.

fn getContext(self: Self, key: K, ctx: Context) ?V

No documentation provided.

fn getEntry(self: Self, key: K) ?Entry

Finds pointers to the key and value storage associated with a key.

fn getEntryAdapted(self: Self, key: anytype, ctx: anytype) ?Entry

No documentation provided.

fn getEntryContext(self: Self, key: K, ctx: Context) ?Entry

No documentation provided.

fn getIndex(self: Self, key: K) ?usize

Finds the index in the entries array where a key is stored

fn getIndexAdapted(self: Self, key: anytype, ctx: anytype) ?usize

No documentation provided.

fn getIndexContext(self: Self, key: K, ctx: Context) ?usize

No documentation provided.

fn getKey(self: Self, key: K) ?K

Find the actual key associated with an adapted key

fn getKeyAdapted(self: Self, key: anytype, ctx: anytype) ?K

No documentation provided.

fn getKeyContext(self: Self, key: K, ctx: Context) ?K

No documentation provided.

fn getKeyPtr(self: Self, key: K) ?*K

Find a pointer to the actual key associated with an adapted key

fn getKeyPtrAdapted(self: Self, key: anytype, ctx: anytype) ?*K

No documentation provided.

fn getKeyPtrContext(self: Self, key: K, ctx: Context) ?*K

No documentation provided.

fn getOrPut(self: *Self, allocator: Allocator, key: K) !GetOrPutResult

If key exists this function cannot fail. If there is an existing item with `key…

If key exists this function cannot fail. If there is an existing item with key, then the result Entry pointer points to it, and found_existing is true. Otherwise, puts a new item with undefined value, and the Entry pointer points to it. Caller should then initialize the value (but not the key).

fn getOrPutAdapted(self: *Self, allocator: Allocator, key: anytype, key_ctx: anytype) !GetOrPutResult

No documentation provided.

fn getOrPutAssumeCapacity(self: *Self, key: K) GetOrPutResult

If there is an existing item with key, then the result Entry pointer points…

If there is an existing item with key, then the result Entry pointer points to it, and found_existing is true. Otherwise, puts a new item with undefined value, and the Entry pointer points to it. Caller should then initialize the value (but not the key). If a new entry needs to be stored, this function asserts there is enough capacity to store it.

fn getOrPutAssumeCapacityAdapted(self: *Self, key: anytype, ctx: anytype) GetOrPutResult

If there is an existing item with key, then the result Entry pointers point…

If there is an existing item with key, then the result Entry pointers point to it, and found_existing is true. Otherwise, puts a new item with undefined key and value, and the Entry pointers point to it. Caller must then initialize both the key and the value. If a new entry needs to be stored, this function asserts there is enough capacity to store it.

fn getOrPutAssumeCapacityContext(self: *Self, key: K, ctx: Context) GetOrPutResult

No documentation provided.

fn getOrPutContext(self: *Self, allocator: Allocator, key: K, ctx: Context) !GetOrPutResult

No documentation provided.

fn getOrPutContextAdapted(self: *Self, allocator: Allocator, key: anytype, key_ctx: anytype, ctx: Context) !GetOrPutResult

No documentation provided.

fn getOrPutValue(self: *Self, allocator: Allocator, key: K, value: V) !GetOrPutResult

No documentation provided.

fn getOrPutValueContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) !GetOrPutResult

No documentation provided.

fn getPtr(self: Self, key: K) ?*V

Find a pointer to the value associated with a key

fn getPtrAdapted(self: Self, key: anytype, ctx: anytype) ?*V

No documentation provided.

fn getPtrContext(self: Self, key: K, ctx: Context) ?*V

No documentation provided.

fn iterator(self: Self) Iterator

Returns an iterator over the pairs in this map. Modifying the map may invalidat…

Returns an iterator over the pairs in this map. Modifying the map may invalidate this iterator.

fn keys(self: Self) []K

Returns the backing array of keys in this map. Modifying the map may invalidate…

Returns the backing array of keys in this map. Modifying the map may invalidate this array.

fn move(self: *Self) Self

Set the map to an empty state, making deinitialization a no-op, and returning a…

Set the map to an empty state, making deinitialization a no-op, and returning a copy of the original.

fn orderedRemove(self: *Self, key: K) bool

If there is an Entry with a matching key, it is deleted from the hash map. Th…

If there is an Entry with a matching key, it is deleted from the hash map. The entry is removed from the underlying array by shifting all elements forward, thereby maintaining the current ordering. Returns true if an entry was removed, false otherwise.

fn orderedRemoveAdapted(self: *Self, key: anytype, ctx: anytype) bool

No documentation provided.

fn orderedRemoveAt(self: *Self, index: usize) void

Deletes the item at the specified index in entries from the hash map. The ent…

Deletes the item at the specified index in entries from the hash map. The entry is removed from the underlying array by shifting all elements forward, thereby maintaining the current ordering.

fn orderedRemoveAtContext(self: *Self, index: usize, ctx: Context) void

No documentation provided.

fn orderedRemoveContext(self: *Self, key: K, ctx: Context) bool

No documentation provided.

fn orderedRemoveContextAdapted(self: *Self, key: anytype, key_ctx: anytype, ctx: Context) bool

No documentation provided.

fn pop(self: *Self) KV

Removes the last inserted Entry in the hash map and returns it.

fn popContext(self: *Self, ctx: Context) KV

No documentation provided.

fn popOrNull(self: *Self) ?KV

Removes the last inserted Entry in the hash map and returns it if count is non…

Removes the last inserted Entry in the hash map and returns it if count is nonzero. Otherwise returns null.

fn popOrNullContext(self: *Self, ctx: Context) ?KV

No documentation provided.

fn promote(self: Self, allocator: Allocator) Managed

Convert from an unmanaged map to a managed map. After calling this, the promot…

Convert from an unmanaged map to a managed map. After calling this, the promoted map should no longer be used.

fn promoteContext(self: Self, allocator: Allocator, ctx: Context) Managed

No documentation provided.

fn put(self: *Self, allocator: Allocator, key: K, value: V) !void

Clobbers any existing data. To detect if a put would clobber existing data, see…

Clobbers any existing data. To detect if a put would clobber existing data, see getOrPut.

fn putAssumeCapacity(self: *Self, key: K, value: V) void

Asserts there is enough capacity to store the new key-value pair. Clobbers any …

Asserts there is enough capacity to store the new key-value pair. Clobbers any existing data. To detect if a put would clobber existing data, see getOrPutAssumeCapacity.

fn putAssumeCapacityContext(self: *Self, key: K, value: V, ctx: Context) void

No documentation provided.

fn putAssumeCapacityNoClobber(self: *Self, key: K, value: V) void

Asserts there is enough capacity to store the new key-value pair. Asserts that …

Asserts there is enough capacity to store the new key-value pair. Asserts that it does not clobber any existing data. To detect if a put would clobber existing data, see getOrPutAssumeCapacity.

fn putAssumeCapacityNoClobberContext(self: *Self, key: K, value: V, ctx: Context) void

No documentation provided.

fn putContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) !void

No documentation provided.

fn putNoClobber(self: *Self, allocator: Allocator, key: K, value: V) !void

Inserts a key-value pair into the hash map, asserting that no previous entry wi…

Inserts a key-value pair into the hash map, asserting that no previous entry with the same key is already present

fn putNoClobberContext(self: *Self, allocator: Allocator, key: K, value: V, ctx: Context) !void

No documentation provided.

fn reIndex(self: *Self, allocator: Allocator) !void

Rebuilds the key indexes. If the underlying entries has been modified directly, …

Rebuilds the key indexes. If the underlying entries has been modified directly, users can call reIndex to update the indexes to account for these new entries.

fn reIndexContext(self: *Self, allocator: Allocator, ctx: Context) !void

No documentation provided.

fn shrinkAndFree(self: *Self, allocator: Allocator, new_len: usize) void

Shrinks the underlying Entry array to new_len elements and discards any asso…

Shrinks the underlying Entry array to new_len elements and discards any associated index entries. Reduces allocated capacity.

fn shrinkAndFreeContext(self: *Self, allocator: Allocator, new_len: usize, ctx: Context) void

No documentation provided.

fn shrinkRetainingCapacity(self: *Self, new_len: usize) void

Shrinks the underlying Entry array to new_len elements and discards any asso…

Shrinks the underlying Entry array to new_len elements and discards any associated index entries. Keeps capacity the same.

fn shrinkRetainingCapacityContext(self: *Self, new_len: usize, ctx: Context) void

No documentation provided.

inline fn sort(self: *Self, sort_ctx: anytype) void

Sorts the entries and then rebuilds the index. sort_ctx must have this method…

Sorts the entries and then rebuilds the index. sort_ctx must have this method: fn lessThan(ctx: @TypeOf(ctx), a_index: usize, b_index: usize) bool

fn sortContext(self: *Self, sort_ctx: anytype, ctx: Context) void

No documentation provided.

fn swapRemove(self: *Self, key: K) bool

If there is an Entry with a matching key, it is deleted from the hash map. Th…

If there is an Entry with a matching key, it is deleted from the hash map. The entry is removed from the underlying array by swapping it with the last element. Returns true if an entry was removed, false otherwise.

fn swapRemoveAdapted(self: *Self, key: anytype, ctx: anytype) bool

No documentation provided.

fn swapRemoveAt(self: *Self, index: usize) void

Deletes the item at the specified index in entries from the hash map. The ent…

Deletes the item at the specified index in entries from the hash map. The entry is removed from the underlying array by swapping it with the last element.

fn swapRemoveAtContext(self: *Self, index: usize, ctx: Context) void

No documentation provided.

fn swapRemoveContext(self: *Self, key: K, ctx: Context) bool

No documentation provided.

fn swapRemoveContextAdapted(self: *Self, key: anytype, key_ctx: anytype, ctx: Context) bool

No documentation provided.

fn values(self: Self) []V

Returns the backing array of values in this map. Modifying the map may invalida…

Returns the backing array of values in this map. Modifying the map may invalidate this array.

Values

DataList
undefined

The MultiArrayList type backing this map

Hash
type

The stored hash type, either u32 or void.

Managed
undefined

The ArrayHashMap type using the same settings as this managed map.