This commit is contained in:
bluepython508 committed 2026-10-22 12:27:19 +01:00
1 parent 3530afb854
commit 6903fcff27
2146 files changed
+112256 -190703

No files matched your search

+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright (c) Tailscale Inc & AUTHORS
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package set
+2 -2
View File
@@ -1,4 +1,4 @@
// Copyright (c) Tailscale Inc & AUTHORS
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package set
@@ -152,7 +152,7 @@ func (s bitSet) values() iter.Seq[uint64] {
return func(yield func(uint64) bool) {
// Hyrum-proofing: randomly iterate in forwards or reverse.
if rand.Uint64()%2 == 0 {
for i := 0; i < bits.UintSize; i++ {
for i := range bits.UintSize {
if s.contains(uint64(i)) && !yield(uint64(i)) {
return
}
+94 -3
View File
@@ -1,4 +1,4 @@
// Copyright (c) Tailscale Inc & AUTHORS
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
// Package set contains set types.
@@ -6,7 +6,12 @@ package set
import (
"encoding/json"
"iter"
"maps"
"reflect"
"sort"
"tailscale.com/types/views"
)
// Set is a set of T.
@@ -24,14 +29,35 @@ func Of[T comparable](slice ...T) Set[T] {
return s
}
// OfSliceView returns a new set constructed from the elements in v.
func OfSliceView[T comparable](v views.Slice[T]) Set[T] {
s := make(Set[T], v.Len())
s.AddSliceView(v)
return s
}
// Clone returns a new set cloned from the elements in s.
func (s Set[T]) Clone() Set[T] {
return maps.Clone(s)
}
// All returns an iterator over all elements in s.
// The iteration order is not specified
// and is not guaranteed to be the same from one call to the next.
func (s Set[T]) All() iter.Seq[T] {
return maps.Keys(s)
}
// Add adds e to s.
func (s Set[T]) Add(e T) { s[e] = struct{}{} }
// AddSeq adds each element of es to s.
func (s Set[T]) AddSeq(es iter.Seq[T]) {
for e := range es {
s.Add(e)
}
}
// AddSlice adds each element of es to s.
func (s Set[T]) AddSlice(es []T) {
for _, e := range es {
@@ -39,6 +65,13 @@ func (s Set[T]) AddSlice(es []T) {
}
}
// AddSliceView adds each element of v to s.
func (s Set[T]) AddSliceView(v views.Slice[T]) {
for _, e := range v.All() {
s.Add(e)
}
}
// AddSet adds each element of es to s.
func (s Set[T]) AddSet(es Set[T]) {
for e := range es {
@@ -53,19 +86,77 @@ func (s *Set[T]) Make() {
}
}
// Slice returns the elements of the set as a slice. The elements will not be
// in any particular order.
// Slice returns the elements of the set as a slice. If the element type is
// ordered (integers, floats, or strings), the elements are returned in sorted
// order. Otherwise, the order is not defined.
func (s Set[T]) Slice() []T {
es := make([]T, 0, s.Len())
for k := range s {
es = append(es, k)
}
if f := genOrderedSwapper(reflect.TypeFor[T]()); f != nil {
sort.Slice(es, f(reflect.ValueOf(es)))
}
return es
}
// genOrderedSwapper returns a generator for a swap function that can be used to
// sort a slice of the given type. If rt is not an ordered type,
// genOrderedSwapper returns nil.
func genOrderedSwapper(rt reflect.Type) func(reflect.Value) func(i, j int) bool {
switch rt.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return func(rv reflect.Value) func(i, j int) bool {
return func(i, j int) bool {
return rv.Index(i).Uint() < rv.Index(j).Uint()
}
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return func(rv reflect.Value) func(i, j int) bool {
return func(i, j int) bool {
return rv.Index(i).Int() < rv.Index(j).Int()
}
}
case reflect.Float32, reflect.Float64:
return func(rv reflect.Value) func(i, j int) bool {
return func(i, j int) bool {
return rv.Index(i).Float() < rv.Index(j).Float()
}
}
case reflect.String:
return func(rv reflect.Value) func(i, j int) bool {
return func(i, j int) bool {
return rv.Index(i).String() < rv.Index(j).String()
}
}
}
return nil
}
// Delete removes e from the set.
func (s Set[T]) Delete(e T) { delete(s, e) }
// DeleteSeq removes all elements in es from the set.
func (s Set[T]) DeleteSeq(es iter.Seq[T]) {
for e := range es {
s.Delete(e)
}
}
// DeleteSlice removes all elements in es from the set.
func (s Set[T]) DeleteSlice(es []T) {
for _, e := range es {
s.Delete(e)
}
}
// DeleteSet removes all elements in es from the set.
func (s Set[T]) DeleteSet(es Set[T]) {
for e := range es {
s.Delete(e)
}
}
// Contains reports whether s contains e.
func (s Set[T]) Contains(e T) bool {
_, ok := s[e]
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright (c) Tailscale Inc & AUTHORS
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package set
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright (c) Tailscale Inc & AUTHORS
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package set