/
itMapsLib.sol
287 lines (240 loc) · 8.85 KB
/
itMapsLib.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
pragma solidity ^0.4.8;
library itMaps {
/* itMapUintUint mapping
KeyIndex uint => Value uint
*/
struct entryUintUint {
// Equal to the index of the key of this item in keys, plus 1.
uint keyIndex;
uint value;
}
struct itMapUintUint {
mapping(uint => entryUintUint) data;
uint[] keys;
}
function insert(itMapUintUint storage self, uint key, uint value) internal returns (bool replaced) {
entryUintUint storage e = self.data[key];
e.value = value;
if (e.keyIndex > 0) {
return true;
} else {
e.keyIndex = ++self.keys.length;
self.keys[e.keyIndex - 1] = key;
return false;
}
}
function remove(itMapUintUint storage self, uint key) internal returns (bool success) {
entryUintUint storage e = self.data[key];
if (e.keyIndex == 0)
return false;
if (e.keyIndex <= self.keys.length) {
// Move an existing element into the vacated key slot.
self.data[self.keys[self.keys.length - 1]].keyIndex = e.keyIndex;
self.keys[e.keyIndex - 1] = self.keys[self.keys.length - 1];
self.keys.length -= 1;
delete self.data[key];
return true;
}
}
function destroy(itMapUintUint storage self) internal {
for (uint i; i<self.keys.length; i++) {
delete self.data[ self.keys[i]];
}
delete self.keys;
return ;
}
function contains(itMapUintUint storage self, uint key) internal constant returns (bool exists) {
return self.data[key].keyIndex > 0;
}
function size(itMapUintUint storage self) internal constant returns (uint) {
return self.keys.length;
}
function get(itMapUintUint storage self, uint key) internal constant returns (uint) {
return self.data[key].value;
}
function getKey(itMapUintUint storage self, uint idx) internal constant returns (uint) {
/* Decrepated, use getKeyByIndex. This kept for backward compatilibity */
return self.keys[idx];
}
function getKeyByIndex(itMapUintUint storage self, uint idx) internal constant returns (uint) {
/* Same as decrepated getKey. getKeyByIndex was introduced to be less ambiguous */
return self.keys[idx];
}
function getValueByIndex(itMapUintUint storage self, uint idx) internal constant returns (uint) {
return self.data[self.keys[idx]].value;
}
/* itMapAddressUint
address => Uint
*/
struct entryAddressUint {
// Equal to the index of the key of this item in keys, plus 1.
uint keyIndex;
uint value;
}
struct itMapAddressUint {
mapping(address => entryAddressUint) data;
address[] keys;
}
function insert(itMapAddressUint storage self, address key, uint value) internal returns (bool replaced) {
entryAddressUint storage e = self.data[key];
e.value = value;
if (e.keyIndex > 0) {
return true;
} else {
e.keyIndex = ++self.keys.length;
self.keys[e.keyIndex - 1] = key;
return false;
}
}
function remove(itMapAddressUint storage self, address key) internal returns (bool success) {
entryAddressUint storage e = self.data[key];
if (e.keyIndex == 0)
return false;
if (e.keyIndex <= self.keys.length) {
// Move an existing element into the vacated key slot.
self.data[self.keys[self.keys.length - 1]].keyIndex = e.keyIndex;
self.keys[e.keyIndex - 1] = self.keys[self.keys.length - 1];
self.keys.length -= 1;
delete self.data[key];
return true;
}
}
function destroy(itMapAddressUint storage self) internal {
for (uint i; i<self.keys.length; i++) {
delete self.data[ self.keys[i]];
}
delete self.keys;
return ;
}
function contains(itMapAddressUint storage self, address key) internal constant returns (bool exists) {
return self.data[key].keyIndex > 0;
}
function size(itMapAddressUint storage self) internal constant returns (uint) {
return self.keys.length;
}
function get(itMapAddressUint storage self, address key) internal constant returns (uint) {
return self.data[key].value;
}
function getKeyByIndex(itMapAddressUint storage self, uint idx) internal constant returns (address) {
return self.keys[idx];
}
function getValueByIndex(itMapAddressUint storage self, uint idx) internal constant returns (uint) {
return self.data[self.keys[idx]].value;
}
/* itMapUintAddress
Uint => Address
*/
struct entryUintAddress {
// Equal to the index of the key of this item in keys, plus 1.
uint keyIndex;
address value;
}
struct itMapUintAddress {
mapping(uint => entryUintAddress) data;
uint[] keys;
}
function insert(itMapUintAddress storage self, uint key, address value) internal returns (bool replaced) {
entryUintAddress storage e = self.data[key];
e.value = value;
if (e.keyIndex > 0) {
return true;
} else {
e.keyIndex = ++self.keys.length;
self.keys[e.keyIndex - 1] = key;
return false;
}
}
function remove(itMapUintAddress storage self, uint key) internal returns (bool success) {
entryUintAddress storage e = self.data[key];
if (e.keyIndex == 0)
return false;
if (e.keyIndex <= self.keys.length) {
// Move an existing element into the vacated key slot.
self.data[self.keys[self.keys.length - 1]].keyIndex = e.keyIndex;
self.keys[e.keyIndex - 1] = self.keys[self.keys.length - 1];
self.keys.length -= 1;
delete self.data[key];
return true;
}
}
function destroy(itMapUintAddress storage self) internal {
for (uint i; i<self.keys.length; i++) {
delete self.data[ self.keys[i]];
}
delete self.keys;
return ;
}
function contains(itMapUintAddress storage self, uint key) internal constant returns (bool exists) {
return self.data[key].keyIndex > 0;
}
function size(itMapUintAddress storage self) internal constant returns (uint) {
return self.keys.length;
}
function get(itMapUintAddress storage self, uint key) internal constant returns (address) {
return self.data[key].value;
}
function getKeyByIndex(itMapUintAddress storage self, uint idx) internal constant returns (uint) {
return self.keys[idx];
}
function getValueByIndex(itMapUintAddress storage self, uint idx) internal constant returns (address) {
return self.data[self.keys[idx]].value;
}
/* itMapUintBool
Uint => Bool
*/
struct entryUintBool {
// Equal to the index of the key of this item in keys, plus 1.
uint keyIndex;
bool value;
}
struct itMapUintBool {
mapping(uint => entryUintBool) data;
uint[] keys;
}
function insert(itMapUintBool storage self, uint key, bool value) internal returns (bool replaced) {
entryUintBool storage e = self.data[key];
e.value = value;
if (e.keyIndex > 0) {
return true;
} else {
e.keyIndex = ++self.keys.length;
self.keys[e.keyIndex - 1] = key;
return false;
}
}
function remove(itMapUintBool storage self, uint key) internal returns (bool success) {
entryUintBool storage e = self.data[key];
if (e.keyIndex == 0)
return false;
if (e.keyIndex <= self.keys.length) {
// Move an existing element into the vacated key slot.
self.data[self.keys[self.keys.length - 1]].keyIndex = e.keyIndex;
self.keys[e.keyIndex - 1] = self.keys[self.keys.length - 1];
self.keys.length -= 1;
delete self.data[key];
return true;
}
}
function destroy(itMapUintBool storage self) internal {
for (uint i; i<self.keys.length; i++) {
delete self.data[ self.keys[i]];
}
delete self.keys;
return ;
}
function contains(itMapUintBool storage self, uint key) internal constant returns (bool exists) {
return self.data[key].keyIndex > 0;
}
function size(itMapUintBool storage self) internal constant returns (uint) {
return self.keys.length;
}
function get(itMapUintBool storage self, uint key) internal constant returns (bool) {
return self.data[key].value;
}
function getKeyByIndex(itMapUintBool storage self, uint idx) internal constant returns (uint) {
return self.keys[idx];
}
function getValueByIndex(itMapUintBool storage self, uint idx) internal constant returns (bool) {
return self.data[self.keys[idx]].value;
}
}