Use stdlib maps/slices

This commit is contained in:
Kovid Goyal 2024-05-04 14:34:40 +05:30
parent e7ed5716a9
commit 405f5ce148
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
27 changed files with 55 additions and 60 deletions

View file

@ -14,6 +14,7 @@ import (
"os"
"os/exec"
"path/filepath"
"slices"
"strconv"
"strings"
"sync"
@ -23,7 +24,6 @@ import (
"github.com/edwvee/exiffix"
"github.com/kovidgoyal/imaging"
"golang.org/x/exp/slices"
)
var _ = fmt.Print

View file

@ -9,10 +9,10 @@ import (
"path/filepath"
"reflect"
"runtime"
"slices"
"strconv"
"golang.org/x/exp/constraints"
"golang.org/x/exp/slices"
)
var _ = fmt.Print

View file

@ -4,12 +4,26 @@ package utils
import (
"fmt"
"golang.org/x/exp/maps"
)
var _ = fmt.Print
func Keys[M ~map[K]V, K comparable, V any](m M) []K {
r := make([]K, 0, len(m))
for k := range m {
r = append(r, k)
}
return r
}
func Values[M ~map[K]V, K comparable, V any](m M) []V {
r := make([]V, 0, len(m))
for _, k := range m {
r = append(r, k)
}
return r
}
type Set[T comparable] struct {
items map[T]struct{}
}
@ -25,7 +39,7 @@ func (self *Set[T]) AddItems(val ...T) {
}
func (self *Set[T]) String() string {
return fmt.Sprintf("%#v", maps.Keys(self.items))
return fmt.Sprintf("%#v", Keys(self.items))
}
func (self *Set[T]) Remove(val T) {
@ -60,7 +74,7 @@ func (self *Set[T]) Iterable() map[T]struct{} {
}
func (self *Set[T]) AsSlice() []T {
return maps.Keys(self.items)
return Keys(self.items)
}
func (self *Set[T]) Intersect(other *Set[T]) (ans *Set[T]) {