commore  1.0.6-SNAPSHOT
All Classes Namespaces Functions Variables Typedefs Enumerations Pages
Array.h
1 //
2 // Copyright (c) 2006-2014 Raphael David / CANTOR
3 //
4 
5 
6 #ifndef CMR_ARRAY_INCLUDED
7 #define CMR_ARRAY_INCLUDED
8 
9 #include "BaseArray.h"
10 
11 namespace commore
12 {
21  template< class T, class A> class Array : public BaseArray
22  {
23  public:
24  typedef T value_type;
25 
29  Array() {}
35  Array(size_t nb)
36  {
37  reserve_(nb, sizeof(T), A::constructor, A::destructor);
38  }
42  Array(const Array& v) : BaseArray()
43  {
44  reserve(v.reserved());
45  add_(v.size(), sizeof(T), (Byte*)v.data_, A::constructor, A::destructor);
46  }
51  {
52  clear();
53  add_(v.size(), sizeof(T), (Byte*)v.data_, A::constructor, A::destructor);
54  return *this;
55  }
60  {
61  clear();
62  }
67  void clear(bool all = true)
68  {
69  clear_(sizeof(T), A::destructor, all);
70  }
78  void resize(int n)
79  {
80  resize_(n, sizeof(T), A::constructor, A::destructor);
81  }
86  T& add()
87  {
88  return *(T*)add_(1, sizeof(T), 0, A::constructor, A::destructor);
89  }
95  T& add(const T& v)
96  {
97  return *(T*)add_(1, sizeof(T), (Byte*)&v, A::constructor, A::destructor);
98  }
105  const T* get_at(int pos) const
106  {
107  return (const T*)at_(pos, sizeof(T));
108  }
115  T* get_at(int pos)
116  {
117  return (T*)at_(pos, sizeof(T));
118  }
125  T& operator [](int pos)
126  {
127  return *get_at(pos);
128  }
135  const T& operator [](int pos) const
136  {
137  return *get_at(pos);
138  }
145  size_t reserve(size_t nb)
146  {
147  return reserve_(nb, sizeof(T), A::constructor, A::destructor);
148  }
154  int compare(const Array<T, A>& a) const
155  {
156  return compare(a, A::comparator);
157  }
163  bool operator< (const Array<T, A>& a) const
164  {
165  return compare(a, A::comparator) < 0;
166  }
172  bool operator== (const Array<T, A>& l) const
173  {
174  return compare(l, A::comparator) == 0;
175  }
176 
182  CMREXD AString& toString(AString& dst) const;
183 
184 
191  CMREXD long read_xml(IBStream& i);
198  CMREXD long write_xml(OBStream& o) const;
199 
206  CMREXD int write(const Path& file) const;
213  CMREXD int read(const Path& file);
220  CMREXD int write_xml(const Path& file) const;
227  CMREXD int read_xml(const Path& file);
234  CMREXD AString& to_xml_string(AString& s) const;
241  CMREXD long from_xml_string(const AString& s);
242 
243 
250  int compare(const Array<T, A>& a, FunctionComparator cmor) const
251  {
252  int r = (int)size() - (int)a.size();
253  if (r == 0)
254  {
255  const_iterator i1 = begin();
256  const_iterator i2 = a.begin();
257  while (r == 0 && i1 && i2)
258  {
259  r = cmor(i1.get_dummy(), i2.get_dummy());
260  i1++;
261  i2++;
262  }
263  }
264  return r;
265  }
266 
271  {
272  public:
277  bool next()
278  {
279  return BaseArray::next_(*this, sizeof(T));
280  }
286  T& current()
287  {
288  return *(T*)get_data();
289  }
295  {
296  next();
297  return *this;
298  }
304  {
305  iterator tmp = *this;
306  next();
307  return tmp;
308  }
315  {
316  return current();
317  }
318  };
323  {
324  public:
329  bool next()
330  {
331  return BaseArray::next_(*this, sizeof(T));
332  }
339  const T& current()
340  {
341  return *(T*)get_data();
342  }
348  {
349  next();
350  return *this;
351  }
357  {
358  const_iterator tmp = *this;
359  next();
360  return tmp;
361  }
367  const T& operator*()
368  {
369  return current();
370  }
371  };
372 
377  {
378  iterator i;
379  begin_(i);
380  return i;
381  }
386  {
387  const_iterator i;
388  begin_(i);
389  return i;
390  }
391 
397  {
398  return begin();
399  }
400 
401  };
402 }
403 
404 #endif
iterator & operator++()
Definition: Array.h:294
bool operator==(const Array< T, A > &l) const
Definition: Array.h:172
T & operator*()
Definition: Array.h:314
const_iterator begin() const
Definition: Array.h:385
Definition: BaseArray.h:93
Definition: AString.h:39
const_iterator cbegin() const
Definition: Array.h:396
bool next()
Definition: Array.h:329
Array(const Array &v)
Definition: Array.h:42
const_iterator & operator++()
Definition: Array.h:347
CMREXD long from_xml_string(const AString &s)
Definition: Array.h:322
size_t reserve(size_t nb)
Definition: Array.h:145
T & add()
Definition: Array.h:86
const T * get_at(int pos) const
Definition: Array.h:105
Byte * at_(size_t i, size_t item_size)
void clear(bool all=true)
Definition: Array.h:67
void resize_(int new_size, size_t item_size, FunctionConstructor c, FunctionDestructor d)
size_t size() const
T * get_at(int pos)
Definition: Array.h:115
size_t reserved() const
Definition: BaseArray.h:114
Array()
Definition: Array.h:29
CMREXD AString & to_xml_string(AString &s) const
CMREXD long read_xml(IBStream &i)
Definition: Path.h:19
const T & current()
Definition: Array.h:339
T & operator[](int pos)
Definition: Array.h:125
CMREXD int write(const Path &file) const
Definition: Array.h:21
void begin_(iterator &)
Byte * add_(size_t nb, size_t item_size, Byte *data, FunctionConstructor c, FunctionDestructor d)
Array< T, A > & operator=(const Array< T, A > &v)
Definition: Array.h:50
const_iterator operator++(int)
Definition: Array.h:356
int compare(const Array< T, A > &a) const
Definition: Array.h:154
iterator begin()
Definition: Array.h:376
CMREXD int read(const Path &file)
~Array()
Definition: Array.h:59
unsigned char Byte
Definition: Type.h:64
CMREXD AString & toString(AString &dst) const
Definition: IOBStream.h:166
void resize(int n)
Definition: Array.h:78
T & current()
Definition: Array.h:286
void clear_(size_t item_size, FunctionDestructor d, bool all)
size_t reserve_(size_t nb, size_t item_size, FunctionConstructor c, FunctionDestructor d)
bool next()
Definition: Array.h:277
CMREXD long write_xml(OBStream &o) const
Definition: BaseArray.h:22
T & add(const T &v)
Definition: Array.h:95
Definition: IOBStream.h:29
Array(size_t nb)
Definition: Array.h:35
Definition: Array.h:270
int compare(const Array< T, A > &a, FunctionComparator cmor) const
Definition: Array.h:250
iterator operator++(int)
Definition: Array.h:303
const T & operator*()
Definition: Array.h:367