-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.hpp
More file actions
91 lines (75 loc) · 1.93 KB
/
set.hpp
File metadata and controls
91 lines (75 loc) · 1.93 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
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
#ifndef __SET_HPP
#define __SET_HPP
#include "list.hpp"
namespace shedskin {
template<class T>
class set : public pyobj {
private:
list<T>* items;
public:
set() : items(new list<T>()) {}
set(list<T>* init) : items(new list<T>()) {
if(init) {
for(__ss_int i = 0; i < len(init); i++) {
add(init->__getfast__(i));
}
}
}
void add(const T& value) {
if(!contains(value)) {
items->append(value);
}
}
bool contains(const T& value) const {
for(__ss_int i = 0; i < len(items); i++) {
if(items->__getfast__(i) == value) return true;
}
return false;
}
bool __contains__(const T& value) const {
return contains(value);
}
void discard(const T& value) {
for (__ss_int i = 0; i < len(items); i++) {
if (items->__getfast__(i) == value) {
items->__remove__(i);
return;
}
}
}
T pop() {
if (len(items) == 0) {
throw std::out_of_range("pop from an empty set");
}
T value = items->__getfast__(0);
items->__remove__(0);
return value;
}
__ss_int __len__() const {
return len(items);
}
T __getitem__(__ss_int index) const {
if(index < 0 || index >= len(items)) return T();
return items->__getfast__(index);
}
class for_in_loop {
typename list<T>::Iterator it;
typename list<T>::Iterator end_it;
public:
for_in_loop() : it(nullptr), end_it(nullptr) {}
for_in_loop(set<T>& s) : it(s.items->begin()), end_it(s.items->end()) {}
bool __next__(T& ref) {
if (it != end_it) {
ref = *it;
++it;
return true;
}
return false;
}
};
~set() {
delete items;
}
};
} // namespace shedskin
#endif