maps #
Description
maps is a module that provides utility functions to make working with maps easier.
new_counter[T]() creates a Counter[T] for builtin map key types. inc(key), dec(key), and add_many(values) change integer counts; missing keys read as zero through get(key). Counts may be zero or negative, and total() sums all counts. most_common(n) returns up to n key/count entries in descending count order without changing the counter. A nonpositive limit returns an empty array.
new_default_map[K, V](factory) creates a DefaultMap[K, V]. get(key) invokes the factory only when the key is absent and stores its result. get_opt(key) checks without creating an entry. set(key, value) stores or replaces a value, and len() counts stored keys. Returned values use ordinary V assignment semantics; references inside them are not deep-cloned. To replace an array or other stored value, retrieve it, modify the local value, and call set with the result.
fn filter #
fn filter[K, V](m map[K]V, f fn (key K, val V) bool) map[K]V
filter filters map entries by the given predicate function
fn flat_map #
fn flat_map[K, V, I](m map[K]V, f fn (key K, val V) []I) []I
flat_map maps map entries into arrays and flattens into a one-dimensional array
fn from_array #
fn from_array[T](array []T) map[int]T
from_array maps array into map with index to element per entry
fn invert #
fn invert[K, V](m map[K]V) map[V]K
invert returns a new map, created by swapping key to value and vice versa for each entry.
fn merge #
fn merge[K, V](m1 map[K]V, m2 map[K]V) map[K]V
merge produces a map, that is the result of merging the first map m1, with the second map m2. If a key exists in both maps, the value from m2, will override the value from m1. The original maps m1 and m2, will not be modified. The return value is a new map.
fn merge_in_place #
fn merge_in_place[K, V](mut m1 map[K]V, m2 map[K]V)
merge_in_place merges all elements of m2 into the mutable map m1. If a key exists in both maps, the value from m1 will be overwritten by the value from m2. Note that this function modifes m1, while m2 will not be.
fn new_counter #
fn new_counter[T]() Counter[T]
new_counter creates an empty Counter.
fn new_default_map #
fn new_default_map[K, V](factory fn () V) DefaultMap[K, V]
new_default_map creates an empty DefaultMap using factory for new keys.
fn to_array #
fn to_array[K, V, I](m map[K]V, f fn (key K, val V) I) []I
to_array maps map entries into one-dimensional array
fn to_map #
fn to_map[K, V, X, Y](m map[K]V, f fn (key K, val V) (X, Y)) map[X]Y
to_map maps map entries into new entries and constructs a new map
fn (Counter[T]) inc #
fn (mut c Counter[T]) inc(key T)
inc adds one occurrence of key.
fn (Counter[T]) dec #
fn (mut c Counter[T]) dec(key T)
dec removes one occurrence of key.
fn (Counter[T]) add_many #
fn (mut c Counter[T]) add_many(items []T)
add_many counts every item once.
fn (Counter[T]) get #
fn (c &Counter[T]) get(key T) int
get returns the count of key (0 if never seen).
fn (Counter[T]) total #
fn (c &Counter[T]) total() int
total returns the sum of all counts.
fn (Counter[T]) most_common #
fn (c &Counter[T]) most_common(n int) []CounterEntry[T]
most_common returns up to n entries ordered by descending count.
fn (DefaultMap[K, V]) get #
fn (mut d DefaultMap[K, V]) get(key K) V
get returns the value for key, creating it with the factory on first access.
fn (DefaultMap[K, V]) get_opt #
fn (d &DefaultMap[K, V]) get_opt(key K) ?V
get_opt returns the value for key, or none if absent (no creation).
fn (DefaultMap[K, V]) set #
fn (mut d DefaultMap[K, V]) set(key K, value V)
set stores value under key.
fn (DefaultMap[K, V]) len #
fn (d &DefaultMap[K, V]) len() int
len returns the number of stored keys.
struct Counter #
struct Counter[T] {
mut:
counts map[T]int
}
Counter counts occurrences of values. Missing keys count 0. It is the V-idiomatic equivalent of Python's collections.Counter.
struct CounterEntry #
struct CounterEntry[T] {
pub:
key T
count int
}
CounterEntry is one counted item: key observed count times.
struct DefaultMap #
struct DefaultMap[K, V] {
mut:
m map[K]V
create fn () V @[required]
}
DefaultMap is a map that creates missing entries with a factory closure on first access. It is the V-idiomatic equivalent of Python's collections.defaultdict.