skipfilter

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 6 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Skipfilter

This package provides a data structure that combines a skip list with a roaring bitmap cache.

Go Reference CI

[!NOTE]

This is a maintained and improved fork of github.com/kevburnsjr/skipfilter

This library was created to efficiently filter a multi-topic message input stream against a set of subscribers, each having a list of topic subscriptions expressed as regular expressions. Ideally, each subscriber should test each topic at most once to determine whether it wants to receive messages from the topic.

In this case, the skip list provides an efficient discontinuous slice of subscribers and the roaring bitmap for each topic provides an efficient ordered discontinuous set of all subscribers that have indicated that they wish to receive messages on the topic.

Filter bitmaps are stored in a cache of variable size (unlimited by default, so nothing is evicted; set to a negative value to disable the cache).

This package is thread-safe.

Documentation

Index

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址