dsa.map
Hash maps with optional lookup, indexed access, removal, and entry iteration.
All modules · Writing docstrings
Imports and exports
Section titled “Imports and exports”import core.{Opt, Hash, GetItem, SetItem}import mem.{Ptr, Buf, heap}import dsa.arr.{Arr}enum Slot[K, V]A backing-table slot. Applications normally work with Map instead.
Slot.Empty
Section titled “Slot.Empty”EmptyA slot that has never held an entry.
Slot.Del
Section titled “Slot.Del”DelA removed entry, retained as a probe-chain marker.
Slot.Used
Section titled “Slot.Used”Used(K, V)A live key and value.
kind Map[K, V]A hash map from K to V. Keys need compatible hash and equality behavior.
Iteration order follows the backing table and is not insertion order.
let counts = Map[Str, Int].new()counts["kata"] = 2print(counts.get("kata").unwrap())print(counts.has("missing"))print(counts.del("kata"))print(counts.len())2falsetrue0Map.slots
Section titled “Map.slots”slots: Arr[Slot[K, V]]Backing slots; direct mutation can violate the map’s invariants.
Map.count
Section titled “Map.count”count: IntNumber of live entries.
Map.cap
Section titled “Map.cap”cap: IntNumber of allocated slots.
Map[@K, @V]
Section titled “Map[@K, @V]”impl Map[@K, @V]Map[@K, @V].new
Section titled “Map[@K, @V].new”func new(): Map[K, V]Create an empty map with eight initial slots.
Map[@K, @V]._idx
Section titled “Map[@K, @V]._idx”func _idx(self, key: K): IntImplementation helper: compute the key’s first probe position.
Map[@K, @V]._find
Section titled “Map[@K, @V]._find”func _find(self, key: K): Tup[Int, Bool]Implementation helper: return a slot index and whether the key was found.
Map[@K, @V]._grow
Section titled “Map[@K, @V]._grow”func _grow(self)Implementation helper: double the backing table and reinsert live entries.
Map[@K, @V].get
Section titled “Map[@K, @V].get”func get(self, key: K): Opt[V]Return the stored value, or Non when the key is absent.
Map[@K, @V].set
Section titled “Map[@K, @V].set”func set(self, key: K, val: V)Insert a key or replace its value. Grow the table when needed.
let labels = Map[Int, Str].new()labels.set(1, "first")labels.set(1, "updated")print(labels[1])print(labels.len())updated1Map[@K, @V].del
Section titled “Map[@K, @V].del”func del(self, key: K): BoolRemove the key. Return true if an entry was removed, otherwise false.
Map[@K, @V].has
Section titled “Map[@K, @V].has”func has(self, key: K): BoolWhether the key has a live entry.
Map[@K, @V].len
Section titled “Map[@K, @V].len”func len(self): IntReturn the number of live entries.
Map[@K, @V] as GetItem[K, V]
Section titled “Map[@K, @V] as GetItem[K, V]”impl Map[@K, @V] as GetItem[K, V]Map[@K, @V] as GetItem[K, V].get_item
Section titled “Map[@K, @V] as GetItem[K, V].get_item”func get_item(self, key: K): VImplement map[key]. Stop with an error if the key is absent.
Map[@K, @V] as SetItem[K, V]
Section titled “Map[@K, @V] as SetItem[K, V]”impl Map[@K, @V] as SetItem[K, V]Map[@K, @V] as SetItem[K, V].set_item
Section titled “Map[@K, @V] as SetItem[K, V].set_item”func set_item(self, key: K, val: V)Implement map[key] = val, inserting or replacing the entry.
MapIter
Section titled “MapIter”kind MapIter[K, V]An iterator over live (key, value) tuples in backing-table order.
MapIter.slots
Section titled “MapIter.slots”slots: Arr[Slot[K, V]]Slots visited by the iterator.
MapIter.cap
Section titled “MapIter.cap”cap: IntNumber of slots to inspect.
MapIter.idx
Section titled “MapIter.idx”idx: IntNext slot index.
MapIter[@K, @V] as Iter[Tup[K, V]]
Section titled “MapIter[@K, @V] as Iter[Tup[K, V]]”impl MapIter[@K, @V] as Iter[Tup[K, V]]MapIter[@K, @V] as Iter[Tup[K, V]].next
Section titled “MapIter[@K, @V] as Iter[Tup[K, V]].next”func next(self): Opt[Tup[K, V]]Yield the next live entry, skipping empty and deleted slots.
Map[@K, @V] as ToIter[Tup[K, V]]
Section titled “Map[@K, @V] as ToIter[Tup[K, V]]”impl Map[@K, @V] as ToIter[Tup[K, V]]Map[@K, @V] as ToIter[Tup[K, V]].to_iter
Section titled “Map[@K, @V] as ToIter[Tup[K, V]].to_iter”func to_iter(self): MapIter[K, V]Create an entry iterator. Avoid mutating the map during iteration.