diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2022-07-13 21:22:14 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2022-07-13 21:39:42 -0600 |
commit | 72a458520fccafe4df8c02c811cb6f64a310616e (patch) | |
tree | 1c424b5376c31524b01f8461b02950eb04d48345 /lib/util | |
parent | 952b677bf7f10da93673e3671f764c54c454bbfe (diff) |
Move the remaining former-generic.go parts out of lib/util/
Diffstat (limited to 'lib/util')
-rw-r--r-- | lib/util/maputil.go | 23 | ||||
-rw-r--r-- | lib/util/sliceutil.go | 69 | ||||
-rw-r--r-- | lib/util/syncmap.go | 40 |
3 files changed, 0 insertions, 132 deletions
diff --git a/lib/util/maputil.go b/lib/util/maputil.go deleted file mode 100644 index d7f1727..0000000 --- a/lib/util/maputil.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com> -// -// SPDX-License-Identifier: GPL-2.0-or-later - -package util - -import ( - "golang.org/x/exp/constraints" -) - -func MapKeys[K comparable, V any](m map[K]V) []K { - ret := make([]K, 0, len(m)) - for k := range m { - ret = append(ret, k) - } - return ret -} - -func SortedMapKeys[K constraints.Ordered, V any](m map[K]V) []K { - ret := MapKeys(m) - SortSlice(ret) - return ret -} diff --git a/lib/util/sliceutil.go b/lib/util/sliceutil.go deleted file mode 100644 index 6e0ce76..0000000 --- a/lib/util/sliceutil.go +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com> -// -// SPDX-License-Identifier: GPL-2.0-or-later - -package util - -import ( - "sort" - - "golang.org/x/exp/constraints" -) - -func InSlice[T comparable](needle T, haystack []T) bool { - for _, straw := range haystack { - if needle == straw { - return true - } - } - return false -} - -func RemoveAllFromSlice[T comparable](haystack []T, needle T) []T { - for i, straw := range haystack { - if needle == straw { - return append( - haystack[:i], - RemoveAllFromSlice(haystack[i+1:], needle)...) - } - } - return haystack -} - -func RemoveAllFromSliceFunc[T any](haystack []T, f func(T) bool) []T { - for i, straw := range haystack { - if f(straw) { - return append( - haystack[:i], - RemoveAllFromSliceFunc(haystack[i+1:], f)...) - } - } - return haystack -} - -func ReverseSlice[T any](slice []T) { - for i := 0; i < len(slice)/2; i++ { - j := (len(slice) - 1) - i - slice[i], slice[j] = slice[j], slice[i] - } -} - -func Max[T constraints.Ordered](a, b T) T { - if a > b { - return a - } - return b -} - -func Min[T constraints.Ordered](a, b T) T { - if a < b { - return a - } - return b -} - -func SortSlice[T constraints.Ordered](slice []T) { - sort.Slice(slice, func(i, j int) bool { - return slice[i] < slice[j] - }) -} diff --git a/lib/util/syncmap.go b/lib/util/syncmap.go deleted file mode 100644 index a281f2d..0000000 --- a/lib/util/syncmap.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com> -// -// SPDX-License-Identifier: GPL-2.0-or-later - -package util - -import ( - "sync" -) - -type SyncMap[K comparable, V any] struct { - inner sync.Map -} - -func (m *SyncMap[K, V]) Delete(key K) { m.inner.Delete(key) } -func (m *SyncMap[K, V]) Load(key K) (value V, ok bool) { - _value, ok := m.inner.Load(key) - if ok { - value = _value.(V) - } - return value, ok -} -func (m *SyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool) { - _value, ok := m.inner.LoadAndDelete(key) - if ok { - value = _value.(V) - } - return value, ok -} -func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) { - _actual, loaded := m.inner.LoadOrStore(key, value) - actual = _actual.(V) - return actual, loaded -} -func (m *SyncMap[K, V]) Range(f func(key K, value V) bool) { - m.inner.Range(func(key, value any) bool { - return f(key.(K), value.(V)) - }) -} -func (m *SyncMap[K, V]) Store(key K, value V) { m.inner.Store(key, value) } |