hashmap

package module
v0.0.0-...-41be27f Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2022 License: MIT Imports: 4 Imported by: 0

README

hashmap

CI Workflow Badge Go Report Card Codecov Godoc Badge

Concurrent hash map implementation in go.

hashmap supports generic types, so go version 1.19 is required.

go get github.com/semihbkgr/hashmap@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 hashmap concurrent hash map

Example (Custom_map)
m := NewMap[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 := NewStringMap[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
}

func New

func New[K, V any](hf HashFunc[K], ef EqualsFunc[K]) ConcurrentHashMap[K, V]

func NewComparableMap

func NewComparableMap[K comparable, V any]() ConcurrentHashMap[K, V]

func NewComparableMapWithCap

func NewComparableMapWithCap[K comparable, V any](capacity int) (ConcurrentHashMap[K, V], error)

func NewIntMap

func NewIntMap[V any]() ConcurrentHashMap[int, V]

func NewIntMapWithCap

func NewIntMapWithCap[V any](capacity int) (ConcurrentHashMap[int, V], error)

func NewMap

func NewMap[K Hasher, V any]() ConcurrentHashMap[K, V]

func NewMapWithCap

func NewMapWithCap[K Hasher, V any](capacity int) (ConcurrentHashMap[K, V], error)

func NewStringMap

func NewStringMap[V any]() ConcurrentHashMap[string, V]

func NewStringMapWithCap

func NewStringMapWithCap[V any](capacity int) (ConcurrentHashMap[string, V], error)

func NewWithCap

func NewWithCap[K, V any](capacity int, hf HashFunc[K], ef EqualsFunc[K]) (ConcurrentHashMap[K, V], error)

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 ConcurrentHashSet

type ConcurrentHashSet[T any] struct {
	// contains filtered or unexported fields
}

func NewComparableSet

func NewComparableSet[T comparable]() ConcurrentHashSet[T]

func NewComparableSetWithCap

func NewComparableSetWithCap[T comparable](capacity int) (ConcurrentHashSet[T], error)

func NewHasherSet

func NewHasherSet[T Hasher]() ConcurrentHashSet[T]

func NewHasherSetWithCap

func NewHasherSetWithCap[T Hasher](capacity int) (ConcurrentHashSet[T], error)

func NewSet

func NewSet[T any](hf HashFunc[T], ef EqualsFunc[T]) ConcurrentHashSet[T]

func NewSetWithCap

func NewSetWithCap[T any](capacity int, hf HashFunc[T], ef EqualsFunc[T]) (ConcurrentHashSet[T], error)

func (*ConcurrentHashSet[T]) Contains

func (s *ConcurrentHashSet[T]) Contains(t T) bool

func (*ConcurrentHashSet[T]) Put

func (s *ConcurrentHashSet[T]) Put(t T)

func (*ConcurrentHashSet[T]) Remove

func (s *ConcurrentHashSet[T]) Remove(t T) bool

type EqualsFunc

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

type HashFunc

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

type Hasher

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

Jump to

Keyboard shortcuts

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