prune: hide implementation details of counted blob set

This commit is contained in:
Michael Eischer committed 2024-05-24 21:42:56 +02:00
1 parent ff4775a15f
commit 93098e9265
4 files changed
+61 -42

No files matched your search

+40 -21
View File
@@ -5,42 +5,54 @@ import "sort"
// CountedBlobSet is a set of blobs. For each blob it also stores a uint8 value
// which can be used to track some information. The CountedBlobSet does not use
// that value in any way. New entries are created with value 0.
type CountedBlobSet map[BlobHandle]uint8
type CountedBlobSet struct {
m map[BlobHandle]uint8
}
// NewCountedBlobSet returns a new CountedBlobSet, populated with ids.
func NewCountedBlobSet(handles ...BlobHandle) CountedBlobSet {
m := make(CountedBlobSet)
func NewCountedBlobSet(handles ...BlobHandle) *CountedBlobSet {
m := CountedBlobSet{}
m.m = make(map[BlobHandle]uint8)
for _, h := range handles {
m[h] = 0
m.m[h] = 0
}
return m
return &m
}
func (s *CountedBlobSet) Get(h BlobHandle) (uint8, bool) {
val, ok := s.m[h]
return val, ok
}
func (s *CountedBlobSet) Set(h BlobHandle, value uint8) {
s.m[h] = value
}
// Has returns true iff id is contained in the set.
func (s CountedBlobSet) Has(h BlobHandle) bool {
_, ok := s[h]
func (s *CountedBlobSet) Has(h BlobHandle) bool {
_, ok := s.m[h]
return ok
}
// Insert adds id to the set.
func (s CountedBlobSet) Insert(h BlobHandle) {
s[h] = 0
func (s *CountedBlobSet) Insert(h BlobHandle) {
s.m[h] = 0
}
// Delete removes id from the set.
func (s CountedBlobSet) Delete(h BlobHandle) {
delete(s, h)
func (s *CountedBlobSet) Delete(h BlobHandle) {
delete(s.m, h)
}
func (s CountedBlobSet) Len() int {
return len(s)
func (s *CountedBlobSet) Len() int {
return len(s.m)
}
// List returns a sorted slice of all BlobHandle in the set.
func (s CountedBlobSet) List() BlobHandles {
list := make(BlobHandles, 0, len(s))
for h := range s {
func (s *CountedBlobSet) List() BlobHandles {
list := make(BlobHandles, 0, len(s.m))
for h := range s.m {
list = append(list, h)
}
@@ -49,7 +61,7 @@ func (s CountedBlobSet) List() BlobHandles {
return list
}
func (s CountedBlobSet) String() string {
func (s *CountedBlobSet) String() string {
str := s.List().String()
if len(str) < 2 {
return "{}"
@@ -59,10 +71,17 @@ func (s CountedBlobSet) String() string {
}
// Copy returns a copy of the CountedBlobSet.
func (s CountedBlobSet) Copy() CountedBlobSet {
cp := make(CountedBlobSet, len(s))
for k, v := range s {
cp[k] = v
func (s *CountedBlobSet) Copy() *CountedBlobSet {
cp := &CountedBlobSet{}
cp.m = make(map[BlobHandle]uint8, len(s.m))
for k, v := range s.m {
cp.m[k] = v
}
return cp
}
func (s *CountedBlobSet) For(cb func(h BlobHandle, value uint8)) {
for k, v := range s.m {
cb(k, v)
}
}