summary refs log tree commit diff stats
path: root/lib/data.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/data.h')
-rw-r--r--lib/data.h380
1 files changed, 0 insertions, 380 deletions
diff --git a/lib/data.h b/lib/data.h deleted file mode 100644 index b8b12b9..0000000 --- a/lib/data.h +++ /dev/null
@@ -1,380 +0,0 @@
1#ifndef DATA_H_C4AEC3DD
2#define DATA_H_C4AEC3DD
3
4#include <sqlite3.h>
5
6namespace verbly {
7
8 class data;
9 class word;
10 class adjective;
11 class noun;
12 class verb;
13 class adverb;
14 class frame;
15 class adjective_query;
16 class adverb_query;
17 class noun_query;
18 class verb_query;
19 class frame_query;
20 class preposition_query;
21
22 class data {
23 private:
24 sqlite3* ppdb;
25
26 friend class adjective_query;
27 friend class noun_query;
28 friend class verb_query;
29 friend class adverb_query;
30 friend class frame_query;
31 friend class preposition_query;
32
33 public:
34 data(std::string datafile);
35
36 data(const data& other) = delete;
37 data& operator=(const data& other) = delete;
38
39 data(data&& other);
40 data& operator=(data&& other);
41
42 ~data();
43
44 verb_query verbs() const;
45 adjective_query adjectives() const;
46 adverb_query adverbs() const;
47 noun_query nouns() const;
48 frame_query frames() const;
49 preposition_query prepositions() const;
50
51 };
52
53 template <class T>
54 class filter {
55 public:
56 enum class type {
57 singleton,
58 group
59 };
60
61 typedef filter<T> value_type;
62
63 type get_type() const
64 {
65 return _type;
66 }
67
68 filter(const filter<T>& other)
69 {
70 _type = other._type;
71 _notlogic = other._notlogic;
72
73 switch (_type)
74 {
75 case type::singleton:
76 {
77 new(&_singleton.elem) T(other._singleton.elem);
78
79 break;
80 }
81
82 case type::group:
83 {
84 new(&_group.elems) std::list<filter<T>>(other._group.elems);
85 _group.orlogic = other._group.orlogic;
86
87 break;
88 }
89 }
90 }
91
92 filter<T>& operator=(const filter<T>& other)
93 {
94 this->~filter();
95
96 _type = other._type;
97 _notlogic = other._notlogic;
98
99 switch (_type)
100 {
101 case type::singleton:
102 {
103 new(&_singleton.elem) T(other._singleton.elem);
104
105 break;
106 }
107
108 case type::group:
109 {
110 new(&_group.elems) std::list<filter<T>>(other._group.elems);
111 _group.orlogic = other._group.orlogic;
112
113 break;
114 }
115 }
116
117 return *this;
118 }
119
120 ~filter()
121 {
122 switch (_type)
123 {
124 case type::singleton:
125 {
126 _singleton.elem.~T();
127
128 break;
129 }
130
131 case type::group:
132 {
133 using list_type = std::list<filter<T>>;
134 _group.elems.~list_type();
135
136 break;
137 }
138 }
139 }
140
141 bool get_notlogic() const
142 {
143 return _notlogic;
144 }
145
146 void set_notlogic(bool _nl)
147 {
148 _notlogic = _nl;
149 }
150
151 std::list<T> inorder_flatten() const
152 {
153 std::list<T> result;
154
155 if (_type == type::singleton)
156 {
157 result.push_back(_singleton.elem);
158 } else if (_type == type::group)
159 {
160 for (auto elem : _group.elems)
161 {
162 auto l = elem.inorder_flatten();
163 result.insert(std::end(result), std::begin(l), std::end(l));
164 }
165 }
166
167 return result;
168 }
169
170 std::set<T> uniq_flatten() const
171 {
172 std::set<T> result;
173
174 if (_type == type::singleton)
175 {
176 result.insert(_singleton.elem);
177 } else if (_type == type::group)
178 {
179 for (auto elem : _group.elems)
180 {
181 auto l = elem.uniq_flatten();
182 result.insert(std::begin(l), std::end(l));
183 }
184 }
185
186 return result;
187 }
188
189 void clean()
190 {
191 if (_type == type::group)
192 {
193 std::list<typename std::list<filter<T>>::iterator> toremove;
194 for (auto it = _group.elems.begin(); it != _group.elems.end(); it++)
195 {
196 it->clean();
197
198 if (it->get_type() == type::group)
199 {
200 if (it->_group.elems.size() == 0)
201 {
202 toremove.push_back(it);
203 } else if (it->_group.elems.size() == 1)
204 {
205 bool truelogic = it->_notlogic != it->_group.elems.front()._notlogic;
206 filter<T> e = it->_group.elems.front();
207 *it = e;
208 it->_notlogic = truelogic;
209 }
210 }
211 }
212
213 for (auto rem : toremove)
214 {
215 _group.elems.erase(rem);
216 }
217
218 if (_group.elems.size() == 1)
219 {
220 bool truelogic = _notlogic != _group.elems.front()._notlogic;
221 filter<T> e = _group.elems.front();
222 *this = e;
223 _notlogic = truelogic;
224 }
225 }
226 }
227
228 // Singleton
229 filter(T _elem, bool _notlogic = false) : _type(type::singleton)
230 {
231 new(&_singleton.elem) T(_elem);
232 this->_notlogic = _notlogic;
233 }
234
235 filter<T>& operator=(T _elem)
236 {
237 *this = filter<T>{_elem};
238
239 return *this;
240 }
241
242 T get_elem() const
243 {
244 assert(_type == type::singleton);
245
246 return _singleton.elem;
247 }
248
249 void set_elem(T _elem)
250 {
251 assert(_type == type::singleton);
252
253 _singleton.elem = _elem;
254 }
255
256 // Group
257 typedef typename std::list<filter<T>>::iterator iterator;
258
259 filter() : _type(type::group)
260 {
261 new(&_group.elems) std::list<filter<T>>();
262 _group.orlogic = false;
263 }
264
265 filter(std::initializer_list<filter<T>> _init) : _type(type::group)
266 {
267 new(&_group.elems) std::list<filter<T>>(_init);
268 _group.orlogic = false;
269 }
270
271 iterator begin()
272 {
273 assert(_type == type::group);
274
275 return _group.elems.begin();
276 }
277
278 iterator end()
279 {
280 assert(_type == type::group);
281
282 return _group.elems.end();
283 }
284
285 filter<T>& operator<<(filter<T> _elem)
286 {
287 assert(_type == type::group);
288
289 _group.elems.push_back(_elem);
290
291 return *this;
292 }
293
294 void push_back(filter<T> _elem)
295 {
296 assert(_type == type::group);
297
298 _group.elems.push_back(_elem);
299 }
300
301 bool get_orlogic() const
302 {
303 assert(_type == type::group);
304
305 return _group.orlogic;
306 }
307
308 void set_orlogic(bool _ol)
309 {
310 assert(_type == type::group);
311
312 _group.orlogic = _ol;
313 }
314
315 bool empty() const
316 {
317 if (_type == type::group)
318 {
319 return _group.elems.empty();
320 } else {
321 return false;
322 }
323 }
324
325 int size() const
326 {
327 assert(_type == type::group);
328
329 return _group.elems.size();
330 }
331
332 private:
333 type _type;
334 bool _notlogic = false;
335 union {
336 struct {
337 T elem;
338 } _singleton;
339 struct {
340 std::list<filter<T>> elems;
341 bool orlogic;
342 } _group;
343 };
344 };
345
346 class binding {
347 public:
348 enum class type {
349 integer,
350 string
351 };
352
353 type get_type() const;
354 binding(const binding& other);
355 ~binding();
356 binding& operator=(const binding& other);
357
358 // Integer
359 binding(int _arg);
360 int get_integer() const;
361 void set_integer(int _arg);
362 binding& operator=(int _arg);
363
364 // String
365 binding(std::string _arg);
366 std::string get_string() const;
367 void set_string(std::string _arg);
368 binding& operator=(std::string _arg);
369
370 private:
371 union {
372 int _integer;
373 std::string _string;
374 };
375 type _type;
376 };
377
378};
379
380#endif /* end of include guard: DATA_H_C4AEC3DD */