Packages

builtin
cbindgen
core
core.alloc
core.alloc.arena
core.alloc.atomic
core.alloc.fixed
core.alloc.gc
core.alloc.heap
core.alloc.log
core.alloc.memdebug
core.alloc.pool
core.alloc.ring
core.arg_parse
core.array
core.avl_tree
core.bucket_array
core.conv
core.doc
core.encoding
core.encoding.base64
core.encoding.csv
core.encoding.hex
core.encoding.json
core.encoding.kdl
core.encoding.osad
core.encoding.utf8
core.hash
core.hash.md5
core.hash.sha1
core.hash.sha256
core.heap
core.intrinsics
core.intrinsics.atomics
core.intrinsics.onyx
core.intrinsics.types
core.intrinsics.wasm
core.io
core.io.binary
core.iter
core.js
core.list
core.map
core.math
core.memory
core.misc
core.net
core.os
core.random
core.set
core.slice
core.string
core.sync
core.test
core.thread
core.time
main
runtime
runtime.info
runtime.platform
runtime.vars
simd

package core.map

Map
Map :: struct (Key_Type: type_expr, Value_Type: type_expr)
    where ValidKey(Key_Type)
{
    allocator: Allocator;
    hashes: [] i32;
    entries: [..] Entry(Key_Type, Value_Type);
}

Map is a generic hash-map implementation that uses chaining. Values can be of any type. Keys must of a type that supports the core.hash.hash, and the '==' operator.

Methods
Map.as_iter
Map.as_iter :: (m: &Map) -> #auto

Produces an iterator that yields all values of the map, in an unspecified order, as Map is unordered.

Map.clear
Map.clear :: (map: &Map) -> void

Removes all entries from the hash map. Does NOT modify memory, so be wary of dangling pointers!

Map.copy
Map.copy :: (oldMap: &Map, allocator: ? Allocator) -> Map(oldMap.Key_Type, oldMap.Value_Type)

Shallow copies a map using the allocator provided if one is provided, or the allocator on the old map otherwise.

Map.delete
Map.delete :: (map: &Map, key: map.Key_Type) -> void

Removes an entry from the map.

Map.empty
Map.empty :: (map: &Map) -> bool

Returns if the map does not contain any elements.

Map.format_map
Map.format_map :: (output: &conv.Format_Output, format: &conv.Format, x: &Map($K, $V)) -> void

Helper procedure to nicely format a Map when printing. Rarely ever called directly, instead used by conv.format_any.

Map.free
Map.free :: (map: &Map) -> void

Destroys a map and frees all memory.

Map.get
Map.get :: (map: &Map, key: map.Key_Type) -> ? map.Value_Type

Returns the value at the specified key, or .None if the value is not present

Map.get_opt
Map.get_opt :: (map: &Map, key: map.Key_Type) -> ? map.Value_Type

DEPRECATED - Use map.get instead.

Returns an Optional of the value at the specified key. The Optional has a value if the key is present, otherwise the optional does not have a value.

Map.get_ptr
Map.get_ptr :: (map: &Map, key: map.Key_Type) -> &map.Value_Type

Returns a pointer to the value at the specified key, or null if the key is not present.

Map.get_ptr_or_create
Map.get_ptr_or_create :: (map: &Map, key: map.Key_Type) -> &map.Value_Type

Returns a pointer to the value at the specified key. If the key is not in the map, a new value is created and inserted, then the pointer to that value is returned.

Map.has
Map.has :: (map: &Map, key: map.Key_Type) -> bool

Returns true if the map contains the key.

Map.init
Map.init :: (map: &Map($K, $V), allocator) -> void

Initializes a map.

Map.literal
Map.literal :: ($Key: type_expr, $Value: type_expr, values: [] MapLiteralValue(Key, Value)) -> #auto

Quickly create a Map with some entries.

Map.literal(str, i32, .[
    .{ "test", 123 },
    .{ "foo",  456 },
]);
Map.make
Map.make :: macro ($Key: type_expr, $Value: type_expr, allocator) -> Map(Key, Value)

Allows for creation of a Map using make().

m := make(Map(str, i32));

Creates and initializes a new map using the types provided.

Map.put
Map.put :: (map: &Map, key: map.Key_Type, value: map.Value_Type) -> void

Sets the value at the specified key, or creates a new entry if the key was not already present.

Map.update
Map.update :: macro (map: &Map, key: map.Key_Type, body: Code) -> void

Helper macro that finds a value by the key, and if it exists, runs the code, providing an it variable that is a pointer to the value.

m: Map(str, i32);
m->update("test") {
    *it += 10;
}

or:

m->update("test", [v](*v += 10));
MapLiteralValue
MapLiteralValue :: struct (K: type_expr, V: type_expr) {
    key: K;
    value: V;
}