ordered_sync_map

package module
v0.0.0-...-3d1aafc Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2023 License: Apache-2.0 Imports: 2 Imported by: 3

README

ordered-sync-map

ordered-sync-map is a package that implements a goroutine-safe ordered map.

usage

Chek the documentation here.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

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

Map is a thread safe and ordered implementation of standard map. K is the type of key and V is the type of value.

func New

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

New returns an initialized Map[K, V].

Example
package main

import (
	"fmt"

	ordered_sync_map "github.com/m-murad/ordered-sync-map"
)

func main() {

	mp := ordered_sync_map.New[string, string]()

	mp.Put("k1", "v1")

	v, ok := mp.Get("k1")
	fmt.Println(v, ok)

	ok = mp.Delete("k2")
	fmt.Println(ok)

	mp.UnorderedRange(func(key, value string) {
		fmt.Println(key, value)
	})

	mp.OrderedRange(func(key, value string) {
		fmt.Println(key, value)
	})

	len := mp.Length()
	fmt.Println(len)

	v, ok = mp.GetOrPut("k1", "v2")
	fmt.Println(v, ok)

	v, ok = mp.GetAndDelete("k1")
	fmt.Println(v, ok)
}

func (*Map[K, V]) Delete

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

Delete deletes the value for a key. It returns a boolean indicating weather the key existed and it was deleted.

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(key K) (V, bool)

Get returns the value stored in the map for a key. If the key is not found in the Map it return the zero value of type V. The bool indicates whether value was found in the map.

func (*Map[K, V]) GetAndDelete

func (m *Map[K, V]) GetAndDelete(key K) (V, bool)

GetAndDelete will get the value saved against the given key. deleted will be true if the key existed previously otherwise it will be false.

func (*Map[K, V]) GetOrPut

func (m *Map[K, V]) GetOrPut(key K, value V) (V, bool)

GetOrPut will return the existing value if the key exists in the Map. If the key did not exist previously it will be added to the Map. updated will be true if the key existed previously otherwise it will be false if the key did not exist and was added to the Map.

func (*Map[k, V]) Length

func (m *Map[k, V]) Length() int

Length will return the length of Map.

func (*Map[K, V]) OrderedRange

func (m *Map[K, V]) OrderedRange(f func(key K, value V))

OrderedRange will range over the map in ab ordered sequence. Parameter func f should not call any method of the Map, eg Get, Put, Delete, UnorderedRange, OrderedRange etc It will cause a deadlock.

func (*Map[K, V]) Put

func (m *Map[K, V]) Put(key K, val V)

Put sets the value for the given key. It will replace the value if the key already exists in the map even if the values are same.

func (*Map[K, V]) UnorderedRange

func (m *Map[K, V]) UnorderedRange(f func(key K, value V))

UnorderedRange will range over the map in an unordered sequence. This is same as ranging over a map using the "for range" syntax. Parameter func f should not call any method of the Map, eg Get, Put, Delete, UnorderedRange, OrderedRange etc It will cause a deadlock.

Jump to

Keyboard shortcuts

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