-
Notifications
You must be signed in to change notification settings - Fork 1
/
sortedMap.go
79 lines (71 loc) · 1.13 KB
/
sortedMap.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
type Node[V any] struct {
prev *Node[V]
next *Node[V]
value V
}
func (n *Node[V]) insertBefore(value V) {
newNode := Node[V]{
next: n,
prev: n.prev,
value: value,
}
if n.prev != nil {
n.prev.next = &newNode
}
n.prev = &newNode
}
func (n *Node[V]) insertAfter(value V) {
newNode := Node[V]{
next: n.next,
prev: n,
value: value,
}
if n.next != nil {
n.next.prev = &newNode
}
n.next = &newNode
}
type SortedMap[K Ordered, V any] struct {
keys *Node[K]
m map[K]V
}
func NewSortedMap[K Ordered, V any]() SortedMap[K, V] {
return SortedMap[K, V]{
m: map[K]V{},
}
}
func (s *SortedMap[K, V]) compare(t1 K, t2 K) int {
if t1 > t2 {
return 1
}
if t1 < t2 {
return -1
}
return 0
}
func (s *SortedMap[K, V]) Put(key K, value V) {
if _, ok := s.m[key]; !ok {
s.insertKey(key)
}
s.m[key] = value
}
func (s *SortedMap[K, V]) insertKey(key K) {
link := s.keys
prev := link
for link != nil {
if key < link.value {
link.insertBefore(key)
return
}
prev = link
link = link.next
}
if prev == nil {
s.keys = &Node[K]{
value: key,
}
} else {
prev.insertAfter(key)
}
}