Documentation
¶
Overview ¶
Package set provides a basic generic set implementation.
Index ¶
- func InsertSetFunc[T, E any](a Collection[T], b Collection[E], transform func(T) E) bool
- func InsertSliceFunc[T, E any](col Collection[T], items []E, transform func(element E) T) bool
- func SliceFunc[T, E any](s Collection[T], transform func(T) E) []E
- type Collection
- type CompareFunc
- type Hash
- type HashFunc
- type HashSet
- func (s *HashSet[T, H]) Contains(item 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(col Collection[T]) Collection[T]
- func (s *HashSet[T, H]) Empty() bool
- func (s *HashSet[T, H]) Equal(o *HashSet[T, H]) bool
- func (s *HashSet[T, H]) EqualSet(col Collection[T]) bool
- func (s *HashSet[T, H]) EqualSlice(items []T) bool
- func (s *HashSet[T, H]) EqualSliceSet(items []T) bool
- func (s *HashSet[T, H]) Insert(item T) bool
- func (s *HashSet[T, H]) InsertSet(col Collection[T]) bool
- func (s *HashSet[T, H]) InsertSlice(items []T) bool
- func (s *HashSet[T, H]) Intersect(col Collection[T]) Collection[T]
- func (s *HashSet[T, H]) Items() iter.Seq[T]
- func (s *HashSet[T, H]) MarshalJSON() ([]byte, error)
- func (s *HashSet[T, H]) ProperSubset(col Collection[T]) bool
- func (s *HashSet[T, H]) Remove(item T) bool
- func (s *HashSet[T, H]) RemoveFunc(f func(item T) bool) bool
- func (s *HashSet[T, H]) RemoveSet(col Collection[T]) 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(col Collection[T]) bool
- func (s *HashSet[T, H]) Union(col Collection[T]) Collection[T]
- func (s *HashSet[T, H]) UnmarshalJSON(data []byte) error
- type Hasher
- type Set
- func (s *Set[T]) Contains(item T) bool
- func (s *Set[T]) ContainsSlice(items []T) bool
- func (s *Set[T]) Copy() *Set[T]
- func (s *Set[T]) Difference(col Collection[T]) Collection[T]
- func (s *Set[T]) Empty() bool
- func (s *Set[T]) Equal(o *Set[T]) bool
- func (s *Set[T]) EqualSet(col Collection[T]) bool
- func (s *Set[T]) EqualSlice(items []T) bool
- func (s *Set[T]) EqualSliceSet(items []T) bool
- func (s *Set[T]) Insert(item T) bool
- func (s *Set[T]) InsertSet(col Collection[T]) bool
- func (s *Set[T]) InsertSlice(items []T) bool
- func (s *Set[T]) Intersect(col Collection[T]) Collection[T]
- func (s *Set[T]) Items() iter.Seq[T]
- func (s *Set[T]) MarshalJSON() ([]byte, error)
- func (s *Set[T]) ProperSubset(col Collection[T]) bool
- func (s *Set[T]) Remove(item T) bool
- func (s *Set[T]) RemoveFunc(f func(T) bool) bool
- func (s *Set[T]) RemoveSet(col Collection[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(col Collection[T]) bool
- func (s *Set[T]) Union(col Collection[T]) Collection[T]
- func (s *Set[T]) UnmarshalJSON(data []byte) error
- type TreeNodeVisit
- type TreeSet
- func (s *TreeSet[T]) Above(item T) *TreeSet[T]
- func (s *TreeSet[T]) AboveEqual(item T) *TreeSet[T]
- func (s *TreeSet[T]) Below(item T) *TreeSet[T]
- func (s *TreeSet[T]) BelowEqual(item T) *TreeSet[T]
- func (s *TreeSet[T]) BottomK(n int) []T
- func (s *TreeSet[T]) Contains(item T) bool
- func (s *TreeSet[T]) ContainsSlice(items []T) bool
- func (s *TreeSet[T]) Copy() *TreeSet[T]
- func (s *TreeSet[T]) Difference(col Collection[T]) Collection[T]
- func (s *TreeSet[T]) Empty() bool
- func (s *TreeSet[T]) Equal(o *TreeSet[T]) bool
- func (s *TreeSet[T]) EqualSet(col Collection[T]) bool
- func (s *TreeSet[T]) EqualSlice(items []T) bool
- func (s *TreeSet[T]) EqualSliceSet(items []T) bool
- func (s *TreeSet[T]) FirstAbove(item T) (T, bool)
- func (s *TreeSet[T]) FirstAboveEqual(item T) (T, bool)
- func (s *TreeSet[T]) FirstBelow(item T) (T, bool)
- func (s *TreeSet[T]) FirstBelowEqual(item T) (T, bool)
- func (s *TreeSet[T]) Insert(item T) bool
- func (s *TreeSet[T]) InsertSet(col Collection[T]) bool
- func (s *TreeSet[T]) InsertSlice(items []T) bool
- func (s *TreeSet[T]) Intersect(col Collection[T]) Collection[T]
- func (s *TreeSet[T]) Items() iter.Seq[T]
- func (s *TreeSet[T]) MarshalJSON() ([]byte, error)
- func (s *TreeSet[T]) Max() T
- func (s *TreeSet[T]) Min() T
- func (s *TreeSet[T]) ProperSubset(col Collection[T]) bool
- func (s *TreeSet[T]) Remove(item T) bool
- func (s *TreeSet[T]) RemoveFunc(f func(T) bool) bool
- func (s *TreeSet[T]) RemoveSet(col Collection[T]) bool
- func (s *TreeSet[T]) RemoveSlice(items []T) bool
- func (s *TreeSet[T]) Size() int
- func (s *TreeSet[T]) Slice() []T
- func (s *TreeSet[T]) String() string
- func (s *TreeSet[T]) StringFunc(f func(T) string) string
- func (s *TreeSet[T]) Subset(col Collection[T]) bool
- func (s *TreeSet[T]) TopK(n int) []T
- func (s *TreeSet[T]) Union(col Collection[T]) Collection[T]
- func (s *TreeSet[T]) UnmarshalJSON(data []byte) error
Examples ¶
- HashSet.Contains
- HashSet.ContainsSlice
- HashSet.Copy
- HashSet.Difference
- HashSet.Empty
- HashSet.Equal
- HashSet.EqualSlice
- HashSet.EqualSliceSet
- HashSet.Insert
- HashSet.InsertSet
- HashSet.InsertSlice
- HashSet.Intersect
- HashSet.MarshalJSON
- HashSet.Remove
- HashSet.RemoveFunc
- HashSet.RemoveSet
- HashSet.RemoveSlice
- HashSet.Size
- HashSet.Slice
- HashSet.String
- HashSet.Subset
- HashSet.Union
- HashSet.UnmarshalJSON
- Set.Contains
- Set.ContainsSlice
- Set.Copy
- Set.Difference
- Set.Empty
- Set.Equal
- Set.EqualSlice
- Set.EqualSliceSet
- Set.Insert
- Set.InsertSet
- Set.InsertSlice
- Set.Intersect
- Set.MarshalJSON
- Set.Remove
- Set.RemoveFunc
- Set.RemoveSet
- Set.RemoveSlice
- Set.Size
- Set.Slice
- Set.String
- Set.Subset
- Set.Union
- Set.UnmarshalJSON
- TreeSet.Above
- TreeSet.AboveEqual
- TreeSet.Below
- TreeSet.BelowEqual
- TreeSet.BottomK
- TreeSet.Contains
- TreeSet.ContainsSlice
- TreeSet.Copy
- TreeSet.Difference
- TreeSet.Empty
- TreeSet.Equal
- TreeSet.EqualSlice
- TreeSet.EqualSliceSet
- TreeSet.FirstAbove
- TreeSet.FirstAboveEqual
- TreeSet.FirstBelow
- TreeSet.FirstBelowEqual
- TreeSet.Insert
- TreeSet.InsertSet
- TreeSet.InsertSlice
- TreeSet.Intersect
- TreeSet.MarshalJSON
- TreeSet.Max
- TreeSet.Min
- TreeSet.Remove
- TreeSet.RemoveFunc
- TreeSet.RemoveSet
- TreeSet.RemoveSlice
- TreeSet.Size
- TreeSet.Slice
- TreeSet.String
- TreeSet.Subset
- TreeSet.TopK
- TreeSet.Union
- TreeSet.UnmarshalJSON
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InsertSetFunc ¶
func InsertSetFunc[T, E any](a Collection[T], b Collection[E], transform func(T) E) bool
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 ¶
func InsertSliceFunc[T, E any](col Collection[T], items []E, transform func(element E) T) bool
InsertSliceFunc inserts all elements from items into col, applying the transform function to each element before insertion.
Returns true if col was modified as a result of the operation.
func SliceFunc ¶
func SliceFunc[T, E any](s Collection[T], transform func(T) E) []E
SliceFunc produces a slice of the elements in s, applying the transform function to each element first.
Types ¶
type Collection ¶
type Collection[T any] interface { // Insert an element into the set. // // Returns true if the set is modified as a result. Insert(T) bool // InsertSlice will insert each element of a given slice into the set. // // Returns true if the set was modified as a result. InsertSlice([]T) bool // InsertSet will insert each element of a given Collection into the set. // // Returns true if the set was modified as a result. InsertSet(Collection[T]) bool // Remove will remove the given element from the set, if present. // // Returns true if the set was modified as a result of the operation. Remove(T) bool // RemoveSlice will remove each element of a slice from the set, if present. // // Returns true if the set was modified as a result of the operation. RemoveSlice([]T) bool // RemoveSet will remove each element of a Collection from the set. // // Returns true if the set was modified as a result of the operation. RemoveSet(Collection[T]) bool // RemoveFunc will remove each element from the set that satisfies the given predicate. // // Returns true if the set was modified as a result of the opearation. RemoveFunc(func(T) bool) bool // Contains returns whether an element is present in the set. Contains(T) bool // ContainsSlice returns whether the set contains the same set of elements as // the given slice. The elements of the slice may contain duplicates. ContainsSlice([]T) bool // Subset returns whether the given Collection is a subset of the set. Subset(Collection[T]) bool // ProperSubset returns whether the given Collection is a proper subset of the set. ProperSubset(Collection[T]) bool // Size returns the number of elements in the set. Size() int // Empty returns whether the set contains no elements. Empty() bool // Union returns a new set containing the unique elements of both this set // and a given Collection. // // https://en.wikipedia.org/wiki/Union_(set_theory) Union(Collection[T]) Collection[T] // Difference returns a new set that contains elements this set that are not // in a given Collection. // // https://en.wikipedia.org/wiki/Complement_(set_theory) Difference(Collection[T]) Collection[T] // Intersect returns a new set that contains only the elements present in // both this and a given Collection. // // https://en.wikipedia.org/wiki/Intersection_(set_theory) Intersect(Collection[T]) Collection[T] // Slice returns a slice of all elements in the set. // // For iterating elements, consider using Items() instead. // // Note: order of elements depends on the underlying implementation. Slice() []T // String creates a string representation of this set. // // Note: string representation depends on underlying implementation. String() string // StringFunc creates a string representation of this set, using the given // function to transform each element into a string. // // Note: string representation depends on underlying implementation. StringFunc(func(T) string) string // EqualSet returns whether this set and a given Collection contain the same // elements. EqualSet(Collection[T]) bool // EqualSlice returns whether this set and a given slice contain the same // elements, where the slice may contain duplicates. EqualSlice([]T) bool // EqualSliceSet returns whether this set and a given slice contain exactly // the same elements, where the slice must not contain duplicates. EqualSliceSet([]T) bool // Items returns a generator function for use with the range keyword // enabling iteration of each element in the set. // // Note: iteration order depends on the underlying implementation. // // for element := range s.Items() { ... } Items() iter.Seq[T] }
Fundamental set operations and familiar utility methods are part of this interface. Each of Set, HashSet, and TreeSet may also provide implementation specific methods not part of this interface.
type CompareFunc ¶
CompareFunc 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
Often T will be a type that satisfies cmp.Ordered, and CompareFunc can be implemented by using cmp.Compare.
type Hash ¶
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 ¶
HashFunc represents a function that that produces a hash value when applied to a given T. Typically this will be implemented as T.Hash but by separating HashFunc a HashSet can be made to make use of any hash implementation.
func HasherFunc ¶
HasherFunc creates a closure around the T.Hash function so that the type can be used as the HashFunc for a HashSet.
type HashSet ¶
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 ¶
HashSetFrom creates a new HashSet containing each element 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 HashSetFromFunc ¶
NewHashSetFromFunc creates a new HashSet containing each element in items.
func NewHashSet ¶
NewHashSet creates a HashSet with underlying capacity of size and will compute hash values from the T.Hash method.
func NewHashSetFunc ¶
NewHashSetFunc creates a HashSet with underlying capacity of size and uses the given hashing function to compute hashes on elements.
A HashSet will automatically grow or shrink its capacity as items are added or removed.
func (*HashSet[T, H]) Contains ¶
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]) ContainsSlice ¶
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 ¶
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 ¶
func (s *HashSet[T, H]) Difference(col Collection[T]) Collection[T]
Difference returns a set that contains elements of s that are not in col.
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 ¶
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 ¶
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]) EqualSet ¶
func (s *HashSet[T, H]) EqualSet(col Collection[T]) bool
EqualSet returns whether s and col contain the same elements.
func (*HashSet[T, H]) EqualSlice ¶
EqualSlice returns whether s and items contain the same elements.
The items slice may contain duplicates.
If the items slice is known to contain no duplicates, EqualSliceSet can be used instead as a faster implementation.
To detect if a slice is a subset of s, use ContainsSlice.
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.EqualSlice([]*person{bill, anna, carl})) fmt.Println(s.EqualSlice([]*person{anna, anna, bill, carl})) fmt.Println(s.EqualSlice([]*person{dave, bill, carl}))
Output: true true false
func (*HashSet[T, H]) EqualSliceSet ¶
EqualSliceSet returns whether s and items contain exactly the same elements.
If items contains duplicates EqualSliceSet will return false. The elements of items are assumed to be set-like. For comparing s to a slice that may contain duplicate elements, use EqualSlice instead.
To detect if a slice is a subset of s, use ContainsSlice.
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.EqualSliceSet([]*person{bill, anna, carl})) fmt.Println(s.EqualSliceSet([]*person{anna, anna, bill, carl})) fmt.Println(s.EqualSliceSet([]*person{dave, bill, carl}))
Output: true false false
func (*HashSet[T, H]) Insert ¶
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 ¶
func (s *HashSet[T, H]) InsertSet(col Collection[T]) bool
InsertSet will insert each element of col into s.
Return true if s was modified (at least one item of col 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 ¶
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 ¶
func (s *HashSet[T, H]) Intersect(col Collection[T]) Collection[T]
Intersect returns a set that contains elements that are present in both s and col.
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]) Items ¶
Items returns a generator function for iterating each element in s by using the range keyword.
for element := range s.Items() { ... }
func (*HashSet[T, H]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
Example ¶
type Foo struct { Persons *HashSet[*person, string] `json:"persons"` } f := Foo{ Persons: HashSetFrom([]*person{ {Name: "anna", ID: 94}, {Name: "bill", ID: 50}, {Name: "carl", ID: 10}, }), } b, _ := json.Marshal(f) fmt.Println(string(b))
Output: {"persons":[{"Name":"anna","ID":94},{"Name":"bill","ID":50},{"Name":"carl","ID":10}]}
func (*HashSet[T, H]) ProperSubset ¶
func (s *HashSet[T, H]) ProperSubset(col Collection[T]) bool
ProperSubset returns whether col is a proper subset of s.
func (*HashSet[T, H]) Remove ¶
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 ¶
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 ¶
func (s *HashSet[T, H]) RemoveSet(col Collection[T]) bool
RemoveSet will remove each element of col from s.
Return true if s was modified (any item of col 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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
func (s *HashSet[T, H]) Subset(col Collection[T]) bool
Subset returns whether col 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 ¶
func (s *HashSet[T, H]) Union(col Collection[T]) Collection[T]
Union returns a set that contains all elements of s and col combined.
Elements in s take priority in the event of colliding hash values.
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 ¶
UnmarshalJSON implements the json.Unmarshaler interface.
Example ¶
TODO: will not work as long as HashFunc cannot be derived from the type parameters.
// type Foo struct { // Persons *HashSet[*person, string] `json:"persons"` // } // in := `{"persons":[{"Name":"anna","ID":94},{"Name":"bill","ID":50},{"Name":"bill","ID":50},{"Name":"carl","ID":10}]}` // var out Foo // _ = json.Unmarshal([]byte(in), &out) // fmt.Println(out.Persons)
Output:
type Hasher ¶
type Hasher[H Hash] interface { Hash() H }
Hasher represents a type that implements a Hash() method. Types that wish to cache a hash value with an internal field should implement Hash accordingly.
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 may be any comparable type. Keep in mind that pointer types or structs containing pointer fields will be compared using shallow equality. For deep equality use HashSet instead.
func FromFunc ¶
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 may be any comparable type. Keep in mind that pointer types or structs containing pointer fields will be compared using shallow equality. For deep equality 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 may be any comparable type. Keep in mind that pointer types or structs containing pointer fields will be compared using shallow equality. For deep equality 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]) ContainsSlice ¶
ContainsSlice returns whether all elements in items are present in s.
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: true 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 ¶
func (s *Set[T]) Difference(col Collection[T]) Collection[T]
Difference returns a set that contains elements of s that are not in col.
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 ¶
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 ¶
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]) EqualSet ¶
func (s *Set[T]) EqualSet(col Collection[T]) bool
EqualSet returns whether s and col contain the same elements.
func (*Set[T]) EqualSlice ¶
EqualSlice returns whether s and items contain the same elements.
The items slice may contain duplicates.
If the items slice is known to contain no duplicates, EqualSliceSet may be used instead as a faster implementation.
To detect if a slice is a subset of s, use ContainsSlice.
Example ¶
s := From([]string{"red", "green", "blue"}) fmt.Println(s.EqualSlice([]string{"red", "blue", "green"})) fmt.Println(s.EqualSlice([]string{"red", "red", "blue", "green"})) fmt.Println(s.EqualSlice([]string{"yellow", "blue", "green"}))
Output: true true false
func (*Set[T]) EqualSliceSet ¶
EqualSliceSet returns whether s and items contain exactly the same elements.
If items contains duplicates EqualSliceSet will return false. The elements of items are assumed to be set-like. For comparing s to a slice that may contain duplicate elements, use EqualSlice instead.
To detect if a slice is a subset of s, use ContainsSlice.
Example ¶
s := From([]string{"red", "green", "blue"}) fmt.Println(s.EqualSliceSet([]string{"red", "blue", "green"})) fmt.Println(s.EqualSliceSet([]string{"red", "red", "blue", "green"})) fmt.Println(s.EqualSliceSet([]string{"yellow", "blue", "green"}))
Output: true false false
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 ¶
func (s *Set[T]) InsertSet(col Collection[T]) bool
InsertSet will insert each element of col into s.
Return true if s was modified (at least one item of col 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 ¶
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 ¶
func (s *Set[T]) Intersect(col Collection[T]) Collection[T]
Intersect returns a set that contains elements that are present in both s and col.
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]) Items ¶
Items returns a generator function for iterating each element in s by using the range keyword.
for element := range s.Items() { ... }
func (*Set[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
Example ¶
type Foo struct { Colors *Set[string] `json:"colors"` } f := Foo{ Colors: From([]string{"red", "green", "blue"}), } b, _ := json.Marshal(f) fmt.Println(string(b))
Output: {"colors":["red","green","blue"]}
func (*Set[T]) ProperSubset ¶
func (s *Set[T]) ProperSubset(col Collection[T]) bool
Subset returns whether col is a proper subset of s.
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 ¶
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 ¶
func (s *Set[T]) RemoveSet(col Collection[T]) bool
RemoveSet will remove each element of col 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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
func (s *Set[T]) Subset(col Collection[T]) bool
Subset returns whether col 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 ¶
func (s *Set[T]) Union(col Collection[T]) Collection[T]
Union returns a set that contains all elements of s and col 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 ¶
UnmarshalJSON implements the json.Unmarshaler interface.
Example ¶
type Foo struct { Colors *Set[string] `json:"colors"` } in := `{"colors":["red","green","green","blue"]}` var out Foo _ = json.Unmarshal([]byte(in), &out) fmt.Println(out.Colors)
Output: [blue green red]
type TreeNodeVisit ¶
TreeNodeVisit is a function that is called for each node in the tree.
type TreeSet ¶
type TreeSet[T any] struct { // contains filtered or unexported fields }
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 ¶
func NewTreeSet[T any](compare CompareFunc[T]) *TreeSet[T]
NewTreeSet creates a TreeSet of type T, comparing elements via a given CompareFunc[T].
T may be any type.
For builtin types, CompareBuiltin provides a convenient CompareFunc implementation.
func TreeSetFrom ¶
func TreeSetFrom[T any](items []T, compare CompareFunc[T]) *TreeSet[T]
TreeSetFrom creates a new TreeSet containing each item in items.
T may be any type.
C is an implementation of CompareFunc[T]. For builtin types, Cmp provides a convenient Compare implementation.
func (*TreeSet[T]) Above ¶
After returns a TreeSet containing the elements of s that are > item.
Example ¶
s := TreeSetFrom[int]([]int{1, 2, 3, 4, 5}, cmp.Compare[int]) fmt.Println(s.Above(3)) fmt.Println(s.Above(5)) fmt.Println(s.Above(10))
Output: [4 5] [] []
func (*TreeSet[T]) AboveEqual ¶
AfterEqual returns a TreeSet containing the elements of s that are ≥ item.
Example ¶
s := TreeSetFrom[int]([]int{1, 2, 3, 4, 5}, cmp.Compare[int]) fmt.Println(s.AboveEqual(3)) fmt.Println(s.AboveEqual(5)) fmt.Println(s.AboveEqual(10))
Output: [3 4 5] [5] []
func (*TreeSet[T]) Below ¶
Below returns a TreeSet containing the elements of s that are < item.
Example ¶
s := TreeSetFrom[int]([]int{1, 2, 3, 4, 5}, cmp.Compare[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]) BelowEqual ¶
BelowEqual returns a TreeSet containing the elements of s that are ≤ item.
Example ¶
s := TreeSetFrom[int]([]int{1, 2, 3, 4, 5}, cmp.Compare[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]) BottomK ¶
BottomK returns the bottom n (largest) elements in s, in descending order.
Example ¶
s := TreeSetFrom[int]([]int{1, 2, 3, 4, 5}, cmp.Compare[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]) Contains ¶
Contains returns whether item is present in s.
Example ¶
s := TreeSetFrom[string]([]string{"red", "green", "blue"}, cmp.Compare[string]) fmt.Println(s.Contains("green")) fmt.Println(s.Contains("orange"))
Output: true false
func (*TreeSet[T]) ContainsSlice ¶
ContainsSlice returns whether all elements in items are present in s.
Example ¶
s := TreeSetFrom[string]([]string{"red", "green", "blue"}, cmp.Compare[string]) fmt.Println(s.ContainsSlice([]string{"red", "green"})) fmt.Println(s.ContainsSlice([]string{"red", "orange"}))
Output: true false
func (*TreeSet[T]) Copy ¶
Copy creates a copy of s.
Individual elements are reference copies.
Example ¶
s := TreeSetFrom[int]([]int{1, 2, 3, 4, 5}, cmp.Compare[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]) Difference ¶
func (s *TreeSet[T]) Difference(col Collection[T]) Collection[T]
Difference returns a set that contains elements of s that are not in col.
Example ¶
s := TreeSetFrom[int]([]int{1, 2, 3, 4, 5}, cmp.Compare[int]) t := TreeSetFrom[int]([]int{5, 4, 3, 2, 1}, cmp.Compare[int]) f := TreeSetFrom[int]([]int{1, 3, 5, 7, 9}, cmp.Compare[int]) fmt.Println(s.Difference(t)) fmt.Println(s.Difference(f))
Output: [] [2 4]
func (*TreeSet[T]) Empty ¶
Empty returns true if there are no elements in s.
Example ¶
s := TreeSetFrom[string]([]string{}, cmp.Compare[string]) fmt.Println(s.Empty()) s.InsertSlice([]string{"red", "green", "blue"}) fmt.Println(s.Empty())
Output: true false
func (*TreeSet[T]) Equal ¶
Equal return whether s and o contain the same elements.
Example ¶
s := TreeSetFrom[int]([]int{1, 2, 3, 4, 5}, cmp.Compare[int]) t := TreeSetFrom[int]([]int{5, 4, 3, 2, 1}, cmp.Compare[int]) f := TreeSetFrom[int]([]int{1, 3, 5, 7, 9}, cmp.Compare[int]) fmt.Println(s.Equal(t)) fmt.Println(s.Equal(f))
Output: true false
func (*TreeSet[T]) EqualSet ¶
func (s *TreeSet[T]) EqualSet(col Collection[T]) bool
EqualSet returns s and col contain the same elements.
func (*TreeSet[T]) EqualSlice ¶
EqualSlice returns whether s and items contain the same elements.
The items slice may contain duplicates.
If the items slice is known to contain no duplicates, EqualSliceSet may be used instead as a faster implementation.
To detect if a slice is a subset of s, use ContainsSlice.
Example ¶
t := TreeSetFrom[int]([]int{5, 4, 3, 2, 1}, cmp.Compare[int]) fmt.Println(t.EqualSlice([]int{1, 2, 3, 4, 5})) fmt.Println(t.EqualSlice([]int{1, 1, 2, 3, 4, 5})) fmt.Println(t.EqualSlice([]int{0, 2, 3, 4, 5}))
Output: true true false
func (*TreeSet[T]) EqualSliceSet ¶
EqualSliceSet returns whether s and items contain exactly the same elements.
If items contains duplicates EqualSliceSet will return false. The elements of items are assumed to be set-like. For comparing s to a slice that may contain duplicate elements, use EqualSlice instead.
To detect if a slice is a subset of s, use ContainsSlice.
Example ¶
t := TreeSetFrom[int]([]int{5, 4, 3, 2, 1}, cmp.Compare[int]) fmt.Println(t.EqualSliceSet([]int{1, 2, 3, 4, 5})) fmt.Println(t.EqualSliceSet([]int{1, 1, 2, 3, 4, 5})) fmt.Println(t.EqualSliceSet([]int{0, 2, 3, 4, 5}))
Output: true false false
func (*TreeSet[T]) FirstAbove ¶
FirstAbove returns the first element strictly above item.
A zero value and false are returned if no such element exists.
Example ¶
s := TreeSetFrom[int]([]int{1, 2, 3, 4, 5}, cmp.Compare[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]) FirstAboveEqual ¶
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]([]int{1, 2, 3, 4, 5}, cmp.Compare[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]) FirstBelow ¶
FirstBelow returns the first element strictly below item.
A zero value and false are returned if no such element exists.
Example ¶
s := TreeSetFrom[int]([]int{1, 2, 3, 4, 5}, cmp.Compare[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]) FirstBelowEqual ¶
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]([]int{1, 2, 3, 4, 5}, cmp.Compare[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]) Insert ¶
Insert item into s.
Returns true if s was modified (item was not already in s), false otherwise.
Example ¶
s := TreeSetFrom[string]([]string{}, cmp.Compare[string]) fmt.Println(s) s.Insert("red") s.Insert("green") s.Insert("blue") fmt.Println(s) // [] // [blue green red]
Output:
func (*TreeSet[T]) InsertSet ¶
func (s *TreeSet[T]) InsertSet(col Collection[T]) bool
InsertSet will insert each element of col 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]([]string{"red", "green"}, cmp.Compare[string]) s2 := TreeSetFrom[string]([]string{"green", "blue"}, cmp.Compare[string]) fmt.Println(s1) fmt.Println(s2) s1.InsertSet(s2) fmt.Println(s1)
Output: [green red] [blue green] [blue green red]
func (*TreeSet[T]) InsertSlice ¶
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]([]string{}, cmp.Compare[string]) fmt.Println(s) s.InsertSlice([]string{"red", "green", "blue"}) fmt.Println(s) // [] // [blue green red]
Output:
func (*TreeSet[T]) Intersect ¶
func (s *TreeSet[T]) Intersect(col Collection[T]) Collection[T]
Intersect returns a set that contains elements that are present in both s and col.
Example ¶
s := TreeSetFrom[int]([]int{1, 2, 3, 4, 5}, cmp.Compare[int]) t := TreeSetFrom[int]([]int{5, 4, 3, 2, 1}, cmp.Compare[int]) f := TreeSetFrom[int]([]int{1, 3, 5, 7, 9}, cmp.Compare[int]) fmt.Println(s.Intersect(t)) fmt.Println(s.Intersect(f))
Output: [1 2 3 4 5] [1 3 5]
func (*TreeSet[T]) Items ¶
Items returns a generator function for iterating each element in s by using the range keyword.
for i, element := range s.Items() { ... }
func (*TreeSet[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
Example ¶
type Foo struct { Colors *TreeSet[string] `json:"colors"` } f := Foo{ Colors: TreeSetFrom([]string{"red", "green", "blue"}, cmp.Compare[string]), } b, _ := json.Marshal(f) fmt.Println(string(b))
Output: {"colors":["blue","green","red"]}
func (*TreeSet[T]) Max ¶
func (s *TreeSet[T]) Max() T
Max returns the largest item in s.
Must not be called on an empty set.
Example ¶
s := TreeSetFrom[int]([]int{1, 2, 3, 4, 5}, cmp.Compare[int]) r := TreeSetFrom[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]) Min ¶
func (s *TreeSet[T]) Min() T
Min returns the smallest item in the set.
Must not be called on an empty set.
Example ¶
s := TreeSetFrom[int]([]int{1, 2, 3, 4, 5}, cmp.Compare[int]) r := TreeSetFrom[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]) ProperSubset ¶
func (s *TreeSet[T]) ProperSubset(col Collection[T]) bool
ProperSubset returns whether col is a proper subset of s.
func (*TreeSet[T]) Remove ¶
Remove item from s.
Returns true if s was modified (item was in s), false otherwise.
Example ¶
s := TreeSetFrom[string]([]string{"red", "green", "blue"}, cmp.Compare[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]) RemoveFunc ¶
RemoveFunc will remove each element from s that satisifies condition f.
Return true if s was modified, false otherwise.
Example ¶
s := TreeSetFrom[int](ints(20), cmp.Compare[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]) RemoveSet ¶
func (s *TreeSet[T]) RemoveSet(col Collection[T]) bool
RemoveSet will remove each element in col from s.
Returns true if s was modified (at least one item in o was in s), false otherwise.
Example ¶
s1 := TreeSetFrom[string]([]string{"a", "b", "c", "d", "e", "f"}, cmp.Compare[string]) s2 := TreeSetFrom[string]([]string{"e", "z", "a"}, cmp.Compare[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]) RemoveSlice ¶
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]([]string{"red", "green", "blue"}, cmp.Compare[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]) Size ¶
Size returns the number of elements in s.
Example ¶
s := TreeSetFrom[string]([]string{"red", "green", "blue"}, cmp.Compare[string]) fmt.Println(s.Size())
Output: 3
func (*TreeSet[T]) Slice ¶
func (s *TreeSet[T]) Slice() []T
Slice returns the elements of s as a slice, in order.
Example ¶
s := TreeSetFrom[int]([]int{1, 2, 3, 4, 5}, cmp.Compare[int]) slice := s.Slice() fmt.Println(slice) fmt.Println(len(slice))
Output: [1 2 3 4 5] 5
func (*TreeSet[T]) String ¶
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]([]int{1, 2, 3, 4, 5}, cmp.Compare[int]) fmt.Println(s.String() == "[1 2 3 4 5]")
Output: true
func (*TreeSet[T]) StringFunc ¶
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]) Subset ¶
func (s *TreeSet[T]) Subset(col Collection[T]) bool
Subset returns whether col is a subset of s.
Example ¶
s1 := TreeSetFrom[string]([]string{"a", "b", "c", "d", "e"}, cmp.Compare[string]) s2 := TreeSetFrom[string]([]string{"b", "d"}, cmp.Compare[string]) s3 := TreeSetFrom[string]([]string{"a", "z"}, cmp.Compare[string]) fmt.Println(s1.Subset(s2)) fmt.Println(s1.Subset(s3))
Output: true false
func (*TreeSet[T]) TopK ¶
TopK returns the top n (smallest) elements in s, in ascending order.
Example ¶
s := TreeSetFrom[int]([]int{1, 2, 3, 4, 5}, cmp.Compare[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]) Union ¶
func (s *TreeSet[T]) Union(col Collection[T]) Collection[T]
Union returns a set that contains all elements of s and col combined.
Example ¶
s := TreeSetFrom[int]([]int{1, 2, 3, 4, 5}, cmp.Compare[int]) t := TreeSetFrom[int]([]int{5, 4, 3, 2, 1}, cmp.Compare[int]) f := TreeSetFrom[int]([]int{1, 3, 5, 7, 9}, cmp.Compare[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]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
Example ¶
TODO: will not work as long as CompareFunc cannot be derived from the type parameters.
// type Foo struct { // Colors *TreeSet[string] `json:"colors"` // } // in := `{"colors":["red","green","green","blue"]}` // var out Foo // _ = json.Unmarshal([]byte(in), &out) // fmt.Println(out.Colors)
Output: