Documentation
¶
Index ¶
- type SkipFilter
- func (sf *SkipFilter[V, F]) Add(value V)
- func (sf *SkipFilter[V, F]) Len() int
- func (sf *SkipFilter[V, F]) MatchAny(filterKeys ...F) []V
- func (sf *SkipFilter[V, F]) MatchFunc(filterKey F, test func(value V) bool) []V
- func (sf *SkipFilter[V, F]) Remove(value V)
- func (sf *SkipFilter[V, F]) Walk(start uint64, callback func(val V) bool) uint64
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type SkipFilter ¶
type SkipFilter[V any, F comparable] struct { // contains filtered or unexported fields }
SkipFilter combines a skip list with a cache of roaring bitmaps
func New ¶
func New[V any, F comparable](test func(value V, filter F) bool, maximumSize int) *SkipFilter[V, F]
New creates a new SkipFilter.
The test function should return true if the value passes the provided filter. maximumSize caps the cache: 0 keeps every filter bitmap, a negative value disables the cache, a positive one should match or exceed the expected filter cardinality.
Example ¶
package main
import (
"fmt"
"github.com/dunglas/skipfilter"
)
func main() {
sf := skipfilter.New(func(value, filter int) bool {
return value%filter == 0
}, 10)
fmt.Printf("%d", sf.Len())
}
Output: 0
func (*SkipFilter[V, F]) Add ¶
func (sf *SkipFilter[V, F]) Add(value V)
Add adds a value to the set
Example ¶
package main
import (
"fmt"
"github.com/dunglas/skipfilter"
)
func main() {
sf := skipfilter.New(func(value, filter int) bool {
return value%filter == 0
}, 10)
for i := 0; i < 10; i++ {
sf.Add(i)
}
sf.Remove(0)
fmt.Printf("%d", sf.Len())
}
Output: 9
func (*SkipFilter[V, F]) Len ¶
func (sf *SkipFilter[V, F]) Len() int
Len returns the number of values in the set
func (*SkipFilter[V, F]) MatchAny ¶
func (sf *SkipFilter[V, F]) MatchAny(filterKeys ...F) []V
MatchAny returns a slice of values in the set matching any of the provided filters
Example ¶
package main
import (
"fmt"
"github.com/dunglas/skipfilter"
)
func main() {
sf := skipfilter.New(func(value, filter int) bool {
return value%filter == 0
}, 10)
for i := 0; i < 10; i++ {
sf.Add(i)
}
fmt.Printf("Multiples of 2: %+v\n", sf.MatchAny(2))
fmt.Printf("Multiples of 3: %+v\n", sf.MatchAny(3))
}
Output: Multiples of 2: [0 2 4 6 8] Multiples of 3: [0 3 6 9]
func (*SkipFilter[V, F]) MatchFunc ¶ added in v1.2.0
func (sf *SkipFilter[V, F]) MatchFunc(filterKey F, test func(value V) bool) []V
MatchFunc returns the values in the set passing test, which replaces the test given to New for this call. It lets filterKey be a compact key, such as a digest, for a filter too large to keep in the cache. Results are cached under filterKey, so test must agree with every other test used with the same key.
Example ¶
package main
import (
"fmt"
"github.com/dunglas/skipfilter"
)
func main() {
sf := skipfilter.New[int, string](nil, 10)
for i := 0; i < 10; i++ {
sf.Add(i)
}
divisors := []int{2, 3}
fmt.Printf("Multiples of 2 or 3: %+v\n", sf.MatchFunc("2,3", func(value int) bool {
for _, d := range divisors {
if value%d == 0 {
return true
}
}
return false
}))
}
Output: Multiples of 2 or 3: [0 2 3 4 6 8 9]
func (*SkipFilter[V, F]) Remove ¶
func (sf *SkipFilter[V, F]) Remove(value V)
Remove removes a value from the set
func (*SkipFilter[V, F]) Walk ¶
func (sf *SkipFilter[V, F]) Walk(start uint64, callback func(val V) bool) uint64
Walk executes callback for each value in the set beginning at `start` index. Return true in callback to continue iterating, false to stop. Returned uint64 is index of `next` element (send as `start` to continue iterating)
Example (All) ¶
package main
import (
"fmt"
"github.com/dunglas/skipfilter"
)
func main() {
sf := skipfilter.New[int, int](nil, 10)
for i := 0; i < 10; i++ {
sf.Add(i)
}
var n []int
sf.Walk(0, func(v int) bool {
n = append(n, v)
return true
})
fmt.Printf("%d", len(n))
}
Output: 10
Example (Limit) ¶
package main
import (
"fmt"
"github.com/dunglas/skipfilter"
)
func main() {
sf := skipfilter.New[int, int](nil, 10)
for i := 0; i < 10; i++ {
sf.Add(i)
}
var n []int
sf.Walk(0, func(v int) bool {
n = append(n, v)
return len(n) < 5
})
fmt.Printf("%d", len(n))
}
Output: 5
Example (Start) ¶
package main
import (
"fmt"
"github.com/dunglas/skipfilter"
)
func main() {
sf := skipfilter.New[int, int](nil, 10)
for i := 0; i < 10; i++ {
sf.Add(i)
}
var n []int
sf.Walk(5, func(v int) bool {
n = append(n, v)
return true
})
fmt.Printf("%d", len(n))
}
Output: 5