ordmap

package module
v0.5.10 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2023 License: BSD-3-Clause Imports: 2 Imported by: 65

README

ordmap: ordered map using Go 1.18 generics

Go Reference

Package ordmap implements an ordered map that retains the order of items added to a slice, while also providing fast key-based map lookup of items, using the Go 1.18 generics system.

The implementation is fully visible and the API provides a minimal subset of methods, compared to other implementations that are heavier, so that additional functionality can be added as needed. Iteration can be performed directly on the Order using standard Go range function.

The slice structure holds the Key and Val for items as they are added, enabling direct updating of the corresponding map, which holds the index into the slice.

Adding and access are fast, while deleting and inserting are relatively slow, requiring updating of the index map, but these are already slow due to the slice updating.

Documentation

Overview

package ordmap implements an ordered map that retains the order of items added to a slice, while also providing fast key-based map lookup of items, using the Go 1.18 generics system.

The implementation is fully visible and the API provides a minimal subset of methods, compared to other implementations that are heavier, so that additional functionality can be added as needed.

The slice structure holds the Key and Val for items as they are added, enabling direct updating of the corresponding map, which holds the index into the slice. Adding and access are fast, while deleting and inserting are relatively slow, requiring updating of the index map, but these are already slow due to the slice updating.

Index

Constants

View Source
const (
	// Version is the version of this package being used
	Version = "v0.5.10"
	// GitCommit is the commit just before the latest version commit
	GitCommit = "5f4ecff"
	// VersionDate is the date-time of the latest version commit in UTC (in the format 'YYYY-MM-DD HH:MM', which is the Go format '2006-01-02 15:04')
	VersionDate = "2023-12-26 23:24"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyVal

type KeyVal[K comparable, V any] struct {
	Key K
	Val V
}

KeyVal represents the Key and Value

type Map

type Map[K comparable, V any] struct {

	// ordered list of values and associated keys -- in order added
	Order []KeyVal[K, V]

	// key to index mapping
	Map map[K]int
}

Map is a generic ordered map that combines the order of a slice and the fast key lookup of a map. A map stores an index into a slice that has the value and key associated with the value.

func Make added in v0.5.1

func Make[K comparable, V any](vals []KeyVal[K, V]) *Map[K, V]

Make constructs a new ordered map with the given key, value pairs

func New

func New[K comparable, V any]() *Map[K, V]

New returns a new ordered map

func (*Map[K, V]) Add

func (om *Map[K, V]) Add(key K, val V)

Add adds a new value for given key. If key already exists in map, it replaces the item at that existing index, otherwise it is added to the end.

func (*Map[K, V]) Copy added in v0.5.9

func (om *Map[K, V]) Copy(from *Map[K, V])

Copy copies all of the entries from the given ordered map into this ordered map. It keeps existing entries in this map unless they also exist in the given map, in which case they are overwritten.

func (*Map[K, V]) DeleteIdx

func (om *Map[K, V]) DeleteIdx(i, j int)

DeleteIdx deletes item(s) within index range [i:j] This is relatively slow because it needs to renumber the index map above the deleted range.

func (*Map[K, V]) DeleteKey

func (om *Map[K, V]) DeleteKey(key K) bool

DeleteKey deletes item by given key, returns true if found

func (*Map[K, V]) GoString added in v0.5.2

func (om *Map[K, V]) GoString() string

GoString returns the map as Go code

func (*Map[K, V]) IdxByKey

func (om *Map[K, V]) IdxByKey(key K) int

IdxByKey returns index of given Key, with a -1 for missing key. See Map.IdxByKeyTry for a version returning a bool for missing key.

func (*Map[K, V]) IdxByKeyTry added in v0.5.1

func (om *Map[K, V]) IdxByKeyTry(key K) (int, bool)

IdxByKeyTry returns index of given Key, along with bool reflecting presence of key.

func (*Map[K, V]) IdxIsValid

func (om *Map[K, V]) IdxIsValid(idx int) error

IdxIsValid returns error if index is invalid

func (*Map[K, V]) Init

func (om *Map[K, V]) Init()

Init initializes the map if not done yet

func (*Map[K, V]) InsertAtIdx

func (om *Map[K, V]) InsertAtIdx(idx int, key K, val V)

InsertAtIdx inserts value with key at given index This is relatively slow because it needs to renumber the index map above the inserted value. It will panic if the key already exists because the behavior is undefined in that situation.

func (*Map[K, V]) KeyByIdx

func (om *Map[K, V]) KeyByIdx(idx int) K

KeyByIdx returns key for given index, in ordered slice.

func (*Map[K, V]) Keys

func (om *Map[K, V]) Keys() []K

Keys returns a slice of keys in order

func (*Map[K, V]) Len

func (om *Map[K, V]) Len() int

Len returns the number of items in the map

func (*Map[K, V]) ReplaceIdx

func (om *Map[K, V]) ReplaceIdx(idx int, key K, val V)

ReplaceIdx replaces value at given index with new item with given key

func (*Map[K, V]) Reset

func (om *Map[K, V]) Reset()

Reset resets the map, removing any existing elements

func (*Map[K, V]) String added in v0.5.2

func (om *Map[K, V]) String() string

String returns the map as a string

func (*Map[K, V]) ValByIdx

func (om *Map[K, V]) ValByIdx(idx int) V

ValByIdx returns value at given index, in ordered slice.

func (*Map[K, V]) ValByKey

func (om *Map[K, V]) ValByKey(key K) V

ValByKey returns value based on Key, with a zero value returned for missing key. See Map.ValByKeyTry for one that returns a bool for missing keys.

func (*Map[K, V]) ValByKeyTry added in v0.5.1

func (om *Map[K, V]) ValByKeyTry(key K) (V, bool)

ValByKeyTry returns value based on Key, along with bool reflecting presence of key.

func (*Map[K, V]) Vals

func (om *Map[K, V]) Vals() []V

Vals returns a slice of vals in order

Jump to

Keyboard shortcuts

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