1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-17 16:12:42 +08:00
This commit is contained in:
2024-05-28 04:27:34 +08:00
parent 38f7cc75c9
commit b0ff4d6fd5

View File

@ -14,20 +14,23 @@ type sorted_set[T constraints.Ordered] struct {
func NewSortedSet[T constraints.Ordered](t ...T) *sorted_set[T] { func NewSortedSet[T constraints.Ordered](t ...T) *sorted_set[T] {
return &sorted_set[T]{ return &sorted_set[T]{
set: NewHashSet[T](), sorted: t,
set: NewHashSet(t...),
} }
} }
func (s *sorted_set[T]) Add(values ...T) { func (s *sorted_set[T]) Add(values ...T) Set[T] {
for _, v := range values { for _, v := range values {
if !s.set.Contains(v) { if !s.set.Contains(v) {
s.sorted = append(s.sorted, v) s.sorted = append(s.sorted, v)
s.set.Add(v) s.set.Add(v)
} }
} }
return s
} }
func (s *sorted_set[T]) Remove(v T) { func (s *sorted_set[T]) Remove(v T) Set[T] {
if s.set.Contains(v) { if s.set.Contains(v) {
for index := range s.sorted { for index := range s.sorted {
if s.sorted[index] == v { if s.sorted[index] == v {
@ -38,6 +41,8 @@ func (s *sorted_set[T]) Remove(v T) {
s.set.Remove(v) s.set.Remove(v)
} }
return s
} }
func (s *sorted_set[T]) Asc() Set[T] { func (s *sorted_set[T]) Asc() Set[T] {