mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
[chore] add some more slice related utility functions + remove duplicated functions (#3149)
This commit is contained in:
@@ -17,48 +17,57 @@
|
||||
|
||||
package util
|
||||
|
||||
import "net/url"
|
||||
// Set represents a hashmap of only keys,
|
||||
// useful for deduplication / key checking.
|
||||
type Set[T comparable] map[T]struct{}
|
||||
|
||||
// UniqueStrings returns a deduplicated version of the given
|
||||
// slice of strings, without changing the order of the entries.
|
||||
func UniqueStrings(strings []string) []string {
|
||||
var (
|
||||
l = len(strings)
|
||||
keys = make(map[string]any, l) // Use map to dedupe items.
|
||||
unique = make([]string, 0, l) // Return slice.
|
||||
)
|
||||
|
||||
for _, str := range strings {
|
||||
// Check if already set as a key in the map;
|
||||
// if not, add to return slice + mark key as set.
|
||||
if _, set := keys[str]; !set {
|
||||
keys[str] = nil // Value doesn't matter.
|
||||
unique = append(unique, str)
|
||||
}
|
||||
// ToSet creates a Set[T] from given values,
|
||||
// noting that this does not maintain any order.
|
||||
func ToSet[T comparable](in []T) Set[T] {
|
||||
set := make(Set[T], len(in))
|
||||
for _, v := range in {
|
||||
set[v] = struct{}{}
|
||||
}
|
||||
|
||||
return unique
|
||||
return set
|
||||
}
|
||||
|
||||
// UniqueURIs returns a deduplicated version of the given
|
||||
// slice of URIs, without changing the order of the entries.
|
||||
func UniqueURIs(uris []*url.URL) []*url.URL {
|
||||
var (
|
||||
l = len(uris)
|
||||
keys = make(map[string]any, l) // Use map to dedupe items.
|
||||
unique = make([]*url.URL, 0, l) // Return slice.
|
||||
)
|
||||
// FromSet extracts the values from set to slice,
|
||||
// noting that this does not maintain any order.
|
||||
func FromSet[T comparable](in Set[T]) []T {
|
||||
out := make([]T, len(in))
|
||||
var i int
|
||||
for v := range in {
|
||||
out[i] = v
|
||||
i++
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
for _, uri := range uris {
|
||||
uriStr := uri.String()
|
||||
|
||||
// Check if already set as a key in the map;
|
||||
// if not, add to return slice + mark key as set.
|
||||
if _, set := keys[uriStr]; !set {
|
||||
keys[uriStr] = nil // Value doesn't matter.
|
||||
unique = append(unique, uri)
|
||||
// In returns input slice filtered to
|
||||
// only contain those in receiving set.
|
||||
func (s Set[T]) In(vs []T) []T {
|
||||
out := make([]T, 0, len(vs))
|
||||
for _, v := range vs {
|
||||
if _, ok := s[v]; ok {
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
|
||||
return unique
|
||||
return out
|
||||
}
|
||||
|
||||
// NotIn is the functional inverse of In().
|
||||
func (s Set[T]) NotIn(vs []T) []T {
|
||||
out := make([]T, 0, len(vs))
|
||||
for _, v := range vs {
|
||||
if _, ok := s[v]; !ok {
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Has returns if value is in Set.
|
||||
func (s Set[T]) Has(v T) bool {
|
||||
_, ok := s[v]
|
||||
return ok
|
||||
}
|
||||
|
Reference in New Issue
Block a user