-
Notifications
You must be signed in to change notification settings - Fork 8
/
lru_shard_list.go
61 lines (47 loc) · 1.66 KB
/
lru_shard_list.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
// Copyright 2023-2024 Phus Lu. All rights reserved.
package lru
import (
"unsafe"
)
func (s *lrushard[K, V]) listInit(size uint32) {
size += 1
if len(s.list) == 0 {
s.list = make([]lrunode[K, V], size)
}
for i := uint32(0); i < size; i++ {
s.list[i].next = (i + 1) % size
s.list[i].prev = (i + size - 1) % size
}
}
func (s *lrushard[K, V]) listBack() uint32 {
return s.list[0].prev
}
func (s *lrushard[K, V]) listMoveToFront(i uint32) {
root := &s.list[0]
if root.next == i {
return
}
base := unsafe.Pointer(root)
nodei := (*lrunode[K, V])(unsafe.Add(base, uintptr(i)*unsafe.Sizeof(s.list[0])))
((*lrunode[K, V])(unsafe.Add(base, uintptr(nodei.prev)*unsafe.Sizeof(s.list[0])))).next = nodei.next
((*lrunode[K, V])(unsafe.Add(base, uintptr(nodei.next)*unsafe.Sizeof(s.list[0])))).prev = nodei.prev
nodei.prev = 0
nodei.next = root.next
root.next = i
((*lrunode[K, V])(unsafe.Add(base, uintptr(nodei.next)*unsafe.Sizeof(s.list[0])))).prev = i
}
func (s *lrushard[K, V]) listMoveToBack(i uint32) {
j := s.list[0].prev
if i == j {
return
}
base := unsafe.Pointer(&s.list[0])
nodei := (*lrunode[K, V])(unsafe.Add(base, uintptr(i)*unsafe.Sizeof(s.list[0])))
at := (*lrunode[K, V])(unsafe.Add(base, uintptr(j)*unsafe.Sizeof(s.list[0])))
((*lrunode[K, V])(unsafe.Add(base, uintptr(nodei.prev)*unsafe.Sizeof(s.list[0])))).next = nodei.next
((*lrunode[K, V])(unsafe.Add(base, uintptr(nodei.next)*unsafe.Sizeof(s.list[0])))).prev = nodei.prev
nodei.prev = j
nodei.next = at.next
((*lrunode[K, V])(unsafe.Add(base, uintptr(j)*unsafe.Sizeof(s.list[0])))).next = i
((*lrunode[K, V])(unsafe.Add(base, uintptr(nodei.next)*unsafe.Sizeof(s.list[0])))).prev = i
}