gonum.org/v1/gonum@v0.14.0/graph/internal/set/set.go (about) 1 // Copyright ©2014 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package set 6 7 import "gonum.org/v1/gonum/graph" 8 9 // Ints is a set of int identifiers. 10 type Ints map[int]struct{} 11 12 // The simple accessor methods for Ints are provided to allow ease of 13 // implementation change should the need arise. 14 15 // Add inserts an element into the set. 16 func (s Ints) Add(e int) { 17 s[e] = struct{}{} 18 } 19 20 // Has reports the existence of the element in the set. 21 func (s Ints) Has(e int) bool { 22 _, ok := s[e] 23 return ok 24 } 25 26 // Remove deletes the specified element from the set. 27 func (s Ints) Remove(e int) { 28 delete(s, e) 29 } 30 31 // Count reports the number of elements stored in the set. 32 func (s Ints) Count() int { 33 return len(s) 34 } 35 36 // IntsEqual reports set equality between the parameters. Sets are equal if 37 // and only if they have the same elements. 38 func IntsEqual(a, b Ints) bool { 39 if intsSame(a, b) { 40 return true 41 } 42 43 if len(a) != len(b) { 44 return false 45 } 46 47 for e := range a { 48 if _, ok := b[e]; !ok { 49 return false 50 } 51 } 52 53 return true 54 } 55 56 // Int64s is a set of int64 identifiers. 57 type Int64s map[int64]struct{} 58 59 // The simple accessor methods for Ints are provided to allow ease of 60 // implementation change should the need arise. 61 62 // Add inserts an element into the set. 63 func (s Int64s) Add(e int64) { 64 s[e] = struct{}{} 65 } 66 67 // Has reports the existence of the element in the set. 68 func (s Int64s) Has(e int64) bool { 69 _, ok := s[e] 70 return ok 71 } 72 73 // Remove deletes the specified element from the set. 74 func (s Int64s) Remove(e int64) { 75 delete(s, e) 76 } 77 78 // Count reports the number of elements stored in the set. 79 func (s Int64s) Count() int { 80 return len(s) 81 } 82 83 // Int64sEqual reports set equality between the parameters. Sets are equal if 84 // and only if they have the same elements. 85 func Int64sEqual(a, b Int64s) bool { 86 if int64sSame(a, b) { 87 return true 88 } 89 90 if len(a) != len(b) { 91 return false 92 } 93 94 for e := range a { 95 if _, ok := b[e]; !ok { 96 return false 97 } 98 } 99 100 return true 101 } 102 103 // Nodes is a set of nodes keyed in their integer identifiers. 104 type Nodes map[int64]graph.Node 105 106 // NewNodes returns a new Nodes. 107 func NewNodes() Nodes { 108 return make(Nodes) 109 } 110 111 // NewNodes returns a new Nodes with the given size hint, n. 112 func NewNodesSize(n int) Nodes { 113 return make(Nodes, n) 114 } 115 116 // The simple accessor methods for Nodes are provided to allow ease of 117 // implementation change should the need arise. 118 119 // Add inserts an element into the set. 120 func (s Nodes) Add(n graph.Node) { 121 s[n.ID()] = n 122 } 123 124 // Remove deletes the specified element from the set. 125 func (s Nodes) Remove(e graph.Node) { 126 delete(s, e.ID()) 127 } 128 129 // Count returns the number of element in the set. 130 func (s Nodes) Count() int { 131 return len(s) 132 } 133 134 // Has reports the existence of the elements in the set. 135 func (s Nodes) Has(n graph.Node) bool { 136 _, ok := s[n.ID()] 137 return ok 138 } 139 140 // CloneNodes returns a clone of src. 141 func CloneNodes(src Nodes) Nodes { 142 dst := make(Nodes, len(src)) 143 for e, n := range src { 144 dst[e] = n 145 } 146 return dst 147 } 148 149 // Equal reports set equality between the parameters. Sets are equal if 150 // and only if they have the same elements. 151 func Equal(a, b Nodes) bool { 152 if same(a, b) { 153 return true 154 } 155 156 if len(a) != len(b) { 157 return false 158 } 159 160 for e := range a { 161 if _, ok := b[e]; !ok { 162 return false 163 } 164 } 165 166 return true 167 } 168 169 // UnionOfNodes returns the union of a and b. 170 // 171 // The union of two sets, a and b, is the set containing all the 172 // elements of each, for instance: 173 // 174 // {a,b,c} UNION {d,e,f} = {a,b,c,d,e,f} 175 // 176 // Since sets may not have repetition, unions of two sets that overlap 177 // do not contain repeat elements, that is: 178 // 179 // {a,b,c} UNION {b,c,d} = {a,b,c,d} 180 func UnionOfNodes(a, b Nodes) Nodes { 181 if same(a, b) { 182 return CloneNodes(a) 183 } 184 185 dst := make(Nodes) 186 for e, n := range a { 187 dst[e] = n 188 } 189 for e, n := range b { 190 dst[e] = n 191 } 192 193 return dst 194 } 195 196 // IntersectionOfNodes returns the intersection of a and b. 197 // 198 // The intersection of two sets, a and b, is the set containing all 199 // the elements shared between the two sets, for instance: 200 // 201 // {a,b,c} INTERSECT {b,c,d} = {b,c} 202 // 203 // The intersection between a set and itself is itself, and thus 204 // effectively a copy operation: 205 // 206 // {a,b,c} INTERSECT {a,b,c} = {a,b,c} 207 // 208 // The intersection between two sets that share no elements is the empty 209 // set: 210 // 211 // {a,b,c} INTERSECT {d,e,f} = {} 212 func IntersectionOfNodes(a, b Nodes) Nodes { 213 if same(a, b) { 214 return CloneNodes(a) 215 } 216 dst := make(Nodes) 217 if len(a) > len(b) { 218 a, b = b, a 219 } 220 for e, n := range a { 221 if _, ok := b[e]; ok { 222 dst[e] = n 223 } 224 } 225 return dst 226 }