summary refs log tree commit diff stats
path: root/lib/data.h
blob: 0d599c4b22515b88856a3be5f8c82fefb2eef35d (plain) (blame)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#ifndef DATA_H_C4AEC3DD
#define DATA_H_C4AEC3DD

#include <sqlite3.h>

namespace verbly {
  
  class data;
  class word;
  class adjective;
  class noun;
  class verb;
  class adverb;
  class frame;
  class adjective_query;
  class adverb_query;
  class noun_query;
  class verb_query;
  class frame_query;
  class preposition_query;
  
  class data {
    private:
      sqlite3* ppdb;
      
      friend class adjective_query;
      friend class noun_query;
      friend class verb_query;
      friend class adverb_query;
      friend class frame_query;
      friend class preposition_query;
      
    public:
      data(std::string datafile);
      
      data(const data& other) = delete;
      data& operator=(const data& other) = delete;
      
      data(data&& other);
      data& operator=(data&& other);
      
      ~data();
      
      verb_query verbs() const;
      adjective_query adjectives() const;
      adverb_query adverbs() const;
      noun_query nouns() const;
      frame_query frames() const;
      preposition_query prepositions() const;
      
  };
  
  template <class T>
  class filter {
    public:
      enum class type {
        singleton,
        group
      };
      
      typedef filter<T> value_type;
      
      type get_type() const
      {
        return _type;
      }
      
      filter(const filter<T>& other)
      {
        _type = other._type;
        _notlogic = other._notlogic;
        
        switch (_type)
        {
          case type::singleton:
          {
            new(&_singleton.elem) T(other._singleton.elem);
            
            break;
          }
          
          case type::group:
          {
            new(&_group.elems) std::list<filter<T>>(other._group.elems);
            _group.orlogic = other._group.orlogic;
            
            break;
          }
        }
      }
      
      filter<T>& operator=(const filter<T>& other)
      {
        this->~filter();
        
        _type = other._type;
        _notlogic = other._notlogic;
        
        switch (_type)
        {
          case type::singleton:
          {
            new(&_singleton.elem) T(other._singleton.elem);
            
            break;
          }
          
          case type::group:
          {
            new(&_group.elems) std::list<filter<T>>(other._group.elems);
            _group.orlogic = other._group.orlogic;
            
            break;
          }
        }
        
        return *this;
      }
      
      ~filter()
      {
        switch (_type)
        {
          case type::singleton:
          {
            _singleton.elem.~T();
            
            break;
          }
          
          case type::group:
          {
            using list_type = std::list<filter<T>>;
            _group.elems.~list_type();
            
            break;
          }
        }
      }
      
      bool get_notlogic() const
      {
        return _notlogic;
      }
      
      void set_notlogic(bool _nl)
      {
        _notlogic = _nl;
      }
      
      std::list<T> inorder_flatten() const
      {
        std::list<T> result;
        
        if (_type == type::singleton)
        {
          result.push_back(_singleton.elem);
        } else if (_type == type::group)
        {
          for (auto elem : _group.elems)
          {
            auto l = elem.inorder_flatten();
            result.insert(std::end(result), std::begin(l), std::end(l));
          }
        }
        
        return result;
      }
      
      std::set<T> uniq_flatten() const
      {
        std::set<T> result;
        
        if (_type == type::singleton)
        {
          result.insert(_singleton.elem);
        } else if (_type == type::group)
        {
          for (auto elem : _group.elems)
          {
            auto l = elem.uniq_flatten();
            result.insert(std::begin(l), std::end(l));
          }
        }
        
        return result;
      }
      
      void clean()
      {
        if (_type == type::group)
        {
          std::list<typename std::list<filter<T>>::iterator> toremove;
          for (auto it = _group.elems.begin(); it != _group.elems.end(); it++)
          {
            it->clean();
            
            if (it->get_type() == type::group)
            {
              if (it->_group.elems.size() == 0)
              {
                toremove.push_back(it);
              } else if (it->_group.elems.size() == 1)
              {
                bool truelogic = it->_notlogic != it->_group.elems.front()._notlogic;
                filter<T> e = it->_group.elems.front();
                *it = e;
                it->_notlogic = truelogic;
              }
            }
          }
          
          for (auto rem : toremove)
          {
            _group.elems.erase(rem);
          }
          
          if (_group.elems.size() == 1)
          {
            bool truelogic = _notlogic != _group.elems.front()._notlogic;
            filter<T> e = _group.elems.front();
            *this = e;
            _notlogic = truelogic;
          }
        }
      }
      
      // Singleton
      filter(T _elem, bool _notlogic = false) : _type(type::singleton)
      {
        new(&_singleton.elem) T(_elem);
        this->_notlogic = _notlogic;
      }
      
      filter<T>& operator=(T _elem)
      {
        *this = filter<T>{_elem};
        
        return *this;
      }
      
      T get_elem() const
      {
        assert(_type == type::singleton);
        
        return _singleton.elem;
      }
      
      void set_elem(T _elem)
      {
        assert(_type == type::singleton);
        
        _singleton.elem = _elem;
      }
      
      // Group
      typedef typename std::list<filter<T>>::iterator iterator;
      
      filter() : _type(type::group)
      {
        new(&_group.elems) std::list<filter<T>>();
        _group.orlogic = false;
      }
      
      filter(std::initializer_list<filter<T>> _init) : _type(type::group)
      {
        new(&_group.elems) std::list<filter<T>>(_init);
        _group.orlogic = false;
      }
      
      iterator begin()
      {
        assert(_type == type::group);
        
        return _group.elems.begin();
      }
      
      iterator end()
      {
        assert(_type == type::group);
        
        return _group.elems.end();
      }
      
      filter<T>& operator<<(filter<T> _elem)
      {
        assert(_type == type::group);
        
        _group.elems.push_back(_elem);
        
        return *this;
      }
      
      void push_back(filter<T> _elem)
      {
        assert(_type == type::group);
        
        _group.elems.push_back(_elem);
      }
      
      bool get_orlogic() const
      {
        assert(_type == type::group);
        
        return _group.orlogic;
      }
      
      void set_orlogic(bool _ol)
      {
        assert(_type == type::group);
        
        _group.orlogic = _ol;
      }
      
      bool empty() const
      {
        if (_type == type::group)
        {
          return _group.elems.empty();
        } else {
          return false;
        }
      }
      
      int size() const
      {
        assert(_type == type::group);
        
        return _group.elems.size();
      }
      
    private:
      type _type;
      bool _notlogic = false;
      union {
        struct {
          T elem;
        } _singleton;
        struct {
          std::list<filter<T>> elems;
          bool orlogic;
        } _group;
      };
  };
  
};

#endif /* end of include guard: DATA_H_C4AEC3DD */