summaryrefslogtreecommitdiff
path: root/utils/value_array.h
blob: 12fc9d87b781ec36e44d6d31a36ce45ca5f8909a (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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#ifndef VALUE_ARRAY_H
#define VALUE_ARRAY_H

//TODO: option for non-constructed version (type_traits pod?), option for small array optimization (if sz < N, store inline in union, see small_vector.h)

#define DBVALUEARRAY(x) x

#include <cstdlib>
#include <algorithm>
#include <new>
#include <boost/range.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits.hpp>
#include <cstring>
#include <boost/functional/hash.hpp>
#ifdef USE_BOOST_SERIALIZE
# include <boost/serialization/split_member.hpp>
# include <boost/serialization/access.hpp>
#endif

//TODO: use awesome type traits (and/or policy typelist argument) to provide these only when possible?
#define VALUE_ARRAY_ADD 1
#define VALUE_ARRAY_MUL 1
#define VALUE_ARRAY_BITWISE 0
#define VALUE_ARRAY_OSTREAM 1

#if VALUE_ARRAY_OSTREAM
# include <iostream>
#endif

// valarray like in that size is fixed (so saves space compared to vector), but same interface as vector (less resize/push_back/insert, of course)
template <class T, class A = std::allocator<T> >
class ValueArray : A // private inheritance so stateless allocator adds no size.
{
  typedef ValueArray<T,A> Self;
public:
#if VALUE_ARRAY_OSTREAM
  friend inline std::ostream & operator << (std::ostream &o,Self const& s) {
    o<<'[';
    for (unsigned i=0,e=s.size();i<e;++i) {
      if (i) o<<' ';
      o<<s[i];
    }
    o<<']';
    return o;
  }
#endif
  static const int SV_MAX=sizeof(T)/sizeof(T*)>1?sizeof(T)/sizeof(T*):1;
  //space optimization: SV_MAX T will fit inside what would otherwise be a pointer to heap data.  todo in the far future if bored.
  typedef T value_type;
  typedef T& reference;
  typedef T const& const_reference;
  typedef T* iterator;
  typedef T const* const_iterator;
  typedef std::size_t size_type;
  typedef std::ptrdiff_t difference_type;
  typedef T* pointer;

  size_type size() const { return sz; }
  bool empty() const { return !sz; }

  iterator begin() { return array; }
  iterator end() { return array + sz; }
  const_iterator begin() const { return array; }
  const_iterator end() const { return array + sz; }

  reference operator[](size_type pos) { return array[pos]; }
  const_reference operator[](size_type pos) const { return array[pos]; }

  reference at(size_type pos) { return array[pos]; }
  const_reference at(size_type pos) const { return array[pos]; }

  reference front() { return array[0]; }
  reference back() { return array[sz-1]; }

  const_reference front() const { return array[0]; }
  const_reference back() const { return array[sz-1]; }

  ValueArray() : sz(0), array(NULL) {}

  friend inline std::size_t hash_value(Self const& x) {
    return boost::hash_range(x.begin(),x.end());
  }

protected:
  void destroy()
  {
    if (!array) return;
    // it's cool that this destroys in reverse order of construction, but why bother?
    for (pointer i=array+sz;i>array;)
      A::destroy(--i);
  }
  void dealloc() {
    if (sz && array != NULL) A::deallocate(array,sz);
    sz=0;
  }
  void alloc(size_type s) {
    array=s?A::allocate(s):0;
    sz=s;
  }

  void realloc(size_type s) {
    if (sz!=s) {
      dealloc();
      alloc(s);
    }
  }

  void reinit_noconstruct(size_type s) {
    destroy();
    realloc(s);
  }

  template <class C,class F>
  inline void init_map(C & c,F const& f) {
    alloc(c.size());
    copy_construct_map(c.begin(),c.end(),array,f);
  }
  template <class C,class F>
  inline void init_map(C const& c,F const& f) {
    alloc(c.size());
    copy_construct_map(c.begin(),c.end(),array,f);
  }
  // warning: std::distance is likely slow on maps (anything other than random access containers.  so container version using size will be better
  template <class I,class F>
  inline void init_range_map(I itr, I end,F const& f) {
    alloc(std::distance(itr,end));
    copy_construct_map(itr,end,array,f);
  }
  template <class I>
  inline void init_range(I itr, I end) {
    alloc(std::distance(itr,end));
    copy_construct(itr,end,array);
  }
  inline void fill(const_reference t) {
    for (pointer i=array,e=array+sz;i!=e;++i)
      new(i) T(t);
  }
  inline void fill() {
    for (pointer i=array,e=array+sz;i!=e;++i)
      new(i) T();
  }

  inline void init(size_type s) {
    alloc(s);
    fill();
  }
  inline void init(size_type s, const_reference t) {
    alloc(s);
    fill(t);
  }
public:
  ValueArray(size_type s, const_reference t)
  {
    init(s,t);
  }
  explicit ValueArray(size_type s)
  {
    init(s);
  }
  void reinit(size_type s, const_reference t) {
    reinit_noconstruct(s);
    fill(t);
  }
  void reinit(size_type s) {
    reinit_noconstruct(s);
    fill();
  }

  //copy any existing data like std::vector.  not A::construct exception safe.  try blah blah?  swap?
  void resize(size_type s, const_reference t = T()) {
    if (s) {
      pointer na=A::allocate(s);
      size_type nc=s<sz ? s : sz;
      size_type i=0;
      for (;i<nc;++i)
        A::construct(na+i,array[i]);
      for (;i<s;++i)
        A::construct(na+i,t);
      clear();
      array=na;
      sz=s;
    } else
      clear();
  }

  template <class I>
  void reinit_range(I itr, I end) {
    reinit_noconstruct(std::distance(itr,end));
    copy_construct(itr,end,array);
  }

  template <class I,class F>
  void reinit_map(I itr,I end,F const& f) {
    reinit_noconstruct(std::distance(itr,end));
    copy_construct_map(itr,end,array,f);
  }

  // warning: std::distance is likely slow on maps,lists (anything other than random access containers.  so container version below using size() will be better
  template <class C,class F>
  void reinit_map(C const& c,F const& f) {
    reinit_noconstruct(c.size());
    copy_construct_map(c.begin(),c.end(),array,f);
  }

  template <class C,class F>
  void reinit_map(C & c,F const& f) {
    reinit_noconstruct(c.size());
    copy_construct_map(c.begin(),c.end(),array,f);
  }

  template <class I>
  ValueArray(I itr, I end)
  {
    init_range(itr,end);
  }
  template <class I,class F>
  ValueArray(I itr, I end,F const& map)
  {
    init_range_map(itr,end,map);
  }

  ~ValueArray() {
    clear();
  }

#undef VALUE_ARRAY_OPEQ
#define VALUE_ARRAY_OPEQ(op) template <class T2,class A2> Self & operator op (ValueArray<T2,A2> const& o) { assert(sz==o.sz); for (int i=0,e=sz;i<=e;++i) array[i] op o.array[i]; return *this; }
#if VALUE_ARRAY_ADD
  VALUE_ARRAY_OPEQ(+=)
  VALUE_ARRAY_OPEQ(-=)
#endif
#if VALUE_ARRAY_MUL
  VALUE_ARRAY_OPEQ(*=)
  VALUE_ARRAY_OPEQ(/=)
#endif
#if VALUE_ARRAY_BITWISE
  VALUE_ARRAY_OPEQ(|=)
  VALUE_ARRAY_OPEQ(*=)
#endif
#undef VALUE_ARRAY_OPEQ
#undef VALUE_ARRAY_BINOP
#define VALUE_ARRAY_BINOP(op,opeq) template <class T2,class A2> friend inline Self operator op (Self x,ValueArray<T2,A2> const& y) { x opeq y; return x; }
#if VALUE_ARRAY_ADD
  VALUE_ARRAY_BINOP(+,+=)
  VALUE_ARRAY_BINOP(-,-=)
#endif
#if VALUE_ARRAY_MUL
  VALUE_ARRAY_BINOP(*,*=)
  VALUE_ARRAY_BINOP(/,/=)
#endif
#if VALUE_ARRAY_BITWISE
  VALUE_ARRAY_BINOP(|,|=)
  VALUE_ARRAY_BINOP(*,*=)
#endif

#undef VALUE_ARRAY_BINOP

  void clear()
  {
    destroy();
    dealloc();
  }

  void swap(ValueArray& other)
  {
    std::swap(sz,other.sz);
    std::swap(array,other.array);
  }

  ValueArray(ValueArray const& other)
    : A(other)
    , sz(other.sz)
    , array(sz?A::allocate(sz):0)

  {
    copy_construct(other.begin(),other.end(),array);
  }

  ValueArray& operator=(ValueArray const& other)
  {
    ValueArray(other).swap(*this);
    return *this;
  }

  template <class Range>
  ValueArray( Range const& v
              , typename boost::disable_if< boost::is_integral<Range> >::type* = 0)
    : sz(boost::size(v))
    , array(sz?A::allocate(sz):0)
  {
    copy_construct(boost::begin(v),boost::end(v),array);
  }

  template <class Range> typename
  boost::disable_if<
    boost::is_integral<Range>
   , ValueArray>::type& operator=(Range const& other)
   {
     ValueArray(other).swap(*this);
     return *this;
   }

private:

  template <class I1>
  void copy_construct(I1 itr, I1 end, T *into)
  {
    for (; itr != end; ++itr, ++into) A::construct(into,*itr);
  }

  //TODO: valgrind doesn't think this works.
  template <class I1,class F>
  void copy_construct_map(I1 itr, I1 end, T *into,F const& f)
  {
    while ( itr != end) {
      DBVALUEARRAY(assert(into<array+sz));
      A::construct(into,f(*itr));
      ++itr;++into;
    }

  }
  //friend class boost::serialization::access;
public:
  template <class Archive>
  void save(Archive& ar, unsigned int version) const
  {
    ar << sz;
    for (size_type i = 0; i != sz; ++i) ar << at(i);
  }

  template <class Archive>
  void load(Archive& ar, unsigned int version)
  {
    size_type s;
    ar >> s;
    ValueArray v(s);
    for (size_type i = 0; i != s; ++i) ar >> v[i];
    this->swap(v);
  }
#ifdef USE_BOOST_SERIALIZE
  BOOST_SERIALIZATION_SPLIT_MEMBER()
#endif
private:
  size_type sz;
  pointer array;
};


template <class T, class A>
bool operator==(ValueArray<T,A> const& v1, ValueArray<T,A> const& v2)
{
  return (v1.size() == v2.size()) and
    std::equal(v1.begin(),v1.end(),v2.begin());
}

template <class A>
bool operator==(ValueArray<char,A> const& v1, ValueArray<char,A> const& v2)
{
  typename ValueArray<char,A>::size_type sz=v1.size();
  return sz == v2.size() &&
    0==std::memcmp(v1.begin(),v2.begin(),sizeof(char)*sz);
}

template <class A>
bool operator==(ValueArray<unsigned char,A> const& v1, ValueArray<unsigned char,A> const& v2)
{
  typename ValueArray<char,A>::size_type sz=v1.size();
  return sz == v2.size() &&
    0==std::memcmp(v1.begin(),v2.begin(),sizeof(char)*sz);
}

template <class T,class A>
bool operator< (ValueArray<T,A> const& v1, ValueArray<T,A> const& v2)
{
  return std::lexicographical_compare( v1.begin()
                                       , v1.end()
                                       , v2.begin()
                                       , v2.end() );
}

template <class A>
bool operator<(ValueArray<unsigned char,A> const& v1, ValueArray<unsigned char,A> const& v2)
{
  typename ValueArray<char,A>::size_type sz=v1.size();
  return sz == v2.size() &&
    std::memcmp(v1.begin(),v2.begin(),sizeof(char)*sz)<0;
}


template <class T,class A>
void memcpy(void *out,ValueArray<T,A> const& v) {
  std::memcpy(out,v.begin(),v.size()*sizeof(T));
}


#endif