-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcp-006-xor-linked-list.linq
189 lines (138 loc) · 3.76 KB
/
dcp-006-xor-linked-list.linq
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<Query Kind="Program">
<Namespace>System.Runtime.InteropServices</Namespace>
</Query>
// This problem was asked by Google.
//
// An XOR linked list is a more memory efficient doubly linked list.
// Instead of each node holding next and prev fields, it holds a
// field named both, which is an XOR of the next node and the previous node.
// Implement an XOR linked list; it has an add(element) which adds the
// element to the end, and a get(index) which returns the node at index.
//
// If using a language that has no pointers (such as Python),
// you can assume you have access to get_pointer and dereference_pointer
// functions that converts between nodes and memory addresses.
void Main()
{
// there is no way to implement this fully in C#
// pinned pointers can not point to reference types as they are
// GC managed and subject for move
var list = new XorLinkedList<int>();
list.Add(1);
Memory.Dump();
list.Dump();
list.Add(2);
Memory.Dump();
list.Dump();
list.Add(3);
Memory.Dump();
list.Dump();
list.Add(4);
Memory.Dump();
list.Dump();
list.Get(0).Dump();
list.Get(1).Dump();
list.Get(2).Dump();
list.Get(3).Dump();
}
public class XorLinkedList<T>
{
private class Node
{
public T Value;
public int XorPointer;
}
private int _headPointer;
private int _tailPointer;
public void Dump()
{
Util.Metatext("*** Dumpling list ***").Dump();
Node current = Memory.Dereference<Node>(_headPointer);
int currentAddress = _headPointer;
int prevAddress = 0;
while (current != null)
{
Util.Metatext($"\taddress: {currentAddress}, val: {current.Value}, xor: {current.XorPointer}").Dump();
if (current.XorPointer == 0)
break;
// iterate step forwrad
int nextAddress = current.XorPointer ^ prevAddress;
prevAddress = currentAddress;
if (nextAddress == 0)
break;
current = Memory.Dereference<Node>(nextAddress);
currentAddress = nextAddress;
}
"".Dump();
}
public T Get(int index)
{
Node current = Memory.Dereference<Node>(_headPointer);
int currentAddress = _headPointer;
int prevAddress = 0;
while (index > 0)
{
// iterate step forwrad
int nextAddress = current.XorPointer ^ prevAddress;
if (nextAddress == 0)
return default(T);
prevAddress = currentAddress;
current = Memory.Dereference<Node>(nextAddress);
currentAddress = nextAddress;
index -= 1;
}
return current.Value;
}
public void Add(T element)
{
Node newNode = new Node()
{
Value = element,
};
if (_tailPointer == 0)
{
// list is empty, add the fist node
int address = Memory.GetAdderss(newNode);
_headPointer = address;
_tailPointer = address;
return;
}
// okay list is not empty
newNode.XorPointer = _tailPointer; // points to the preivous
Node oldTail = Memory.Dereference<Node>(_tailPointer);
_tailPointer = Memory.GetAdderss(newNode);
oldTail.XorPointer ^= _tailPointer;
}
}
public static class Memory
{
private static Dictionary<int, object> _memory = new Dictionary<int, object>();
private static Random _rnd = new Random();
public static int GetAdderss(object o)
{
// see if object in the map
if (_memory.ContainsValue(o))
return _memory.First(x => x.Value == o).Key;
int address = 0;
do
{
address = _rnd.Next();
}
while (_memory.ContainsKey(address));
_memory[address] = o;
return address;
}
public static T Dereference<T>(int address)
{
if (!_memory.ContainsKey(address))
throw new Exception($"SegFault! nothing at {address}");
return (T)_memory[address];
}
public static void Dump()
{
Util.Metatext("*** Dumpling memory ***").Dump();
foreach (var item in _memory)
Util.Metatext($"{item.Key,16}: {item.Value}").Dump();
"".Dump();
}
}