2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-27 15:26:37 +00:00
restic/internal/restic/blob_set.go

110 lines
1.9 KiB
Go
Raw Normal View History

2016-08-31 18:58:57 +00:00
package restic
2016-08-03 19:51:20 +00:00
import "sort"
// BlobSet is a set of blobs.
2016-08-31 18:58:57 +00:00
type BlobSet map[BlobHandle]struct{}
2016-08-03 19:51:20 +00:00
// NewBlobSet returns a new BlobSet, populated with ids.
2016-08-31 18:58:57 +00:00
func NewBlobSet(handles ...BlobHandle) BlobSet {
2016-08-03 19:51:20 +00:00
m := make(BlobSet)
for _, h := range handles {
m[h] = struct{}{}
}
return m
}
// Has returns true iff id is contained in the set.
2016-08-31 18:58:57 +00:00
func (s BlobSet) Has(h BlobHandle) bool {
2016-08-03 19:51:20 +00:00
_, ok := s[h]
return ok
}
// Insert adds id to the set.
2016-08-31 18:58:57 +00:00
func (s BlobSet) Insert(h BlobHandle) {
2016-08-03 19:51:20 +00:00
s[h] = struct{}{}
}
// Delete removes id from the set.
2016-08-31 18:58:57 +00:00
func (s BlobSet) Delete(h BlobHandle) {
2016-08-03 19:51:20 +00:00
delete(s, h)
}
// Equals returns true iff s equals other.
func (s BlobSet) Equals(other BlobSet) bool {
if len(s) != len(other) {
return false
}
for h := range s {
if _, ok := other[h]; !ok {
return false
}
}
return true
}
// Merge adds the blobs in other to the current set.
func (s BlobSet) Merge(other BlobSet) {
for h := range other {
s.Insert(h)
}
}
// Intersect returns a new set containing the handles that are present in both sets.
func (s BlobSet) Intersect(other BlobSet) (result BlobSet) {
result = NewBlobSet()
set1 := s
set2 := other
// iterate over the smaller set
if len(set2) < len(set1) {
set1, set2 = set2, set1
}
for h := range set1 {
if set2.Has(h) {
result.Insert(h)
}
}
return result
}
// Sub returns a new set containing all handles that are present in s but not in
// other.
func (s BlobSet) Sub(other BlobSet) (result BlobSet) {
result = NewBlobSet()
for h := range s {
if !other.Has(h) {
result.Insert(h)
}
}
return result
}
2016-08-31 18:58:57 +00:00
// List returns a sorted slice of all BlobHandle in the set.
func (s BlobSet) List() BlobHandles {
list := make(BlobHandles, 0, len(s))
2016-08-03 19:51:20 +00:00
for h := range s {
list = append(list, h)
}
sort.Sort(list)
return list
}
func (s BlobSet) String() string {
str := s.List().String()
if len(str) < 2 {
return "{}"
}
return "{" + str[1:len(str)-1] + "}"
}