hashset

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2024 License: Apache-2.0 Imports: 0 Imported by: 0

README

hashset

Introduction

In this repository, we implemented one foundational data structure: Set based on Map in golang. We have:
Add(value int64): Adds the specified element to this set.
Contains(value int64) bool: Returns true if this set contains the specified element.
Remove(value int64): Removes the specified element from this set.
Range(f func(value int64) bool): Function f executes by taking element in the set as parameter sequentially until f returns false
Len() int: Returns the number of elements of this set.

We made two experiments in order to measure the overall performance of the new hashset:

  1. the chosen value's type: empty struct vs. bool
  2. the impact of checking the existence of the key before add/remove an item

Features

  • The API of hashset is totally compatible with skipset link
  • Usually, developers implement the set in golang by setting the value of <key,value> pair to bool or int. However, We proved that using empty struct is more space efficiency and slightly time efficiency.

When to use hashset

Hashset doesnt guarantee concurrent safe. If you do need a concurrent safe set, go for skipset [link] -> https://github.com/bytedance/gopkg/tree/develop/collection/skipset

Quickstart

package main

import (
	"fmt"
	"github.com/bytedance/gopkg/collection/hashset"
)

func main() {
	l := hashset.NewInt()

	for _, v := range []int{10, 12, 15} {
		if l.Add(v) {
			fmt.Println("hashset add", v)
		}
	}

	if l.Contains(10) {
		fmt.Println("hashset contains 10")
	}

	l.Range(func(value int) bool {
		fmt.Println("hashset range found ", value)
		return true
	})

	l.Remove(15)
	fmt.Printf("hashset contains %d items\r\n", l.Len())
}

Benchmark

go version: go1.15.10 linux/amd64
CPU: Intel(R) Xeon(R) Platinum 8260 CPU @ 2.40GHz (4C8T)
OS: Debian 4.14.81.bm.15
MEMORY: 16G

$ go test -run=None -bench=. -benchtime=1000000x -benchmem -count=10 -cpu=4 > 1000000x20x4.txt
$ benchstat 1000000x20x4.txt
name                             time/op
ValueAsBool-4                    301ns ± 7%
ValueAsEmptyStruct-4             300ns ± 7%
AddAfterContains-4               334ns ± 5%
AddWithoutContains-4             303ns ± 9%
RemoveAfterContains_Missing-4    177ns ± 4%
RemoveWithoutContains_Missing-4  176ns ± 7%
RemoveAfterContains_Hitting-4    205ns ± 2%
RemoveWithoutContains_Hitting-4  135ns ±16%

name                             alloc/op
ValueAsBool-4                    54.0B ± 0%
ValueAsEmptyStruct-4             49.0B ± 0%
AddAfterContains-4               49.0B ± 0%
AddWithoutContains-4             49.0B ± 0%
RemoveAfterContains_Missing-4    0.00B
RemoveWithoutContains_Missing-4  0.00B
RemoveAfterContains_Hitting-4    0.00B
RemoveWithoutContains_Hitting-4  0.00B

Documentation

Overview

Example
l := NewInt()

for _, v := range []int{10, 12, 15} {
	l.Add(v)
}

if l.Contains(10) {
	fmt.Println("hashset contains 10")
}

l.Range(func(value int) bool {
	fmt.Println("hashset range found ", value)
	return true
})

l.Remove(15)
fmt.Printf("hashset contains %d items\r\n", l.Len())
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Float32Set

type Float32Set map[float32]struct{}

func NewFloat32

func NewFloat32() Float32Set

NewFloat32 returns an empty float32 set

func NewFloat32WithSize

func NewFloat32WithSize(size int) Float32Set

NewFloat32WithSize returns an empty float32 set initialized with specific size

func (Float32Set) Add

func (s Float32Set) Add(value float32) bool

Add adds the specified element to this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

func (Float32Set) Contains

func (s Float32Set) Contains(value float32) bool

Contains returns true if this set contains the specified element

func (Float32Set) Len

func (s Float32Set) Len() int

func (Float32Set) Range

func (s Float32Set) Range(f func(value float32) bool)

Range calls f sequentially for each value present in the hashset. If f returns false, range stops the iteration.

func (Float32Set) Remove

func (s Float32Set) Remove(value float32) bool

Remove removes the specified element from this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

type Float64Set

type Float64Set map[float64]struct{}

func NewFloat64

func NewFloat64() Float64Set

NewFloat64 returns an empty float64 set

func NewFloat64WithSize

func NewFloat64WithSize(size int) Float64Set

NewFloat64WithSize returns an empty float64 set initialized with specific size

func (Float64Set) Add

func (s Float64Set) Add(value float64) bool

Add adds the specified element to this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

func (Float64Set) Contains

func (s Float64Set) Contains(value float64) bool

Contains returns true if this set contains the specified element

func (Float64Set) Len

func (s Float64Set) Len() int

func (Float64Set) Range

func (s Float64Set) Range(f func(value float64) bool)

Range calls f sequentially for each value present in the hashset. If f returns false, range stops the iteration.

func (Float64Set) Remove

func (s Float64Set) Remove(value float64) bool

Remove removes the specified element from this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

type Int16Set

type Int16Set map[int16]struct{}

func NewInt16

func NewInt16() Int16Set

NewInt16 returns an empty int16 set

func NewInt16WithSize

func NewInt16WithSize(size int) Int16Set

NewInt16WithSize returns an empty int16 set initialized with specific size

func (Int16Set) Add

func (s Int16Set) Add(value int16) bool

Add adds the specified element to this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

func (Int16Set) Contains

func (s Int16Set) Contains(value int16) bool

Contains returns true if this set contains the specified element

func (Int16Set) Len

func (s Int16Set) Len() int

func (Int16Set) Range

func (s Int16Set) Range(f func(value int16) bool)

Range calls f sequentially for each value present in the hashset. If f returns false, range stops the iteration.

func (Int16Set) Remove

func (s Int16Set) Remove(value int16) bool

Remove removes the specified element from this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

type Int32Set

type Int32Set map[int32]struct{}

func NewInt32

func NewInt32() Int32Set

NewInt32 returns an empty int32 set

func NewInt32WithSize

func NewInt32WithSize(size int) Int32Set

NewInt32WithSize returns an empty int32 set initialized with specific size

func (Int32Set) Add

func (s Int32Set) Add(value int32) bool

Add adds the specified element to this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

func (Int32Set) Contains

func (s Int32Set) Contains(value int32) bool

Contains returns true if this set contains the specified element

func (Int32Set) Len

func (s Int32Set) Len() int

func (Int32Set) Range

func (s Int32Set) Range(f func(value int32) bool)

Range calls f sequentially for each value present in the hashset. If f returns false, range stops the iteration.

func (Int32Set) Remove

func (s Int32Set) Remove(value int32) bool

Remove removes the specified element from this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

type Int64Set

type Int64Set map[int64]struct{}

func NewInt64

func NewInt64() Int64Set

NewInt64 returns an empty int64 set

func NewInt64WithSize

func NewInt64WithSize(size int) Int64Set

NewInt64WithSize returns an empty int64 set initialized with specific size

func (Int64Set) Add

func (s Int64Set) Add(value int64) bool

Add adds the specified element to this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

func (Int64Set) Contains

func (s Int64Set) Contains(value int64) bool

Contains returns true if this set contains the specified element

func (Int64Set) Len

func (s Int64Set) Len() int

Len returns the number of elements of this set

func (Int64Set) Range

func (s Int64Set) Range(f func(value int64) bool)

Range calls f sequentially for each value present in the hashset. If f returns false, range stops the iteration.

func (Int64Set) Remove

func (s Int64Set) Remove(value int64) bool

Remove removes the specified element from this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

type IntSet

type IntSet map[int]struct{}

func NewInt

func NewInt() IntSet

NewInt returns an empty int set

func NewIntWithSize

func NewIntWithSize(size int) IntSet

NewIntWithSize returns an empty int set initialized with specific size

func (IntSet) Add

func (s IntSet) Add(value int) bool

Add adds the specified element to this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

func (IntSet) Contains

func (s IntSet) Contains(value int) bool

Contains returns true if this set contains the specified element

func (IntSet) Len

func (s IntSet) Len() int

func (IntSet) Range

func (s IntSet) Range(f func(value int) bool)

Range calls f sequentially for each value present in the hashset. If f returns false, range stops the iteration.

func (IntSet) Remove

func (s IntSet) Remove(value int) bool

Remove removes the specified element from this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

type Uint16Set

type Uint16Set map[uint16]struct{}

func NewUint16

func NewUint16() Uint16Set

NewUint16 returns an empty uint16 set

func NewUint16WithSize

func NewUint16WithSize(size int) Uint16Set

NewUint16WithSize returns an empty uint16 set initialized with specific size

func (Uint16Set) Add

func (s Uint16Set) Add(value uint16) bool

Add adds the specified element to this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

func (Uint16Set) Contains

func (s Uint16Set) Contains(value uint16) bool

Contains returns true if this set contains the specified element

func (Uint16Set) Len

func (s Uint16Set) Len() int

func (Uint16Set) Range

func (s Uint16Set) Range(f func(value uint16) bool)

Range calls f sequentially for each value present in the hashset. If f returns false, range stops the iteration.

func (Uint16Set) Remove

func (s Uint16Set) Remove(value uint16) bool

Remove removes the specified element from this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

type Uint32Set

type Uint32Set map[uint32]struct{}

func NewUint32

func NewUint32() Uint32Set

NewUint32 returns an empty uint32 set

func NewUint32WithSize

func NewUint32WithSize(size int) Uint32Set

NewUint32WithSize returns an empty uint32 set initialized with specific size

func (Uint32Set) Add

func (s Uint32Set) Add(value uint32) bool

Add adds the specified element to this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

func (Uint32Set) Contains

func (s Uint32Set) Contains(value uint32) bool

Contains returns true if this set contains the specified element

func (Uint32Set) Len

func (s Uint32Set) Len() int

func (Uint32Set) Range

func (s Uint32Set) Range(f func(value uint32) bool)

Range calls f sequentially for each value present in the hashset. If f returns false, range stops the iteration.

func (Uint32Set) Remove

func (s Uint32Set) Remove(value uint32) bool

Remove removes the specified element from this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

type Uint64Set

type Uint64Set map[uint64]struct{}

func NewUint64

func NewUint64() Uint64Set

NewUint64 returns an empty uint64 set

func NewUint64WithSize

func NewUint64WithSize(size int) Uint64Set

NewUint64WithSize returns an empty uint64 set initialized with specific size

func (Uint64Set) Add

func (s Uint64Set) Add(value uint64) bool

Add adds the specified element to this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

func (Uint64Set) Contains

func (s Uint64Set) Contains(value uint64) bool

Contains returns true if this set contains the specified element

func (Uint64Set) Len

func (s Uint64Set) Len() int

func (Uint64Set) Range

func (s Uint64Set) Range(f func(value uint64) bool)

Range calls f sequentially for each value present in the hashset. If f returns false, range stops the iteration.

func (Uint64Set) Remove

func (s Uint64Set) Remove(value uint64) bool

Remove removes the specified element from this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

type UintSet

type UintSet map[uint]struct{}

func NewUint

func NewUint() UintSet

NewUint returns an empty uint set

func NewUintWithSize

func NewUintWithSize(size int) UintSet

NewUintWithSize returns an empty uint set initialized with specific size

func (UintSet) Add

func (s UintSet) Add(value uint) bool

Add adds the specified element to this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

func (UintSet) Contains

func (s UintSet) Contains(value uint) bool

Contains returns true if this set contains the specified element

func (UintSet) Len

func (s UintSet) Len() int

func (UintSet) Range

func (s UintSet) Range(f func(value uint) bool)

Range calls f sequentially for each value present in the hashset. If f returns false, range stops the iteration.

func (UintSet) Remove

func (s UintSet) Remove(value uint) bool

Remove removes the specified element from this set Always returns true due to the build-in map doesn't indicate caller whether the given element already exists Reserves the return type for future extension

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳