-
Notifications
You must be signed in to change notification settings - Fork 2
/
set.cpp
92 lines (75 loc) · 2.19 KB
/
set.cpp
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
#include "set.h"
#include "bool.h"
#include "int.h"
#include "assert.h"
#include "none.h"
#include "set_iterator.h"
value make$set() {
value ret;
ret.type = value::SET;
ret.setval = new set_t();
return ret;
}
value add$set$(value self, value v) {
assert(self.type == value::SET);
self.setval->insert(v);
return make$set();
}
value remove$set$(value self, value v) {
assert(self.type == value::SET);
auto it = self.setval->find(v);
if (it == self.setval->end())
throw std::runtime_error("key error");
self.setval->erase(it);
return make$set();
}
value __contains__$set$(value self, value v) {
assert(self.type == value::SET);
return make$int_(self.setval->find(v) != self.setval->end());
}
value __bool__$set(value self) {
assert(self.type == value::SET);
return make$int_(!self.setval->empty());
}
value __len__$set(value self) {
assert(self.type == value::SET);
return make$int_(self.setval->size());
}
value __eq__$set$set(value x, value y) {
assert(x.type == value::SET && y.type == value::SET);
return make$int_(*x.setval == *y.setval);
}
value __ne__$set$set(value x, value y) {
assert(x.type == value::SET && y.type == value::SET);
return make$int_(*x.setval != *y.setval);
}
value __lt__$set$set(value x, value y) {
assert(x.type == value::SET && y.type == value::SET);
if (x.setval->size() >= y.setval->size())
return make$int_(false);
for (value v : *x.setval) {
if (y.setval->find(v) == y.setval->end())
return make$int_(false);
}
return make$int_(true);
}
value __le__$set$set(value x, value y) {
assert(x.type == value::SET && y.type == value::SET);
if (x.setval->size() > y.setval->size())
return make$int_(false);
for (value v : *x.setval) {
if (y.setval->find(v) == y.setval->end())
return make$int_(false);
}
return make$int_(true);
}
value __ge__$set$set(value x, value y) {
return __le__$set$set(y, x);
}
value __gt__$set$set(value x, value y) {
return __lt__$set$set(y, x);
}
value __iter__$set(value self) {
assert(self.type == value::SET);
return make$set_iterator(self.setval);
}