-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.hpp
196 lines (141 loc) · 4.25 KB
/
list.hpp
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
#ifndef __LIST_HPP__
#define __LIST_HPP__
#include "collection.hpp"
using namespace std;
namespace immut {
template <typename T>
class list : public collection<T> {
Type type = L;
struct Item {
Item(T v, shared_ptr<const Item> tail) :
_hash(rand()), _val(v), _next(move(tail)) {}
explicit Item(T v) : _val(v) {}
int _hash;
T _val;
shared_ptr<const Item> _next;
};
friend Item;
shared_ptr<const Item> _head;
explicit list(shared_ptr<const Item> items) : _head(move(items)) {}
public:
list() {}
list(T v, list const &tail) :
_head(make_shared<Item>(v, tail._head)) {}
explicit list(T v) : _head(make_shared<Item>(v)) {}
list(initializer_list<T> init)
{
for (auto it = rbegin(init); it != rend(init); ++it) {
_head = make_shared<Item>(*it, _head);
}
}
shared_ptr<const Item> get_root() const { return _head; }
virtual bool isEmpty() const { return !_head; }
virtual T front() const
{
assert(!isEmpty());
return _head->_val;
}
list pop_front() const
{
assert(!isEmpty());
return list(_head->_next);
}
list push_front(T v) const { return list(v, *this); }
list take(int n)
{
if (n <= 0 || isEmpty())
return list();
return pop_front().take(n - 1).push_front(front());
}
list insertedAt(int i, T v) const
{
if (i == 0)
return push_front(v);
else {
assert(!isEmpty());
return list<T>(front(), pop_front().insertedAt(i - 1, v));
}
}
list removeAt(int i, int j) const
{
if (isEmpty())
return list();
if (i == j)
return pop_front();
return list(front(), pop_front().removeAt(i + 1, j));
}
list removed(T v) const
{
if (isEmpty())
return list();
if (v == front())
return pop_front();
return list(front(), pop_front().removed(v));
}
bool contains(T v) const
{
if (isEmpty())
return false;
if (v == front())
return true;
return pop_front().contains(v);
};
template <typename F>
void forEach(const F f) const
{
Item const *it = get_root().get();
while (it != nullptr) {
f(it->_val);
it = it->_next.get();
}
}
bool operator==(const list &rhs)
{
Item const *it1 = get_root().get();
Item const *it2 = rhs.get_root().get();
while (!(it1 == nullptr || it2 == nullptr)) {
if (it1->_val != it2->_val)
return false;
it1 = it1->_next.get();
it2 = it2->_next.get();
}
if ((it1 == nullptr && it2 != nullptr) ||
(it1 != nullptr && it2 == nullptr))
return false;
return true;
}
bool operator > (const list &rhs)
{
return get_root().get()->_hash > rhs.get_root().get()->_hash;
}
bool operator < (const list &rhs)
{
return get_root().get()->_hash < rhs.get_root().get()->_hash;
}
const T operator[](int idx) const
{
const Item *it1 = get_root().get();
while (idx--) {
if (it1 == nullptr)
throw invalid_argument("Index out of bound");
it1 = it1->_next.get();
}
return it1->_val;
}
string str() const
{
stringstream os;
os << "[ ";
forEach([&os](T v) {
os << v << " ";
});
os << "]";
return os.str();
}
friend ostream &operator<<(ostream &os, const list &t)
{
return os << t.str();
}
};
}
#endif