-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLibCLL.sol
230 lines (194 loc) · 6.32 KB
/
LibCLL.sol
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
file: LibCLL.sol
ver: 0.4.1
updated:18-Dec-2017
author: Darryl Morris
email: o0ragman0o AT gmail.com
A Solidity library for implementing a data indexing regime using
a circular linked list.
This library provisions lookup, navigation and key/index storage
functionality which can be used in conjunction with an array or mapping.
NOTICE: This library uses internal functions only and so cannot be compiled
and deployed independently from its calling contract.
This software 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 MIT Licence for further details.
<https://opensource.org/licenses/MIT>.
Release Notes:
* Changed to MIT licensing
* minimum Solidity version 0.4.17
* using `view` modifier in place of `constant`
* VERSION type changed from `string` to `bytes32`
*/
pragma solidity ^0.4.17;
// LibCLL using `uint` keys
library LibCLLu {
bytes32 constant public VERSION = "LibCLLu 0.4.1";
uint constant NULL = 0;
uint constant HEAD = 0;
bool constant PREV = false;
bool constant NEXT = true;
struct CLL{
mapping (uint => mapping (bool => uint)) cll;
}
// n: node id d: direction r: return node id
// Return existential state of a list.
function exists(CLL storage self)
internal view returns (bool)
{
if (self.cll[HEAD][PREV] != HEAD || self.cll[HEAD][NEXT] != HEAD)
return true;
}
// Returns the number of elements in the list
function sizeOf(CLL storage self)
internal view returns (uint r)
{
uint i = step(self, HEAD, NEXT);
while (i != HEAD) {
i = step(self, i, NEXT);
r++;
}
return;
}
// Returns the links of a node as and array
function getNode(CLL storage self, uint n)
internal view returns (uint[2])
{
return [self.cll[n][PREV], self.cll[n][NEXT]];
}
// Returns the link of a node `n` in direction `d`.
function step(CLL storage self, uint n, bool d)
internal view returns (uint)
{
return self.cll[n][d];
}
// Can be used before `insert` to build an ordered list
// `a` an existing node to search from, e.g. HEAD.
// `b` value to seek
// `r` first node beyond `b` in direction `d`
function seek(CLL storage self, uint a, uint b, bool d)
internal view returns (uint r)
{
r = step(self, a, d);
while ((b!=r) && ((b < r) != d)) r = self.cll[r][d];
return;
}
// Creates a bidirectional link between two nodes on direction `d`
function stitch(CLL storage self, uint a, uint b, bool d) internal {
self.cll[b][!d] = a;
self.cll[a][d] = b;
}
// Insert node `b` beside existing node `a` in direction `d`.
function insert (CLL storage self, uint a, uint b, bool d) internal {
uint c = self.cll[a][d];
stitch (self, a, b, d);
stitch (self, b, c, d);
}
// Remove node
function remove(CLL storage self, uint n) internal returns (uint) {
if (n == NULL) return;
stitch(self, self.cll[n][PREV], self.cll[n][NEXT], NEXT);
delete self.cll[n][PREV];
delete self.cll[n][NEXT];
return n;
}
// Push a new node before or after the head
function push(CLL storage self, uint n, bool d) internal {
insert(self, HEAD, n, d);
}
// Pop a new node from before or after the head
function pop(CLL storage self, bool d) internal returns (uint) {
return remove(self, step(self, HEAD, d));
}
}
// LibCLL using `int` keys
library LibCLLi {
bytes32 constant public VERSION = "LibCLLi 0.4.1";
int constant NULL = 0;
int constant HEAD = 0;
bool constant PREV = false;
bool constant NEXT = true;
struct CLL{
mapping (int => mapping (bool => int)) cll;
}
// n: node id d: direction r: return node id
// Return existential state of a list.
function exists(CLL storage self)
internal view returns (bool)
{
if (self.cll[HEAD][PREV] != HEAD || self.cll[HEAD][NEXT] != HEAD)
return true;
}
// Returns the number of elements in the list
function sizeOf(CLL storage self)
internal view returns (uint r)
{
int i = step(self, HEAD, NEXT);
while (i != HEAD) {
i = step(self, i, NEXT);
r++;
}
return;
}
// Returns the links of a node as and array
function getNode(CLL storage self, int n)
internal view returns (int[2])
{
return [self.cll[n][PREV], self.cll[n][NEXT]];
}
// Returns the link of a node `n` in direction `d`.
function step(CLL storage self, int n, bool d)
internal view returns (int)
{
return self.cll[n][d];
}
// Can be used before `insert` to build an ordered list
// `a` an existing node to search from, e.g. HEAD.
// `b` value to seek
// `r` first node beyond `b` in direction `d`
function seek(CLL storage self, int a, int b, bool d)
internal view returns (int r)
{
r = step(self, a, d);
while ((b!=r) && ((b < r) != d)) r = self.cll[r][d];
return;
}
// Creates a bidirectional link between two nodes on direction `d`
function stitch(CLL storage self, int a, int b, bool d)
internal
{
self.cll[b][!d] = a;
self.cll[a][d] = b;
}
// Insert node `b` beside existing node `a` in direction `d`.
function insert (CLL storage self, int a, int b, bool d)
internal
{
int c = self.cll[a][d];
stitch (self, a, b, d);
stitch (self, b, c, d);
}
// Remove node
function remove(CLL storage self, int n)
internal returns (int)
{
if (n == NULL) return;
stitch(self, self.cll[n][PREV], self.cll[n][NEXT], NEXT);
delete self.cll[n][PREV];
delete self.cll[n][NEXT];
return n;
}
// Push a new node before or after the head
function push(CLL storage self, int n, bool d)
internal
{
insert(self, HEAD, n, d);
}
// Pop a new node from before or after the head
function pop(CLL storage self, bool d)
internal returns (int)
{
return remove(self, step(self, HEAD, d));
}
}