mirror of
https://github.com/pomerium/pomerium.git
synced 2025-08-04 09:19:39 +02:00
sets: convert set types to generics (#3519)
* sets: convert set types to generics * sets: use internal sets package
This commit is contained in:
parent
92a9251cde
commit
b5ac7dbc76
9 changed files with 61 additions and 54 deletions
|
@ -6,10 +6,12 @@ type Hash[T comparable] struct {
|
|||
}
|
||||
|
||||
// NewHash creates a new Hash set.
|
||||
func NewHash[T comparable]() *Hash[T] {
|
||||
return &Hash[T]{
|
||||
func NewHash[T comparable](initialValues ...T) *Hash[T] {
|
||||
s := &Hash[T]{
|
||||
m: make(map[T]struct{}),
|
||||
}
|
||||
s.Add(initialValues...)
|
||||
return s
|
||||
}
|
||||
|
||||
// Add adds a value to the set.
|
||||
|
|
|
@ -1,22 +1,22 @@
|
|||
package sets
|
||||
|
||||
// A SizeLimitedStringSet is a StringSet which is limited to a given size. Once
|
||||
// A SizeLimited is a Set which is limited to a given size. Once
|
||||
// the capacity is reached an element will be removed at random.
|
||||
type SizeLimitedStringSet struct {
|
||||
m map[string]struct{}
|
||||
type SizeLimited[T comparable] struct {
|
||||
m map[T]struct{}
|
||||
capacity int
|
||||
}
|
||||
|
||||
// NewSizeLimitedStringSet create a new SizeLimitedStringSet.
|
||||
func NewSizeLimitedStringSet(capacity int) *SizeLimitedStringSet {
|
||||
return &SizeLimitedStringSet{
|
||||
m: make(map[string]struct{}),
|
||||
// NewSizeLimited create a new SizeLimited.
|
||||
func NewSizeLimited[T comparable](capacity int) *SizeLimited[T] {
|
||||
return &SizeLimited[T]{
|
||||
m: make(map[T]struct{}),
|
||||
capacity: capacity,
|
||||
}
|
||||
}
|
||||
|
||||
// Add adds an element to the set.
|
||||
func (s *SizeLimitedStringSet) Add(element string) {
|
||||
func (s *SizeLimited[T]) Add(element T) {
|
||||
s.m[element] = struct{}{}
|
||||
for len(s.m) > s.capacity {
|
||||
for k := range s.m {
|
||||
|
@ -27,7 +27,7 @@ func (s *SizeLimitedStringSet) Add(element string) {
|
|||
}
|
||||
|
||||
// ForEach iterates over all the elements in the set.
|
||||
func (s *SizeLimitedStringSet) ForEach(callback func(element string) bool) {
|
||||
func (s *SizeLimited[T]) ForEach(callback func(element T) bool) {
|
||||
for k := range s.m {
|
||||
if !callback(k) {
|
||||
return
|
||||
|
|
|
@ -1,59 +1,65 @@
|
|||
package sets
|
||||
|
||||
import "github.com/google/btree"
|
||||
import (
|
||||
"github.com/google/btree"
|
||||
"golang.org/x/exp/constraints"
|
||||
)
|
||||
|
||||
func lessFn(a, b string) bool { return a < b }
|
||||
|
||||
// A SortedString is a set of strings with sorted iteration.
|
||||
type SortedString struct {
|
||||
b *btree.BTreeG[string]
|
||||
// A Sorted is a set with sorted iteration.
|
||||
type Sorted[T any] struct {
|
||||
b *btree.BTreeG[T]
|
||||
less func(a, b T) bool
|
||||
}
|
||||
|
||||
// NewSortedString creates a new sorted string set.
|
||||
func NewSortedString() *SortedString {
|
||||
return &SortedString{
|
||||
b: btree.NewG[string](8, lessFn),
|
||||
// NewSorted creates a new sorted string set.
|
||||
func NewSorted[T constraints.Ordered]() *Sorted[T] {
|
||||
less := func(a, b T) bool {
|
||||
return a < b
|
||||
}
|
||||
return &Sorted[T]{
|
||||
b: btree.NewG(8, less),
|
||||
less: less,
|
||||
}
|
||||
}
|
||||
|
||||
// Add adds a string to the set.
|
||||
func (s *SortedString) Add(elements ...string) {
|
||||
func (s *Sorted[T]) Add(elements ...T) {
|
||||
for _, element := range elements {
|
||||
s.b.ReplaceOrInsert(element)
|
||||
}
|
||||
}
|
||||
|
||||
// Clear clears the set.
|
||||
func (s *SortedString) Clear() {
|
||||
s.b = btree.NewG[string](8, lessFn)
|
||||
func (s *Sorted[T]) Clear() {
|
||||
s.b = btree.NewG(8, s.less)
|
||||
}
|
||||
|
||||
// Delete deletes an element from the set.
|
||||
func (s *SortedString) Delete(element string) {
|
||||
func (s *Sorted[T]) Delete(element T) {
|
||||
s.b.Delete(element)
|
||||
}
|
||||
|
||||
// ForEach iterates over the set in ascending order.
|
||||
func (s *SortedString) ForEach(callback func(element string) bool) {
|
||||
s.b.Ascend(func(item string) bool {
|
||||
func (s *Sorted[T]) ForEach(callback func(element T) bool) {
|
||||
s.b.Ascend(func(item T) bool {
|
||||
return callback(item)
|
||||
})
|
||||
}
|
||||
|
||||
// Has returns true if the elment is in the set.
|
||||
func (s *SortedString) Has(element string) bool {
|
||||
func (s *Sorted[T]) Has(element T) bool {
|
||||
return s.b.Has(element)
|
||||
}
|
||||
|
||||
// Size returns the size of the set.
|
||||
func (s *SortedString) Size() int {
|
||||
func (s *Sorted[T]) Size() int {
|
||||
return s.b.Len()
|
||||
}
|
||||
|
||||
// ToSlice returns a slice of all the elements in the set.
|
||||
func (s *SortedString) ToSlice() []string {
|
||||
arr := make([]string, 0, s.Size())
|
||||
s.b.Ascend(func(item string) bool {
|
||||
func (s *Sorted[T]) ToSlice() []T {
|
||||
arr := make([]T, 0, s.Size())
|
||||
s.b.Ascend(func(item T) bool {
|
||||
arr = append(arr, item)
|
||||
return true
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue