orderedmap

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2023 License: MIT Imports: 0 Imported by: 0

README

orderedmap Go Reference CI Status MIT License

An ordered map for Golang.

This library provides the following functionalities:

  • A map which is like a Go standard map, and provide same methods with Go sync.Map except CompareAndDelete and CompareAndSwap. (Concurrent use is not supported.)
  • Front and Back methods that iterates map entries in the order of key insertions.
  • Ldelete and LoadAndLdelete methods for logical deletions, because Store and Delete are slower than Go standard map.
  • LoadOrStoreFunc method which stores a result of a give function when an entry for the specified key is not present.

Importing this package

import "github.com/sttk-go/orderedmap"

Usage

The usage of this framework is described on the overview in the go package document.

See https://pkg.go.dev/github.com/sttk-go/orderedmap#pkg-overview.

Supporting Go versions

This framework supports Go 1.18 or later.

Actual test results for each Go version:
% gvm-fav
Now using version go1.18.10
go version go1.18.10 darwin/amd64
ok  	github.com/sttk-go/orderedmap	0.128s	coverage: 100.0% of statements

Now using version go1.19.10
go version go1.19.10 darwin/amd64
ok  	github.com/sttk-go/orderedmap	0.124s	coverage: 100.0% of statements

Now using version go1.20.5
go version go1.20.5 darwin/amd64
ok  	github.com/sttk-go/orderedmap	0.171s	coverage: 100.0% of statements

Back to go1.20.5
Now using version go1.20.5

License

Copyright (C) 2023 Takayuki Sato

This program is free software under MIT License.
See the file LICENSE in this distribution for more details.

Documentation

Overview

Package orderedmap provides Map type which is a map presering the order of key insertions.

Usage

To create an ordered map is as follows:

om := orderedmap.New[string, string]()

To add a map entry is as follows:

om.Store("foo", "hoge")
prev, swapped := om.Swap("bar", "fuga")
actual, loaded := om.LoadOrStore("baz", "fuga")

To get a value for a key is as follows:

om.Load("foo")

To delete a map entry is as follows:

om.Delete("bar")
v,, deleted := om.LoadAndDelete("baz")

To delete a map entry logically is as follows:

om.Ldelete("bar")
v,, deleted := om.LoadAndLdelete("baz")

To iterate map entries is as folLows. The order is same with key insertions:

om.Range(func(k, v) bool { ... })
for ent := om.Front(); ent != nil; ent = ent.Next() {
    k := ent.Key(); v : = ent.Value(); ...
}
for ent := om.Back(); ent != nil; ent = ent.Prev() {
    k := ent.Key(); v : = ent.Value(); ...
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Entry is a struct which is a map element and holds a pair of key and value. This struct also has methods: Next and Prev which moves next or previous entties sequencially.

func (*Entry[K, V]) Key

func (ent *Entry[K, V]) Key() K

Key is a method which returns the key of this entry.

func (*Entry[K, V]) Next

func (ent *Entry[K, V]) Next() *Entry[K, V]

Next is a method which returns the next entry of this entry. If this entry is a last entry of an ordered map, the returned value is nil.

func (*Entry[K, V]) Prev

func (ent *Entry[K, V]) Prev() *Entry[K, V]

Prev is a method which returns the previous entry of this entry. If this entry is a head entry of an ordered map, the returned value is nil.

func (*Entry[K, V]) Value

func (ent *Entry[K, V]) Value() V

Value is a method which returns the value of this entry.

type Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Map is a struct which represents a map similar with Go standard map, or sync.Map, but preserves the order in which keys were inserted.

This map has same methods with sync.Map except CompareAndDelete and CompareAndSwap. (But not support concurrent use.) Its Range method processes a key and a value of each map entry, and the processing order is same with the order of key insertions. And this map also has methods: Front and Back, which iterate this map entries in the order of key insertions and in that reverse order.

func New

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

New is a function which creates a new ordered map, which is ampty.

func (*Map[K, V]) Back

func (om *Map[K, V]) Back() *Entry[K, V]

Back is a method which returns the last entry of this map.

func (*Map[K, V]) Delete

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

Delete is a method which deletes a value for a key.

func (*Map[K, V]) Front

func (om *Map[K, V]) Front() *Entry[K, V]

Front is a method which returns the head entry of this map.

func (*Map[K, V]) Ldelete

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

Ldelete is a method which logically deletes a value for a key.

func (*Map[K, V]) Len

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

Len is a method which returns the number of entries in this map.

func (*Map[K, V]) Load

func (om *Map[K, V]) Load(key K) (value V, ok bool)

Load is a method which returns a value stored in this map for a key. If no value was found for a key, the ok result is false.

func (*Map[K, V]) LoadAndDelete

func (om *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool)

LoadAndDelete is a method which deletes a value for a key, and returns the previous value if any. The loaded flag is true if the key was present.

func (*Map[K, V]) LoadAndLdelete

func (om *Map[K, V]) LoadAndLdelete(key K) (value V, loaded bool)

LoadAndLdelete is a method which logically deletes a value for a key, and returns the previous value if any. The loaded flag is true if the key was present.

func (*Map[K, V]) LoadOrStore

func (om *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

LoadOrStore is a method which returns a value for a key if presents, otherwise stores and returns a given value. The loaded flag is true if the value was loaded, false if stored.

func (*Map[K, V]) LoadOrStoreFunc added in v0.2.0

func (om *Map[K, V]) LoadOrStoreFunc(
	key K,
	fn func() (V, error),
) (actual V, loaded bool, err error)

LoadOrStoreFunc is a method which returns a value for a key if presents, otherwise executes a give function, then stores and returns the result value. The loaded flag is true if the value was loaded, false if stored.

func (*Map[K, V]) Range

func (om *Map[K, V]) Range(fn func(key K, value V) bool)

Range is a method which calls the specified function: fn sequentially for each key and value in this map. If fn returns false, this method stops the iteration.

func (*Map[K, V]) Store

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

Store is a method which sets a value for a key

func (*Map[K, V]) Swap

func (om *Map[K, V]) Swap(key K, value V) (previous V, loaded bool)

Swap is a method which sets a value for a key. If the key was present, this map returns the previous value and the loaded flag which is set to true.

Jump to

Keyboard shortcuts

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