summaryrefslogtreecommitdiff
path: root/src/cstore/svector.hpp
blob: bc04fdc630185d604369623b1c86bf6e632c402b (plain)
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
349
350
351
352
353
354
355
356
/*
 * Copyright (C) 2011 Vyatta, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _SVECTOR_HPP_
#define _SVECTOR_HPP_
#include <cstdlib>
#include <cstring>
#include <string>
#include <tr1/functional>

#include <cstore/util.hpp>

namespace cstore {

template<class P>
class svector {
public:
  static const char ELEM_SEP = P::separator;
  static const bool RAW_CSTR_DATA = (ELEM_SEP != 0);
  static const bool RANDOM_ACCESS = (ELEM_SEP == 0);
  static const size_t STATIC_NUM_ELEMS = P::static_num_elems;
  static const size_t STATIC_BUF_LEN = P::static_buf_len;

  svector();
  svector(const svector<P>& v);
  svector(const char *raw_cstr);
  svector(const std::string& str);
  svector(const char *raw_data, size_t dlen);
  ~svector();

  void push_back(const char *e);
  void pop_back();
  void pop_back(std::string& last);

  svector<P>& operator=(const char *raw_cstr) {
    assign(raw_cstr);
    return *this;
  };
  svector<P>& operator=(const std::string& str) {
    return operator=(str.c_str());
  };
  svector<P>& operator=(const svector<P>& v);
  svector<P>& operator/=(const svector<P>& v);
  svector<P> operator/(const svector<P>& v) {
    svector<P> lhs(*this);
    lhs /= v;
    return lhs;
  };
  void assign(const char *raw_cstr) {
    assign_raw_cstr(raw_cstr, Int2Type<RAW_CSTR_DATA>());
  };
  void assign(const char *raw_data, size_t dlen);

  bool operator==(const svector<P>& rhs) const {
    return (_len == rhs._len && memcmp(_data, rhs._data, _len) == 0);
  };
  const char *operator[](size_t idx) const {
    return elem_at(idx, Int2Type<RANDOM_ACCESS>());
  };

  size_t size() const { return _num_elems; };
  size_t length() const { return _len; };
  const char *get_cstr() const {
    return get_raw_cstr(Int2Type<RAW_CSTR_DATA>());
  };
  const char *get_data(size_t& dlen) const {
    dlen = _len;
    return _data;
  };
  size_t hash() const {
    return std::tr1::_Fnv_hash<sizeof(size_t)>::hash(_data, _len);
  };
  std::string to_string() const {
    return to_string(Int2Type<RAW_CSTR_DATA>());
  };

private:
  size_t _num_elems;
  size_t _len;
  size_t _ebuf_size;
  size_t _buf_size;
  char **_elems;
  char *_elems_buf[STATIC_NUM_ELEMS];
  char **_elems_dbuf;
  char *_data;
  char _data_buf[STATIC_BUF_LEN];
  char *_data_dbuf;

  void grow_data();
  void grow_elems();
  void inc_num_elems() {
    ++_num_elems;
    if (_num_elems >= _ebuf_size) {
      grow_elems();
    }
  };
  void assign_raw_cstr(const char *raw_cstr, Int2Type<true>) {
    assign(raw_cstr, strlen(raw_cstr));
  };
  const char *get_raw_cstr(Int2Type<true>) const {
    return _data;
  };
  const char *elem_at(size_t idx, Int2Type<true>) const {
    return (idx < _num_elems ? (_elems[idx] + 1) : NULL);
  };
  std::string to_string(Int2Type<true>) const {
    return std::string(_data);
  };
  std::string to_string(Int2Type<false>) const {
    std::string ret;
    for (size_t i = 0; i < size(); i++) {
      if (i > 0) {
        ret += " ";
      }
      ret += elem_at(i, Int2Type<RANDOM_ACCESS>());
    }
    return ret;
  };
};

template<class P>
svector<P>::svector()
  : _num_elems(0), _len(0), _ebuf_size(STATIC_NUM_ELEMS),
    _buf_size(STATIC_BUF_LEN), _elems(_elems_buf), _elems_dbuf(0),
    _data(_data_buf), _data_dbuf(0)
{
  _elems[0] = _data;
  _data[0] = 0;
}

template<class P>
svector<P>::svector(const svector<P>& v)
  : _num_elems(0), _len(0), _ebuf_size(STATIC_NUM_ELEMS),
    _buf_size(STATIC_BUF_LEN), _elems(_elems_buf), _elems_dbuf(0),
    _data(_data_buf), _data_dbuf(0)
{
  _elems[0] = _data;
  _data[0] = 0;
  operator=(v);
}

template<class P>
svector<P>::svector(const char *raw_cstr)
  : _num_elems(0), _len(0), _ebuf_size(STATIC_NUM_ELEMS),
    _buf_size(STATIC_BUF_LEN), _elems(_elems_buf), _elems_dbuf(0),
    _data(_data_buf), _data_dbuf(0)
{
  assign(raw_cstr);
}

template<class P>
svector<P>::svector(const std::string& str)
  : _num_elems(0), _len(0), _ebuf_size(STATIC_NUM_ELEMS),
    _buf_size(STATIC_BUF_LEN), _elems(_elems_buf), _elems_dbuf(0),
    _data(_data_buf), _data_dbuf(0)
{
  assign(str.c_str());
}

template<class P>
svector<P>::svector(const char *raw_data, size_t dlen)
  : _num_elems(0), _len(0), _ebuf_size(STATIC_NUM_ELEMS),
    _buf_size(STATIC_BUF_LEN), _elems(_elems_buf), _elems_dbuf(0),
    _data(_data_buf), _data_dbuf(0)
{
  assign(raw_data, dlen);
}

template<class P>
svector<P>::~svector()
{
  if (_elems_dbuf) {
    delete [] _elems_dbuf;
  }
  if (_data_dbuf) {
    delete [] _data_dbuf;
  }
}

template<class P> void
svector<P>::push_back(const char *e)
{
  size_t elen = strlen(e);
  while ((_len + elen + 2) >= _buf_size) {
    // make sure there's space for (data + SEP + e + 0)
    grow_data();
  }

  char *start = _elems[_num_elems];
  *start = ELEM_SEP;
  memcpy(start + 1, e, elen + 1);
  inc_num_elems();
  _len += (elen + 1);
  _elems[_num_elems] = start + 1 + elen;
}

template<class P> void
svector<P>::pop_back()
{
  if (_num_elems == 0) {
    return;
  }
  --_num_elems;
  _len = _elems[_num_elems] - _data;
  _data[_len] = 0;
}

template<class P> void
svector<P>::pop_back(std::string& last)
{
  if (_num_elems == 0) {
    return;
  }
  last = _elems[_num_elems - 1] + 1;
  pop_back();
}

template<class P> svector<P>&
svector<P>::operator=(const svector<P>& v)
{
  if (this == &v) {
    return *this;
  }

  while (_buf_size < v._buf_size) {
    grow_data();
  }
  if (_ebuf_size >= v._ebuf_size) {
    _num_elems = v._num_elems;
  } else {
    _num_elems = 0;
    while (_num_elems < v._num_elems) {
      inc_num_elems();
    }
  }
  _len = v._len;
  memcpy(_data, v._data, _len + 1);
  memcpy(_elems, v._elems, sizeof(char *) * (_num_elems + 1));
  const char *o0 = _elems[0];
  for (size_t i = 0; i <= _num_elems; i++) {
    _elems[i] = _data + (_elems[i] - o0);
  }
  return *this;
}

template<class P> svector<P>&
svector<P>::operator/=(const svector<P>& v)
{
  while ((_len + v._len + 1) >= _buf_size) {
    // make sure there's space for (data + data + 0)
    grow_data();
  }

  size_t v_num_elems = v._num_elems;
  size_t olen = _len;
  memcpy(_data + _len, v._data, v._len + 1);
  _len += v._len;
  for (size_t i = 1; i <= v_num_elems; i++) {
    inc_num_elems();
    _elems[_num_elems] = _data + olen + (v._elems[i] - v._data);
  }
  return *this;
}

template<class P> void
svector<P>::assign(const char *raw_data, size_t dlen)
{
  _num_elems = 0;
  _len = 0;
  _elems[0] = _data;
  if (dlen == 0 || (dlen == 1 && raw_data[0] == ELEM_SEP)) {
    _data[0] = 0;
    return;
  }

  while ((dlen + 2) >= _buf_size) {
    // make sure there's space for (SEP + raw_data + 0)
    grow_data();
  }
  _len = dlen;

  if (raw_data[0] != ELEM_SEP) {
    _data[0] = ELEM_SEP;
    memcpy(_data + 1, raw_data, _len + 1);
    ++_len;
  } else {
    memcpy(_data, raw_data, _len + 1);
  }
  for (size_t i = 1; i < _len; i++) {
    if (_data[i] == ELEM_SEP) {
      inc_num_elems();
      _elems[_num_elems] = &(_data[i]);
    }
  }
  inc_num_elems();
  _elems[_num_elems] = _data + _len;
}

template<class P> void
svector<P>::grow_data()
{
  char *tmp = new char[_buf_size + STATIC_BUF_LEN];
  if (!tmp) {
    // doomed
    exit(1);
  }
  memcpy(tmp, _data, _len + 1);

  if (tmp != _data_dbuf) {
    for (size_t i = 0; i <= _num_elems; i++) {
      _elems[i] = tmp + (_elems[i] - _data);
    }
  }
  
  if (_data_dbuf) {
    delete [] _data_dbuf;
  }
  _data_dbuf = tmp;
  _data = _data_dbuf;
  _buf_size += STATIC_BUF_LEN;
}

template<class P> void
svector<P>::grow_elems()
{
  char **tmp = new char *[_ebuf_size + STATIC_NUM_ELEMS];
  if (!tmp) {
    // doomed
    exit(1);
  }
  memcpy(tmp, _elems, _ebuf_size * sizeof(char *));
  if (_elems_dbuf) {
    delete [] _elems_dbuf;
  }
  _elems_dbuf = tmp;
  _elems = _elems_dbuf;
  _ebuf_size += STATIC_NUM_ELEMS;
}

} // end namespace cstore

#endif /* _SVECTOR_HPP_ */