Documentation ¶
Overview ¶
Package hashmap provides a lock-free and thread-safe HashMap.
Index ¶
- Constants
- type HashMap
- func (m *HashMap[Key, Value]) Del(key Key) bool
- func (m *HashMap[Key, Value]) FillRate() int
- func (m *HashMap[Key, Value]) Get(key Key) (Value, bool)
- func (m *HashMap[Key, Value]) GetOrInsert(key Key, value Value) (Value, bool)
- func (m *HashMap[Key, Value]) Grow(newSize uintptr)
- func (m *HashMap[Key, Value]) Insert(key Key, value Value) bool
- func (m *HashMap[Key, Value]) Len() int
- func (m *HashMap[Key, Value]) Range(f func(Key, Value) bool)
- func (m *HashMap[Key, Value]) Set(key Key, value Value)
- func (m *HashMap[Key, Value]) SetHasher(hasher func(Key) uintptr)
- func (m *HashMap[Key, Value]) String() string
- type KeyValue
- type List
- func (l *List[Key, Value]) Add(element, searchStart *ListElement[Key, Value]) (existed bool, inserted bool)
- func (l *List[Key, Value]) AddOrUpdate(element, searchStart *ListElement[Key, Value]) bool
- func (l *List[Key, Value]) Delete(element *ListElement[Key, Value])
- func (l *List[Key, Value]) First() *ListElement[Key, Value]
- func (l *List[Key, Value]) Len() int
- type ListElement
Constants ¶
const DefaultSize = 8
DefaultSize is the default size for a zero allocated map.
const MaxFillRate = 50
MaxFillRate is the maximum fill rate for the slice before a resize will happen.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HashMap ¶
type HashMap[Key comparable, Value any] struct { // contains filtered or unexported fields }
HashMap implements a read optimized hash map.
func New ¶
func New[Key comparable, Value any]() *HashMap[Key, Value]
New returns a new HashMap instance.
func NewSized ¶ added in v1.0.2
func NewSized[Key comparable, Value any](size uintptr) *HashMap[Key, Value]
NewSized returns a new HashMap instance with a specific initialization size.
func (*HashMap[Key, Value]) Del ¶
Del deletes the key from the map and returns whether the key was deleted.
func (*HashMap[Key, Value]) FillRate ¶ added in v1.0.2
FillRate returns the fill rate of the map as a percentage integer.
func (*HashMap[Key, Value]) GetOrInsert ¶
GetOrInsert returns the existing value for the key if present. Otherwise, it stores and returns the given value. The returned bool is true if the value was loaded, false if stored.
func (*HashMap[Key, Value]) Grow ¶
Grow resizes the hashmap to a new size, the size gets rounded up to next power of 2. To double the size of the hashmap use newSize 0. This function returns immediately, the resize operation is done in a goroutine. No resizing is done in case of another resize operation already being in progress.
func (*HashMap[Key, Value]) Insert ¶
Insert sets the value under the specified key to the map if it does not exist yet. If a resizing operation is happening concurrently while calling Insert, the item might show up in the map after the resize operation is finished. Returns true if the item was inserted or false if it existed.
func (*HashMap[Key, Value]) Range ¶ added in v1.0.3
Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.
func (*HashMap[Key, Value]) Set ¶
func (m *HashMap[Key, Value]) Set(key Key, value Value)
Set sets the value under the specified key to the map. An existing item for this key will be overwritten. If a resizing operation is happening concurrently while calling Set, the item might show up in the map after the resize operation is finished.
type KeyValue ¶
type KeyValue[Key comparable, Value any] struct { Key Key Value Value }
KeyValue represents a key/value that is returned by the iterator.
type List ¶
type List[Key comparable, Value any] struct { // contains filtered or unexported fields }
List is a sorted linked list.
func NewList ¶
func NewList[Key comparable, Value any]() *List[Key, Value]
NewList returns an initialized list.
func (*List[Key, Value]) Add ¶
func (l *List[Key, Value]) Add(element, searchStart *ListElement[Key, Value]) (existed bool, inserted bool)
Add adds an item to the list and returns false if an item for the hash existed. searchStart = nil will start to search at the head item.
func (*List[Key, Value]) AddOrUpdate ¶
func (l *List[Key, Value]) AddOrUpdate(element, searchStart *ListElement[Key, Value]) bool
AddOrUpdate adds or updates an item to the list.
func (*List[Key, Value]) Delete ¶
func (l *List[Key, Value]) Delete(element *ListElement[Key, Value])
Delete deletes an element from the list.
func (*List[Key, Value]) First ¶
func (l *List[Key, Value]) First() *ListElement[Key, Value]
First returns the first item of the list.
type ListElement ¶
type ListElement[Key comparable, Value any] struct { // contains filtered or unexported fields }
ListElement is an element of a list.
func (*ListElement[Key, Value]) Next ¶
func (e *ListElement[Key, Value]) Next() *ListElement[Key, Value]
Next returns the item on the right.
func (*ListElement[Key, Value]) Value ¶
func (e *ListElement[Key, Value]) Value() Value
Value returns the value of the list item.