HashSet

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2023 License: MIT Imports: 4 Imported by: 0

README

Fast hash set implementation based on HopMap. For details on implementation and benchmarks see the Maps/HopMap/README.md

Usage:

import hashset "github.com/g-m-twostay/go-utils/Sets/HashSet"

h:=16//the neighborhood size parameter H in hopscotch hashing.
seed:=uint(0)//seed for hash function
isz:=uint(7)//the amount of elements that the initial table should be able to handle.

S:=hashset.New[int](h, isz, seed)

println(S.Put(1))//prints true if the insertion is successful, i.e. when this element is new.
println(S.Has(1))//true
println(S.Has(2))//false
println(S.Remove(1))//true, 1 is succesfully removed, i.e. 1 exists in the set
pritnln(S.Remove(2))//false

for i:=0;i<10;i++{
    println(S.Put(i))//true
}

S.Range(func(i int)bool{
    println(i)
    return true
})//prints 1-10

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HashSet

type HashSet[E comparable] struct {
	Seed Go_Utils.Hasher
	// contains filtered or unexported fields
}

func New

func New[E comparable](h byte, size, seed uint) *HashSet[E]

New HashSet of type E. h is the neighborhood size parameter in Hopscotch hashing, 16 is a good value. size is used to calculate the initial table size that should handle size elements without resizing.

func (*HashSet[E]) Has

func (u *HashSet[E]) Has(e E) bool

Has e in the set. Returns true if e is present in the set.

func (*HashSet[E]) Put

func (u *HashSet[E]) Put(e E) bool

Put e into the set. Returns true if successful.

func (*HashSet[E]) Range

func (u *HashSet[E]) Range(f func(E) bool)

Range over elements in a snapshot of the set at the time of the call to Range and call f on the elements. Stops when f returns false. It uses range to iterate through the bucket array, so concurrent modification during iteration won't be visible to f.

func (*HashSet[E]) Remove

func (u *HashSet[E]) Remove(e E) bool

Remove e from the set. Returns true if the removal is successful.

func (*HashSet[E]) Size

func (u *HashSet[E]) Size() uint

Size of the set.

func (*HashSet[E]) Take

func (u *HashSet[E]) Take() (e E)

Take an arbitrary element from the set. Returns zero value if the set is empty. Doesn't guarantee which element it will return. Faster than iterating with Range.

Jump to

Keyboard shortcuts

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