Documentation
¶
Overview ¶
Package ordered is a zero dependency, generic ordered map implementation with iterator methods.
An ordered Map maintains insertion order using a doubly-linked list, providing O(1) time complexity for reads, writes, and deletes with O(n) space complexity. The map and linked list share value state to avoid data duplication and unnecessary allocations.
Map is not safe for concurrent access.
Index ¶
- type Map
- func (m *Map[K, V]) Clear()
- func (m *Map[K, V]) Delete(key K)
- func (m *Map[K, V]) Entries() iter.Seq2[K, V]
- func (m *Map[K, V]) Get(key K) (value V, present bool)
- func (m *Map[K, V]) Insert(key K, value V)
- func (m *Map[K, V]) Keys() iter.Seq[K]
- func (m *Map[K, V]) Len() int
- func (m *Map[K, V]) Pop(key K) (value V, present bool)
- func (m *Map[K, V]) Push(key K, value V)
- func (m *Map[K, V]) Reverse() Reversed[K, V]
- func (m *Map[K, V]) Set(key K, value V)
- func (m *Map[K, V]) Values() iter.Seq[V]
- type Option
- type Reversed
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 an ordered map backed by a doubly-linked list.
func NewMap ¶
func NewMap[K comparable, V any](opts ...Option) *Map[K, V]
NewMap returns a new, empty ordered map. Options can be provided to configure the map during construction (see WithCapacity).
Example ¶
package main
import (
"fmt"
"go.treyburn.dev/ordered"
)
func main() {
m := ordered.NewMap[string, int]()
m.Set("a", 1)
m.Set("b", 2)
m.Set("c", 3)
fmt.Println(m.Len())
fmt.Println(m.Get("a"))
fmt.Println(m.Get("missing"))
}
Output: 3 1 true 0 false
Example (WithCapacity) ¶
package main
import (
"fmt"
"go.treyburn.dev/ordered"
)
func main() {
m := ordered.NewMap[string, int](ordered.WithCapacity(10))
m.Set("a", 1)
m.Set("b", 2)
m.Set("c", 3)
fmt.Println(m.Len())
for k, v := range m.Entries() {
fmt.Printf("%s:%d ", k, v)
}
fmt.Println()
}
Output: 3 a:1 b:2 c:3
func (*Map[K, V]) Delete ¶
func (m *Map[K, V]) Delete(key K)
Delete removes a given key from the map. If the key does not exist then this is a no-op. The Map order is updated to reflect the removal of this item.
func (*Map[K, V]) Entries ¶
Entries yields an iterator of key/value pair entries over the map's ordered keyspace.
Example ¶
package main
import (
"fmt"
"go.treyburn.dev/ordered"
)
func main() {
m := ordered.NewMap[string, int]()
m.Set("a", 1)
m.Set("b", 2)
m.Set("c", 3)
for k, v := range m.Entries() {
fmt.Printf("%s => %d\n", k, v)
}
}
Output: a => 1 b => 2 c => 3
func (*Map[K, V]) Get ¶
Get retrieves the value for a given key and a bool indicating whether the key was present.
func (*Map[K, V]) Insert ¶
func (m *Map[K, V]) Insert(key K, value V)
Insert stores the value for the given key as the first entry in the map. If the key is already present, its value is updated, and it is moved to the front of the order.
Example ¶
package main
import (
"fmt"
"go.treyburn.dev/ordered"
)
func main() {
m := ordered.NewMap[string, int]()
m.Set("a", 1)
m.Set("b", 2)
m.Set("c", 3)
// Insert on an existing key updates the value and moves it to the front
m.Insert("c", 30)
for k, v := range m.Entries() {
fmt.Printf("%s:%d ", k, v)
}
fmt.Println()
}
Output: c:30 a:1 b:2
Example (NewKey) ¶
package main
import (
"fmt"
"go.treyburn.dev/ordered"
)
func main() {
m := ordered.NewMap[string, int]()
m.Set("a", 1)
m.Set("b", 2)
// Insert a new key: added to the front
m.Insert("c", 3)
for k, v := range m.Entries() {
fmt.Printf("%s:%d ", k, v)
}
fmt.Println()
}
Output: c:3 a:1 b:2
func (*Map[K, V]) Pop ¶
Pop returns the value for a given key and a bool indicating whether the key was present. Additionally, the key is removed from the map and the order updated to reflect this.
Example ¶
package main
import (
"fmt"
"go.treyburn.dev/ordered"
)
func main() {
m := ordered.NewMap[string, int]()
m.Set("a", 1)
m.Set("b", 2)
m.Set("c", 3)
v, ok := m.Pop("b")
fmt.Println(v, ok)
v, ok = m.Pop("missing")
fmt.Println(v, ok)
for k, v := range m.Entries() {
fmt.Printf("%s:%d ", k, v)
}
fmt.Println()
}
Output: 2 true 0 false a:1 c:3
func (*Map[K, V]) Push ¶
func (m *Map[K, V]) Push(key K, value V)
Push stores the value for the given key as the last entry in the map. If the key already exists, its value is updated, and it is moved to the back of the order.
Example ¶
package main
import (
"fmt"
"go.treyburn.dev/ordered"
)
func main() {
m := ordered.NewMap[string, int]()
m.Set("a", 1)
m.Set("b", 2)
m.Set("c", 3)
// Push on an existing key updates the value and moves it to the back
m.Push("a", 10)
for k, v := range m.Entries() {
fmt.Printf("%s:%d ", k, v)
}
fmt.Println()
}
Output: b:2 c:3 a:10
func (*Map[K, V]) Reverse ¶
Reverse returns a Reversed view that yields iterators in reverse order over the Map keyspace.
Example ¶
package main
import (
"fmt"
"go.treyburn.dev/ordered"
)
func main() {
m := ordered.NewMap[string, int]()
m.Set("a", 1)
m.Set("b", 2)
m.Set("c", 3)
for k, v := range m.Reverse().Entries() {
fmt.Printf("%s => %d\n", k, v)
}
}
Output: c => 3 b => 2 a => 1
func (*Map[K, V]) Set ¶
func (m *Map[K, V]) Set(key K, value V)
Set stores a value for a given key. If the key already exists, then the associated value is updated. If the key is new to the map then it is added as the latest item in order.
Example ¶
package main
import (
"fmt"
"go.treyburn.dev/ordered"
)
func main() {
m := ordered.NewMap[string, int]()
m.Set("a", 1)
m.Set("b", 2)
m.Set("c", 3)
// Set on an existing key updates the value but preserves order
m.Set("a", 10)
for k, v := range m.Entries() {
fmt.Printf("%s:%d ", k, v)
}
fmt.Println()
}
Output: a:10 b:2 c:3
type Option ¶
type Option func(*config)
Option allows for additional configurations to be set during construction of the Map.
func WithCapacity ¶
WithCapacity specifies an initial capacity for the backing store of the Map during construction. If the given capacity is less than or equal to 0, then it will be silently ignored.
type Reversed ¶
type Reversed[K comparable, V any] interface { // Keys yields a reverse iterator of keys over the map's ordered keyspace. Keys() iter.Seq[K] // Values yields a reverse iterator of values over the map's ordered keyspace. Values() iter.Seq[V] // Entries yields a reverse iterator of key/value pair entries over the map's ordered keyspace. Entries() iter.Seq2[K, V] // contains filtered or unexported methods }
Reversed is an interface that provides reverse iteration over an ordered keyspace.