intset

package module
v1.0.3-0...-37ee0d7 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2022 License: MIT Imports: 1 Imported by: 2

README

IntSet

Go Reference Go Report Card GitHub license

A specialized set for integers or runes, ideal when:

  • The number of elements is known ahead of time (or a good approximation)
  • The number of elements doesn't change drastically over time
  • The values are naturally random

As long as the number of elements within the set stays close to the originally specified size (I don't know the magic number, so let's say ±10%), and that they stay evenly distributed. the set will exhibit good read and write performance, as well as decent memory usage. When packed, read performance is roughly 7 times better than a map[int]struct{}.

set := intset.NewSized(1000000)  // or intset.NewSized32(1000000) or intset.NewRune(1000000)
set.Set(32)
set.Exists(32)

Methods

The int, uint32 and rune variations have the same API.

  • Set(int) or Set(uint32) or Set(rune)
  • Exists(int) bool or Exists(uint32) bool or Exists(rune) bool
  • Remove(int) bool or Remove(uint32) bool or Remove(rune) bool
  • Len() int
  • Each(f func(value int)) or Each(f func(value uint32)) or Each(f func(value rune))

Intersections and Unions

Two or more sets can be intersected by calling Intersect, Intersect32, or IntersectRune. This is largely a reference implementation and callers should consider implementing their own. For example, maybe you want to stop after finding X matches, want to use a pooled array object to hold intermediary objects, or are fine with getting an array back (rather than a set) (all of which should result in much better performance).

The method is called via:

result := intset.Intersect([]Set{s1, s2})
// or
result := intset.Intersect32([]Set32{s1, s2})
// or
result := intset.IntersectRune([]Set32{s1, s2})

Union, Union32, and UnionRune can be similarly used.

Advanced Sizing

The NewSizedConfig, NewSized32Config and NewRuneConfig functions can be used to have more control over how the set behaves. These functions take the size, as normal, as well as a Config:

config1 := intset.NewConfig()
set1 := intset.NewSizedConfig(1000000, config1)

config1 := intset.NewConfig().BucketSize(16)
set2 := intset.NewSizedConfig(1000000, config2)

config3 := intset.NewConfig().BucketSize(16).BucketGrowBy(4)
set3 := intset.NewSizedConfig(1000000, config3)

See This Pull Request to see the performance/memory tradeoff of possible values. In short though, the default BucketSize=4 & BucketGrowBy=1, results in faster probing at the cost of higher memory use.

Documentation

Overview

Package intset provides a specialized set for integers or runes

Package intset provides a specialized set for integers or runes

Package intset provides a specialized set for integers or runes

Package intset provides a specialized set for integers or runes

Package intset provides a specialized set for integers or runes

Index

Constants

This section is empty.

Variables

View Source
var Default = NewConfig()

Default is a default Config which favors probing performance at the cost of memory.

Functions

This section is empty.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config defines the configuration for creating a new set

Smaller values for bucketSize will speed up lookups but also increase memory usage. Smaller values for bucketGrowBy will slow down the set capacity growth rate but also slow down insertions.

func NewConfig

func NewConfig() *Config

NewConfig creates a new config with usable defaults

func (*Config) BucketGrowBy

func (c *Config) BucketGrowBy(growBy uint32) *Config

BucketGrowBy sets the amount a bucket will grow by when full

func (*Config) BucketSize

func (c *Config) BucketSize(size uint32) *Config

BucketSize sets the initial bucket size

type Rune

type Rune struct {
	// contains filtered or unexported fields
}

Rune stores rune set data

func IntersectRune

func IntersectRune(sets SetsRune) *Rune

IntersectRune returns the intersection of an array of sets

func NewRune

func NewRune(size rune) *Rune

NewRune creates an empty rune set with target capacity specified by size using default configuration

func NewRuneConfig

func NewRuneConfig(size rune, config *Config) *Rune

NewRuneConfig creates an empty rune set with target capacity specified by size

func UnionRune

func UnionRune(sets SetsRune) *Rune

UnionRune returns the union of an array of sets

func (Rune) Each

func (s Rune) Each(f func(value rune))

Each iterates through the set items and applies function f to each set item

func (*Rune) Exists

func (s *Rune) Exists(value rune) bool

Exists returns true if the value exists in the set

func (Rune) Len

func (s Rune) Len() int

Len returns the total number of elements in the set

func (*Rune) Remove

func (s *Rune) Remove(value rune) bool

Remove returns true if the value existed in the rune set before being removed

func (*Rune) Set

func (s *Rune) Set(value rune)

Set adds a value to the rune set

type Set

type Set interface {
	Len() int
	Exists(value int) bool
	Each(f func(value int))
}

Set defines int set methods

type Set32

type Set32 interface {
	Len() int
	Exists(value uint32) bool
	Each(f func(value uint32))
}

Set32 defines uint32 set methods

type SetRune

type SetRune interface {
	Len() int
	Exists(value rune) bool
	Each(f func(value rune))
}

SetRune defines rune set methods

type Sets

type Sets []Set

Sets is array of Set

func (Sets) Len

func (s Sets) Len() int

func (Sets) Less

func (s Sets) Less(i, j int) bool

func (Sets) Swap

func (s Sets) Swap(i, j int)

type Sets32

type Sets32 []Set32

Sets32 is array of Set32

func (Sets32) Len

func (s Sets32) Len() int

func (Sets32) Less

func (s Sets32) Less(i, j int) bool

func (Sets32) Swap

func (s Sets32) Swap(i, j int)

type SetsRune

type SetsRune []SetRune

SetsRune is array of Rune

func (SetsRune) Len

func (s SetsRune) Len() int

func (SetsRune) Less

func (s SetsRune) Less(i, j int) bool

func (SetsRune) Swap

func (s SetsRune) Swap(i, j int)

type Sized

type Sized struct {
	// contains filtered or unexported fields
}

Sized stores int set data

func Intersect

func Intersect(sets Sets) *Sized

Intersect returns the intersection of an array of sets

func NewSized

func NewSized(size int) *Sized

NewSized creates an empty int set with target capacity specified by size using default configuration

func NewSizedConfig

func NewSizedConfig(size int, config *Config) *Sized

NewSizedConfig creates an empty int set with target capacity specified by size

func Union

func Union(sets Sets) *Sized

Union returns the union of an array of sets

func (Sized) Each

func (s Sized) Each(f func(value int))

Each iterates through the set items and applies function f to each set item

func (*Sized) Exists

func (s *Sized) Exists(value int) bool

Exists returns true if the value exists in the set

func (Sized) Len

func (s Sized) Len() int

Len returns the total number of elements in the set

func (*Sized) Remove

func (s *Sized) Remove(value int) bool

Remove returns true if the value existed in the int set before being removed

func (*Sized) Set

func (s *Sized) Set(value int)

Set adds a value to the int set

type Sized32

type Sized32 struct {
	// contains filtered or unexported fields
}

Sized32 stores uint32 set data

func Intersect32

func Intersect32(sets Sets32) *Sized32

Intersect32 returns the intersection of an array of sets

func NewSized32

func NewSized32(size uint32) *Sized32

NewSized32 creates an empty int set with target capacity specified by size using default configuration

func NewSized32Config

func NewSized32Config(size uint32, config *Config) *Sized32

NewSized32Config creates an empty uint32 set with target capacity specified by size

func Union32

func Union32(sets Sets32) *Sized32

Union32 returns the union of an array of sets

func (Sized32) Each

func (s Sized32) Each(f func(value uint32))

Each iterates through the set items and applies function f to each set item

func (*Sized32) Exists

func (s *Sized32) Exists(value uint32) bool

Exists returns true if the value exists in the set

func (Sized32) Len

func (s Sized32) Len() int

Len returns the total number of elements in the set

func (*Sized32) Remove

func (s *Sized32) Remove(value uint32) bool

Remove returns true if the value existed in the uint32 set before being removed

func (*Sized32) Set

func (s *Sized32) Set(value uint32)

Set adds a value to the uint32 set

Jump to

Keyboard shortcuts

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