github.com/fawick/restic@v0.1.1-0.20171126184616-c02923fbfc79/internal/restic/idset.go (about) 1 package restic 2 3 import "sort" 4 5 // IDSet is a set of IDs. 6 type IDSet map[ID]struct{} 7 8 // NewIDSet returns a new IDSet, populated with ids. 9 func NewIDSet(ids ...ID) IDSet { 10 m := make(IDSet) 11 for _, id := range ids { 12 m[id] = struct{}{} 13 } 14 15 return m 16 } 17 18 // Has returns true iff id is contained in the set. 19 func (s IDSet) Has(id ID) bool { 20 _, ok := s[id] 21 return ok 22 } 23 24 // Insert adds id to the set. 25 func (s IDSet) Insert(id ID) { 26 s[id] = struct{}{} 27 } 28 29 // Delete removes id from the set. 30 func (s IDSet) Delete(id ID) { 31 delete(s, id) 32 } 33 34 // List returns a slice of all IDs in the set. 35 func (s IDSet) List() IDs { 36 list := make(IDs, 0, len(s)) 37 for id := range s { 38 list = append(list, id) 39 } 40 41 sort.Sort(list) 42 43 return list 44 } 45 46 // Equals returns true iff s equals other. 47 func (s IDSet) Equals(other IDSet) bool { 48 if len(s) != len(other) { 49 return false 50 } 51 52 for id := range s { 53 if _, ok := other[id]; !ok { 54 return false 55 } 56 } 57 58 // length + one-way comparison is sufficient implication of equality 59 60 return true 61 } 62 63 // Merge adds the blobs in other to the current set. 64 func (s IDSet) Merge(other IDSet) { 65 for id := range other { 66 s.Insert(id) 67 } 68 } 69 70 // Intersect returns a new set containing the IDs that are present in both sets. 71 func (s IDSet) Intersect(other IDSet) (result IDSet) { 72 result = NewIDSet() 73 74 set1 := s 75 set2 := other 76 77 // iterate over the smaller set 78 if len(set2) < len(set1) { 79 set1, set2 = set2, set1 80 } 81 82 for id := range set1 { 83 if set2.Has(id) { 84 result.Insert(id) 85 } 86 } 87 88 return result 89 } 90 91 // Sub returns a new set containing all IDs that are present in s but not in 92 // other. 93 func (s IDSet) Sub(other IDSet) (result IDSet) { 94 result = NewIDSet() 95 for id := range s { 96 if !other.Has(id) { 97 result.Insert(id) 98 } 99 } 100 101 return result 102 } 103 104 func (s IDSet) String() string { 105 str := s.List().String() 106 if len(str) < 2 { 107 return "{}" 108 } 109 110 return "{" + str[1:len(str)-1] + "}" 111 }