hashset

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2023 License: MIT Imports: 2 Imported by: 7

README

hashset

import "github.com/zyedidia/generic/hashset"

Package hashset provides an implementation of a hashset.

Example

package main

import (
	"fmt"

	g "github.com/zyedidia/generic"
	"github.com/zyedidia/generic/hashset"
)

func main() {
	set := hashset.New[string](3, g.Equals[string], g.HashString)
	set.Put("foo")
	set.Put("bar")
	set.Put("baz")

	fmt.Println(set.Has("foo"))
	fmt.Println(set.Has("quux"))

	set.Remove("foo")

	fmt.Println(set.Has("foo"))
	fmt.Println(set.Has("bar"))

	set.Clear()

	fmt.Println(set.Has("foo"))
	fmt.Println(set.Has("bar"))
}
Output
true
false
false
true
false
false

Index

type Set

Set implements a hashset, using the hashmap as the underlying storage.

type Set[K any] struct {
    // contains filtered or unexported fields
}
func New
func New[K any](capacity uint64, equals g.EqualsFn[K], hash g.HashFn[K]) *Set[K]

New returns an empty hashset.

func Of
func Of[K comparable](capacity uint64, equals g.EqualsFn[K], hash g.HashFn[K], vals ...K) *Set[K]

Of returns a new hashset initialized with the given 'vals'

func (*Set[K]) Clear
func (s *Set[K]) Clear()

Clear removes all elements from the set.

func (*Set[K]) Copy
func (s *Set[K]) Copy() *Set[K]

Copy returns a copy of this set.

func (*Set[K]) Each
func (s *Set[K]) Each(fn func(key K))

Each calls 'fn' on every item in the set in no particular order.

func (*Set[K]) Has
func (s *Set[K]) Has(val K) bool

Has returns true only if 'val' is in the set.

func (*Set[K]) Put
func (s *Set[K]) Put(val K)

Put adds 'val' to the set.

func (*Set[K]) Remove
func (s *Set[K]) Remove(val K)

Remove removes 'val' from the set.

func (*Set[K]) Size
func (s *Set[K]) Size() int

Size returns the number of elements in the set.

Generated by gomarkdoc

Documentation

Overview

Package hashset provides an implementation of a hashset.

Example
package main

import (
	"fmt"

	g "github.com/zyedidia/generic"
	"github.com/zyedidia/generic/hashset"
)

func main() {
	set := hashset.New[string](3, g.Equals[string], g.HashString)
	set.Put("foo")
	set.Put("bar")
	set.Put("baz")

	fmt.Println(set.Has("foo"))
	fmt.Println(set.Has("quux"))

	set.Remove("foo")

	fmt.Println(set.Has("foo"))
	fmt.Println(set.Has("bar"))

	set.Clear()

	fmt.Println(set.Has("foo"))
	fmt.Println(set.Has("bar"))
}
Output:

true
false
false
true
false
false

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set[K any] struct {
	// contains filtered or unexported fields
}

Set implements a hashset, using the hashmap as the underlying storage.

func New

func New[K any](capacity uint64, equals g.EqualsFn[K], hash g.HashFn[K]) *Set[K]

New returns an empty hashset.

func Of added in v1.2.0

func Of[K any](capacity uint64, equals g.EqualsFn[K], hash g.HashFn[K], vals ...K) *Set[K]

Of returns a new hashset initialized with the given 'vals'

func (*Set[K]) Clear added in v1.2.0

func (s *Set[K]) Clear()

Clear removes all elements from the set.

func (*Set[K]) Copy added in v0.2.0

func (s *Set[K]) Copy() *Set[K]

Copy returns a copy of this set.

func (*Set[K]) Each

func (s *Set[K]) Each(fn func(key K))

Each calls 'fn' on every item in the set in no particular order.

func (*Set[K]) Has

func (s *Set[K]) Has(val K) bool

Has returns true only if 'val' is in the set.

func (*Set[K]) Put

func (s *Set[K]) Put(val K)

Put adds 'val' to the set.

func (*Set[K]) Remove

func (s *Set[K]) Remove(val K)

Remove removes 'val' from the set.

func (*Set[K]) Size

func (s *Set[K]) Size() int

Size returns the number of elements in the set.

Jump to

Keyboard shortcuts

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