cowmaps

package
v0.0.0-...-fe7a19e Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package cowmaps defines various copy-on-write functions useful with maps of any type.

Example
package main

import (
	"fmt"

	"github.com/phelmkamp/immut/cowmaps"
	"github.com/phelmkamp/immut/romaps"
)

func main() {
	m1 := map[string]int{"fiz": 3}
	m2 := cowmaps.CopyOnWrite(m1)
	cowmaps.Copy(&m2, romaps.Freeze(map[string]int{"foo": 42, "bar": 7}))
	fmt.Println(m2)
	fmt.Println(m1)
}
Output:

map[bar:7 fiz:3 foo:42]
map[fiz:3]
Example (Concurrent)

Example_concurrent demonstrates that two concurrent goroutines can access the same map without the use of channels or locks.

package main

import (
	"fmt"
	"math/rand"
	"strconv"
	"time"

	"github.com/phelmkamp/immut/cowmaps"
	"github.com/phelmkamp/immut/romaps"
)

func makeMap(n int) map[string]*int {
	rand.Seed(42)
	m := make(map[string]*int, n)
	for i := 0; i < n; i++ {
		v := rand.Intn(n)
		m[strconv.Itoa(v)] = &v
	}
	return m
}

func main() {
	m := cowmaps.CopyOnWrite(makeMap(5_000))
	go func() {
		for {
			// delete 1 pair after slight delay
			time.Sleep(1 * time.Millisecond)
			kDel := romaps.Keys(m.RO)[0]
			cowmaps.DeleteFunc(&m, func(k string, v *int) bool {
				return k == kDel
			})
		}
	}()
	go func() {
		for {
			// read all pairs constantly
			// without COW panic is possible
			// but ro is guaranteed not to change
			ro := m.RO
			for _, k := range romaps.Keys(ro) {
				v, _ := ro.Index(k)
				_ = fmt.Sprint(*v)
			}
		}
	}()
	// run for 1 sec
	time.Sleep(1 * time.Second)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clear

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

Clear removes all entries from m, leaving it empty. Note: The underlying map is reallocated before the write-operation is performed.

func Copy

func Copy[K comparable, V any](dst *Map[K, V], src romaps.Map[K, V])

Copy copies all key/value pairs in src adding them to dst. When a key in src is already present in dst, the value in dst will be overwritten by the value associated with the key in src. Note: The underlying map is cloned before the write-operation is performed.

func DeleteFunc

func DeleteFunc[K comparable, V any](m *Map[K, V], del func(K, V) bool)

DeleteFunc deletes any key/value pairs from m for which del returns true. Note: The underlying map is cloned before the write-operation is performed.

func DoAll

func DoAll[K comparable, V any](m *Map[K, V], cap int, ops ...Doer[K, V])

DoAll does all the supplied operations on the map with minimal reallocation. The initial capacity of the reallocated map is cap (or len(m) if cap is not sufficient). Note: The underlying map is cloned before the write-operations are performed.

Example
package main

import (
	"fmt"
	"github.com/phelmkamp/immut/cowmaps"
)

func main() {
	m := cowmaps.CopyOnWrite(map[string]int{"foo": 1})
	cowmaps.DoAll(&m, m.RO.Len(),
		cowmaps.DoCopy(map[string]int{"bar": 2, "baz": 3}),                              // map[foo:1 bar:2 baz:3]
		cowmaps.DoDeleteFunc[string, int](func(_ string, v int) bool { return v == 2 }), // map[foo:1 baz:3]
		cowmaps.DoSetIndex("baz", 2),                                                    // map[foo:1 baz:2]
		cowmaps.DoDelete[string, int]("foo"),                                            // map[baz:2]
	)
	fmt.Println(m)
}
Output:

map[baz:2]

Types

type Doer

type Doer[K comparable, V any] interface {
	// contains filtered or unexported methods
}

Doer defines a method for doing an operation on a map.

func DoCopy

func DoCopy[K comparable, V any](src map[K]V) Doer[K, V]

DoCopy returns the maps.Copy operation.

func DoDelete

func DoDelete[K comparable, V any](k K) Doer[K, V]

DoDelete returns the delete operation.

func DoDeleteFunc

func DoDeleteFunc[K comparable, V any](del func(K, V) bool) Doer[K, V]

DoDeleteFunc returns the maps.DeleteFunc operation.

func DoSetIndex

func DoSetIndex[K comparable, V any](k K, v V) Doer[K, V]

DoSetIndex returns the set index operation.

type Map

type Map[K comparable, V any] struct {
	RO romaps.Map[K, V] // wraps a read-only map
}

Map wraps a copy-on-write map.

func CopyOnWrite

func CopyOnWrite[K comparable, V any](m map[K]V) Map[K, V]

CopyOnWrite returns a copy-on-write wrapper for the given map.

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(k K) (v V, ok bool)

Delete deletes the element with the specified key from the map. If there is no such element, delete is a no-op. Note: The underlying map is reallocated before the write-operation is performed.

func (*Map[K, V]) SetIndex

func (m *Map[K, V]) SetIndex(k K, v V)

SetIndex sets the element associated with k to v. Note: The underlying map is reallocated before the write-operation is performed.

func (Map[K, V]) String

func (m Map[K, V]) String() string

String returns the underlying map formatted as a string.

Jump to

Keyboard shortcuts

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