Documentation
¶
Overview ¶
Package orderedmap implements an ordered map, i.e. a map that also keeps track of the order in which keys were inserted.
All operations are constant-time.
Example ¶
package main
import (
"fmt"
"github.com/mroth/orderedmap"
)
func main() {
m := orderedmap.New[string, int]()
m.Set("foo", 1)
m.Set("bar", 2)
value, ok := m.Get("foo")
if ok {
fmt.Println(value)
}
}
Output: 1
Index ¶
- type OrderedMap
- func (m *OrderedMap[K, V]) All() iter.Seq2[K, V]
- func (m *OrderedMap[K, V]) Backward() iter.Seq2[K, V]
- func (m *OrderedMap[K, V]) Delete(key K)
- func (m *OrderedMap[K, V]) Get(key K) (value V, ok bool)
- func (m *OrderedMap[K, V]) Keys() iter.Seq[K]
- func (m *OrderedMap[K, V]) Len() int
- func (m *OrderedMap[K, V]) Set(key K, value V)
- func (m *OrderedMap[K, V]) Values() iter.Seq[V]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type OrderedMap ¶
type OrderedMap[K comparable, V any] struct { // contains filtered or unexported fields }
OrderedMap is an ordered map that holds key value pairs and is able to iterate over values based on insertion order.
func WithCapacity ¶
func WithCapacity[K comparable, V any](n int) *OrderedMap[K, V]
WithCapacity creates a new ordered map with a capacity hint of n, similar to make(map[K]V, n).
func (*OrderedMap[K, V]) All ¶ added in v0.2.0
func (m *OrderedMap[K, V]) All() iter.Seq2[K, V]
All returns an iterator over key-value pairs from m. The ordering will be oldest to newest, based on when a key was first set.
Example ¶
package main
import (
"fmt"
"github.com/mroth/orderedmap"
)
func main() {
m := orderedmap.New[string, int]()
m.Set("foo", 1)
m.Set("bar", 2)
m.Set("baz", 3)
for k, v := range m.All() {
fmt.Printf("k = %v, v = %v\n", k, v)
}
}
Output: k = foo, v = 1 k = bar, v = 2 k = baz, v = 3
func (*OrderedMap[K, V]) Backward ¶ added in v0.2.0
func (m *OrderedMap[K, V]) Backward() iter.Seq2[K, V]
Backward returns an iterator over key-value pairs from m in reverse. The ordering will be newest to oldest, based on when a key was first set.
Example ¶
package main
import (
"fmt"
"github.com/mroth/orderedmap"
)
func main() {
m := orderedmap.New[string, int]()
m.Set("foo", 1)
m.Set("bar", 2)
m.Set("baz", 3)
for k, v := range m.Backward() {
fmt.Printf("k = %v, v = %v\n", k, v)
}
}
Output: k = baz, v = 3 k = bar, v = 2 k = foo, v = 1
func (*OrderedMap[K, V]) Delete ¶
func (m *OrderedMap[K, V]) Delete(key K)
Delete deletes the value for a key.
func (*OrderedMap[K, V]) Get ¶
func (m *OrderedMap[K, V]) Get(key K) (value V, ok bool)
Get returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.
func (*OrderedMap[K, V]) Keys ¶ added in v0.2.0
func (m *OrderedMap[K, V]) Keys() iter.Seq[K]
Keys returns an iterator over keys in m. The ordering will be oldest to newest, based on when a key was first set.
Example ¶
package main
import (
"fmt"
"github.com/mroth/orderedmap"
)
func main() {
m := orderedmap.New[string, int]()
m.Set("foo", 1)
m.Set("bar", 2)
m.Set("baz", 3)
for k := range m.Keys() {
fmt.Println(k)
}
}
Output: foo bar baz
func (*OrderedMap[K, V]) Len ¶
func (m *OrderedMap[K, V]) Len() int
Len returns the length of the ordered map.
func (*OrderedMap[K, V]) Set ¶
func (m *OrderedMap[K, V]) Set(key K, value V)
Set sets the value for a key.
func (*OrderedMap[K, V]) Values ¶ added in v0.2.0
func (m *OrderedMap[K, V]) Values() iter.Seq[V]
Values returns an iterator over values in m. The ordering will be oldest to newest, based on when a key was first set.
Example ¶
package main
import (
"fmt"
"github.com/mroth/orderedmap"
)
func main() {
m := orderedmap.New[string, string]()
m.Set("foo", "uno")
m.Set("bar", "dos")
m.Set("baz", "tres")
for v := range m.Values() {
fmt.Println(v)
}
}
Output: uno dos tres