Repository navigation
Add CappedCountStore: a Go multiset core with 0/1/MANY counts - #469
Merged
Merged
Conversation
CountStore keeps, per canonical key, a value and a count, sorted by key with no zero entries, so equal contents give equal entries whatever the order of operations. The count policy is per store: exact, or capped at Many (0, 1, MANY), where removing from Many has two outcomes and the caller picks one. It is plain Go with no Starlark and no language change, meant as the shared core for later users: the checker's own state (such as threads) and a spec-visible multiset. Tests: lib/countstore_test.go, random add/remove sequences against exact counts for both policies, canonical equality, outcomes and errors, clone and rekey. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The capped policy's increment, removal outcomes and capping now go through CardinalityCounter (Inc, Dec, NewCardinalityCounter), so the 0/1/MANY rules live in one place. A capped count is the counter's ValueCode. New test checks the two agree. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Drop the count policy and the exact counts. Each entry's count is a CardinalityCounter (ONE or MANY; absent is ZERO), so Add is the counter's Inc, RemoveOutcomes is its Dec, and there are no branches on a policy. Exact counts can come later as a separate type if needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Remove returns an error for a nil outcome instead of panicking. - Entries returns a copy, so callers cannot break the sort order or the no-ZERO invariant. - Document the key contract: the key is the element's identity, a complete canonical encoding or a collision-resistant hash; Add keeps the first value and Equal compares keys and counts. - Rekey merges with CardinalityCounter.Plus (new: the abstract sum) and builds into a temporary store, so a panicking key leaves it unchanged. - Total returns a shared MANY counter instead of allocating one. - Tests for each, plus Rekey MANY+MANY, idempotence, clone independence and the empty store; Plus is tested against concrete sums. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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.
Requested by JP · project thread
Summary
Adds
lib.CappedCountStore[T], a plain Go multiset. Each entry's count is aCardinalityCounter: ONE or MANY, with an absent element reading as ZERO. This PR doesn't use Starlark, doesn't change the language and doesn't change any existing behaviour.Addunder an existing key keeps the first value and counts one more copy, andEqualcompares keys and counts only.Addis the counter'sInc.RemoveOutcomesis itsDec: removing from ONE leaves ZERO; removing from MANY leaves ONE or MANY.Remove(key, outcome), typically once in each fork. A nil or invalid outcome is an error.Rekeymerges entries that end up with the same key using the newCardinalityCounter.Plus, the abstract sum.Add,RemoveOutcomes,Remove,Count,Get,Has,Len(distinct elements),Total(a counter),Entries,Equal,Clone(fn)andRekey(fn).Entriesreturns a copy of the entries, so callers can't break the invariants. Values are shared, not copied.Clone(nil)is shallow.Rekeybuilds the new store before replacing the old one, so a key function that panics leaves the store unchanged.Intended use. The planned consumer is the capped abstraction of process threads, where an action can have any number of concurrent copies. Advancing one copy of a thread whose count is MANY forks into ONE or MANY for the copies left behind. That scheduler design will come in its own PR. This store is not meant for exact deduplication of identical threads, because it can't represent exact multiplicity. If exact counts are ever needed, they would be a separate type.
Tests
This is a data structure, not a stateful protocol, so it gets property tests instead of a FizzBee spec:
lib/countstore_test.goandlib/cardinality_test.go.Has,LenandTotalmust equal the abstraction of the exact counts. Entries must stay sorted with no ZERO.Entriesreturns doesn't change the store;Addwith a different value under the same key keeps the first; a clone stays independent of later changes; the empty store.Rekey: MANY + MANY and ONE + ONE merge to MANY, a lone ONE stays ONE,Rekeyis idempotent, and a panicking key leaves the store unchanged.Clonecopies values deeply when given a clone function.CardinalityCounter.Plusmatches the abstraction of concrete sums for 0 to 3.go test ./lib -run 'CountStore|Cardinality'passes.🤖 Generated with Claude Code
https://claude.ai/code/session_013eDw2fcqNcKpnpHXHYFi7n