maps

package module
v0.0.0-...-83d470c Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2025 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromAbstractMaps

func FromAbstractMaps[Key, Value any, Map AbstractMap[Key, Value]](m Map, ams ...AbstractMap[Key, Value]) Map

func FromGoMaps

func FromGoMaps[Key comparable, Value any, Map AbstractMap[Key, Value]](m Map, gms ...map[Key]Value) Map

Types

type AbstractMap

type AbstractMap[Key, Value any] interface {
	MapOps[Key, Value]
	Clear()
	CompareAndDelete(key Key, old Value) (deleted bool)
	CompareAndSwap(key Key, old, new Value) (swapped bool)
	Len() int
	LoadAndDelete(key Key) (value Value, loaded bool)
	LoadOrStore(key Key, value Value) (actual Value, loaded bool)
	Keys(f func(key Key) bool)
	Values(f func(value Value) bool)
	Swap(key Key, value Value) (previous Value, loaded bool)
}

type DefaultAbstractMap

type DefaultAbstractMap[Key, Value any] struct {
	// contains filtered or unexported fields
}

func NewDefaultAbstractMap

func NewDefaultAbstractMap[Key, Value any](impl AbstractMap[Key, Value]) *DefaultAbstractMap[Key, Value]

func (*DefaultAbstractMap[Key, Value]) Clear

func (m *DefaultAbstractMap[Key, Value]) Clear()

func (*DefaultAbstractMap[K, V]) CompareAndDelete

func (m *DefaultAbstractMap[K, V]) CompareAndDelete(key K, old V) (deleted bool)

func (*DefaultAbstractMap[K, V]) CompareAndSwap

func (m *DefaultAbstractMap[K, V]) CompareAndSwap(key K, old, new V) (swapped bool)

func (*DefaultAbstractMap[K, V]) Keys

func (m *DefaultAbstractMap[K, V]) Keys(f func(key K) bool)

func (*DefaultAbstractMap[K, V]) Len

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

func (*DefaultAbstractMap[K, V]) LoadAndDelete

func (m *DefaultAbstractMap[K, V]) LoadAndDelete(key K) (value V, loaded bool)

func (*DefaultAbstractMap[K, V]) LoadOrStore

func (m *DefaultAbstractMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

func (*DefaultAbstractMap[K, V]) Swap

func (m *DefaultAbstractMap[K, V]) Swap(key K, value V) (previous V, loaded bool)

func (*DefaultAbstractMap[K, V]) Values

func (m *DefaultAbstractMap[K, V]) Values(f func(value V) bool)

type MapOps

type MapOps[Key, Value any] interface {
	Delete(key Key)
	Load(key Key) (value Value, ok bool)
	Range(f func(key Key, value Value) bool)
	Store(key Key, value Value)
}

type OrderedMap

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

OrderedMap implements AbstractMap with insertion order preservation. It uses a hybrid approach: a standard Go map for O(1) key lookups combined with a doubly-linked list to maintain insertion order. This provides O(1) performance for Store, Load, and Delete operations while ensuring Range operations iterate in insertion order.

func NewOrderedMap

func NewOrderedMap[K comparable, V any]() *OrderedMap[K, V]

NewOrderedMap creates a new OrderedMap instance. The map is initialized empty with no memory pre-allocation, allowing it to grow dynamically as items are added.

func (*OrderedMap[K, V]) Delete

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

Delete removes a key-value pair from the map. If the key exists, it's removed from both the map and the list. If the key doesn't exist, this operation is a no-op. Time complexity: O(1)

func (*OrderedMap[K, V]) Len

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

Len returns the number of key-value pairs in the map. This leverages the built-in map's length for O(1) performance rather than counting list elements. Time complexity: O(1)

func (*OrderedMap[K, V]) Load

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

Load retrieves the value associated with a key. Returns the value and true if the key exists, or the zero value and false if the key doesn't exist. Time complexity: O(1)

func (*OrderedMap[K, V]) Range

func (om *OrderedMap[K, V]) Range(f func(key K, value V) bool)

Range calls the provided function for each key-value pair in insertion order. The iteration stops early if the function returns false. This method traverses the linked list, ensuring consistent insertion order regardless of Go's map iteration randomization. Time complexity: O(n) where n is the number of elements

func (*OrderedMap[K, V]) Store

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

Store adds or updates a key-value pair in the map. If the key already exists, its value is updated in-place without changing its position in the iteration order. If the key is new, it's appended to the end of the order. Time complexity: O(1)

type UnorderedMap

type UnorderedMap[Key comparable, Value any] struct {
	*DefaultAbstractMap[Key, Value]
	// contains filtered or unexported fields
}

func NewUnorderedMap

func NewUnorderedMap[Key comparable, Value any]() *UnorderedMap[Key, Value]

func (*UnorderedMap[Key, Value]) Delete

func (um *UnorderedMap[Key, Value]) Delete(key Key)

func (*UnorderedMap[Key, Value]) Len

func (um *UnorderedMap[Key, Value]) Len() int

func (*UnorderedMap[Key, Value]) Load

func (um *UnorderedMap[Key, Value]) Load(key Key) (value Value, ok bool)

func (*UnorderedMap[Key, Value]) Range

func (um *UnorderedMap[Key, Value]) Range(f func(key Key, value Value) bool)

func (*UnorderedMap[Key, Value]) Store

func (um *UnorderedMap[Key, Value]) Store(key Key, value Value)

Jump to

Keyboard shortcuts

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