Documentation ¶
Overview ¶
Package priority_map implements a key-value store where key-value pairs can be accessed either by key or the order of values. The value must be orderable (a < b) and the key must be comparable (a == b).
1. Get(K) V 2. Set(K, V) 3. Delete(K) 4. Top() (K, V, bool) 5. Pop() (K, V, bool) 6. Size() int
Usage
pm, _ := NewPriorityMap[int, string](func (v1, v2 string) bool { return v1 < v2 })
pm.Set(1, "a") pm.Get(1) // returns "a" pm.Set(2, "b") pm.Set(3, "c") pm.Top() // returns (1, "a", true) pm.Pop() // returns (1, "a", true) pm.Delete(1) pm.Size() // returns 2
See more usage example in the priority_map_test.go.
Index ¶
- type Element
- type PriorityMap
- func (pm *PriorityMap[K, V]) Delete(key K)
- func (pm *PriorityMap[K, V]) Get(k K) (V, bool)
- func (pm *PriorityMap[K, V]) Map() map[K]*Element[K, V]
- func (pm *PriorityMap[K, V]) Pop() (K, V, bool)
- func (pm *PriorityMap[K, V]) Set(k K, v V)
- func (pm *PriorityMap[K, V]) Size() int
- func (pm *PriorityMap[K, V]) Top() (K, V, bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Element ¶
type Element[K comparable, V any] struct { Key K Value V // contains filtered or unexported fields }
Element is the unit of data stored in hash map and the heap.
type PriorityMap ¶
type PriorityMap[K comparable, V any] struct { // contains filtered or unexported fields }
PriorityMap keeps key-value pairs in a hash map and provides access to the pair of the minimum value.
func NewPriorityMap ¶
func NewPriorityMap[K comparable, V any](less func(v1, v2 V) bool) *PriorityMap[K, V]
NewPriorityMap returns a PriorityMap where values are ordered by the given less function.
func (*PriorityMap[K, V]) Delete ¶
func (pm *PriorityMap[K, V]) Delete(key K)
Delete deletes the key-value pair of the key.
func (*PriorityMap[K, V]) Get ¶
func (pm *PriorityMap[K, V]) Get(k K) (V, bool)
Get returns the value associated with the key
func (*PriorityMap[K, V]) Map ¶
func (pm *PriorityMap[K, V]) Map() map[K]*Element[K, V]
Map returns the underlying map. It is here to provide an efficient way of iterating over all key-value pairs.
func (*PriorityMap[K, V]) Pop ¶
func (pm *PriorityMap[K, V]) Pop() (K, V, bool)
Pop removes and returns the key-value pair of the smallest value. It returns flase if the set is empty.
func (*PriorityMap[K, V]) Set ¶
func (pm *PriorityMap[K, V]) Set(k K, v V)
Set inserts a k-v pair if the key does not exist. Otherwise, Set updates the value.
func (*PriorityMap[K, V]) Size ¶
func (pm *PriorityMap[K, V]) Size() int
Size returns the number of key-value pairs.
func (*PriorityMap[K, V]) Top ¶
func (pm *PriorityMap[K, V]) Top() (K, V, bool)
Top returns the key-value pair of the smallest value. It returns false if the set is empty.