hashmap

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 29, 2024 License: MIT Imports: 1 Imported by: 0

README

Chaining hash table

package main

import (
	"fmt"

	"github.com/howz97/algorithm/search/hashmap"
	. "github.com/howz97/algorithm/util"
)

func main() {
	hm := hashmap.New[Str, string]()
	hm.Put(Str("a"), "A")
	hm.Put(Str("b"), "B")
	hm.Put(Str("c"), "C")
	hm.Put(Str("d"), "D")
	hm.Put(Str("e"), "E")
	hm.Put(Str("f"), "F")
	hm.Put(Str("g"), "G")
	hm.Put(Str("h"), "H")
	hm.Put(Str("i"), "I")
	fmt.Println(hm.String())

	fmt.Println("delete (d/f/g/x) ...")
	hm.Del(Str("d"))
	hm.Del(Str("f"))
	hm.Del(Str("g"))
	hm.Del(Str("x"))
	fmt.Println(hm.String())

	fmt.Println("delete all ...")
	hm.Range(func(key Str, _ string) bool {
		hm.Del(key)
		return true
	})
	fmt.Println(hm.String())

	// size=9
	// bucket0: (b:B) -> (d:D) -> (f:F) -> (h:H) -> nil
	// bucket1: (a:A) -> (c:C) -> (e:E) -> (g:G) -> (i:I) -> nil

	// delete (d/f/g/x) ...
	// size=6
	// bucket0: (b:B) -> (h:H) -> nil
	// bucket1: (a:A) -> (c:C) -> (e:E) -> (i:I) -> nil

	// delete all ...
	// size=0
	// bucket0: nil
}

Documentation

Index

Constants

View Source
const (
	MinSizeTbl = 1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Chaining

type Chaining[CH ComparableHasher, T any] struct {
	Num uint
	// contains filtered or unexported fields
}

func New

func New[CH ComparableHasher, T any](args ...uint) *Chaining[CH, T]

func (*Chaining[CH, T]) Clean

func (c *Chaining[CH, T]) Clean()

func (*Chaining[CH, T]) Del

func (c *Chaining[CH, T]) Del(k CH)

func (*Chaining[CH, T]) Get

func (c *Chaining[CH, T]) Get(k CH) (T, bool)

func (*Chaining[CH, T]) Put

func (c *Chaining[CH, T]) Put(k CH, v T)

func (*Chaining[CH, T]) Range

func (c *Chaining[CH, T]) Range(fn func(key CH, val T) bool)

func (*Chaining[CH, T]) Size

func (c *Chaining[CH, T]) Size() uint

func (*Chaining[CH, T]) String

func (c *Chaining[CH, T]) String() string

type ComparableHasher

type ComparableHasher interface {
	comparable
	Hash() uintptr
}

ComparableHasher is a type constraint that matches all comparable types with a Hash method.

Jump to

Keyboard shortcuts

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