hashset

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: MIT, MIT Imports: 4 Imported by: 0 modules, 0 packages

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.

Documentation

Overview

Package hashset holds the compact hash sets that the rules compiler uses for the long lists of an "in" term.

Both sets use open addressing with linear probing over a power of two table holding uint32 element indexes (index+1, so 0 means empty). The table is sized to between 1.5 and 3 slots per element (load factor 1/3 to 2/3), which keeps probe chains short while costing only 4-8 bytes per element.

uint32 offsets and indexes are safe because the constructors refuse to build a set when the element count or the total string bytes would not fit in a uint32; the caller then keeps the plain linear-scan list.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IntSet

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

IntSet is a hash index over a long []int, which the caller keeps anyway. Slots come from Fibonacci hashing (multiply by 2^64/phi, keep the top bits).

func NewIntSet

func NewIntSet(ints []int) *IntSet

NewIntSet indexes ints, which the set keeps and the caller must not change. It returns nil when the list has too many elements for 32 bit indexes: the caller keeps the plain list then.

func (*IntSet) Has

func (s *IntSet) Has(target int) bool

Has reports whether target is an element of the set.

type StrBuilder

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

StrBuilder collects the elements of a StrSet. The zero value is not usable: start it with StrBuilder.Init.

func (*StrBuilder) Add

func (builder *StrBuilder) Add(s string)

Add appends the next element. The elements must not be more than the count, nor longer in all than the total, that the builder was started with.

func (*StrBuilder) Build

func (builder *StrBuilder) Build() *StrSet

Build returns the set of the elements added. The builder must not be used afterwards.

func (*StrBuilder) Init

func (builder *StrBuilder) Init(count, total int) bool

Init starts the build of a set of count strings with total bytes in all. It returns false when the set would not fit 32 bit offsets: the caller keeps the plain list then. The builder must not be copied after Init.

type StrSet

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

StrSet is a long string list stored in one allocation, plus an open addressing hash index over it. Compared with a []string and a map[string]struct{} it has no per-string allocation or header, and no map buckets: about 18 bytes per element for 8 byte strings instead of 75.

The string hash is maphash, a fast, well mixed hash. Every set draws its own random seed, so lookups cannot be steered into long probe chains by crafted keys; the seed is state of the set, not of the package.

func (*StrSet) Has

func (s *StrSet) Has(target string) bool

Has reports whether target is an element of the set.

func (*StrSet) Slice

func (s *StrSet) Slice() []string

Slice expands the list, in the order of the elements, into a new []string (the strings share the packed data).

Jump to

Keyboard shortcuts

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