This commit is contained in:
parent
2b703e9af2
commit
70c2da4a4d
40 changed files with 821 additions and 117 deletions
38
src/set/Ordered.go
Normal file
38
src/set/Ordered.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
package set
|
||||
|
||||
import (
|
||||
"iter"
|
||||
"slices"
|
||||
)
|
||||
|
||||
// Ordered is an ordered set.
|
||||
type Ordered[T comparable] struct {
|
||||
values []T
|
||||
}
|
||||
|
||||
// Add adds a value to the set if it doesn't exist yet.
|
||||
// It returns `false` if it already exists, `true` if it was added.
|
||||
func (set *Ordered[T]) Add(value T) bool {
|
||||
if slices.Contains(set.values, value) {
|
||||
return false
|
||||
}
|
||||
|
||||
set.values = append(set.values, value)
|
||||
return true
|
||||
}
|
||||
|
||||
// All returns an iterator over all the values in the set.
|
||||
func (set *Ordered[T]) All() iter.Seq[T] {
|
||||
return func(yield func(T) bool) {
|
||||
for _, value := range set.values {
|
||||
if !yield(value) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Count returns the number of elements in the set.
|
||||
func (set *Ordered[T]) Count() int {
|
||||
return len(set.values)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue