chore: teach set
module about iter.Seq
(#6676)
- Add a new `Seq` function to the `Set` type, this returns an iterator over the values. - Convert some users of the `Values` method to allow for more optimal code. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6676 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
parent
46e60ce966
commit
443f7d59f9
9 changed files with 38 additions and 28 deletions
|
@ -11,7 +11,7 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/container"
|
||||
|
@ -143,8 +143,7 @@ func (l *LayeredFS) ListFiles(name string, fileMode ...bool) ([]string, error) {
|
|||
}
|
||||
}
|
||||
}
|
||||
files := fileSet.Values()
|
||||
sort.Strings(files)
|
||||
files := slices.Sorted(fileSet.Seq())
|
||||
return files, nil
|
||||
}
|
||||
|
||||
|
@ -184,8 +183,7 @@ func listAllFiles(layers []*Layer, name string, fileMode ...bool) ([]string, err
|
|||
if err := list(name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files := fileSet.Values()
|
||||
sort.Strings(files)
|
||||
files := slices.Sorted(fileSet.Seq())
|
||||
return files, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,11 @@
|
|||
|
||||
package container
|
||||
|
||||
import (
|
||||
"iter"
|
||||
"maps"
|
||||
)
|
||||
|
||||
type Set[T comparable] map[T]struct{}
|
||||
|
||||
// SetOf creates a set and adds the specified elements to it.
|
||||
|
@ -63,3 +68,9 @@ func (s Set[T]) Values() []T {
|
|||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
// Seq returns a iterator over the elements in the set.
|
||||
// It returns a single-use iterator.
|
||||
func (s Set[T]) Seq() iter.Seq[T] {
|
||||
return maps.Keys(s)
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
package container
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -29,6 +30,14 @@ func TestSet(t *testing.T) {
|
|||
assert.True(t, s.Contains("key4"))
|
||||
assert.True(t, s.Contains("key5"))
|
||||
|
||||
values := s.Values()
|
||||
called := 0
|
||||
for value := range s.Seq() {
|
||||
called++
|
||||
assert.True(t, slices.Contains(values, value))
|
||||
}
|
||||
assert.EqualValues(t, len(values), called)
|
||||
|
||||
s = SetOf("key6", "key7")
|
||||
assert.False(t, s.Contains("key1"))
|
||||
assert.True(t, s.Contains("key6"))
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
@ -47,13 +46,6 @@ func SliceRemoveAll[T comparable](slice []T, target T) []T {
|
|||
return slices.DeleteFunc(slice, func(t T) bool { return t == target })
|
||||
}
|
||||
|
||||
// Sorted returns the sorted slice
|
||||
// Note: The parameter is sorted inline.
|
||||
func Sorted[S ~[]E, E cmp.Ordered](values S) S {
|
||||
slices.Sort(values)
|
||||
return values
|
||||
}
|
||||
|
||||
// TODO: Replace with "maps.Values" once available, current it only in golang.org/x/exp/maps but not in standard library
|
||||
func ValuesOfMap[K comparable, V any](m map[K]V) []V {
|
||||
values := make([]V, 0, len(m))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue