Repository navigation
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
collection.Set[T comparable], aMap[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 sizeAll,Map[U comparable],Filter,Fold[U],StringDecisions
Insert/Deleterather than the issue'sAdd/Remove, so it reads likeTree, the other set in the package.InsertorDeletereturns the very same set and allocates nothing, asTreedoes.UnionandDifference.Intersectiononly looks values up, which allocates nothing either way, soBenchmarkSetIntersectionshows it instead (1 ms naive vs ~200 ns now, same cost in both orders).Stringsorts 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.Checks
gofmt,go vet,golangci-lint(0 issues),go test -race -cover ./..., the no-dependencygo listcheck, andgo mod tidyleavinggo.mod/go.sumunchanged.README gains a
Setsection backed byExampleSet.This change was prepared by an AI agent and may not have been independently reviewed or tested.
🤖 Generated with Claude Code