some testing ang enchancments
This commit is contained in:
107
slice.go
107
slice.go
@@ -2,7 +2,6 @@ package extypes
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"slices"
|
||||
)
|
||||
|
||||
type Slice[T any] []T
|
||||
@@ -26,17 +25,11 @@ func (s Slice[T]) Last() T {
|
||||
}
|
||||
func (s Slice[T]) Index(el T) int {
|
||||
for i := range s {
|
||||
//t1 := reflect.TypeOf(el)
|
||||
//t2 := reflect.TypeOf(s[i])
|
||||
//if t1 != t2 {
|
||||
// continue
|
||||
//}
|
||||
if reflect.DeepEqual(s[i], el) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
//return slices.Index(s, el)
|
||||
}
|
||||
|
||||
// Swap is mutable func
|
||||
@@ -64,13 +57,9 @@ func (s Slice[T]) Pop(index int) Slice[T] {
|
||||
if index == 0 {
|
||||
return s[1:]
|
||||
}
|
||||
out := make(Slice[T], 0, s.Len()-1)
|
||||
for i, e := range s {
|
||||
if i == index {
|
||||
continue
|
||||
}
|
||||
out = append(out, e)
|
||||
}
|
||||
out := make(Slice[T], s.Len()-1)
|
||||
copy(out, s[:index])
|
||||
copy(out[index:], s[index+1:])
|
||||
return out
|
||||
}
|
||||
func (s Slice[T]) Remove(el T) Slice[T] {
|
||||
@@ -96,6 +85,9 @@ func (s Slice[T]) ToAnyArray() []any {
|
||||
}
|
||||
return out
|
||||
}
|
||||
func (s Slice[T]) ToSet() Set[T] {
|
||||
return NewSetFrom(s)
|
||||
}
|
||||
|
||||
// ForEach is mutable func
|
||||
func (s Slice[T]) ForEach(f func(e T) T) Slice[T] {
|
||||
@@ -105,86 +97,17 @@ func (s Slice[T]) ForEach(f func(e T) T) Slice[T] {
|
||||
return s
|
||||
}
|
||||
|
||||
type Set[T comparable] []T
|
||||
|
||||
func NewSetFrom[T comparable](slice []T) Set[T] {
|
||||
s := make(Set[T], 0)
|
||||
for _, v := range slice {
|
||||
if s.Index(v) >= 0 {
|
||||
continue
|
||||
}
|
||||
s = append(s, v)
|
||||
func (s Slice[T]) Equal(s2 Slice[T]) bool {
|
||||
if s.Len() != s2.Len() {
|
||||
return false
|
||||
}
|
||||
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[s.Len()-1]
|
||||
}
|
||||
func (s Set[T]) Index(el T) int {
|
||||
return slices.Index(s, el)
|
||||
}
|
||||
func (s Set[T]) Swap(i, j int) {
|
||||
s[i], s[j] = s[j], s[i]
|
||||
}
|
||||
func (s Set[T]) Add(v T) Set[T] {
|
||||
index := s.Index(v)
|
||||
if index >= 0 {
|
||||
return s
|
||||
if s.Cap() != s2.Cap() {
|
||||
return false
|
||||
}
|
||||
return append(s, v)
|
||||
}
|
||||
func (s Set[T]) Pop(index int) Set[T] {
|
||||
if index == 0 {
|
||||
return s[1:]
|
||||
}
|
||||
out := make(Set[T], 0, s.Len()-1)
|
||||
for i, e := range s {
|
||||
if i == index {
|
||||
continue
|
||||
}
|
||||
out = append(out, e)
|
||||
}
|
||||
return out
|
||||
}
|
||||
func (s Set[T]) Remove(el T) Set[T] {
|
||||
index := s.Index(el)
|
||||
if index == -1 {
|
||||
return s
|
||||
}
|
||||
return s.Pop(index)
|
||||
}
|
||||
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)
|
||||
for i := range s {
|
||||
if !reflect.DeepEqual(s[i], s2[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
func (s Set[T]) ToSlice() Slice[T] {
|
||||
out := make(Slice[T], s.Len())
|
||||
copy(out, s)
|
||||
return out
|
||||
}
|
||||
func (s Set[T]) ToArray() []T {
|
||||
out := make([]T, len(s))
|
||||
copy(out, s)
|
||||
return out
|
||||
}
|
||||
func (s Set[T]) ToAnyArray() []any {
|
||||
out := make([]any, len(s))
|
||||
for i, v := range s {
|
||||
out[i] = v
|
||||
}
|
||||
return out
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user