-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.go
37 lines (31 loc) · 972 Bytes
/
dictionary.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package container
import (
"fmt"
"github.com/seekerror/stdlib/pkg/lang"
"golang.org/x/exp/constraints"
)
// Dictionary is an associative mapping, typically implemented as a hashtable or binary search tree.
type Dictionary[K, V any] interface {
// List returns an iterator over all elements in an implementation-defined order.
List() lang.Iterator[KV[K, V]]
// Find returns the value associated with the key, if present.
Find(k K) (V, bool)
// Insert sets the value of the key. Returns prior value, if present.
Insert(k K, v V) (V, bool)
// Remove removes the key. Returns removed value, if present.
Remove(k K) (V, bool)
}
// KV is a key-value pair.
type KV[K, V any] struct {
K K
V V
}
func (kv KV[K, V]) String() string {
return fmt.Sprintf("%v:%v", kv.K, kv.V)
}
// InsertAll inserts all map elements in the dictionary.
func InsertAll[K constraints.Ordered, V any](m map[K]V, dict Dictionary[K, V]) {
for k, v := range m {
dict.Insert(k, v)
}
}