From 689eb8a5e285097341240408ef7e438c30edb886 Mon Sep 17 00:00:00 2001 From: ScuroNeko Date: Wed, 4 Feb 2026 11:39:08 +0300 Subject: [PATCH] slices additions; v0.3.8 --- slice.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- version.go | 4 +-- 2 files changed, 85 insertions(+), 4 deletions(-) diff --git a/slice.go b/slice.go index 3964800..219b0a2 100644 --- a/slice.go +++ b/slice.go @@ -1,8 +1,10 @@ package laniakea -type Slice[T any] []T +import "slices" -func NewSliceFrom[T any](slice []T) Slice[T] { +type Slice[T comparable] []T + +func NewSliceFrom[T comparable](slice []T) Slice[T] { s := make(Slice[T], len(slice)) copy(s[:], slice) return s @@ -52,6 +54,13 @@ func (s Slice[T]) Pop(index int) Slice[T] { } return out } +func (s Slice[T]) Remove(el T) Slice[T] { + index := slices.Index(s, el) + if index == -1 { + return s + } + return s.Pop(index) +} func (s Slice[T]) Push(e T) Slice[T] { return append(s, e) } @@ -68,3 +77,75 @@ func (s Slice[T]) ToAnyArray() []any { } return out } + +type Set[T comparable] []T + +func NewSetFrom[T comparable](slice []T) Set[T] { + s := make(Set[T], 0) + for _, v := range slice { + if slices.Index(slice, v) >= 0 { + continue + } + s = append(s, v) + } + 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]) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} +func (s Set[T]) Add(v T) Set[T] { + index := slices.Index(s, v) + if index >= 0 { + return s + } + return append(s, v) +} +func (s Set[T]) Pop(index int) Set[T] { + if index == 0 { + return s[1:] + } + out := make(Set[T], s.Len()-index) + for i, e := range s { + if i == index { + continue + } + out[i] = e + } + return out +} +func (s Set[T]) Remove(el T) Set[T] { + index := slices.Index(s, el) + if index == -1 { + return s + } + return s.Pop(index) +} +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 +} diff --git a/version.go b/version.go index 017460d..a98aaa7 100644 --- a/version.go +++ b/version.go @@ -1,8 +1,8 @@ package laniakea const ( - VersionString = "0.3.7" + VersionString = "0.3.8" VersionMajor = 0 VersionMinor = 3 - VersionPatch = 7 + VersionPatch = 8 )