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