-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdict.cpp
More file actions
64 lines (54 loc) · 1.55 KB
/
dict.cpp
File metadata and controls
64 lines (54 loc) · 1.55 KB
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
#include "dict.h"
#include "bool.h"
#include "int.h"
#include "assert.h"
#include "none.h"
#include "dict_iterator.h"
value make$dict() {
value ret;
ret.type = value::DICT;
ret.dictval = new dict_t();
return ret;
}
value __getitem__$dict$(value self, value k) {
assert(self.type == value::DICT);
auto it = self.dictval->find(k);
if (it == self.dictval->end()) {
throw std::runtime_error("key error");
}
return it->second;
}
value __setitem__$dict$$(value self, value k, value v) {
assert(self.type == value::DICT);
(*self.dictval)[k] = v;
return make$none();
}
value __contains__$dict$(value self, value k) {
assert(self.type == value::DICT);
return make$bool_(self.dictval->find(k) != self.dictval->end());
}
value __delitem__$dict$(value self, value k) {
assert(self.type == value::DICT);
self.dictval->erase(k);
return make$none();
}
value __bool__$dict(value self) {
assert(self.type == value::DICT);
return make$bool_(!self.dictval->empty());
}
value __len__$dict(value self) {
assert(self.type == value::DICT);
return make$int_(self.dictval->size());
}
value __eq__$dict$dict(value x, value y) {
assert(x.type == value::DICT && y.type == value::DICT);
return make$bool_(*x.dictval == *y.dictval);
}
value __ne__$dict$dict(value x, value y) {
assert(x.type == value::DICT && y.type == value::DICT);
return make$bool_(*x.dictval != *y.dictval);
}
value __iter__$dict(value self) {
assert(self.type == value::DICT);
return make$dict_iterator(self.dictval);
}