Skip to content

HashMap

I am a hashtable-based map/dict ADT. Keys and values are arbitrary 64-bit values with pass-by-value semantics. HashMap__new_owned can help with cleanup if the keys and/or values are pointers to owned objects. Using null as a key works if hash, cmp, and drop_key are null-safe. Using null as a value works if drop_value is null-safe.

The table implementation uses "separate chaining", even though it auto-resizes based on capacity. The load factor is 0.75, and cannot be configured.

HashMap

pub struct HashMap

Type of HashMaps.

Keys

pub struct Keys

Type of HashMap's key iterator.

HashMap__new

pub fn HashMap__new(void* hash, void* cmp)

I construct a new HashMap with the specified hash and comparator functions. null can be passed for either to use identity, useful with primitives or when pointer-equality is appropriate.

HashMap__new_owned

pub fn HashMap__new_owned(void* hash, void* cmp, void* drop_key, void* drop_value)

I construct a new HashMap with the specified hash and comparator functions, along with functions for dropping keys and values. null can be passed as hash and cmp to use identity, useful with primitives or when pointer-equality is appropriate. null can be passed for the drop functions when keys and/or values are not owned allocations.

HashMap_contains

pub fn HashMap_contains(HashMap* self, void key)

I return whether the HashMap has an entry for the passed key.

HashMap_delete

pub fn HashMap_delete(HashMap* self, void key)

I ensure the HashMap does not contains an entry with the provided key. If one existed, its value is returned, otherwise null.

HashMap_drop

pub fn HashMap_drop(HashMap* self)

I drop the HashMap, along with its keys and values (if owned).

HashMap_get

pub fn HashMap_get(HashMap* self, void key)

I return the value in the HashMap for the passed key, or null if no entry exists.

HashMap_keys

pub fn HashMap_keys(HashMap* self)

I create a new Keys over this map's keys, in unspecified order. Its behavior is undefined if the map is modified during iteration.

HashMap_put

pub fn HashMap_put(HashMap* self, void key, void value)

I put a new entry into the HashMap, returning the prior value associated with the key, or null if no entry previously existed.

HashMap_size

pub fn HashMap_size(HashMap* self)

I return the number of entries in the HashMap.

Keys_drop

pub fn Keys_drop(Keys* self)

I drop the iterator.

Keys_next

pub fn Keys_next(Keys* self)

I return a pointer to the next key, or null if exhausted.