-
Notifications
You must be signed in to change notification settings - Fork 31
/
XorLinkedList.go
164 lines (142 loc) · 3.17 KB
/
XorLinkedList.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
161
162
163
164
// 异或链表,利用计算机的的位异或操作(⊕),来降低双向链表的存储需求。
// https://zh.wikipedia.org/wiki/%E5%BC%82%E6%88%96%E9%93%BE%E8%A1%A8
// https://www.cnblogs.com/lovemdx/p/3257334.html
// API:
// - NewXorLinkedList() *XorLinkedList
// - Append(value X)
// - AppendLeft(value X)
// - Pop()
// - PopLeft()
// - Front() X
// - Back() X
// - Empty() bool
// - Reverse()
// - Concat(left, right *XorLinkedList) *XorLinkedList
// - Len() int
package main
import (
"fmt"
"time"
"unsafe"
)
func main() {
queue := NewXorLinkedList()
queue.Append(1)
queue.Append(2)
queue.Append(2)
queue.Append(2)
queue.Append(10)
fmt.Println(queue.Front(), queue.Back())
queue.Reverse()
fmt.Println(queue.Front(), queue.Back())
queue2 := NewXorLinkedList()
queue2.Append(3)
queue2.Append(4)
q3 := Concat(queue, queue2)
fmt.Println(q3.Front(), q3.Back())
Q := NewXorLinkedList()
time1 := time.Now()
for i := 0; i < 1e6; i++ {
Q.Append(i)
}
fmt.Println(time.Since(time1))
}
type X = int
type XorLinkedList struct {
front *xorNode
back *xorNode
size int
}
func NewXorLinkedList() *XorLinkedList {
return &XorLinkedList{}
}
// 合并两个链表, 返回合并后的链表,原链表会被破坏.
func Concat(left, right *XorLinkedList) *XorLinkedList {
if left.Empty() {
return right
}
if right.Empty() {
return left
}
left.back.link ^= ptoi(right.front)
right.front.link ^= ptoi(left.back)
res := &XorLinkedList{}
res.front = left.front
res.back = right.back
res.size = left.size + right.size
left.front = nil
left.back = nil
right.front = nil
right.back = nil
return res
}
func (list *XorLinkedList) Append(value X) {
if list.Empty() {
next := newXorNode(value, 0)
list.front = next
list.back = next
} else {
next := newXorNode(value, ptoi(list.back))
list.back.link ^= ptoi(next)
list.back = next
}
list.size++
}
func (list *XorLinkedList) AppendLeft(value X) {
list.Reverse()
list.Append(value)
list.Reverse()
list.size++
}
func (list *XorLinkedList) Pop() (res X) {
if list.Empty() {
panic("empty list")
}
res = list.back.value
if list.front == list.back {
list.front = nil
list.back = nil
} else {
next := itop(list.back.link)
next.link ^= ptoi(list.back)
list.back = next
}
list.size--
return
}
func (list *XorLinkedList) PopLeft() (res X) {
res = list.front.value
list.Reverse()
list.Pop()
list.Reverse()
list.size--
return
}
func (list *XorLinkedList) Empty() bool {
return list.front == nil
}
func (list *XorLinkedList) Reverse() {
list.front, list.back = list.back, list.front
}
func (list *XorLinkedList) Front() X {
return list.front.value
}
func (list *XorLinkedList) Back() X {
return list.back.value
}
func (list *XorLinkedList) Len() int {
return list.size
}
type xorNode struct {
value X
link uintptr
}
func newXorNode(value X, link uintptr) *xorNode {
return &xorNode{value, link}
}
func ptoi(ptr *xorNode) uintptr {
return uintptr(unsafe.Pointer(ptr))
}
func itop(id uintptr) *xorNode {
return (*xorNode)(unsafe.Pointer(id))
}