161 lines
3.5 KiB
Go
161 lines
3.5 KiB
Go
package extypes
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
)
|
|
|
|
// Set is a uniqueness-enforcing collection backed by a slice.
|
|
//
|
|
// Order is preserved based on first appearance. Equality is order-sensitive
|
|
// because the underlying representation is a slice.
|
|
type Set[T any] []T
|
|
|
|
// NewSetFrom returns a new Set with duplicate values removed.
|
|
func NewSetFrom[T any](slice []T) Set[T] {
|
|
s := make(Set[T], 0)
|
|
for _, v := range slice {
|
|
if s.Index(v) >= 0 {
|
|
continue
|
|
}
|
|
s = append(s, v)
|
|
}
|
|
return s
|
|
}
|
|
|
|
// Len returns the number of elements in s.
|
|
func (s Set[T]) Len() int { return len(s) }
|
|
|
|
// Cap returns the capacity of s.
|
|
func (s Set[T]) Cap() int { return cap(s) }
|
|
|
|
// Get returns the element at index.
|
|
func (s Set[T]) Get(index int) T { return s[index] }
|
|
|
|
// Last returns the last element.
|
|
func (s Set[T]) Last() T { return s.Get(s.Len() - 1) }
|
|
|
|
// First returns the first element.
|
|
func (s Set[T]) First() T { return s.Get(0) }
|
|
|
|
// Index returns the first index of el or -1 when el is not present.
|
|
// Comparison uses reflect.DeepEqual.
|
|
func (s Set[T]) Index(el T) int {
|
|
for i := range s {
|
|
if reflect.DeepEqual(s[i], el) {
|
|
return i
|
|
}
|
|
}
|
|
return -1
|
|
}
|
|
|
|
// Swap swaps two elements in place.
|
|
func (s Set[T]) Swap(i, j int) {
|
|
s[i], s[j] = s[j], s[i]
|
|
}
|
|
|
|
// Add appends v if it is not already present.
|
|
func (s Set[T]) Add(v T) Set[T] {
|
|
index := s.Index(v)
|
|
if index >= 0 {
|
|
return s
|
|
}
|
|
return append(s, v)
|
|
}
|
|
|
|
// Pop removes the element at index and returns the resulting set.
|
|
func (s Set[T]) Pop(index int) Set[T] {
|
|
if index == 0 {
|
|
return s[1:]
|
|
}
|
|
out := make(Set[T], s.Len()-1)
|
|
copy(out, s[:index])
|
|
copy(out[index:], s[index+1:])
|
|
return out
|
|
}
|
|
|
|
// Remove removes the first matching element and returns the resulting set.
|
|
func (s Set[T]) Remove(el T) Set[T] {
|
|
index := s.Index(el)
|
|
if index == -1 {
|
|
return s
|
|
}
|
|
return s.Pop(index)
|
|
}
|
|
|
|
// Equal reports whether both sets have the same length and pairwise equal
|
|
// elements in the same order according to reflect.DeepEqual.
|
|
func (s Set[T]) Equal(s2 Set[T]) bool {
|
|
if s.Len() != s2.Len() {
|
|
return false
|
|
}
|
|
for i := range s {
|
|
if !reflect.DeepEqual(s[i], s2[i]) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// ToSlice returns a copy of s as a Slice.
|
|
func (s Set[T]) ToSlice() Slice[T] { return NewSliceFrom(s) }
|
|
|
|
// ToTuple returns a Tuple view of s without copying.
|
|
func (s Set[T]) ToTuple() Tuple[T] { return NewTupleFrom(s) }
|
|
|
|
// ToArray returns a copy of s as a built-in slice.
|
|
func (s Set[T]) ToArray() []T {
|
|
out := make([]T, len(s))
|
|
copy(out, s)
|
|
return out
|
|
}
|
|
|
|
// ToAnyArray returns a copy of s converted to []any.
|
|
func (s Set[T]) ToAnyArray() []any {
|
|
out := make([]any, len(s))
|
|
for i, v := range s {
|
|
out[i] = v
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Filter returns a new Set containing only elements accepted by f.
|
|
// The result preserves order and therefore remains duplicate-free.
|
|
func (s Set[T]) Filter(f func(e T) bool) Set[T] {
|
|
out := make(Set[T], 0)
|
|
for _, v := range s {
|
|
if f(v) {
|
|
out = append(out, v)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// ForEach calls f for every element and returns s.
|
|
func (s Set[T]) ForEach(f func(int, T)) Set[T] {
|
|
for i, v := range s {
|
|
f(i, v)
|
|
}
|
|
return s
|
|
}
|
|
|
|
// Map applies f to each element in place and returns s.
|
|
// It does not remove duplicates that may be produced by f.
|
|
func (s Set[T]) Map(f func(e T) T) Set[T] {
|
|
for i, v := range s {
|
|
s[i] = f(v)
|
|
}
|
|
return s
|
|
}
|
|
|
|
// Join formats every element with fmt.Sprintf("%v", value) and joins them
|
|
// using sep.
|
|
func (s Set[T]) Join(sep string) string {
|
|
st := make([]string, len(s))
|
|
for i, v := range s {
|
|
st[i] = fmt.Sprintf("%v", v)
|
|
}
|
|
return strings.Join(st, sep)
|
|
}
|