hashmap

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2021 License: Apache-2.0 Imports: 5 Imported by: 2

README

Golang hashmap, which is thread-safe

Example

package main

import (
    "fmt"
    "time"

    "github.com/wuyongjia/hashmap"
)

type Item struct {
	value int
	str   string
}

var capacity = 5
var hm1 *hashmap.HM
var hm2 *hashmap.HM
var hm3 *hashmap.HM

func main() {
	// #1
	hm1 = hashmap.New(capacity)

	hm1.Put(1, "value1")
	hm1.Put(101, 100001)
	hm1.Put(102, 3.14)
	hm1.Put(123, float32(3.1415))
	hm1.Put(333, &Item{
		value: 123,
		str:   "abcdefghijk",
	})

	fmt.Println(hm1.Get(1).(string))
	fmt.Println(hm1.Get(101).(int))
	fmt.Println(hm1.Get(102).(float64))
	fmt.Println(hm1.Get(123).(float32))

	var item = hm1.Get(333).(*Item)
	fmt.Println("value:", item.value, ", str:", item.str)

	hm1.UpdateWithFunc(333, func(value interface{}) {
		var item = value.(*Item)
		item.value = 321
		item.str = "ABCDEFGHIJKLMN"
	})

	item = hm1.Get(333).(*Item)
	fmt.Println("value:", item.value, ", str:", item.str)

	fmt.Println("count: ", hm1.GetCount(), "\n\n")

	// #2
	hm2 = hashmap.New(capacity)
	for i := 1; i <= capacity; i++ {
		go func(idx int) {
			hm2.Put([]byte(fmt.Sprintf("key%d", idx)), idx)
		}(i)
	}

	time.Sleep(time.Second)

	hm2.Remove([]byte(fmt.Sprintf("key%d", 2)))
	hm2.Remove([]byte(fmt.Sprintf("key%d", 3)))

	hm2.Iterate(func(key interface{}, value interface{}) {
		fmt.Println("key:", string(key.([]byte)), ", value:", value.(int))
	})

	fmt.Println("count: ", hm2.GetCount(), "\n\n")

	// #3
	hm3 = hashmap.New(capacity)
	for i := 1; i <= capacity; i++ {
		go func(idx int) {
			hm3.Put(fmt.Sprintf("key%d", idx), fmt.Sprintf("value%d", idx))
		}(i)
	}

	hm3.Put("mykey123", "hello")

	time.Sleep(time.Second)

	hm3.Iterate(func(key interface{}, value interface{}) {
		fmt.Println("key:", string(key.(string)), ", value:", value.(string))
	})

	fmt.Println("count: ", hm3.GetCount())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EqualFunc added in v1.0.1

type EqualFunc func(v1, v2 interface{}) bool

type HM

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

func New

func New(capacity int) *HM

func (*HM) Exists added in v1.0.2

func (hm *HM) Exists(key interface{}) bool

func (*HM) Expand

func (hm *HM) Expand(capacity int) *HM

func (*HM) Get

func (hm *HM) Get(key interface{}) interface{}

func (*HM) GetCapacity added in v1.0.6

func (hm *HM) GetCapacity() int

func (*HM) GetCount

func (hm *HM) GetCount() int

func (*HM) Iterate

func (hm *HM) Iterate(readFunc ReadFunc)

func (*HM) IterateAndUpdate

func (hm *HM) IterateAndUpdate(isValidAndUpdateFunc IsValidAndUpdataFunc)

func (*HM) Put

func (hm *HM) Put(key interface{}, value interface{})

func (*HM) Remove

func (hm *HM) Remove(key interface{})

func (*HM) RemoveAndUpdate

func (hm *HM) RemoveAndUpdate(key interface{}, updateFunc UpdateFunc)

func (*HM) RemoveUnsafe added in v1.0.5

func (hm *HM) RemoveUnsafe(key interface{})

func (*HM) UpdateWithFunc

func (hm *HM) UpdateWithFunc(key interface{}, updateFunc UpdateFunc)

type IsValidAndUpdataFunc

type IsValidAndUpdataFunc func(key interface{}, value interface{}) bool

type Pairs

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

type ReadFunc

type ReadFunc func(key interface{}, value interface{})

type UpdateFunc

type UpdateFunc func(value interface{})

Jump to

Keyboard shortcuts

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