Skip to content

feat: add a persistent hash Map built on the CHAMP trie - #25

Merged
glours merged 1 commit into
mainfrom
map
Oct 2, 2026
Merged

glours merged 1 commit into
mainfrom
map

Conversation

@glours

@glours glours commented Oct 2, 2026

Copy link
Copy Markdown
Owner

Adds collection.Map[K comparable, V any], the consumer the CHAMP trie from #24 was landed for. Closes #5.

API

  • EmptyMap, CollectMap(iter.Seq2[K, V]) — last value wins on a repeated key
  • IsEmpty, Len — O(1)
  • Get (returns control.Option[V]), ContainsKey, Put, Delete — O(log32 n)
  • All (iter.Seq2), Keys, Values (iter.Seq) — O(n)
  • Map[U], Filter, Fold[U], String

Decisions

  • Map maps the values and keeps the keys — Vavr's mapValues. Mapping whole entries could merge keys and would not be a functor. No key changes, so the result reuses the trie's exact shape (champMapValues) and nothing is hashed again: O(n).
  • Iteration order is unspecified, as for a Go map. String sorts by key the way fmt does, so printing is deterministic and usable in Example tests.
  • No ToGoMap / FromGoMap: CollectMap(maps.All(m)) and maps.Collect(m.All()) already cover both directions, shown in ExampleCollectMap.
  • A float NaN key behaves as in a Go map (added, never found), pinned by a test against the built-in map.

Sharing, measured

  • A Put into a map of 10 000 entries allocates 7 times; a Delete of an absent key allocates nothing (TestMapSharesStructure).
  • Benchmarks: Put goes from 6 to 10 allocations while the map grows from 1 000 to 1 000 000 entries; Get allocates nothing.

Checks

gofmt, go vet, golangci-lint (0 issues), go test -race -cover ./..., the no-dependency go list check, and go mod tidy leaving go.mod/go.sum unchanged.

README gains a Map section backed by ExampleMap and ExampleCollectMap.


This change was prepared by an AI agent and may not have been independently reviewed or tested.

🤖 Generated with Claude Code

Map[K, V] is the consumer the trie was landed for. Keys only need to be
comparable, so structs, arrays and pointers work, which is what sets it apart
from Tree.

Map maps the values and keeps the keys, which is Vavr's mapValues: mapping
whole entries could merge keys and would not be a functor. Because no key
changes, the result reuses the trie's exact shape and nothing is hashed again.

Iteration order is unspecified, as for a Go map, but String sorts by key the
way fmt does, so printing a Map is deterministic and usable in examples.

There is no dedicated conversion to or from a Go map: CollectMap(maps.All(m))
and maps.Collect(m.All()) already cover both directions.

Sharing is measured rather than claimed: a Put into a map of ten thousand
entries allocates seven times, a Delete of an absent key allocates nothing,
and the benchmarks show Put going from six to ten allocations while the map
grows a thousandfold.

Closes #5

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
@glours
glours merged commit 7c08f20 into main Oct 2, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a Map implementation in collection package

1 participant