Documentation ¶
Overview ¶
Package lmap provides an implementation of a linked map. It is the generic version of github.com/nik0sc/lmap, with some improvements.
Index ¶
- type Iterator
- type LinkedMap
- func (l *LinkedMap[K, V]) Copy() *LinkedMap[K, V]
- func (l *LinkedMap[K, _]) Delete(k K) (ok bool)
- func (l *LinkedMap[K, V]) ForEach(f func(k K, v V) bool)
- func (l *LinkedMap[K, V]) Get(k K, bump bool) (v V, ok bool)
- func (l *LinkedMap[K, V]) Head(pop bool) (k K, v V, ok bool)
- func (l *LinkedMap[K, V]) Iterator() Iterator[K, V]
- func (l *LinkedMap[_, _]) Len() int
- func (l *LinkedMap[K, V]) Next(k K) (kn K, vn V, ok bool)
- func (l *LinkedMap[K, V]) Prev(k K) (kp K, vp V, ok bool)
- func (l *LinkedMap[K, V]) Set(k K, v V, bumpOnExist bool)
- func (l *LinkedMap[K, V]) Tail(pop bool) (k K, v V, ok bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Iterator ¶
type Iterator[K comparable, V any] struct { // contains filtered or unexported fields }
Iterator is a map iterator object. See LinkedMap.Iterator.
type LinkedMap ¶
type LinkedMap[K comparable, V any] struct { // contains filtered or unexported fields }
LinkedMap is a map combined with a linked list. It preserves insertion order and therefore iteration order as well. LinkedMap is not safe for concurrent use.
func New ¶
func New[K comparable, V any]() *LinkedMap[K, V]
New returns a pointer to a new LinkedMap.
func (*LinkedMap[K, V]) Copy ¶
Copy returns a deep copy of the LinkedMap. Keys and values are copied. Note: Pointer-typed values will still end up pointing to the same location in memory.
func (*LinkedMap[K, _]) Delete ¶
Delete behaves like `delete(l, k)`. If the key was not found, ok will be false.
func (*LinkedMap[K, V]) ForEach ¶
ForEach allows ordered iteration over the map as with `for k, v := range l {}`. The function f is called for every key-value pair in order. If f returns false at any iteration, the iteration process is stopped early.
The result of modifying the map while iterating over it is undefined.
func (*LinkedMap[K, V]) Get ¶
Get behaves like the map access `v, ok := l[k]`. If bump is true and k is in the map, k is moved to the tail of the list, as if it were removed and added back to the map.
func (*LinkedMap[K, V]) Head ¶
Head returns the head element of the linked list. If pop is true, the head element is also removed from the map and list. If ok is false, no element was found.
func (*LinkedMap[K, V]) Iterator ¶
Iterator returns an iterator object starting at the head of the LinkedMap. The usual idiom for using an iterator is:
i := l.Iterator() for i.Next() { k, v := i.Entry() // do stuff with k and v ... }
The iterator may be abandoned at any time. Just like LinkedMap, it is not safe for concurrent use, although multiple goroutines each holding their own iterator may independently iterate over the LinkedMap.
func (*LinkedMap[K, V]) Next ¶
Next returns the key and value of the element after k. If k is not in the map, or k is already the last element, ok is false, and kn and vn are their zero values. This may also be used to iterate over the map.
func (*LinkedMap[K, V]) Prev ¶
Prev returns the key and value of the element before k. If k is not in the map, or k is already the first element, ok is false, and kp and vp are their zero values.