chmap

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2022 License: MIT Imports: 3 Imported by: 0

README

chmap

CI Workflow Badge Go Report Card Codecov Godoc Badge

Concurrent hash map implementation in go.

chmap supports generic types, so go version 1.18 is required.

go get github.com/semihbkgr/chmap@latest

string type key map example

m := NewString[int]()

m.Put("nine", 9)

if val, ok := m.Get("nine"); ok {
    _ = val + 11
    // ...
}

_ = m.GetOrDefault("twenty-three", 23)

if ok := m.Contains("nine"); ok {
    // ...
}

custom type key map example

type User struct {
    ID   uint
    Name string
}

func (u User) Hash() uint32 {
    return uint32(u.ID)
}

func (u User) Equals(a any) bool {
    user, ok := a.(User)
    if !ok {
        return false
    }
    return u.ID == user.ID
}

type UserContext map[string]any

func main() {

    m := New[User, UserContext]()

    user := User{
        ID:   1,
        Name: "alice",
    }

    userContext:=make(map[string]any)
    userContext["sessionID"]=sessionID()

    m.Put(user, userContext)

    if val, ok := m.Get(User{ID: 1}); ok {
        _ = val["sessionID"]
        // ...
    }

}

Documentation

Overview

Package chmap concurrent hash map

Example (Custom_map)
m := New[User, UserContext]()

user := User{
	ID:   1,
	Name: "alice",
}

userContext := make(map[string]any)
userContext["sessionID"] = rand.Int()

m.Put(user, userContext)

if val, ok := m.Get(User{ID: 1}); ok {
	_ = val["sessionID"]
	// ...
}
Output:

Example (String_map)
m := NewString[int]()

m.Put("nine", 9)

if val, ok := m.Get("nine"); ok {
	_ = val + 11
	// ...
}

_ = m.GetOrDefault("twenty-three", 23)

if ok := m.Contains("nine"); ok {
	// ...
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConcurrentHashMap

type ConcurrentHashMap[k, v any] struct {
	// contains filtered or unexported fields
}

ConcurrentHashMap thread-safe string:any map

func New

func New[k Hasher, v any]() ConcurrentHashMap[k, v]

New returns ConcurrentHashMap with default capacity.

func NewString

func NewString[v any]() ConcurrentHashMap[string, v]

NewString returns string type key ConcurrentHashMap with default capacity.

func NewStringWithCap

func NewStringWithCap[v any](capacity int) (chm ConcurrentHashMap[string, v], err error)

NewStringWithCap returns string type key ConcurrentHashMap with given capacity.

func NewWithCap

func NewWithCap[k Hasher, v any](capacity int) (chm ConcurrentHashMap[k, v], err error)

NewWithCap returns ConcurrentHashMap with given capacity.

func NewWithCapAndFuncs

func NewWithCapAndFuncs[k, v any](capacity int, hf HashFunc[k], ef EqualsFunc[k]) (chm ConcurrentHashMap[k, v], err error)

NewWithCapAndFuncs returns ConcurrentHashMap with the given capacity and funcs.

func NewWithFuncs

func NewWithFuncs[k, v any](hf HashFunc[k], ef EqualsFunc[k]) (ConcurrentHashMap[k, v], error)

NewWithFuncs returns ConcurrentHashMap with the given funcs and default capacity.

func (*ConcurrentHashMap[k, v]) Contains

func (m *ConcurrentHashMap[k, v]) Contains(key k) bool

Contains returns if there is an entry mapped by the given key.

func (*ConcurrentHashMap[k, v]) Get

func (m *ConcurrentHashMap[k, v]) Get(key k) (v, bool)

Get returns value of the entry mapped by given key. If there is mopping by given key, it returns false.

func (*ConcurrentHashMap[k, v]) GetOrDefault

func (m *ConcurrentHashMap[k, v]) GetOrDefault(key k, defVal v) v

GetOrDefault returns the value of the entry mapped by the given key. If there is mopping by the given key, it returns default value argument.

func (*ConcurrentHashMap[k, v]) Put

func (m *ConcurrentHashMap[k, v]) Put(key k, val v)

Put maps the given key to the value, and saves the entry. In case of there is already an entry mapped by the given key, it updates the value of the entry.

func (*ConcurrentHashMap[k, v]) Remove

func (m *ConcurrentHashMap[k, v]) Remove(key k) (v, bool)

Remove removes the entry mapped by the given key and returns value of removed entry and true. In case of there is entry by the given key, It returns nil and false.

func (*ConcurrentHashMap[k, v]) Size

func (m *ConcurrentHashMap[k, v]) Size() int

Size returns the count of entries in the map

type EqualsFunc

type EqualsFunc[k any] func(k1, k2 k) bool

type HashFunc

type HashFunc[k any] func(key k) uint32

type Hasher

type Hasher interface {
	Hash() uint32
	Equals(a any) bool
}

Hasher general interface to provide hash and equals function

Jump to

Keyboard shortcuts

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