Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
948ac0a1b0
|
|||
|
7d930b6e97
|
30
slice.go
30
slice.go
@@ -1,10 +1,13 @@
|
||||
package extypes
|
||||
|
||||
import "slices"
|
||||
import (
|
||||
"reflect"
|
||||
"slices"
|
||||
)
|
||||
|
||||
type Slice[T comparable] []T
|
||||
type Slice[T any] []T
|
||||
|
||||
func NewSliceFrom[T comparable](slice []T) Slice[T] {
|
||||
func NewSliceFrom[T any](slice []T) Slice[T] {
|
||||
s := make(Slice[T], len(slice))
|
||||
copy(s, slice)
|
||||
return s
|
||||
@@ -22,7 +25,18 @@ func (s Slice[T]) Last() T {
|
||||
return s.Get(s.Len() - 1)
|
||||
}
|
||||
func (s Slice[T]) Index(el T) int {
|
||||
return slices.Index(s, el)
|
||||
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
|
||||
@@ -50,12 +64,12 @@ func (s Slice[T]) Pop(index int) Slice[T] {
|
||||
if index == 0 {
|
||||
return s[1:]
|
||||
}
|
||||
out := make(Slice[T], s.Len()-index)
|
||||
out := make(Slice[T], 0, s.Len()-1)
|
||||
for i, e := range s {
|
||||
if i == index {
|
||||
continue
|
||||
}
|
||||
out[i] = e
|
||||
out = append(out, e)
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -132,12 +146,12 @@ func (s Set[T]) Pop(index int) Set[T] {
|
||||
if index == 0 {
|
||||
return s[1:]
|
||||
}
|
||||
out := make(Set[T], s.Len()-index)
|
||||
out := make(Set[T], 0, s.Len()-1)
|
||||
for i, e := range s {
|
||||
if i == index {
|
||||
continue
|
||||
}
|
||||
out[i] = e
|
||||
out = append(out, e)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user