orderedmap

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2024 License: MIT Imports: 6 Imported by: 0

README

# orderedmap

An implementation of an ordered map.  It's not very efficient, I'm sure.
It uses a slice alongside the (private, builtin, non-ordered) map to
keep the keys in order.

It *should* be safe for concurrent use.  All the methods on OrderedMap
are protected with a (read-write) mutex.

I took the tests from elliotchance/orderedmap, but ripped out the
testify/assert stuff, and replaced the bits used (poorly, I'm sure!)
with my own stuff (but supplemented liberally *from* the testify
library).  That all ended up in assert_test.go.

(I need to put that in its own project so I can copy it into forks of
projects where I rip out testify/assert.  I don't like
libraries/projects I use to depend on testify, it seems really really
silly to me.)

## TODO

- Implementing the commented out functions at the bottom of
  orderedmap.go from package maps.
- Implementing the "extras" (Front(), Back(), something else?) from
  elliotchance/orderedmap.

## license

MIT

Documentation

Overview

DO NOT EDIT THIS FILE GENERATED BY cmd/gencompareany/gencompareany.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal added in v0.4.0

func Equal[K cmp.Ordered, V any](m1, m2 *OrderedMap[K, V]) bool

This allocates a lot more memory than the above Equal.

There are lots of ways to do this!

Types

type OrderedMap

type OrderedMap[K cmp.Ordered, V any] struct {
	// contains filtered or unexported fields
}

func New

func New[K cmp.Ordered, V any]() *OrderedMap[K, V]

New returns an OrderedMap ready to use.

Example:

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

func NewFromKeysValues

func NewFromKeysValues[K cmp.Ordered, V any](keys []K, values []V) *OrderedMap[K, V]

NewFromKeysValues returns an OrderedMap with keys[i]=>values[i] for all 0 <= i < len(keys). It's an error if len(keys) != len(values).

Parallel arrays technique.

func NewFromPairs added in v0.3.0

func NewFromPairs[K cmp.Ordered, V any](pairs []Pair[K, V]) *OrderedMap[K, V]

Example use:

m := orderedmap.NewFromPairs([]orderedmap.Pair[string, int]{
	{"cat", 600},
	{"badger", 500},
	{"centipede", 400},
	{"bog", 300},
	{"foist", 200},
	{"cake", 100},
})

func (*OrderedMap[K, V]) All added in v0.5.0

func (m *OrderedMap[K, V]) All() iter.Seq2[K, V]

func (*OrderedMap[K, V]) Delete

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

Delete deletes the key and its associated value. It returns a boolean indicating whether the key existed or not.

Same as for Set, my inclination was to not return a value, but elliotchance/orderedmap Delete does. So...

Delete will remove a key from the map. It will return true if the key was removed (the key did exist).

func (*OrderedMap[K, V]) DeleteWithSlicesDeleteFunc added in v0.5.0

func (m *OrderedMap[K, V]) DeleteWithSlicesDeleteFunc(key K) bool

Alternative Delete using slices package.

func (*OrderedMap[K, V]) Get

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

Get returns the value associated with key and a boolean indicating whether or not the key exists.

Does this alias memory? No. This is tested by TestGet/DoesGetAliasMemory.

func (*OrderedMap[K, V]) Keys

func (m *OrderedMap[K, V]) Keys() iter.Seq[K]

func (*OrderedMap[K, V]) Len

func (m *OrderedMap[K, V]) Len() int

Len returns the length of m.

func (*OrderedMap[K, V]) MarshalJSON added in v0.4.0

func (m *OrderedMap[K, V]) MarshalJSON() ([]byte, error)

func (*OrderedMap[K, V]) Set

func (m *OrderedMap[K, V]) Set(key K, value V) bool

Set sets key=>value. If key exists, its value is replaced and key retains its order in the list. If key doesn't exist, it's added at the end of the ordering.

My inclination was for this to not return a value, but elliotchance/orderedmap (where I took the tests from) Set *does* return a bool. So... I guess I will.

Here is their function comment:

Set will set (or replace) a value for a key. If the key was new, then true will be returned. The returned value will be false if the value was replaced (even if the value was the same).

func (*OrderedMap[K, V]) Sort added in v0.5.0

func (m *OrderedMap[K, V]) Sort()

func (*OrderedMap[K, V]) UnmarshalJSON added in v0.4.0

func (m *OrderedMap[K, V]) UnmarshalJSON(b []byte) error

func (*OrderedMap[K, V]) Values

func (m *OrderedMap[K, V]) Values() iter.Seq[V]

type Pair added in v0.3.0

type Pair[K cmp.Ordered, V any] struct {
	Key   K
	Value V
}

Directories

Path Synopsis
cmd
gencompareany command

Jump to

Keyboard shortcuts

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