Documentation
¶
Index ¶
- type KV
- type SortedMap
- func (sm *SortedMap[Map, K, V]) All() iter.Seq2[K, V]
- func (sm *SortedMap[Map, K, V]) Collect() Map
- func (sm *SortedMap[Map, K, V]) CollectAll() []KV[K, V]
- func (sm *SortedMap[Map, K, V]) CollectKeys() []K
- func (sm *SortedMap[Map, K, V]) CollectValues() []V
- func (sm *SortedMap[Map, K, V]) Delete(key K) (val *V, existed bool)
- func (sm *SortedMap[Map, K, V]) Get(key K) (V, bool)
- func (sm *SortedMap[Map, K, V]) Insert(key K, val V)
- func (sm *SortedMap[Map, K, V]) Keys() iter.Seq[K]
- func (sm *SortedMap[Map, K, V]) Len() int
- func (sm *SortedMap[Map, K, V]) Values() iter.Seq[V]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type SortedMap ¶
type SortedMap[Map ~map[K]V, K comparable, V any] struct { // contains filtered or unexported fields }
SortedMap is a map-like struct that keeps sorted by key or value. It uses a heap to maintain the order.
func New ¶
func New[Map ~map[K]V, K comparable, V any](less func(i, j KV[K, V]) bool) *SortedMap[Map, K, V]
New creates a new SortedMap with `less` as the comparison function The complexity is O(1)
Example ¶
sm := New[map[string]int, string, int](func(i, j KV[string, int]) bool { return i.Key < j.Key }) sm.Insert("Alice", 30) sm.Insert("Bob", 42) for k, v := range sm.All() { fmt.Println(k, v) }
Output: Alice 30 Bob 42
func NewFromMap ¶
func NewFromMap[Map ~map[K]V, K comparable, V any](m Map, less func(i, j KV[K, V]) bool) *SortedMap[Map, K, V]
NewFromMap creates a new SortedMap with `less` as the comparison function and populates it with the contents of `m`. The complexity is O(n log n) where n = len(m).
Example ¶
sm := NewFromMap(map[string]int{ "Bob": 42, "Alice": 30, "Charlie": 25, }, func(i, j KV[string, int]) bool { return i.Key < j.Key }) for k, v := range sm.All() { fmt.Println(k, v) }
Output: Alice 30 Bob 42 Charlie 25
func (*SortedMap[Map, K, V]) All ¶
All returns a sequence of key-value pairs
Example ¶
sm := NewFromMap(map[string]int{ "Bob": 42, "Alice": 30, "Charlie": 25, }, func(i, j KV[string, int]) bool { return i.Key < j.Key }) for k, v := range sm.All() { fmt.Println(k, v) }
Output: Alice 30 Bob 42 Charlie 25
func (*SortedMap[Map, K, V]) Collect ¶
func (sm *SortedMap[Map, K, V]) Collect() Map
Collect returns a regular map with an *unordered* content off the SortedMap
Example ¶
sm := NewFromMap(map[string]int{ "Bob": 42, "Alice": 30, "Charlie": 25, }, func(i, j KV[string, int]) bool { return i.Key < j.Key }) fmt.Println(sm.Collect())
Output: map[Alice:30 Bob:42 Charlie:25]
func (*SortedMap[Map, K, V]) CollectAll ¶ added in v0.3.0
CollectAll returns a slice of key-value pairs
Example ¶
sm := NewFromMap(map[int]string{ 1: "one", 3: "three", 2: "two", 5: "five", 4: "four", }, func(i, j KV[int, string]) bool { return i.Key < j.Key }) fmt.Println(sm.CollectAll())
Output: [{1 one} {2 two} {3 three} {4 four} {5 five}]
func (*SortedMap[Map, K, V]) CollectKeys ¶ added in v0.3.0
func (sm *SortedMap[Map, K, V]) CollectKeys() []K
CollectKeys returns a slice of the map’s keys
Example ¶
sm := NewFromMap(map[string]int{ "Bob": 42, "Alice": 30, "Charlie": 25, }, func(i, j KV[string, int]) bool { return i.Key < j.Key }) fmt.Println(sm.CollectKeys())
Output: [Alice Bob Charlie]
func (*SortedMap[Map, K, V]) CollectValues ¶ added in v0.3.0
func (sm *SortedMap[Map, K, V]) CollectValues() []V
CollectValues returns a slice of the map's values
Example ¶
sm := NewFromMap(map[string]int{ "Bob": 42, "Alice": 30, "Charlie": 25, }, func(i, j KV[string, int]) bool { return i.Key < j.Key }) fmt.Println(sm.CollectValues())
Output: [30 42 25]
func (*SortedMap[Map, K, V]) Delete ¶
Delete removes the key from the map and returns the value associated with the key and a boolean indicating if the key existed in the map. The complexity is O(n) where n = len(sm.h.xs)
Example ¶
sm := NewFromMap(map[string]int{ "Bob": 42, "Alice": 30, "Charlie": 25, }, func(i, j KV[string, int]) bool { return i.Key < j.Key }) val, ok := sm.Delete("Alice") fmt.Println(ptrVal(val), ok) val, ok = sm.Delete("Alice") fmt.Println(ptrVal(val), ok)
Output: 30 true 0 false
func (*SortedMap[Map, K, V]) Get ¶
Get returns the value associated with the key and a boolean indicating if the key exists in the map The complexity is O(1)
Example ¶
sm := NewFromMap(map[string]int{ "Bob": 42, "Alice": 30, "Charlie": 25, }, func(i, j KV[string, int]) bool { return i.Key < j.Key }) val, ok := sm.Get("Alice") fmt.Println(val, ok)
Output: 30 true
func (*SortedMap[Map, K, V]) Insert ¶
func (sm *SortedMap[Map, K, V]) Insert(key K, val V)
Insert adds a key-value pair to the map. If the key already exists, the value is updated
Example ¶
sm := New[map[string]int, string, int](func(i, j KV[string, int]) bool { return i.Key < j.Key }) sm.Insert("Alice", 30) sm.Insert("Bob", 42) for k, v := range sm.All() { fmt.Println(k, v) }
Output: Alice 30 Bob 42
func (*SortedMap[Map, K, V]) Keys ¶
Keys returns a sequence of keys
Example ¶
sm := NewFromMap(map[string]int{ "Bob": 42, "Alice": 30, "Charlie": 25, }, func(i, j KV[string, int]) bool { return i.Key < j.Key }) for k := range sm.Keys() { fmt.Println(k) }
Output: Alice Bob Charlie