gorgonia.org/gorgonia@v0.9.17/node_set.go (about) 1 // Generated by: main 2 // TypeWriter: set 3 // Directive: +gen on *Node 4 5 package gorgonia 6 7 // Set is a modification of https://github.com/deckarep/golang-set 8 // The MIT License (MIT) 9 // Copyright (c) 2013 Ralph Caraveo (deckarep@gmail.com) 10 11 // NodeSet is the primary type that represents a set 12 type NodeSet map[*Node]struct{} 13 14 // NewNodeSet creates and returns a reference to an empty set. 15 func NewNodeSet(a ...*Node) NodeSet { 16 s := make(NodeSet) 17 for _, i := range a { 18 s.Add(i) 19 } 20 return s 21 } 22 23 // ToSlice returns the elements of the current set as a slice 24 func (set NodeSet) ToSlice() Nodes { 25 var s Nodes 26 for v := range set { 27 s = append(s, v) 28 } 29 return s 30 } 31 32 // Add adds an item to the current set if it doesn't already exist in the set. 33 func (set NodeSet) Add(i *Node) bool { 34 _, found := set[i] 35 set[i] = struct{}{} 36 return !found //False if it existed already 37 } 38 39 // Contains determines if a given item is already in the set. 40 func (set NodeSet) Contains(i *Node) bool { 41 _, found := set[i] 42 return found 43 } 44 45 // ContainsAll determines if the given items are all in the set 46 func (set NodeSet) ContainsAll(i ...*Node) bool { 47 for _, v := range i { 48 if !set.Contains(v) { 49 return false 50 } 51 } 52 return true 53 } 54 55 // IsSubset determines if every item in the other set is in this set. 56 func (set NodeSet) IsSubset(other NodeSet) bool { 57 for elem := range set { 58 if !other.Contains(elem) { 59 return false 60 } 61 } 62 return true 63 } 64 65 // IsSuperset determines if every item of this set is in the other set. 66 func (set NodeSet) IsSuperset(other NodeSet) bool { 67 return other.IsSubset(set) 68 } 69 70 // Union returns a new set with all items in both sets. 71 func (set NodeSet) Union(other NodeSet) NodeSet { 72 unionedSet := NewNodeSet() 73 74 for elem := range set { 75 unionedSet.Add(elem) 76 } 77 for elem := range other { 78 unionedSet.Add(elem) 79 } 80 return unionedSet 81 } 82 83 // Intersect returns a new set with items that exist only in both sets. 84 func (set NodeSet) Intersect(other NodeSet) NodeSet { 85 intersection := NewNodeSet() 86 // loop over smaller set 87 if set.Cardinality() < other.Cardinality() { 88 for elem := range set { 89 if other.Contains(elem) { 90 intersection.Add(elem) 91 } 92 } 93 } else { 94 for elem := range other { 95 if set.Contains(elem) { 96 intersection.Add(elem) 97 } 98 } 99 } 100 return intersection 101 } 102 103 // Difference returns a new set with items in the current set but not in the other set 104 func (set NodeSet) Difference(other NodeSet) NodeSet { 105 differencedSet := NewNodeSet() 106 for elem := range set { 107 if !other.Contains(elem) { 108 differencedSet.Add(elem) 109 } 110 } 111 return differencedSet 112 } 113 114 // SymmetricDifference returns a new set with items in the current set or the other set but not in both. 115 func (set NodeSet) SymmetricDifference(other NodeSet) NodeSet { 116 aDiff := set.Difference(other) 117 bDiff := other.Difference(set) 118 return aDiff.Union(bDiff) 119 } 120 121 // Clear clears the entire set to be the empty set. 122 func (set *NodeSet) Clear() { 123 *set = make(NodeSet) 124 } 125 126 // Remove allows the removal of a single item in the set. 127 func (set NodeSet) Remove(i *Node) { 128 delete(set, i) 129 } 130 131 // Cardinality returns how many items are currently in the set. 132 func (set NodeSet) Cardinality() int { 133 return len(set) 134 } 135 136 // Iter returns a channel of type *Node that you can range over. 137 func (set NodeSet) Iter() <-chan *Node { 138 ch := make(chan *Node) 139 go func() { 140 for elem := range set { 141 ch <- elem 142 } 143 close(ch) 144 }() 145 146 return ch 147 } 148 149 // Equal determines if two sets are equal to each other. 150 // If they both are the same size and have the same items they are considered equal. 151 // Order of items is not relevant for sets to be equal. 152 func (set NodeSet) Equal(other NodeSet) bool { 153 if set.Cardinality() != other.Cardinality() { 154 return false 155 } 156 for elem := range set { 157 if !other.Contains(elem) { 158 return false 159 } 160 } 161 return true 162 } 163 164 // Clone returns a clone of the set. 165 // Does NOT clone the underlying elements. 166 func (set NodeSet) Clone() NodeSet { 167 clonedSet := NewNodeSet() 168 for elem := range set { 169 clonedSet.Add(elem) 170 } 171 return clonedSet 172 }