work on HashMap and slices
This commit is contained in:
46
map.go
Normal file
46
map.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package laniakea
|
||||||
|
|
||||||
|
type HashMap[K comparable, V any] map[K]V
|
||||||
|
|
||||||
|
func NewMapFrom[K comparable, V any](m map[K]V) HashMap[K, V] {
|
||||||
|
out := make(HashMap[K, V])
|
||||||
|
for k, v := range m {
|
||||||
|
out[k] = v
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
func (m HashMap[K, V]) Len() int {
|
||||||
|
return len(m)
|
||||||
|
}
|
||||||
|
func (m HashMap[K, V]) Add(key K, val V) HashMap[K, V] {
|
||||||
|
m[key] = val
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
func (m HashMap[K, V]) Remove(key K) HashMap[K, V] {
|
||||||
|
delete(m, key)
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
func (m HashMap[K, V]) Get(key K) V {
|
||||||
|
return m[key]
|
||||||
|
}
|
||||||
|
func (m HashMap[K, V]) GetOrDefault(key K, def V) V {
|
||||||
|
v, ok := m[key]
|
||||||
|
if !ok {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
func (m HashMap[K, V]) Contains(key K) bool {
|
||||||
|
_, ok := m[key]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
func (m HashMap[K, V]) Filter(f func(K, V) bool) HashMap[K, V] {
|
||||||
|
out := make(HashMap[K, V])
|
||||||
|
for k, v := range m {
|
||||||
|
if !f(k, v) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out[k] = v
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
26
slice.go
26
slice.go
@@ -6,7 +6,7 @@ type Slice[T comparable] []T
|
|||||||
|
|
||||||
func NewSliceFrom[T comparable](slice []T) Slice[T] {
|
func NewSliceFrom[T comparable](slice []T) Slice[T] {
|
||||||
s := make(Slice[T], len(slice))
|
s := make(Slice[T], len(slice))
|
||||||
copy(s[:], slice)
|
copy(s, slice)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
func (s Slice[T]) Len() int {
|
func (s Slice[T]) Len() int {
|
||||||
@@ -21,6 +21,11 @@ func (s Slice[T]) Get(index int) T {
|
|||||||
func (s Slice[T]) Last() T {
|
func (s Slice[T]) Last() T {
|
||||||
return s.Get(s.Len() - 1)
|
return s.Get(s.Len() - 1)
|
||||||
}
|
}
|
||||||
|
func (s Slice[T]) Index(el T) int {
|
||||||
|
return slices.Index(s, el)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Swap is mutable func
|
||||||
func (s Slice[T]) Swap(i, j int) Slice[T] {
|
func (s Slice[T]) Swap(i, j int) Slice[T] {
|
||||||
s[i], s[j] = s[j], s[i]
|
s[i], s[j] = s[j], s[i]
|
||||||
return s
|
return s
|
||||||
@@ -55,7 +60,7 @@ func (s Slice[T]) Pop(index int) Slice[T] {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
func (s Slice[T]) Remove(el T) Slice[T] {
|
func (s Slice[T]) Remove(el T) Slice[T] {
|
||||||
index := slices.Index(s, el)
|
index := s.Index(el)
|
||||||
if index == -1 {
|
if index == -1 {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
@@ -78,12 +83,20 @@ func (s Slice[T]) ToAnyArray() []any {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ForEach is mutable func
|
||||||
|
func (s Slice[T]) ForEach(f func(e T) T) Slice[T] {
|
||||||
|
for i, v := range s {
|
||||||
|
s[i] = f(v)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
type Set[T comparable] []T
|
type Set[T comparable] []T
|
||||||
|
|
||||||
func NewSetFrom[T comparable](slice []T) Set[T] {
|
func NewSetFrom[T comparable](slice []T) Set[T] {
|
||||||
s := make(Set[T], 0)
|
s := make(Set[T], 0)
|
||||||
for _, v := range slice {
|
for _, v := range slice {
|
||||||
if slices.Index(slice, v) >= 0 {
|
if s.Index(v) >= 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
s = append(s, v)
|
s = append(s, v)
|
||||||
@@ -102,11 +115,14 @@ func (s Set[T]) Get(index int) T {
|
|||||||
func (s Set[T]) Last() T {
|
func (s Set[T]) Last() T {
|
||||||
return s[s.Len()-1]
|
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) {
|
func (s Set[T]) Swap(i, j int) {
|
||||||
s[i], s[j] = s[j], s[i]
|
s[i], s[j] = s[j], s[i]
|
||||||
}
|
}
|
||||||
func (s Set[T]) Add(v T) Set[T] {
|
func (s Set[T]) Add(v T) Set[T] {
|
||||||
index := slices.Index(s, v)
|
index := s.Index(v)
|
||||||
if index >= 0 {
|
if index >= 0 {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
@@ -126,7 +142,7 @@ func (s Set[T]) Pop(index int) Set[T] {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
func (s Set[T]) Remove(el T) Set[T] {
|
func (s Set[T]) Remove(el T) Set[T] {
|
||||||
index := slices.Index(s, el)
|
index := s.Index(el)
|
||||||
if index == -1 {
|
if index == -1 {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package laniakea
|
package laniakea
|
||||||
|
|
||||||
const (
|
const (
|
||||||
VersionString = "0.3.8"
|
VersionString = "0.3.9"
|
||||||
VersionMajor = 0
|
VersionMajor = 0
|
||||||
VersionMinor = 3
|
VersionMinor = 3
|
||||||
VersionPatch = 8
|
VersionPatch = 9
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user