Documentation
¶
Index ¶
- type Set
- func (s *Set) Clear()
- func (s Set) Collect() (ss SetSlice)
- func (s Set) Contains(si SetItem) (ok bool)
- func (s Set) Difference(other Set) (set Set)
- func (s Set) Insert(si SetItem) (ok bool)
- func (s Set) Intersection(other Set) (set Set)
- func (s Set) IsEmpty() bool
- func (s Set) IsEqual(other Set) bool
- func (s Set) Remove(si SetItem) (ok bool)
- func (s Set) Size() int
- func (s Set) SubsetOf(other Set) bool
- func (s Set) SymmetricDifference(other Set) (set Set)
- func (s Set) ToString() string
- func (s Set) Union(other Set) (set Set)
- type SetItem
- type SetSlice
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Set ¶
type Set map[SetItem]membership
Set is just a set.
func (*Set) Clear ¶
func (s *Set) Clear()
Clear clears the set, removing all values.
Example ¶
package main
import (
"fmt"
coll "github.com/xgodev/boost/utils/collections"
)
func main() {
set := coll.MakeSet("cat", "dog", "cow")
set.Clear()
fmt.Println(set.Size())
}
Output: 0
func (Set) Collect ¶
Collect returns a slice of all the elements present in the set in arbitrary order.
func (Set) Contains ¶
Contains returns true if the set contains a value.
Example ¶
package main
import (
"fmt"
coll "github.com/xgodev/boost/utils/collections"
)
func main() {
set := coll.MakeSet("cat", "dog", "cow")
ok := set.Contains("cat")
fmt.Println(ok)
ok = set.Contains("buffalo")
fmt.Println(ok)
}
Output: true false
func (Set) Difference ¶
Difference returns the set of values that are in s but not in other.
Example ¶
package main
import (
"fmt"
coll "github.com/xgodev/boost/utils/collections"
)
func main() {
set1 := coll.MakeSet(1, 2, 3)
set2 := coll.MakeSet(4, 2, 3, 4)
diff1 := set1.Difference(set2)
fmt.Println(diff1.Contains(1))
fmt.Println(diff1.Contains(2))
fmt.Println(diff1.Contains(3))
fmt.Println(diff1.Contains(4))
diff2 := set2.Difference(set1)
fmt.Println(diff2.Contains(4))
fmt.Println(diff2.Contains(1))
fmt.Println(diff2.Contains(2))
fmt.Println(diff2.Contains(3))
}
Output: true false false false true false false false
func (Set) Insert ¶
Insert adds a value to the set. If the set did not have this value present, true is returned. If the set did have this value present, false is returned.
Example ¶
package main
import (
"fmt"
coll "github.com/xgodev/boost/utils/collections"
)
func main() {
set := coll.MakeSet()
ok := set.Insert("cat")
fmt.Println(ok)
ok = set.Insert("cat")
fmt.Println(ok)
}
Output: true false
func (Set) Intersection ¶
Intersection returns the set of values that are both in s and other.
Example ¶
package main
import (
"fmt"
coll "github.com/xgodev/boost/utils/collections"
)
func main() {
set1 := coll.MakeSet("cat", "dog", "cow")
set2 := coll.MakeSet("cat", "duck", "bull")
intersection := set1.Intersection(set2)
fmt.Println(intersection.Contains("cat"))
fmt.Println(intersection.Contains("dog"))
fmt.Println(intersection.Contains("cow"))
fmt.Println(intersection.Contains("duck"))
fmt.Println(intersection.Contains("bull"))
}
Output: true false false false false
func (Set) IsEmpty ¶
IsEmpty returns true if the set contains no elements.
Example ¶
package main
import (
"fmt"
coll "github.com/xgodev/boost/utils/collections"
)
func main() {
set := coll.MakeSet()
fmt.Println(set.IsEmpty())
set.Insert("cat")
fmt.Println(set.IsEmpty())
}
Output: true false
func (Set) IsEqual ¶
IsEqual returns true if s and other are the same.
Example ¶
package main
import (
"fmt"
coll "github.com/xgodev/boost/utils/collections"
)
func main() {
set1 := coll.MakeSet("cat", "dog", "cow")
set2 := coll.MakeSet("cat", "dog")
set3 := coll.MakeSet("frog")
set4 := coll.MakeSet("cow", "cat", "dog")
fmt.Println(set4.IsEqual(set4))
fmt.Println(set1.IsEqual(set4))
fmt.Println(set2.IsEqual(set3))
}
Output: true true false
func (Set) Remove ¶
Remove a value from the set. Returns true if the value was present in the set.
Example ¶
package main
import (
"fmt"
coll "github.com/xgodev/boost/utils/collections"
)
func main() {
set := coll.MakeSet("cat", "dog", "cow")
ok := set.Remove("cat")
fmt.Println(ok)
ok = set.Remove("cat")
fmt.Println(ok)
}
Output: true false
func (Set) SubsetOf ¶
SubsetOf returns true if s is a subset of other.
Example ¶
package main
import (
"fmt"
coll "github.com/xgodev/boost/utils/collections"
)
func main() {
set1 := coll.MakeSet("cat", "dog", "cow")
set2 := coll.MakeSet("cat", "dog")
set3 := coll.MakeSet("frog")
set4 := coll.MakeSet("cat", "dog", "frog", "cow")
fmt.Println(set2.SubsetOf(set1))
fmt.Println(set1.SubsetOf(set2))
fmt.Println(set4.SubsetOf(set3))
fmt.Println(set3.SubsetOf(set4))
}
Output: true false false true
func (Set) SymmetricDifference ¶
SymmetricDifference returns the set of values that are in s or in other but not in both.
Example ¶
package main
import (
"fmt"
coll "github.com/xgodev/boost/utils/collections"
)
func main() {
set1 := coll.MakeSet(1, 2, 3)
set2 := coll.MakeSet(4, 2, 3, 4)
symDiff := set1.SymmetricDifference(set2)
fmt.Println(symDiff.Contains(1))
fmt.Println(symDiff.Contains(4))
fmt.Println(symDiff.Contains(2))
fmt.Println(symDiff.Contains(3))
}
Output: true true false false
func (Set) Union ¶
Union returns the set of values that are in s or in other, without duplicates.
Example ¶
package main
import (
"fmt"
coll "github.com/xgodev/boost/utils/collections"
)
func main() {
set1 := coll.MakeSet("cat", "dog", "cow")
set2 := coll.MakeSet("cat", "duck", "bull")
union := set1.Union(set2)
fmt.Println(union.Contains("cat"))
fmt.Println(union.Contains("dog"))
fmt.Println(union.Contains("cow"))
fmt.Println(union.Contains("duck"))
fmt.Println(union.Contains("bull"))
}
Output: true true true true true