Skip to content

feat: add a persistent hash Set built on Map - #26

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

glours merged 1 commit into
mainfrom
set

Conversation

@glours

@glours glours commented Oct 2, 2026

Copy link
Copy Markdown
Owner

Adds collection.Set[T comparable], a Map[T, struct{}] as #4 asked: one trie, one set of guarantees, no second implementation. Closes #4.

API

  • EmptySet, SetOf(values...), CollectSet(iter.Seq[T])
  • IsEmpty, Len — O(1)
  • Contains, Insert, Delete — O(log32 n)
  • Union, Intersection, Difference — O(m log32 n), m the smaller size
  • All, Map[U comparable], Filter, Fold[U], String

Decisions

  • Insert / Delete rather than the issue's Add / Remove, so it reads like Tree, the other set in the package.
  • A no-op Insert or Delete returns the very same set and allocates nothing, as Tree does.
  • The set operations walk the smaller operand. A test drove this against a naive version first: walking the large side cost ~70 000 allocations for Union and Difference. Intersection only looks values up, which allocates nothing either way, so BenchmarkSetIntersection shows it instead (1 ms naive vs ~200 ns now, same cost in both orders).
  • String sorts by rendering, since elements are only comparable: deterministic, but numbers sort as text (Set(1, 10, 2)). Pinned by a test and stated in the godoc and README.
  • A structural trie merge (O(n + m) with subtree sharing) would be faster for two large sets; left out of scope.

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 Set section backed by ExampleSet.


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

🤖 Generated with Claude Code

Set[T] is a Map[T, struct{}], as #4 asked: one trie, one set of guarantees,
no second implementation. It takes Insert and Delete rather than the Add and
Remove the issue named, so that it reads like Tree, the other set in the
package.

Inserting a value that is already there hands back the very same set without
allocating, as Tree does. Map.Put cannot promise that, since it cannot compare
values, but a set has no value to replace.

Union, Intersection and Difference walk the smaller of the two sets, so
combining a handful of values with a large one stays cheap whichever side it
is on. A test measures it for Union and Difference, where walking the large
side cost about seventy thousand allocations. Intersection only looks values
up, which allocates nothing either way, so a benchmark shows it instead: the
two orders now cost the same.

Elements are only comparable, so String sorts them by their rendering to
print deterministically. Numbers therefore sort as text, which a test pins
and the README states.

Closes #4

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 15e7230 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 Set implementation in collection package

1 participant