docs: add godoc and README files in English and Russian
This commit is contained in:
55
set.go
55
set.go
@@ -6,8 +6,13 @@ import (
|
||||
"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 {
|
||||
@@ -18,12 +23,24 @@ func NewSetFrom[T any](slice []T) Set[T] {
|
||||
}
|
||||
return s
|
||||
}
|
||||
func (s Set[T]) Len() int { return len(s) }
|
||||
func (s Set[T]) Cap() int { return cap(s) }
|
||||
func (s Set[T]) Get(index int) T { return s[index] }
|
||||
func (s Set[T]) Last() T { return s.Get(s.Len() - 1) }
|
||||
func (s Set[T]) First() T { return s.Get(0) }
|
||||
|
||||
// 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) {
|
||||
@@ -32,9 +49,13 @@ func (s Set[T]) Index(el T) int {
|
||||
}
|
||||
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 {
|
||||
@@ -42,6 +63,8 @@ func (s Set[T]) Add(v T) Set[T] {
|
||||
}
|
||||
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:]
|
||||
@@ -51,6 +74,8 @@ func (s Set[T]) Pop(index int) Set[T] {
|
||||
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 {
|
||||
@@ -58,6 +83,9 @@ func (s Set[T]) Remove(el T) Set[T] {
|
||||
}
|
||||
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
|
||||
@@ -70,13 +98,20 @@ func (s Set[T]) Equal(s2 Set[T]) bool {
|
||||
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 {
|
||||
@@ -84,6 +119,9 @@ func (s Set[T]) ToAnyArray() []any {
|
||||
}
|
||||
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 {
|
||||
@@ -93,6 +131,8 @@ func (s Set[T]) Filter(f func(e T) bool) Set[T] {
|
||||
}
|
||||
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)
|
||||
@@ -100,7 +140,8 @@ func (s Set[T]) ForEach(f func(int, T)) Set[T] {
|
||||
return s
|
||||
}
|
||||
|
||||
// Map is mutable func
|
||||
// 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)
|
||||
@@ -108,6 +149,8 @@ func (s Set[T]) Map(f func(e T) T) Set[T] {
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user