Documentation
¶
Overview ¶
Example ¶
package main import ( "fmt" "strings" "github.com/aezhar/haxxmap" ) // your custom hash function // the hash function signature must adhere to `func(keyType) uintptr` func customStringHasher(s string) uintptr { return uintptr(len(s)) } // your custom comparison function // This allows for more complex key types to be compared func customStringCompare(l, r string) bool { return strings.ToLower(l) == strings.ToLower(r) } func main() { // initialize a string-string map m := haxxmap.New[string, string]( // this overrides the default xxHash algorithm haxxmap.WithHasher[string, string](customStringHasher), // this overrides the default key comparison function haxxmap.WithComparator[string, string](customStringCompare), ) m.Set("one", "1") m.Set("Two", "2") m.Set("three", "3") if val, ok := m.Get("One"); ok { fmt.Println(val) } if val, ok := m.Get("two"); ok { fmt.Println(val) } if val, ok := m.Get("three"); ok { fmt.Println(val) } }
Output: 1 2 3
Index ¶
- type EqualFn
- type HashFn
- type Hashable
- type Map
- func (m *Map[K, V]) CompareAndSwap(key K, oldValue, newValue V) bool
- func (m *Map[K, V]) Del(keys ...K)
- func (m *Map[K, V]) Fillrate() uintptr
- func (m *Map[K, V]) ForEach(lambda func(K, V) bool)
- func (m *Map[K, V]) Get(key K) (value V, ok bool)
- func (m *Map[K, V]) GetAndDel(key K) (value V, ok bool)
- func (m *Map[K, V]) GetOrCompute(key K, valueFn func() V) (actual V, loaded bool)
- func (m *Map[K, V]) GetOrSet(key K, value V) (actual V, loaded bool)
- func (m *Map[K, V]) Grow(newSize uintptr)
- func (m *Map[K, V]) Len() uintptr
- func (m *Map[K, V]) Set(key K, value V)
- func (m *Map[K, V]) Swap(key K, newValue V) (oldValue V, swapped bool)
- type Option
- type Options
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Map ¶
Map implements the concurrent hashmap
func (*Map[K, V]) CompareAndSwap ¶
func (m *Map[K, V]) CompareAndSwap(key K, oldValue, newValue V) bool
CompareAndSwap atomically updates a map entry given its key by comparing current value to `oldValue` and setting it to `newValue` if the above comparison is successful It returns a boolean indicating whether the CompareAndSwap was successful or not
func (*Map[K, V]) Del ¶
func (m *Map[K, V]) Del(keys ...K)
Del deletes key/keys from the map Bulk deletion is more efficient than deleting keys one by one
func (*Map[K, V]) Fillrate ¶
func (m *Map[K, V]) Fillrate() uintptr
Fillrate returns the fill rate of the map as an percentage integer
func (*Map[K, V]) ForEach ¶
func (m *Map[K, V]) ForEach(lambda func(K, V) bool)
ForEach iterates over key-value pairs and executes the lambda provided for each such pair lambda must return `true` to continue iteration and `false` to break iteration
func (*Map[K, V]) Get ¶
func (m *Map[K, V]) Get(key K) (value V, ok bool)
Get retrieves an element from the map returns `false“ if element is absent
func (*Map[K, V]) GetAndDel ¶
func (m *Map[K, V]) GetAndDel(key K) (value V, ok bool)
GetAndDel deletes the key from the map, returning the previous value if any.
func (*Map[K, V]) GetOrCompute ¶
func (m *Map[K, V]) GetOrCompute(key K, valueFn func() V) (actual V, loaded bool)
GetOrCompute is similar to GetOrSet but the value to be set is obtained from a constructor the value constructor is called only once
func (*Map[K, V]) GetOrSet ¶
func (m *Map[K, V]) GetOrSet(key K, value V) (actual V, loaded bool)
GetOrSet returns the existing value for the key if present Otherwise, it stores and returns the given value The loaded result is true if the value was loaded, false if stored
func (*Map[K, V]) Grow ¶
func (m *Map[K, V]) Grow(newSize uintptr)
Grow resizes the hashmap to a new size, gets rounded up to next power of 2 To double the size of the hashmap use newSize 0 No resizing is done in case of another resize operation already being in progress Growth and map bucket policy is inspired from https://github.com/cornelk/hashmap
func (*Map[K, V]) Len ¶
func (m *Map[K, V]) Len() uintptr
Len returns the number of key-value pairs within the map
type Option ¶
Option changes the behavior of the hashmap
func WithComparator ¶
WithComparator specifies the compare function to test keys for equality
func WithHasher ¶
WithHasher specifies the hash function to use