sortedmap

package module
v0.0.0-...-31b1a72 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2024 License: MIT Imports: 5 Imported by: 1

README

Sorted Map

A sorted map implementation. Probably just one of the millions out there. It uses a map, an RWMutex and a sorted slice underneath. It also uses binary search for insertion and deletion of the keys to make them performant.

Examples

key1, key2, key3 := "key1", "key2", "key3"
value1, value2, value2b, value3 := 1, 2, -2, 3

sm := sortedmap.New[string, int]().
    Set(key3, value3).
    Set(key2, value2).
    Set(key2, value2b).
    Set(key1, value1)

sm.Get(key2) // -2
sm.Len() // 3
sm.Has(key2) // true
sm.HasAll(key1, key2) // true
sm.HasAll(key1, key2, "nope") // false
sm.HasAny(key1, key2) // true
sm.HasAny(key1, key2, "nope") // true
sm.Delete(key1)
sm.Values() // [1, -2, 3]

for key, value := sm.Items() {
    fmt.Println(key, value)
}
Creating a sorted map with capacity

Creating a sorted map with capacity will mimic creating a map with a capacity, making it faster to insert until the given capacity, but using more memory at creation.

key1, key2, key3 := "key1", "key2", "key3"
value1, value2, value2b, value3 := 1, 2, -2, 3

sm := sortedmap.NewWithCapacity[string, int](200).
    Set(key1, value1).
    Set(key2, value2).
    Set(key2, value2b).
    Set(key3, value3)

sm.Get(key2) // -2
sm.Len() // 3
Creating a sorted map with an initial item
key1, key2, key3 := "key1", "key2", "key3"
value1, value2, value2b, value3 := 1, 2, -2, 3

sm := sortedmap.NewFrom(key1, value1)

sm.Get(key1) // 1
sm.Len() // 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrKeyDoesNotExist = errors.New("key does not exist")

Functions

This section is empty.

Types

type SortedMap

type SortedMap[K constraints.Ordered, T any] struct {
	// contains filtered or unexported fields
}

func New

func New[K constraints.Ordered, T any]() *SortedMap[K, T]

func NewFrom

func NewFrom[K constraints.Ordered, T any](key K, value T) *SortedMap[K, T]

func NewWithCapacity

func NewWithCapacity[K constraints.Ordered, T any](capacity int) *SortedMap[K, T]

func (*SortedMap[K, T]) Delete

func (sm *SortedMap[K, T]) Delete(keys ...K) *SortedMap[K, T]

func (*SortedMap[K, T]) Get

func (sm *SortedMap[K, T]) Get(key K) (T, error)

func (*SortedMap[K, T]) Has

func (sm *SortedMap[K, T]) Has(key K) bool

func (*SortedMap[K, T]) HasAll

func (sm *SortedMap[K, T]) HasAll(keys ...K) bool

func (*SortedMap[K, T]) HasAny

func (sm *SortedMap[K, T]) HasAny(keys ...K) bool

func (*SortedMap[K, T]) Items

func (sm *SortedMap[K, T]) Items() iter.Seq2[K, T]

func (*SortedMap[K, T]) Keys

func (sm *SortedMap[K, T]) Keys() []K

func (*SortedMap[K, T]) Len

func (sm *SortedMap[K, T]) Len() int

func (*SortedMap[K, T]) MustGet

func (sm *SortedMap[K, T]) MustGet(key K) T

func (*SortedMap[K, T]) Set

func (sm *SortedMap[K, T]) Set(key K, value T) *SortedMap[K, T]

func (*SortedMap[K, T]) Values

func (sm *SortedMap[K, T]) Values() []T

Jump to

Keyboard shortcuts

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