diff options
Diffstat (limited to 'vendor/github.com/jesseduffield/generics/list/comparable_list.go')
-rw-r--r-- | vendor/github.com/jesseduffield/generics/list/comparable_list.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/vendor/github.com/jesseduffield/generics/list/comparable_list.go b/vendor/github.com/jesseduffield/generics/list/comparable_list.go new file mode 100644 index 000000000..21d56a80b --- /dev/null +++ b/vendor/github.com/jesseduffield/generics/list/comparable_list.go @@ -0,0 +1,49 @@ +package list + +import ( + "golang.org/x/exp/slices" +) + +type ComparableList[T comparable] struct { + *List[T] +} + +func NewComparable[T comparable]() *ComparableList[T] { + return &ComparableList[T]{List: New[T]()} +} + +func NewComparableFromSlice[T comparable](slice []T) *ComparableList[T] { + return &ComparableList[T]{List: NewFromSlice(slice)} +} + +func (l *ComparableList[T]) Equal(other *ComparableList[T]) bool { + return l.EqualSlice(other.ToSlice()) +} + +func (l *ComparableList[T]) EqualSlice(other []T) bool { + return slices.Equal(l.ToSlice(), other) +} + +func (l *ComparableList[T]) Compact() { + l.slice = slices.Compact(l.slice) +} + +func (l *ComparableList[T]) Index(needle T) int { + return slices.Index(l.slice, needle) +} + +func (l *ComparableList[T]) Contains(needle T) bool { + return slices.Contains(l.slice, needle) +} + +func (l *ComparableList[T]) SortFuncInPlace(test func(a T, b T) bool) { + slices.SortFunc(l.slice, test) +} + +func (l *ComparableList[T]) SortFunc(test func(a T, b T) bool) *ComparableList[T] { + newSlice := slices.Clone(l.slice) + + slices.SortFunc(newSlice, test) + + return NewComparableFromSlice(newSlice) +} |