-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
160 lines (136 loc) · 2.98 KB
/
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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package fastcache
import "unsafe"
var (
sizeOfLRUNode = unsafe.Sizeof(listNode{})
sizeOfLRU = unsafe.Sizeof(list{})
)
// listNode of list
type listNode struct {
prev uint64 // prev listNode
next uint64 // next listNode
}
func (ln *listNode) SetPrev(base uintptr, node *listNode) {
ln.prev = uint64(uintptr(unsafe.Pointer(node)) - base)
}
func (ln *listNode) SetNext(base uintptr, node *listNode) {
ln.next = uint64(uintptr(unsafe.Pointer(node)) - base)
}
func (ln *listNode) Prev(base uintptr) *listNode {
return (*listNode)(unsafe.Pointer(base + uintptr(ln.prev)))
}
func (ln *listNode) Next(base uintptr) *listNode {
return (*listNode)(unsafe.Pointer(base + uintptr(ln.next)))
}
// Offset return *listNode offset base ptr
func (ln *listNode) Offset(base uintptr) uint64 {
return uint64(uintptr(unsafe.Pointer(ln)) - base)
}
// list double linked list
type list struct {
root listNode
len uint64
}
func (l *list) Init(base uintptr) {
l.root.SetNext(base, &l.root)
l.root.SetPrev(base, &l.root)
l.len = 0
}
func (l *list) Len() uint64 {
return l.len
}
func (l *list) Front(base uintptr) *listNode {
if l.len == 0 {
return nil
}
return l.root.Next(base)
}
func (l *list) Back(base uintptr) *listNode {
if l.len == 0 {
return nil
}
return l.root.Prev(base)
}
func (l *list) Remove(base uintptr, e *listNode) {
l.remove(base, e)
}
func (l *list) PushFront(base uintptr, e *listNode) *listNode {
return l.insert(base, e, &l.root)
}
func (l *list) PushBack(base uintptr, e *listNode) *listNode {
return l.insert(base, e, l.root.Prev(base))
}
func (l *list) MoveToFront(base uintptr, e *listNode) {
/**
if e.list != l || l.root.next == e {
return
}
l.move(e, &l.root)
*/
if l.root.next == e.Offset(base) {
return
}
l.move(base, e, &l.root)
}
func (l *list) MoveToBack(base uintptr, e *listNode) {
/**
if e.list != l || l.root.prev == e {
return
}
l.move(e, l.root.prev)
*/
if l.root.prev == e.Offset(base) {
return
}
l.move(base, e, l.root.Prev(base))
}
func (l *list) insert(base uintptr, e, at *listNode) *listNode {
/**
e.prev = at
e.next = at.next
e.prev.next = e
e.next.prev = e
e.hashMapBucket = l
l.len++
*/
e.SetPrev(base, at)
e.SetNext(base, at.Next(base))
e.Prev(base).SetNext(base, e)
e.Next(base).SetPrev(base, e)
l.len++
return e
}
func (l *list) remove(base uintptr, e *listNode) {
/**
e.prev.next = e.next
e.next.prev = e.prev
e.next = nil // avoid memory leaks
e.prev = nil // avoid memory leaks
e.list = nil
l.len--
*/
e.Prev(base).SetNext(base, e.Next(base))
e.Next(base).SetPrev(base, e.Prev(base))
l.len--
}
func (l *list) move(base uintptr, e, at *listNode) {
/**
if e == at {
return
}
e.prev.next = e.next
e.next.prev = e.prev
e.prev = at
e.next = at.next
e.prev.next = e
e.next.prev = e
*/
if e == at {
return
}
e.Prev(base).SetNext(base, e.Next(base))
e.Next(base).SetPrev(base, e.Prev(base))
e.SetPrev(base, at)
e.SetNext(base, at.Next(base))
e.Prev(base).SetNext(base, e)
e.Next(base).SetPrev(base, e)
}