Documentation ¶
Overview ¶
Package set provides a basic generic set implementation.
Index ¶
- func Cmp[B BuiltIn](x, y B) int
- func InsertSetFunc[T, E any](a Common[T], b Common[E], transform func(T) E) bool
- func InsertSliceFunc[T, E any](s Common[T], items []E, f func(element E) T)
- func SliceFunc[T, E any](s Common[T], transform func(T) E) []E
- type BuiltIn
- type Common
- type Compare
- type Hash
- type HashFunc
- type HashSet
- func (s *HashSet[T, H]) Contains(item T) bool
- func (s *HashSet[T, H]) ContainsAll(items []T) bool
- func (s *HashSet[T, H]) ContainsSlice(items []T) bool
- func (s *HashSet[T, H]) Copy() *HashSet[T, H]
- func (s *HashSet[T, H]) Difference(o *HashSet[T, H]) *HashSet[T, H]
- func (s *HashSet[T, H]) Empty() bool
- func (s *HashSet[T, H]) Equal(o *HashSet[T, H]) bool
- func (s *HashSet[T, H]) EqualSlice(items []T) bool
- func (s *HashSet[T, H]) ForEach(visit func(T) bool)
- func (s *HashSet[T, H]) Insert(item T) bool
- func (s *HashSet[T, H]) InsertAll(items []T) booldeprecated
- func (s *HashSet[T, H]) InsertSet(o *HashSet[T, H]) bool
- func (s *HashSet[T, H]) InsertSlice(items []T) bool
- func (s *HashSet[T, H]) Intersect(o *HashSet[T, H]) *HashSet[T, H]
- func (s *HashSet[T, H]) List() []Tdeprecated
- func (s *HashSet[T, H]) MarshalJSON() ([]byte, error)
- func (s *HashSet[T, H]) Remove(item T) bool
- func (s *HashSet[T, H]) RemoveAll(items []T) booldeprecated
- func (s *HashSet[T, H]) RemoveFunc(f func(item T) bool) bool
- func (s *HashSet[T, H]) RemoveSet(o *HashSet[T, H]) bool
- func (s *HashSet[T, H]) RemoveSlice(items []T) bool
- func (s *HashSet[T, H]) Size() int
- func (s *HashSet[T, H]) Slice() []T
- func (s *HashSet[T, H]) String() string
- func (s *HashSet[T, H]) StringFunc(f func(element T) string) string
- func (s *HashSet[T, H]) Subset(o *HashSet[T, H]) bool
- func (s *HashSet[T, H]) Union(o *HashSet[T, H]) *HashSet[T, H]
- func (s *HashSet[T, H]) UnmarshalJSON(data []byte) error
- type Set
- func (s *Set[T]) Contains(item T) bool
- func (s *Set[T]) ContainsAll(items []T) bool
- func (s *Set[T]) ContainsSlice(items []T) bool
- func (s *Set[T]) Copy() *Set[T]
- func (s *Set[T]) Difference(o *Set[T]) *Set[T]
- func (s *Set[T]) Empty() bool
- func (s *Set[T]) Equal(o *Set[T]) bool
- func (s *Set[T]) EqualSlice(items []T) bool
- func (s *Set[T]) ForEach(visit func(T) bool)
- func (s *Set[T]) Insert(item T) bool
- func (s *Set[T]) InsertAll(items []T) booldeprecated
- func (s *Set[T]) InsertSet(o *Set[T]) bool
- func (s *Set[T]) InsertSlice(items []T) bool
- func (s *Set[T]) Intersect(o *Set[T]) *Set[T]
- func (s *Set[T]) List() []Tdeprecated
- func (s *Set[T]) MarshalJSON() ([]byte, error)
- func (s *Set[T]) Remove(item T) bool
- func (s *Set[T]) RemoveAll(items []T) booldeprecated
- func (s *Set[T]) RemoveFunc(f func(T) bool) bool
- func (s *Set[T]) RemoveSet(o *Set[T]) bool
- func (s *Set[T]) RemoveSlice(items []T) bool
- func (s *Set[T]) Size() int
- func (s *Set[T]) Slice() []T
- func (s *Set[T]) String() string
- func (s *Set[T]) StringFunc(f func(element T) string) string
- func (s *Set[T]) Subset(o *Set[T]) bool
- func (s *Set[T]) Union(o *Set[T]) *Set[T]
- func (s *Set[T]) UnmarshalJSON(data []byte) error
- type TreeNodeVisit
- type TreeSet
- func (s *TreeSet[T, C]) Above(item T) *TreeSet[T, C]
- func (s *TreeSet[T, C]) AboveEqual(item T) *TreeSet[T, C]
- func (s *TreeSet[T, C]) Below(item T) *TreeSet[T, C]
- func (s *TreeSet[T, C]) BelowEqual(item T) *TreeSet[T, C]
- func (s *TreeSet[T, C]) BottomK(n int) []T
- func (s *TreeSet[T, C]) Contains(item T) bool
- func (s *TreeSet[T, C]) ContainsSlice(items []T) bool
- func (s *TreeSet[T, C]) Copy() *TreeSet[T, C]
- func (s *TreeSet[T, C]) Difference(o *TreeSet[T, C]) *TreeSet[T, C]
- func (s *TreeSet[T, C]) Empty() bool
- func (s *TreeSet[T, C]) Equal(o *TreeSet[T, C]) bool
- func (s *TreeSet[T, C]) EqualSlice(items []T) bool
- func (s *TreeSet[T, C]) FilterSlice(filter func(T) bool) []T
- func (s *TreeSet[T, C]) FirstAbove(item T) (T, bool)
- func (s *TreeSet[T, C]) FirstAboveEqual(item T) (T, bool)
- func (s *TreeSet[T, C]) FirstBelow(item T) (T, bool)
- func (s *TreeSet[T, C]) FirstBelowEqual(item T) (T, bool)
- func (s *TreeSet[T, C]) ForEach(visit func(T) bool)
- func (s *TreeSet[T, C]) Insert(item T) bool
- func (s *TreeSet[T, C]) InsertSet(o *TreeSet[T, C]) bool
- func (s *TreeSet[T, C]) InsertSlice(items []T) bool
- func (s *TreeSet[T, C]) Intersect(o *TreeSet[T, C]) *TreeSet[T, C]
- func (s *TreeSet[T, C]) MarshalJSON() ([]byte, error)
- func (s *TreeSet[T, C]) Max() T
- func (s *TreeSet[T, C]) Min() T
- func (s *TreeSet[T, C]) Remove(item T) bool
- func (s *TreeSet[T, C]) RemoveFunc(f func(T) bool) bool
- func (s *TreeSet[T, C]) RemoveSet(o *TreeSet[T, C]) bool
- func (s *TreeSet[T, C]) RemoveSlice(items []T) bool
- func (s *TreeSet[T, C]) Size() int
- func (s *TreeSet[T, C]) Slice() []T
- func (s *TreeSet[T, C]) String() string
- func (s *TreeSet[T, C]) StringFunc(f func(element T) string) string
- func (s *TreeSet[T, C]) Subset(o *TreeSet[T, C]) bool
- func (s *TreeSet[T, C]) TopK(n int) []T
- func (s *TreeSet[T, C]) Union(o *TreeSet[T, C]) *TreeSet[T, C]
- func (s *TreeSet[T, C]) UnmarshalJSON(data []byte) error
Examples ¶
- Cmp (Ints)
- Cmp (Strings)
- Compare (Contestant)
- HashSet.Contains
- HashSet.ContainsAll
- HashSet.ContainsSlice
- HashSet.Copy
- HashSet.Difference
- HashSet.Empty
- HashSet.Equal
- HashSet.Insert
- HashSet.InsertSet
- HashSet.InsertSlice
- HashSet.Intersect
- HashSet.Remove
- HashSet.RemoveFunc
- HashSet.RemoveSet
- HashSet.RemoveSlice
- HashSet.Size
- HashSet.Slice
- HashSet.String
- HashSet.Subset
- HashSet.Union
- Set.Contains
- Set.ContainsAll
- Set.ContainsSlice
- Set.Copy
- Set.Difference
- Set.Empty
- Set.Equal
- Set.Insert
- Set.InsertSet
- Set.InsertSlice
- Set.Intersect
- Set.Remove
- Set.RemoveFunc
- Set.RemoveSet
- Set.RemoveSlice
- Set.Size
- Set.Slice
- Set.String
- Set.Subset
- Set.Union
- TreeSet.Above
- TreeSet.AboveEqual
- TreeSet.Below
- TreeSet.BelowEqual
- TreeSet.BottomK
- TreeSet.Contains
- TreeSet.ContainsSlice
- TreeSet.Copy
- TreeSet.Difference
- TreeSet.Empty
- TreeSet.Equal
- TreeSet.FirstAbove
- TreeSet.FirstAboveEqual
- TreeSet.FirstBelow
- TreeSet.FirstBelowEqual
- TreeSet.Insert
- TreeSet.InsertSet
- TreeSet.InsertSlice
- TreeSet.Intersect
- TreeSet.Max
- TreeSet.Min
- TreeSet.Remove
- TreeSet.RemoveFunc
- TreeSet.RemoveSet
- TreeSet.RemoveSlice
- TreeSet.Size
- TreeSet.Slice
- TreeSet.String
- TreeSet.Subset
- TreeSet.TopK
- TreeSet.Union
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Cmp ¶ added in v0.1.10
Cmp is a Compare function for the specified builtin type B.
Common to use with string, int, etc.
Example (Ints) ¶
s := NewTreeSet[int, Compare[int]](Cmp[int]) s.Insert(50) s.Insert(42) s.Insert(100) fmt.Println(s) fmt.Println("min:", s.Min()) fmt.Println("max:", s.Max())
Output: [42 50 100] min: 42 max: 100
Example (Strings) ¶
s := NewTreeSet[string, Compare[string]](Cmp[string]) s.Insert("red") s.Insert("green") s.Insert("blue") fmt.Println(s) fmt.Println("min:", s.Min()) fmt.Println("max:", s.Max())
Output: [blue green red] min: blue max: red
func InsertSetFunc ¶ added in v0.1.13
InsertSetFunc inserts the elements of a into b, applying the transform function to each element before insertion.
Returns true if b was modified as a result.
func InsertSliceFunc ¶ added in v0.1.13
InsertSliceFunc inserts all elements from the slice into the set
Types ¶
type BuiltIn ¶ added in v0.1.10
type BuiltIn interface { ~string | ~int | ~uint | ~int64 | ~uint64 | ~int32 | ~uint32 | ~int16 | ~uint16 | ~int8 | ~uint8 }
BuiltIn types compatible with Cmp
type Common ¶ added in v0.1.13
type Common[T any] interface { // Slice returns a slice of all elements in the set. // // Note: order of elements depends on the underlying implementation. Slice() []T // Insert an element into the set. // // Returns true if the set is modified as a result. Insert(T) bool // InsertSlice inserts all elements from the slice into the set. // // Returns true if the set was modified as a result. InsertSlice([]T) bool // Size returns the number of elements in the set. Size() int // ForEach will call the callback function for each element in the set. // If the callback returns false, the iteration will stop. // // Note: iteration order depends on the underlying implementation. ForEach(func(T) bool) }
Common is a minimal interface that all sets implement.
type Compare ¶ added in v0.1.10
Compare represents a function that compares two elements.
Must return < 0 if the first parameter is less than the second parameter 0 if the two parameters are equal > 0 if the first parameters is greater than the second parameter
Example (Contestant) ¶
type contestant struct { name string score int } compare := func(a, b contestant) int { return a.score - b.score } s := NewTreeSet[contestant, Compare[contestant]](compare) s.Insert(contestant{name: "alice", score: 80}) s.Insert(contestant{name: "dave", score: 90}) s.Insert(contestant{name: "bob", score: 70}) fmt.Println(s)
Output: [{bob 70} {alice 80} {dave 90}]
type Hash ¶ added in v0.1.5
type Hash interface { ~string | ~int | ~uint | ~int64 | ~uint64 | ~int32 | ~uint32 | ~int16 | ~uint16 | ~int8 | ~uint8 }
Hash represents the output type of a Hash() function defined on a type.
A Hash could be string-like or int-like. A string hash could be something like and md5, sha1, or GoString() representation of a type. An int hash could be something like the prime multiple hash code of a type.
type HashFunc ¶ added in v0.1.5
type HashFunc[H Hash] interface { Hash() H }
HashFunc is a generic type constraint for any type that implements a Hash() method with a Hash return type.
type HashSet ¶ added in v0.1.5
HashSet is a generic implementation of the mathematical data structure, oriented around the use of a HashFunc to make hash values from other types.
func HashSetFrom ¶ added in v0.1.5
HashSetFrom creates a new HashSet containing each item in items.
T must implement HashFunc[H], where H is of type Hash. This allows custom types that include non-comparable fields to provide their own hash algorithm.
func NewHashSet ¶ added in v0.1.5
NewHashSet creates a HashSet with underlying capacity of size.
A HashSet will automatically grow or shrink its capacity as items are added or removed.
T must implement HashFunc[H], where H is of Hash type. This allows custom types that include non-comparable fields to provide their own hash algorithm.
func (*HashSet[T, H]) Contains ¶ added in v0.1.5
Contains returns whether item is present in s.
Example ¶
anna := &person{name: "anna", id: 94} bill := &person{name: "bill", id: 50} carl := &person{name: "carl", id: 10} dave := &person{name: "dave", id: 32} s := HashSetFrom[*person, string]([]*person{anna, bill, carl}) fmt.Println(s.Contains(anna)) fmt.Println(s.Contains(dave))
Output: true false
func (*HashSet[T, H]) ContainsAll ¶ added in v0.1.5
ContainsAll returns whether s contains at least every item in items.
Example ¶
anna := &person{name: "anna", id: 94} bill := &person{name: "bill", id: 50} carl := &person{name: "carl", id: 10} dave := &person{name: "dave", id: 32} s := HashSetFrom[*person, string]([]*person{anna, bill, carl}) fmt.Println(s.ContainsAll([]*person{anna, bill})) fmt.Println(s.ContainsAll([]*person{anna, dave}))
Output: true false
func (*HashSet[T, H]) ContainsSlice ¶ added in v0.1.5
ContainsSlice returns whether s contains the same set of of elements that are in items. The elements of items may contain duplicates.
If the slice is known to be set-like (no duplicates), EqualSlice provides a more efficient implementation.
Example ¶
anna := &person{name: "anna", id: 94} bill := &person{name: "bill", id: 50} carl := &person{name: "carl", id: 10} dave := &person{name: "dave", id: 32} s := HashSetFrom[*person, string]([]*person{anna, bill, carl}) fmt.Println(s.ContainsSlice([]*person{anna, bill})) fmt.Println(s.ContainsSlice([]*person{anna, bill, carl})) fmt.Println(s.ContainsSlice([]*person{carl, dave}))
Output: false true false
func (*HashSet[T, H]) Copy ¶ added in v0.1.5
Copy creates a shallow copy of s.
Example ¶
anna := &person{name: "anna", id: 94} bill := &person{name: "bill", id: 50} carl := &person{name: "carl", id: 10} s := HashSetFrom[*person, string]([]*person{anna, bill, carl}) c := s.Copy() fmt.Println(c)
Output: [anna bill carl]
func (*HashSet[T, H]) Difference ¶ added in v0.1.5
Difference returns a set that contains elements of s that are not in o.
Example ¶
anna := &person{name: "anna", id: 94} bill := &person{name: "bill", id: 50} carl := &person{name: "carl", id: 10} dave := &person{name: "dave", id: 32} s1 := HashSetFrom[*person, string]([]*person{anna, bill, carl}) s2 := HashSetFrom[*person, string]([]*person{anna, bill, dave}) difference := s1.Difference(s2) fmt.Println(s1) fmt.Println(s2) fmt.Println(difference)
Output: [anna bill carl] [anna bill dave] [carl]
func (*HashSet[T, H]) Empty ¶ added in v0.1.7
Empty returns true if s contains no elements, false otherwise.
Example ¶
s := NewHashSet[*person, string](0) fmt.Println(s.Empty())
Output: true
func (*HashSet[T, H]) Equal ¶ added in v0.1.5
Equal returns whether s and o contain the same elements.
Example ¶
anna := &person{name: "anna", id: 94} bill := &person{name: "bill", id: 50} carl := &person{name: "carl", id: 10} dave := &person{name: "dave", id: 32} s1 := HashSetFrom[*person, string]([]*person{anna, bill, carl}) s2 := HashSetFrom[*person, string]([]*person{anna, bill, carl}) s3 := HashSetFrom[*person, string]([]*person{anna, bill, dave}) fmt.Println(s1.Equal(s2)) fmt.Println(s1.Equal(s3))
Output: true false
func (*HashSet[T, H]) EqualSlice ¶ added in v0.1.5
EqualSlice returns whether s and items contain the same elements.
If items contains duplicates EqualSlice will return false; it is assumed that items is itself set-like. For comparing equality with a slice that may contain duplicates, use ContainsSlice.
func (*HashSet[T, H]) Insert ¶ added in v0.1.5
Insert item into s.
Return true if s was modified (item was not already in s), false otherwise.
Example ¶
s := NewHashSet[*person, string](10) s.Insert(&person{name: "dave", id: 108}) s.Insert(&person{name: "armon", id: 101}) s.Insert(&person{name: "mitchell", id: 100}) s.Insert(&person{name: "armon", id: 101}) fmt.Println(s)
Output: [armon dave mitchell]
func (*HashSet[T, H]) InsertSet ¶ added in v0.1.5
InsertSet will insert each element of o into s.
Return true if s was modified (at least one item of o was not already in s), false otherwise.
Example ¶
anna := &person{name: "anna", id: 94} bill := &person{name: "bill", id: 50} carl := &person{name: "carl", id: 10} dave := &person{name: "dave", id: 32} s1 := HashSetFrom[*person, string]([]*person{anna, carl}) s2 := HashSetFrom[*person, string]([]*person{carl, dave, bill}) s2.InsertSet(s1) fmt.Println(s1) fmt.Println(s2)
Output: [anna carl] [anna bill carl dave]
func (*HashSet[T, H]) InsertSlice ¶ added in v0.1.10
InsertSlice will insert each item in items into s.
Return true if s was modified (at least one item was not already in s), false otherwise.
Example ¶
s := NewHashSet[*person, string](10) s.InsertSlice([]*person{ {name: "dave", id: 108}, {name: "mitchell", id: 100}, {name: "dave", id: 108}, {name: "armon", id: 101}, }) fmt.Println(s)
Output: [armon dave mitchell]
func (*HashSet[T, H]) Intersect ¶ added in v0.1.5
Intersect returns a set that contains elements that are present in both s and o.
Example ¶
anna := &person{name: "anna", id: 94} bill := &person{name: "bill", id: 50} carl := &person{name: "carl", id: 10} dave := &person{name: "dave", id: 32} s1 := HashSetFrom[*person, string]([]*person{anna, bill, carl}) s2 := HashSetFrom[*person, string]([]*person{anna, bill, dave}) intersect := s1.Intersect(s2) fmt.Println(s1) fmt.Println(s2) fmt.Println(intersect)
Output: [anna bill carl] [anna bill dave] [anna bill]
func (*HashSet[T, H]) MarshalJSON ¶ added in v0.1.11
MarshalJSON implements the json.Marshaler interface.
func (*HashSet[T, H]) Remove ¶ added in v0.1.5
Remove will remove item from s.
Return true if s was modified (item was present), false otherwise.
Example ¶
anna := &person{name: "anna", id: 94} bill := &person{name: "bill", id: 50} carl := &person{name: "carl", id: 10} dave := &person{name: "dave", id: 32} s := HashSetFrom[*person, string]([]*person{anna, carl, dave, bill}) fmt.Println(s) s.Remove(carl) fmt.Println(s)
Output: [anna bill carl dave] [anna bill dave]
func (*HashSet[T, H]) RemoveFunc ¶ added in v0.1.9
RemoveFunc will remove each element from s that satisfies condition f.
Return true if s was modified, false otherwise.
Example ¶
anna := &person{name: "anna", id: 94} bill := &person{name: "bill", id: 50} carl := &person{name: "carl", id: 10} dave := &person{name: "dave", id: 32} s := HashSetFrom[*person, string]([]*person{anna, bill, carl, dave}) idAbove50 := func(p *person) bool { return p.id >= 50 } fmt.Println(s) s.RemoveFunc(idAbove50) fmt.Println(s)
Output: [anna bill carl dave] [carl dave]
func (*HashSet[T, H]) RemoveSet ¶ added in v0.1.5
RemoveSet will remove each element of o from s.
Return true if s was modified (any item of o was present in s), false otherwise.
Example ¶
anna := &person{name: "anna", id: 94} bill := &person{name: "bill", id: 50} carl := &person{name: "carl", id: 10} dave := &person{name: "dave", id: 32} s := HashSetFrom[*person, string]([]*person{carl, dave, bill}) r := HashSetFrom[*person, string]([]*person{anna, carl}) fmt.Println(s) s.RemoveSet(r) fmt.Println(s)
Output: [bill carl dave] [bill dave]
func (*HashSet[T, H]) RemoveSlice ¶ added in v0.1.10
RemoveSlice will remove each item in items from s.
Return true if s was modified (any item was present), false otherwise.
Example ¶
anna := &person{name: "anna", id: 94} bill := &person{name: "bill", id: 50} carl := &person{name: "carl", id: 10} dave := &person{name: "dave", id: 32} s := HashSetFrom[*person, string]([]*person{anna, carl, dave, bill}) fmt.Println(s) s.RemoveSlice([]*person{anna, carl}) fmt.Println(s)
Output: [anna bill carl dave] [bill dave]
func (*HashSet[T, H]) Size ¶ added in v0.1.5
Size returns the cardinality of s.
Example ¶
anna := &person{name: "anna", id: 94} bill := &person{name: "bill", id: 50} carl := &person{name: "carl", id: 10} s := HashSetFrom[*person, string]([]*person{anna, bill, carl}) fmt.Println(s.Size())
Output: 3
func (*HashSet[T, H]) Slice ¶ added in v0.1.8
func (s *HashSet[T, H]) Slice() []T
Slice creates a copy of s as a slice.
The result is not ordered.
Example ¶
anna := &person{name: "anna", id: 94} bill := &person{name: "bill", id: 50} carl := &person{name: "carl", id: 10} s := HashSetFrom[*person, string]([]*person{anna, bill, carl}) slice := s.Slice() sort.Slice(slice, func(a, b int) bool { return slice[a].id < slice[b].id }) fmt.Println(slice)
Output: [carl bill anna]
func (*HashSet[T, H]) String ¶ added in v0.1.5
String creates a string representation of s, using "%v" printf formatting to transform each element into a string. The result contains elements sorted by their lexical string order.
Example ¶
anna := &person{name: "anna", id: 94} bill := &person{name: "bill", id: 50} carl := &person{name: "carl", id: 10} s := HashSetFrom[*person, string]([]*person{anna, bill, carl}) fmt.Println(s.String())
Output: [anna bill carl]
func (*HashSet[T, H]) StringFunc ¶ added in v0.1.8
StringFunc creates a string representation of s, using f to transform each element into a string. The result contains elements sorted by their string order.
func (*HashSet[T, H]) Subset ¶ added in v0.1.5
Subset returns whether o is a subset of s.
Example ¶
anna := &person{name: "anna", id: 94} bill := &person{name: "bill", id: 50} carl := &person{name: "carl", id: 10} dave := &person{name: "dave", id: 32} s1 := HashSetFrom[*person, string]([]*person{anna, bill, carl}) s2 := HashSetFrom[*person, string]([]*person{anna, bill}) s3 := HashSetFrom[*person, string]([]*person{bill, carl, dave}) fmt.Println(s1.Subset(s2)) fmt.Println(s1.Subset(s3))
Output: true false
func (*HashSet[T, H]) Union ¶ added in v0.1.5
Union returns a set that contains all elements of s and o combined.
Example ¶
anna := &person{name: "anna", id: 94} bill := &person{name: "bill", id: 50} carl := &person{name: "carl", id: 10} dave := &person{name: "dave", id: 32} s1 := HashSetFrom[*person, string]([]*person{anna, bill, carl}) s2 := HashSetFrom[*person, string]([]*person{anna, bill, dave}) union := s1.Union(s2) fmt.Println(s1) fmt.Println(s2) fmt.Println(union)
Output: [anna bill carl] [anna bill dave] [anna bill carl dave]
func (*HashSet[T, H]) UnmarshalJSON ¶ added in v0.1.11
UnmarshalJSON implements the json.Unmarshaler interface.
type Set ¶
type Set[T comparable] struct { // contains filtered or unexported fields }
Set is a simple, generic implementation of the set mathematical data structure. It is optimized for correctness and convenience, as a replacement for the use of map[interface{}]struct{}.
func From ¶
func From[T comparable](items []T) *Set[T]
From creates a new Set containing each item in items.
T must *not* be of pointer type, nor contain pointer fields, which are comparable but not in the way you expect. For these types, use HashSet instead.
func FromFunc ¶ added in v0.1.3
func FromFunc[A any, T comparable](items []A, conversion func(A) T) *Set[T]
FromFunc creates a new Set containing a conversion of each item in items.
T must *not* be of pointer type, nor contain pointer fields, which are comparable but not in the way you expect. For these types, use HashSet instead.
func New ¶
func New[T comparable](size int) *Set[T]
New creates a new Set with initial underlying capacity of size.
A Set will automatically grow or shrink its capacity as items are added or removed.
T must *not* be of pointer type, nor contain pointer fields, which are comparable but not in the way you expect. For these types, use HashSet instead.
func (*Set[T]) Contains ¶
Contains returns whether item is present in s.
Example ¶
s := From([]string{"red", "green", "blue"}) fmt.Println(s.Contains("red")) fmt.Println(s.Contains("orange"))
Output: true false
func (*Set[T]) ContainsAll ¶
ContainsAll returns whether s contains at least every item in items.
Example ¶
s := From([]string{"red", "green", "blue"}) fmt.Println(s.ContainsAll([]string{"red", "blue"})) fmt.Println(s.ContainsAll([]string{"red", "orange"}))
Output: true false
func (*Set[T]) ContainsSlice ¶ added in v0.1.4
ContainsSlice returns whether s contains the same set of of elements that are in items. The elements of items may contain duplicates.
If the slice is known to be set-like (no duplicates), EqualSlice provides a more efficient implementation.
Example ¶
s := From([]string{"red", "green", "blue"}) fmt.Println(s.ContainsSlice([]string{"red", "blue"})) fmt.Println(s.ContainsSlice([]string{"red", "blue", "orange"})) fmt.Println(s.ContainsSlice([]string{"red", "blue", "green"}))
Output: false false true
func (*Set[T]) Copy ¶
Copy creates a copy of s.
Example ¶
s := From([]string{"red", "green", "blue"}) t := s.Copy() fmt.Println(t)
Output: [blue green red]
func (*Set[T]) Difference ¶
Difference returns a set that contains elements of s that are not in o.
Example ¶
t1 := From([]string{"red", "green", "blue"}) t2 := From([]string{"red", "blue"}) t3 := From([]string{"red", "orange"}) fmt.Println(t1.Difference(t2)) fmt.Println(t1.Difference(t3))
Output: [green] [blue green]
func (*Set[T]) Empty ¶ added in v0.1.7
Empty returns true if s contains no elements, false otherwise.
Example ¶
s := New[string](10) fmt.Println(s.Empty())
Output: true
func (*Set[T]) Equal ¶ added in v0.1.2
Equal returns whether s and o contain the same elements.
Example ¶
t1 := From([]string{"red", "green", "blue"}) t2 := From([]string{"red", "blue"}) t3 := From([]string{"red", "green", "yellow"}) t4 := From([]string{"red", "green", "blue"}) fmt.Println(t1.Equal(t2)) fmt.Println(t1.Equal(t3)) fmt.Println(t1.Equal(t4))
Output: false false true
func (*Set[T]) EqualSlice ¶ added in v0.1.4
EqualSlice returns whether s and items contain the same elements.
If items contains duplicates EqualSlice will return false; it is assumed that items is itself set-like. For comparing equality with a slice that may contain duplicates, use ContainsSlice.
func (*Set[T]) Insert ¶
Insert item into s.
Return true if s was modified (item was not already in s), false otherwise.
Example ¶
s := New[int](10) s.Insert(1) s.Insert(1) s.Insert(2) s.Insert(3) s.Insert(2) fmt.Println(s)
Output: [1 2 3]
func (*Set[T]) InsertSet ¶
InsertSet will insert each element of o into s.
Return true if s was modified (at least one item of o was not already in s), false otherwise.
Example ¶
s := New[int](10) s.InsertSet(From([]int{1, 1, 2, 3, 2})) fmt.Println(s)
Output: [1 2 3]
func (*Set[T]) InsertSlice ¶ added in v0.1.10
InsertSlice will insert each item in items into s.
Return true if s was modified (at least one item was not already in s), false otherwise.
Example ¶
s := New[int](10) s.InsertSlice([]int{1, 1, 2, 3, 2}) fmt.Println(s)
Output: [1 2 3]
func (*Set[T]) Intersect ¶
Intersect returns a set that contains elements that are present in both s and o.
Example ¶
t1 := From([]string{"red", "green", "blue"}) t2 := From([]string{"red", "blue"}) t3 := From([]string{"red", "orange"}) t4 := From([]string{"yellow"}) fmt.Println(t1.Intersect(t2)) fmt.Println(t1.Intersect(t3)) fmt.Println(t1.Intersect(t4))
Output: [blue red] [red] []
func (*Set[T]) MarshalJSON ¶ added in v0.1.11
MarshalJSON implements the json.Marshaler interface.
func (*Set[T]) Remove ¶
Remove will remove item from s.
Return true if s was modified (item was present), false otherwise.
Example ¶
s := New[int](10) s.InsertSlice([]int{1, 1, 2, 3, 2}) s.Remove(2) fmt.Println(s)
Output: [1 3]
func (*Set[T]) RemoveFunc ¶ added in v0.1.9
RemoveFunc will remove each element from s that satisfies condition f.
Return true if s was modified, false otherwise.
Example ¶
s := From([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}) even := func(i int) bool { return i%2 == 0 } s.RemoveFunc(even) fmt.Println(s)
Output: [1 3 5 7 9]
func (*Set[T]) RemoveSet ¶
RemoveSet will remove each element of o from s.
Return true if s was modified (any item of o was present in s), false otherwise.
Example ¶
s := New[int](10) s.InsertSlice([]int{1, 1, 2, 3, 2}) s.RemoveSet(From([]int{2, 3})) fmt.Println(s)
Output: [1]
func (*Set[T]) RemoveSlice ¶ added in v0.1.10
RemoveSlice will remove each item in items from s.
Return true if s was modified (any item was present), false otherwise.
Example ¶
s := New[int](10) s.InsertSlice([]int{1, 1, 2, 3, 2}) s.RemoveSlice([]int{2, 3}) fmt.Println(s)
Output: [1]
func (*Set[T]) Size ¶
Size returns the cardinality of s.
Example ¶
s := From([]string{"red", "green", "blue"}) fmt.Println(s.Size())
Output: 3
func (*Set[T]) Slice ¶ added in v0.1.8
func (s *Set[T]) Slice() []T
Slice creates a copy of s as a slice. Elements are in no particular order.
Example ¶
s := From([]string{"red", "green", "blue"}) t := s.Slice() sort.Strings(t) fmt.Println(t)
Output: [blue green red]
func (*Set[T]) String ¶ added in v0.1.1
String creates a string representation of s, using "%v" printf formating to transform each element into a string. The result contains elements sorted by their lexical string order.
Example ¶
s := From([]string{"red", "green", "blue"}) fmt.Println(s.String())
Output: [blue green red]
func (*Set[T]) StringFunc ¶ added in v0.1.8
StringFunc creates a string representation of s, using f to transform each element into a string. The result contains elements sorted by their lexical string order.
func (*Set[T]) Subset ¶ added in v0.1.2
Subset returns whether o is a subset of s.
Example ¶
t1 := From([]string{"red", "green", "blue"}) t2 := From([]string{"red", "blue"}) t3 := From([]string{"red", "orange"}) fmt.Println(t1.Subset(t2)) fmt.Println(t1.Subset(t3))
Output: true false
func (*Set[T]) Union ¶
Union returns a set that contains all elements of s and o combined.
Example ¶
t1 := From([]string{"red", "green", "blue"}) t2 := From([]string{"red", "blue"}) t3 := From([]string{"red", "orange"}) fmt.Println(t1.Union(t2)) fmt.Println(t1.Union(t3))
Output: [blue green red] [blue green orange red]
func (*Set[T]) UnmarshalJSON ¶ added in v0.1.11
UnmarshalJSON implements the json.Unmarshaler interface.
type TreeNodeVisit ¶ added in v0.1.13
TreeNodeVisit is a function that is called for each node in the tree.
type TreeSet ¶ added in v0.1.10
TreeSet provides a generic sortable set implementation for Go. Enables fast storage and retrieval of ordered information. Most effective in cases where data is regularly being added and/or removed and fast lookup properties must be maintained.
The underlying data structure is a Red-Black Binary Search Tree. https://en.wikipedia.org/wiki/Red–black_tree
Not thread safe, and not safe for concurrent modification.
func NewTreeSet ¶ added in v0.1.10
NewTreeSet creates a TreeSet of type T, comparing elements via C.
T may be any type.
C is an implementation of Compare[T]. For builtin types, Cmp provides a convenient Compare implementation.
func TreeSetFrom ¶ added in v0.1.10
TreeSetFrom creates a new TreeSet containing each item in items.
T may be any type.
C is an implementation of Compare[T]. For builtin types, Cmp provides a convenient Compare implementation.
func (*TreeSet[T, C]) Above ¶ added in v0.1.12
After returns a TreeSet containing the elements of s that are > item.
Example ¶
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int]) fmt.Println(s.Above(3)) fmt.Println(s.Above(5)) fmt.Println(s.Above(10))
Output: [4 5] [] []
func (*TreeSet[T, C]) AboveEqual ¶ added in v0.1.12
AfterEqual returns a TreeSet containing the elements of s that are ≥ item.
Example ¶
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int]) fmt.Println(s.AboveEqual(3)) fmt.Println(s.AboveEqual(5)) fmt.Println(s.AboveEqual(10))
Output: [3 4 5] [5] []
func (*TreeSet[T, C]) Below ¶ added in v0.1.12
Below returns a TreeSet containing the elements of s that are < item.
Example ¶
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int]) fmt.Println(s.Below(1)) fmt.Println(s.Below(3)) fmt.Println(s.Below(10))
Output: [] [1 2] [1 2 3 4 5]
func (*TreeSet[T, C]) BelowEqual ¶ added in v0.1.12
BelowEqual returns a TreeSet containing the elements of s that are ≤ item.
Example ¶
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int]) fmt.Println(s.BelowEqual(1)) fmt.Println(s.BelowEqual(3)) fmt.Println(s.BelowEqual(10))
Output: [1] [1 2 3] [1 2 3 4 5]
func (*TreeSet[T, C]) BottomK ¶ added in v0.1.10
BottomK returns the bottom n (largest) elements in s, in descending order.
Example ¶
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int]) fmt.Println(s.BottomK(0)) fmt.Println(s.BottomK(1)) fmt.Println(s.BottomK(3)) fmt.Println(s.BottomK(5))
Output: [] [5] [5 4 3] [5 4 3 2 1]
func (*TreeSet[T, C]) Contains ¶ added in v0.1.10
Contains returns whether item is present in s.
Example ¶
s := TreeSetFrom[string, Compare[string]]([]string{"red", "green", "blue"}, Cmp[string]) fmt.Println(s.Contains("green")) fmt.Println(s.Contains("orange"))
Output: true false
func (*TreeSet[T, C]) ContainsSlice ¶ added in v0.1.10
ContainsSlice returns whether s contains the same set of elements that are in items. The items slice may contain duplicate elements.
If the items slice is known to be set-like (no duplicates), EqualSlice provides a more efficient implementation.
Example ¶
s := TreeSetFrom[string, Compare[string]]([]string{"red", "green", "blue"}, Cmp[string]) fmt.Println(s.ContainsSlice([]string{"red", "green"})) fmt.Println(s.ContainsSlice([]string{"red", "orange"}))
Output: true false
func (*TreeSet[T, C]) Copy ¶ added in v0.1.10
Copy creates a copy of s.
Individual elements are reference copies.
Example ¶
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int]) c := s.Copy() s.Remove(2) s.Remove(4) fmt.Println(s) fmt.Println(c)
Output: [1 3 5] [1 2 3 4 5]
func (*TreeSet[T, C]) Difference ¶ added in v0.1.10
Difference returns a set that contains elements of s that are not in o.
Example ¶
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int]) t := TreeSetFrom[int, Compare[int]]([]int{5, 4, 3, 2, 1}, Cmp[int]) f := TreeSetFrom[int, Compare[int]]([]int{1, 3, 5, 7, 9}, Cmp[int]) fmt.Println(s.Difference(t)) fmt.Println(s.Difference(f))
Output: [] [2 4]
func (*TreeSet[T, C]) Empty ¶ added in v0.1.10
Empty returns true if there are no elements in s.
Example ¶
s := TreeSetFrom[string, Compare[string]]([]string{}, Cmp[string]) fmt.Println(s.Empty()) s.InsertSlice([]string{"red", "green", "blue"}) fmt.Println(s.Empty())
Output: true false
func (*TreeSet[T, C]) Equal ¶ added in v0.1.10
Equal return whether s and o contain the same elements.
Example ¶
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int]) t := TreeSetFrom[int, Compare[int]]([]int{5, 4, 3, 2, 1}, Cmp[int]) f := TreeSetFrom[int, Compare[int]]([]int{1, 3, 5, 7, 9}, Cmp[int]) fmt.Println(s.Equal(t)) fmt.Println(s.Equal(f))
Output: true false
func (*TreeSet[T, C]) EqualSlice ¶ added in v0.1.10
EqualSlice returns whether s and items contain the same elements.
func (*TreeSet[T, C]) FilterSlice ¶ added in v0.1.13
FilterSlice returns the elements of s that satisfy the predicate f.
func (*TreeSet[T, C]) FirstAbove ¶ added in v0.1.12
FirstAbove returns the first element strictly above item.
A zero value and false are returned if no such element exists.
Example ¶
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int]) fmt.Println(s.FirstAbove(3)) fmt.Println(s.FirstAbove(5)) fmt.Println(s.FirstAbove(10))
Output: 4 true 0 false 0 false
func (*TreeSet[T, C]) FirstAboveEqual ¶ added in v0.1.12
FirstAboveEqual returns the first element above item (or item itself if present).
A zero value and false are returned if no such element exists.
Example ¶
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int]) fmt.Println(s.FirstAboveEqual(3)) fmt.Println(s.FirstAboveEqual(5)) fmt.Println(s.FirstAboveEqual(10))
Output: 3 true 5 true 0 false
func (*TreeSet[T, C]) FirstBelow ¶ added in v0.1.12
FirstBelow returns the first element strictly below item.
A zero value and false are returned if no such element exists.
Example ¶
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int]) fmt.Println(s.FirstBelow(1)) fmt.Println(s.FirstBelow(3)) fmt.Println(s.FirstBelow(10))
Output: 0 false 2 true 5 true
func (*TreeSet[T, C]) FirstBelowEqual ¶ added in v0.1.12
FirstBelowEqual returns the first element below item (or item itself if present).
A zero value and false are returned if no such element exists.
Example ¶
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int]) fmt.Println(s.FirstBelowEqual(1)) fmt.Println(s.FirstBelowEqual(3)) fmt.Println(s.FirstBelowEqual(10))
Output: 1 true 3 true 5 true
func (*TreeSet[T, C]) Insert ¶ added in v0.1.10
Insert item into s.
Returns true if s was modified (item was not already in s), false otherwise.
Example ¶
s := TreeSetFrom[string, Compare[string]]([]string{}, Cmp[string]) fmt.Println(s) s.Insert("red") s.Insert("green") s.Insert("blue") fmt.Println(s) // [] // [blue green red]
Output:
func (*TreeSet[T, C]) InsertSet ¶ added in v0.1.13
InsertSet will insert each element of o into s.
Return true if s was modified (at least one item of o was not already in s), false otherwise.
Example ¶
s1 := TreeSetFrom[string, Compare[string]]([]string{"red", "green"}, Cmp[string]) s2 := TreeSetFrom[string, Compare[string]]([]string{"green", "blue"}, Cmp[string]) fmt.Println(s1) fmt.Println(s2) s1.InsertSet(s2) fmt.Println(s1)
Output: [green red] [blue green] [blue green red]
func (*TreeSet[T, C]) InsertSlice ¶ added in v0.1.10
InsertSlice will insert each item in items into s.
Return true if s was modified (at least one item was not already in s), false otherwise.
Example ¶
s := TreeSetFrom[string, Compare[string]]([]string{}, Cmp[string]) fmt.Println(s) s.InsertSlice([]string{"red", "green", "blue"}) fmt.Println(s) // [] // [blue green red]
Output:
func (*TreeSet[T, C]) Intersect ¶ added in v0.1.10
Intersect returns a set that contains elements that are present in both s and o.
Example ¶
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int]) t := TreeSetFrom[int, Compare[int]]([]int{5, 4, 3, 2, 1}, Cmp[int]) f := TreeSetFrom[int, Compare[int]]([]int{1, 3, 5, 7, 9}, Cmp[int]) fmt.Println(s.Intersect(t)) fmt.Println(s.Intersect(f))
Output: [1 2 3 4 5] [1 3 5]
func (*TreeSet[T, C]) MarshalJSON ¶ added in v0.1.11
MarshalJSON implements the json.Marshaler interface.
func (*TreeSet[T, C]) Max ¶ added in v0.1.10
func (s *TreeSet[T, C]) Max() T
Max returns the largest item in s.
Must not be called on an empty set.
Example ¶
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int]) r := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, func(a int, b int) int { return b - a }) fmt.Println("asc:", s.Max()) fmt.Println("desc:", r.Max())
Output: asc: 5 desc: 1
func (*TreeSet[T, C]) Min ¶ added in v0.1.10
func (s *TreeSet[T, C]) Min() T
Min returns the smallest item in the set.
Must not be called on an empty set.
Example ¶
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int]) r := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, func(a int, b int) int { return b - a }) fmt.Println("asc:", s.Min()) fmt.Println("desc:", r.Min())
Output: asc: 1 desc: 5
func (*TreeSet[T, C]) Remove ¶ added in v0.1.10
Remove item from s.
Returns true if s was modified (item was in s), false otherwise.
Example ¶
s := TreeSetFrom[string, Compare[string]]([]string{"red", "green", "blue"}, Cmp[string]) fmt.Println(s) fmt.Println(s.Remove("green")) fmt.Println(s.Remove("orange")) fmt.Println(s)
Output: [blue green red] true false [blue red]
func (*TreeSet[T, C]) RemoveFunc ¶ added in v0.1.13
RemoveFunc will remove each element from s that satisifies condition f.
Return true if s was modified, false otherwise.
Example ¶
s := TreeSetFrom[int, Compare[int]](ints(20), Cmp[int]) fmt.Println(s) even := func(i int) bool { return i%3 != 0 } s.RemoveFunc(even) fmt.Println(s)
Output: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] [3 6 9 12 15 18]
func (*TreeSet[T, C]) RemoveSet ¶ added in v0.1.13
RemoveSet will remove each element in o from s.
Returns true if s was modified (at least one item in o was in s), false otherwise.
Example ¶
s1 := TreeSetFrom[string, Compare[string]]([]string{"a", "b", "c", "d", "e", "f"}, Cmp[string]) s2 := TreeSetFrom[string, Compare[string]]([]string{"e", "z", "a"}, Cmp[string]) fmt.Println(s1) fmt.Println(s2) s1.RemoveSet(s2) fmt.Println(s1)
Output: [a b c d e f] [a e z] [b c d f]
func (*TreeSet[T, C]) RemoveSlice ¶ added in v0.1.10
RemoveSlice will remove each item in items from s.
Return true if s was modified (any item was in s), false otherwise.
Example ¶
s := TreeSetFrom[string, Compare[string]]([]string{"red", "green", "blue"}, Cmp[string]) fmt.Println(s) fmt.Println(s.RemoveSlice([]string{"red", "blue"})) fmt.Println(s.RemoveSlice([]string{"orange", "white"})) fmt.Println(s)
Output: [blue green red] true false [green]
func (*TreeSet[T, C]) Size ¶ added in v0.1.10
Size returns the number of elements in s.
Example ¶
s := TreeSetFrom[string, Compare[string]]([]string{"red", "green", "blue"}, Cmp[string]) fmt.Println(s.Size())
Output: 3
func (*TreeSet[T, C]) Slice ¶ added in v0.1.10
func (s *TreeSet[T, C]) Slice() []T
Slice returns the elements of s as a slice, in order.
Example ¶
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int]) slice := s.Slice() fmt.Println(slice) fmt.Println(len(slice))
Output: [1 2 3 4 5] 5
func (*TreeSet[T, C]) String ¶ added in v0.1.10
String creates a string representation of s, using "%v" printf formatting each element into a string. The result contains elements in order.
Example ¶
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int]) fmt.Println(s.String() == "[1 2 3 4 5]")
Output: true
func (*TreeSet[T, C]) StringFunc ¶ added in v0.1.10
StringFunc creates a string representation of s, using f to transform each element into a string. The result contains elements in order.
func (*TreeSet[T, C]) Subset ¶ added in v0.1.10
Subset returns whether o is a subset of s.
Example ¶
s1 := TreeSetFrom[string, Compare[string]]([]string{"a", "b", "c", "d", "e"}, Cmp[string]) s2 := TreeSetFrom[string, Compare[string]]([]string{"b", "d"}, Cmp[string]) s3 := TreeSetFrom[string, Compare[string]]([]string{"a", "z"}, Cmp[string]) fmt.Println(s1.Subset(s2)) fmt.Println(s1.Subset(s3))
Output: true false
func (*TreeSet[T, C]) TopK ¶ added in v0.1.10
TopK returns the top n (smallest) elements in s, in ascending order.
Example ¶
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int]) fmt.Println(s.TopK(0)) fmt.Println(s.TopK(1)) fmt.Println(s.TopK(3)) fmt.Println(s.TopK(5))
Output: [] [1] [1 2 3] [1 2 3 4 5]
func (*TreeSet[T, C]) Union ¶ added in v0.1.10
Union returns a set that contains all elements of s and o combined.
Example ¶
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int]) t := TreeSetFrom[int, Compare[int]]([]int{5, 4, 3, 2, 1}, Cmp[int]) f := TreeSetFrom[int, Compare[int]]([]int{1, 3, 5, 7, 9}, Cmp[int]) fmt.Println(s.Union(t)) fmt.Println(s.Union(f))
Output: [1 2 3 4 5] [1 2 3 4 5 7 9]
func (*TreeSet[T, C]) UnmarshalJSON ¶ added in v0.1.11
UnmarshalJSON implements the json.Unmarshaler interface.