mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 08:32:40 +08:00
iter
This commit is contained in:
@ -25,7 +25,7 @@ func (s hash_set[T]) Contain(value T) bool {
|
||||
|
||||
func (s hash_set[T]) Clone() hash_set[T] {
|
||||
set := NewHashSet[T]()
|
||||
set.Add(s.Values()...)
|
||||
set.Add(s.ToSlice()...)
|
||||
return set
|
||||
}
|
||||
|
||||
@ -38,7 +38,7 @@ func (s hash_set[T]) Iterate(fn func(value T)) {
|
||||
// Union creates a new set contain all element of set s and other
|
||||
func (s hash_set[T]) Union(other hash_set[T]) hash_set[T] {
|
||||
set := s.Clone()
|
||||
set.Add(other.Values()...)
|
||||
set.Add(other.ToSlice()...)
|
||||
return set
|
||||
}
|
||||
|
||||
@ -54,7 +54,7 @@ func (s hash_set[T]) Intersection(other hash_set[T]) hash_set[T] {
|
||||
return set
|
||||
}
|
||||
|
||||
func (s hash_set[T]) Values() []T {
|
||||
func (s hash_set[T]) ToSlice() []T {
|
||||
values := make([]T, 0, s.Size())
|
||||
s.Iterate(func(value T) {
|
||||
values = append(values, value)
|
||||
|
@ -1,4 +1,8 @@
|
||||
package sets
|
||||
|
||||
type Set[T comparable] interface {
|
||||
Add(values ...T)
|
||||
Remove(v T)
|
||||
Contain(value T) bool
|
||||
IsEmpty() bool
|
||||
}
|
||||
|
@ -5,4 +5,12 @@ type sorted_set[T comparable] struct {
|
||||
set Set[T]
|
||||
}
|
||||
|
||||
func NewSortedSet[T comparable]() *sorted_set[T] {
|
||||
return &sorted_set[T]{
|
||||
set: NewHashSet[T](),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *sorted_set[T]) ToSlice() []T {
|
||||
return s.sorted
|
||||
}
|
||||
|
Reference in New Issue
Block a user