-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
133 lines (113 loc) · 3.69 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
Copyright (C) 2024 Carl-Philip Hänsch
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package NonLockingReadMap
import "sort"
import "unsafe"
import "sync/atomic"
import "golang.org/x/exp/constraints"
/*
this is a read optimized map.
properties of this map:
- read in O(log(N))
- read is always nonblocking
- write in O(N*log(N))
- write is optimistic, worst case is a eternal loop (with a probability of 0%)
- use this map if you read often but write very seldom
- internally, a ordered list is rebuilt each time there is a write
*/
type KeyGetter[TK constraints.Ordered] interface {
GetKey() TK
}
type NonLockingReadMap[T KeyGetter[TK], TK constraints.Ordered] struct {
p atomic.Pointer[[]*T]
}
func New[T KeyGetter[TK], TK constraints.Ordered] () NonLockingReadMap[T, TK] {
var result NonLockingReadMap[T, TK]
result.p.Store(new([]*T))
return result
}
func (m NonLockingReadMap[T, TK]) GetAll() []*T {
return *m.p.Load()
}
func (m NonLockingReadMap[T, TK]) Get(key TK) *T {
v, _, _ := m.FindItem(key)
return v
}
func (m NonLockingReadMap[T, TK]) FindItem(key TK) (*T, int, *[]*T) {
items := m.p.Load() // atomically work on the current list
var lower int = 0
var upper int = len(*items)
for {
if lower == upper {
return nil, -1, items // item does not exist
}
pivot := (lower + upper) / 2
item := (*items)[pivot]
itemkey := (*item).GetKey()
if key == itemkey {
// found item (item + pivot) --> do atomic compare and swap
return item, pivot, items // return old item
} else if key < itemkey {
upper = pivot
} else {
lower = pivot + 1
}
}
}
func (m *NonLockingReadMap[T, TK]) Set(v *T) (*T) {
restart:
item, pivot, handle := m.FindItem((*v).GetKey())
if pivot != -1 {
// replace in-place
if !atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&(*handle)[pivot])), unsafe.Pointer(item), unsafe.Pointer(v)) {
goto restart
}
// also check if our list stayed unchanged
if !m.p.CompareAndSwap(handle, handle) {
goto restart
}
}
newhandle := new([]*T) // new pointer wrapper
*newhandle = make([]*T, 0, len(*handle) + 1) // create new slice
*newhandle = append(*newhandle, (*handle)...) // copy old array
*newhandle = append(*newhandle, v) // add new item
sort.Slice(*newhandle, func (i, j int) bool { // sort
return (*(*newhandle)[i]).GetKey() < (*(*newhandle)[j]).GetKey()
})
if !m.p.CompareAndSwap(handle, newhandle) {
goto restart
}
return nil // because we inserted a new element
}
/* returns true if the key was present */
func (m *NonLockingReadMap[T, TK]) Remove(key TK) *T {
restart:
item, pivot, handle := m.FindItem(key)
if pivot == -1 {
return item // value does not exist
}
// rebuild the array without the element
newhandle := new([]*T)
*newhandle = make([]*T, 0, len(*handle) - 1)
*newhandle = append(*newhandle, (*handle)[0:pivot]...)
*newhandle = append(*newhandle, (*handle)[pivot+1:]...)
sort.Slice(*newhandle, func (i, j int) bool { // sort
return (*(*newhandle)[i]).GetKey() < (*(*newhandle)[j]).GetKey()
})
if !m.p.CompareAndSwap(handle, newhandle) {
goto restart
}
// return the removed item
return item
}