lmap

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2023 License: AGPL-3.0 Imports: 0 Imported by: 0

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

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.

func (*Iterator[K, V]) Entry

func (i *Iterator[K, V]) Entry() (k K, v V)

Entry returns the current key and value of the iterator.

func (*Iterator[K, V]) Next

func (i *Iterator[K, V]) Next() bool

Next advances the iterator and returns whether there is anything to be read with Entry. Next must be called before Entry.

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

func (l *LinkedMap[K, V]) Copy() *LinkedMap[K, V]

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

func (l *LinkedMap[K, _]) Delete(k K) (ok bool)

Delete behaves like `delete(l, k)`. If the key was not found, ok will be false.

func (*LinkedMap[K, V]) ForEach

func (l *LinkedMap[K, V]) ForEach(f func(k K, v V) bool)

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

func (l *LinkedMap[K, V]) Get(k K, bump bool) (v V, ok bool)

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

func (l *LinkedMap[K, V]) Head(pop bool) (k K, v V, ok bool)

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

func (l *LinkedMap[K, V]) Iterator() Iterator[K, V]

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[_, _]) Len

func (l *LinkedMap[_, _]) Len() int

Len behaves like `len(l)`. This is a constant-time operation.

func (*LinkedMap[K, V]) Next

func (l *LinkedMap[K, V]) Next(k K) (kn K, vn V, ok bool)

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

func (l *LinkedMap[K, V]) Prev(k K) (kp K, vp V, ok bool)

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.

func (*LinkedMap[K, V]) Set

func (l *LinkedMap[K, V]) Set(k K, v V, bumpOnExist bool)

Set behaves like the map set `l[k] = v`. If bumpOnExist 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 into the map. Otherwise, if k is not in the map, it is appended to the tail of the list.

func (*LinkedMap[K, V]) Tail

func (l *LinkedMap[K, V]) Tail(pop bool) (k K, v V, ok bool)

Tail returns the tail element of the linked list. If pop is true, the tail element is also removed from the map and list. If ok is false, no element was found.

Jump to

Keyboard shortcuts

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