restic/backend/idset.go

71 lines
1.1 KiB
Go
Raw Normal View History

2015-07-26 20:04:03 +00:00
package backend
// IDSet is a set of IDs.
type IDSet map[ID]struct{}
// NewIDSet returns a new IDSet, populated with ids.
func NewIDSet(ids ...ID) IDSet {
m := make(IDSet)
for _, id := range ids {
m[id] = struct{}{}
}
return m
}
// Has returns true iff id is contained in the set.
func (s IDSet) Has(id ID) bool {
_, ok := s[id]
return ok
}
// Insert adds id to the set.
func (s IDSet) Insert(id ID) {
s[id] = struct{}{}
}
// Delete removes id from the set.
func (s IDSet) Delete(id ID) {
delete(s, id)
}
2015-08-16 14:07:51 +00:00
// List returns a slice of all IDs in the set.
func (s IDSet) List() IDs {
list := make(IDs, 0, len(s))
for id := range s {
list = append(list, id)
}
return list
}
2015-10-25 14:28:01 +00:00
// Equals returns true iff s equals other.
func (s IDSet) Equals(other IDSet) bool {
if len(s) != len(other) {
return false
}
for id := range s {
if _, ok := other[id]; !ok {
return false
}
}
for id := range other {
if _, ok := s[id]; !ok {
return false
}
}
return true
}
2015-08-16 14:07:51 +00:00
func (s IDSet) String() string {
str := s.List().String()
if len(str) < 2 {
return "{}"
}
2015-11-01 21:14:44 +00:00
return "{" + str[1:len(str)-1] + "}"
2015-08-16 14:07:51 +00:00
}