-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
simple_map.move
344 lines (301 loc) · 9.68 KB
/
simple_map.move
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/// This module provides a solution for sorted maps, that is it has the properties that
/// 1) Keys point to Values
/// 2) Each Key must be unique
/// 3) A Key can be found within O(N) time
/// 4) The keys are unsorted.
/// 5) Adds and removals take O(N) time
module aptos_std::simple_map {
use std::error;
use std::option;
use std::vector;
/// Map key already exists
const EKEY_ALREADY_EXISTS: u64 = 1;
/// Map key is not found
const EKEY_NOT_FOUND: u64 = 2;
struct SimpleMap<Key, Value> has copy, drop, store {
data: vector<Element<Key, Value>>,
}
struct Element<Key, Value> has copy, drop, store {
key: Key,
value: Value,
}
public fun length<Key: store, Value: store>(map: &SimpleMap<Key, Value>): u64 {
vector::length(&map.data)
}
public fun create<Key: store, Value: store>(): SimpleMap<Key, Value> {
SimpleMap {
data: vector::empty(),
}
}
public fun borrow<Key: store, Value: store>(
map: &SimpleMap<Key, Value>,
key: &Key,
): &Value {
let maybe_idx = find(map, key);
assert!(option::is_some(&maybe_idx), error::invalid_argument(EKEY_NOT_FOUND));
let idx = option::extract(&mut maybe_idx);
&vector::borrow(&map.data, idx).value
}
public fun borrow_mut<Key: store, Value: store>(
map: &mut SimpleMap<Key, Value>,
key: &Key,
): &mut Value {
let maybe_idx = find(map, key);
assert!(option::is_some(&maybe_idx), error::invalid_argument(EKEY_NOT_FOUND));
let idx = option::extract(&mut maybe_idx);
&mut vector::borrow_mut(&mut map.data, idx).value
}
public fun contains_key<Key: store, Value: store>(
map: &SimpleMap<Key, Value>,
key: &Key,
): bool {
let maybe_idx = find(map, key);
option::is_some(&maybe_idx)
}
public fun destroy_empty<Key: store, Value: store>(map: SimpleMap<Key, Value>) {
let SimpleMap { data } = map;
vector::destroy_empty(data);
}
public fun add<Key: store, Value: store>(
map: &mut SimpleMap<Key, Value>,
key: Key,
value: Value,
) {
let maybe_idx = find(map, &key);
assert!(option::is_none(&maybe_idx), error::invalid_argument(EKEY_ALREADY_EXISTS));
vector::push_back(&mut map.data, Element { key, value });
}
public fun remove<Key: store, Value: store>(
map: &mut SimpleMap<Key, Value>,
key: &Key,
): (Key, Value) {
let maybe_idx = find(map, key);
assert!(option::is_some(&maybe_idx), error::invalid_argument(EKEY_NOT_FOUND));
let placement = option::extract(&mut maybe_idx);
let Element { key, value } = vector::swap_remove(&mut map.data, placement);
(key, value)
}
fun find<Key: store, Value: store>(
map: &SimpleMap<Key, Value>,
key: &Key,
): option::Option<u64>{
let leng = vector::length(&map.data);
let i = 0;
while (i < leng) {
let element = vector::borrow(&map.data, i);
if (&element.key == key){
return option::some(i)
};
i = i + 1;
};
option::none<u64>()
}
/// Apply the function to each key-value pair in the map, consuming it.
public inline fun for_each<Key, Value>(map: SimpleMap<Key, Value>, f: |Key, Value|) {
let SimpleMap {data} = map;
vector::for_each(data, |elem| {
let Element {key, value} = elem;
f(key, value)
})
}
/// Apply the function to a reference of each key-value pair in the map.
public inline fun for_each_ref<Key, Value>(map: &SimpleMap<Key, Value>, f: |&Key, &Value|) {
vector::for_each_ref(&map.data, |elem| {
let e : &Element<Key, Value> = elem;
f(&e.key, &e.value)
})
}
/// Apply the function to a reference of each key-value pair in the map.
public inline fun for_each_mut<Key, Value>(map: &mut SimpleMap<Key, Value>, f: |&Key, &mut Value|) {
vector::for_each_mut(&mut map.data, |elem| {
let e : &mut Element<Key, Value> = elem;
f(&mut e.key, &mut e.value)
})
}
/// Fold the function over the key-value pairs of the map.
public inline fun fold<Accumulator, Key, Value>(
map: SimpleMap<Key, Value>,
init: Accumulator,
f: |Accumulator,Key,Value|Accumulator
): Accumulator {
for_each(map, |key, value| init = f(init, key, value));
init
}
/// Map the function over the key-value pairs of the map.
public inline fun map<Key, Value1, Value2>(
map: SimpleMap<Key, Value1>,
f: |Value1|Value2
): SimpleMap<Key, Value2> {
let data = vector::empty();
for_each(map, |key, value| vector::push_back(&mut data, Element {key, value: f(value)}));
SimpleMap {data}
}
/// Map the function over the key-value pairs of the map without modifying it.
public inline fun map_ref<Key: copy, Value1, Value2>(
map: &SimpleMap<Key, Value1>,
f: |&Value1|Value2
): SimpleMap<Key, Value2> {
let data = vector::empty();
for_each_ref(map, |key, value| {
let key = *key;
vector::push_back(&mut data, Element {key, value: f(value)});
});
SimpleMap {data}
}
/// Filter entries in the map.
public inline fun filter<Key:drop, Value:drop>(
map: SimpleMap<Key, Value>,
p: |&Value|bool
): SimpleMap<Key, Value> {
let data = vector::empty();
for_each(map, |key, value| {
if (p(&value)) {
vector::push_back(&mut data, Element {key, value});
}
});
SimpleMap {data}
}
/// Return true if any key-value pair in the map satisfies the predicate.
public inline fun any<Key, Value>(
map: &SimpleMap<Key, Value>,
p: |&Key, &Value|bool
): bool {
let SimpleMap {data} = map;
let result = false;
let i = 0;
while (i < vector::length(data)) {
let Element {key, value} = vector::borrow(data, i);
result = p(key, value);
if (result) {
break
};
i = i + 1
};
result
}
/// Return true if all key-value pairs in the map satisfies the predicate.
public inline fun all<Key, Value>(
map: &SimpleMap<Key, Value>,
p: |&Key, &Value|bool
): bool {
!any(map, |k, v| !p(k, v))
}
#[test]
public fun add_remove_many() {
let map = create<u64, u64>();
assert!(length(&map) == 0, 0);
assert!(!contains_key(&map, &3), 1);
add(&mut map, 3, 1);
assert!(length(&map) == 1, 2);
assert!(contains_key(&map, &3), 3);
assert!(borrow(&map, &3) == &1, 4);
*borrow_mut(&mut map, &3) = 2;
assert!(borrow(&map, &3) == &2, 5);
assert!(!contains_key(&map, &2), 6);
add(&mut map, 2, 5);
assert!(length(&map) == 2, 7);
assert!(contains_key(&map, &2), 8);
assert!(borrow(&map, &2) == &5, 9);
*borrow_mut(&mut map, &2) = 9;
assert!(borrow(&map, &2) == &9, 10);
remove(&mut map, &2);
assert!(length(&map) == 1, 11);
assert!(!contains_key(&map, &2), 12);
assert!(borrow(&map, &3) == &2, 13);
remove(&mut map, &3);
assert!(length(&map) == 0, 14);
assert!(!contains_key(&map, &3), 15);
destroy_empty(map);
}
#[test]
#[expected_failure]
public fun add_twice() {
let map = create<u64, u64>();
add(&mut map, 3, 1);
add(&mut map, 3, 1);
remove(&mut map, &3);
destroy_empty(map);
}
#[test]
#[expected_failure]
public fun remove_twice() {
let map = create<u64, u64>();
add(&mut map, 3, 1);
remove(&mut map, &3);
remove(&mut map, &3);
destroy_empty(map);
}
#[test_only]
fun make(k1: u64, v1: u64, k2: u64, v2: u64): SimpleMap<u64, u64> {
let m = create();
add(&mut m, k1, v1);
add(&mut m, k2, v2);
m
}
#[test]
fun test_for_each() {
let m = make(1, 4, 2, 5);
let s = 0;
for_each(m, |x, y| {
s = s + x + y;
});
assert!(s == 12, 0)
}
#[test]
fun test_for_each_ref() {
let m = make(1, 4, 2, 5);
let s = 0;
for_each_ref(&m, |x, y| {
s = s + *x + *y;
});
assert!(s == 12, 0)
}
#[test]
fun test_for_each_mut() {
let m = make(1, 4, 2, 5);
for_each_mut(&mut m, |_key, val| {
let val : &mut u64 = val;
*val = *val + 1
});
assert!(*borrow(&m, &1) == 5, 1)
}
#[test]
fun test_fold() {
let m = make(1, 4, 2, 5);
let r = fold(m, 0, |accu, key, val| {
accu + key + val
});
assert!(r == 12, 0);
}
#[test]
fun test_map() {
let m = make(1, 4, 2, 5);
let r = map(m, |val| val + 1);
assert!(*borrow(&r, &1) == 5, 1)
}
#[test]
fun test_map_ref() {
let m = make(1, 4, 2, 5);
let r = map_ref(&m, |val| *val + 1);
assert!(*borrow(&r, &1) == 5, 1)
}
#[test]
fun test_filter() {
let m = make(1, 4, 2, 5);
let r = filter(m, |val| *val > 4);
assert!(length(&r) == 1, 1);
assert!(*borrow(&r, &2) == 5, 1)
}
#[test]
fun test_any() {
let m = make(1, 4, 2, 5);
let r = any(&m, |_k, v| *v > 4);
assert!(r, 1)
}
#[test]
fun test_all() {
let m = make(1, 4, 2, 5);
let r = all(&m, |_k, v| *v > 4);
assert!(!r, 1)
}
}