Skip to content

Commit

Permalink
Use map[T]struct{} instead of map[T]bool for internal set types
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Apr 3, 2024
1 parent 45cb280 commit 26d4332
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
26 changes: 11 additions & 15 deletions fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,16 @@ func (a *realActionLog) validate(db *statedb.DB, t *testing.T) {
defer a.Unlock()

// Collapse the log down to objects that are alive at the end.
alive := map[statedb.Table[fuzzObj]]map[uint64]bool{}
alive := map[statedb.Table[fuzzObj]]map[uint64]struct{}{}
for _, e := range a.log {
aliveThis, ok := alive[e.table]
if !ok {
aliveThis = map[uint64]bool{}
aliveThis = map[uint64]struct{}{}
alive[e.table] = aliveThis
}
switch e.act {
case actInsert:
aliveThis[e.id] = true
aliveThis[e.id] = struct{}{}
case actDelete:
delete(aliveThis, e.id)
case actDeleteAll:
Expand All @@ -136,31 +136,27 @@ func (a *realActionLog) validate(db *statedb.DB, t *testing.T) {
for table, expected := range alive {
txn := db.ReadTxn()
iter, _ := table.All(txn)
actual := map[uint64]bool{}
actual := map[uint64]struct{}{}
for obj, _, ok := iter.Next(); ok; obj, _, ok = iter.Next() {
actual[obj.id] = true
actual[obj.id] = struct{}{}
}
require.Equal(t, expected, actual, "validate failed, mismatching ids: %v",
setSymmetricDifference(actual, expected))
}
}

func setSymmetricDifference[T comparable, M map[T]bool](s1, s2 M) M {
func setSymmetricDifference[T comparable, M map[T]struct{}](s1, s2 M) M {
counts := make(map[T]int, len(s1)+len(s2))
for k1, v1 := range s1 {
if v1 {
counts[k1] = 1
}
for k1 := range s1 {
counts[k1] = 1
}
for k2, v2 := range s2 {
if v2 {
counts[k2]++
}
for k2 := range s2 {
counts[k2]++
}
result := M{}
for k, count := range counts {
if count == 1 {
result[k] = true
result[k] = struct{}{}
}
}
return result
Expand Down
10 changes: 5 additions & 5 deletions reconciler/example/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ func (ops *MemoOps) Delete(ctx context.Context, txn statedb.ReadTxn, memo *Memo)

// Prune unexpected memos.
func (ops *MemoOps) Prune(ctx context.Context, txn statedb.ReadTxn, iter statedb.Iterator[*Memo]) error {
expected := map[string]bool{}
expected := map[string]struct{}{}
for memo, _, ok := iter.Next(); ok; memo, _, ok = iter.Next() {
expected[memo.Name] = true
expected[memo.Name] = struct{}{}
}

// Find unexpected files
unexpected := map[string]bool{}
unexpected := map[string]struct{}{}
if entries, err := os.ReadDir(ops.directory); err != nil {
return err
} else {
for _, entry := range entries {
if !expected[entry.Name()] {
unexpected[entry.Name()] = true
if _, ok := expected[entry.Name()]; !ok {
unexpected[entry.Name()] = struct{}{}
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ func NewTable[Obj any](
}

// Validate that indexes have unique ids.
indexNames := map[string]bool{}
indexNames[primaryIndexer.indexName()] = true
indexNames := map[string]struct{}{}
indexNames[primaryIndexer.indexName()] = struct{}{}
for _, indexer := range secondaryIndexers {
if indexNames[indexer.indexName()] {
if _, ok := indexNames[indexer.indexName()]; ok {
return nil, tableError(tableName, fmt.Errorf("index %q: %w", indexer.indexName(), ErrDuplicateIndex))
}
indexNames[indexer.indexName()] = true
indexNames[indexer.indexName()] = struct{}{}
}
for name := range indexNames {
if strings.HasPrefix(name, reservedIndexPrefix) {
Expand Down

0 comments on commit 26d4332

Please sign in to comment.