Imagine++
Array.h
1 // ===========================================================================
2 // Imagine++ Libraries
3 // Copyright (C) Imagine
4 // For detailed information: http://imagine.enpc.fr/software
5 // ===========================================================================
6 
7 namespace Imagine {
10 
11 
20  template <typename T> class Array {
21  private:
22  int* _count; // pointer to reference counter
23  T *_data; // pointer to data
24  size_t _size; // size of array
25  // Allocates memory
26  void alloc(size_t size,T* ptr=0,bool handleDelete=false)
27  {
28  _size=size;
29  if (!ptr) {
30  if (size > 0)
31  {
32  _data= new T[size];
33  _count=new int(1);
34  }
35  else
36  {
37  _data = 0;
38  _count = 0;
39  }
40  }
41  else {
42  _data=ptr;
43  _count= (handleDelete? new int(1): 0);
44  }
45  }
46  // Frees memory unless reference counter indicates other users
47  void kill()
48  {
49  if (!_count)
50  return;
51  (*_count)--;
52  if (!(*_count)) {
53  delete _count;
54  delete[] _data;
55  _data = 0;
56  _count = 0;
57  _size = 0;
58  }
59  }
60  // Declares myself as a new user
61  void copy(const Array& A)
62  {
63  _count=A._count;
64  _data=A._data;
65  _size=A._size;
66  if(_count)
67  (*_count)++;
68  }
69  public:
71  typedef T* iterator;
73  typedef const T* const_iterator;
74 
80  Array(): _count(0), _data(0), _size(0) {}
87  explicit Array(size_t size) { alloc(size); }
99  Array(T* ptr, size_t size,bool handleDelete=false) { alloc(size,ptr,handleDelete); }
106  explicit Array(const Array& A) { copy(A); }
114  template <typename T2> Array(const Array<T2>& A) {
115  alloc(A.size());
116  for (size_t i=0;i<A.size();i++) _data[i] = T(A[i]) ;
117  }
125  Array(const std::list<T>& L) {
126  alloc(L.size());
127  size_t i=0;
128  for (typename std::list<T>::const_iterator it=L.begin();it!=L.end();++it,++i)
129  (*this)[i]= *it;
130  }
133  virtual ~Array() { kill(); }
143  void setSize(size_t size) {
144  if (size==_size) return;
145  kill(); alloc(size);
146  }
154  Array& operator = (const Array& A) { kill(); copy(A); return *this; }
163  template <typename T2> Array& operator = (const Array<T2>& A) {
164  kill();
165  alloc(A.size());
166  for (size_t i=0;i<A.size();i++) _data[i] = T( A[i] );
167  return *this;
168  }
175  Array clone() const {
176  Array A(_size);
177  std::copy( begin(), end(), A.begin() );
178  return A;
179  }
187  bool empty() const { return (_size==0); }
194  size_t size() const { return _size; }
202  const T& operator[](size_t i) const { assert(i<_size);return _data[i]; }
210  T& operator[](size_t i) { assert(i<_size); return _data[i]; }
217  T* data() { return _data; }
224  const T* data() const { return _data; }
231  iterator begin() { return _data; }
238  const_iterator begin() const { return _data; }
245  iterator end() { return _data+_size; }
252  const_iterator end() const { return _data+_size; }
261 
262  Array getSubArray(size_t offset, size_t size) const {
263  Array A(size);
264  for (size_t i=0;i<A.size();i++) {
265  A[i] = (*this)[i+offset];
266  }
267  return A;
268  }
276  Array& fill(const T& x) {
277  std::fill(begin(),end(),x);
278  return *this;
279  }
287  bool operator == (const Array& A) const {
288  if (A.size() != size()) return false;
289  for (size_t i=0;i<size();i++)
290  if (!(_data[i] == A[i]))
291  return false;
292  return true;
293  }
301  bool operator != (const Array& A) const {
302  return !(*this == A);
303  }
312  friend std::pair<T,T> range(const Array& A) {
313  const_iterator it = A.begin();
314  std::pair<T,T> r(*it,*it);
315  for ( ++it; it != A.end() ; ++it) {
316  r.first=std::min(r.first,*it);
317  r.second=std::max(r.second,*it);
318  }
319  return r;
320  }
329  friend std::pair<T,T> prange(const Array& A) {
330  const_iterator it = A.begin();
331  std::pair<T,T> r(*it,*it);
332  for ( ++it; it != A.end() ; ++it) {
333  r.first=pmin(r.first,*it);
334  r.second=pmax(r.second,*it);
335  }
336  return r;
337  }
346  friend void write(std::ostream& out,const Array& A) {
347  size_t n = A._size;
348  out.write((const char*)&n,sizeof(size_t));
349  out.write((const char*)A._data,(std::streamsize)A._size*sizeof(T));
350  }
359  friend void read(std::istream& in,Array& A) {
360  size_t n;
361  in.read((char*)&n,sizeof(size_t));
362  A.setSize(n);
363  in.read((char*)A._data,(std::streamsize)A._size*sizeof(T));
364  }
374  friend inline std::ostream& operator<<(std::ostream& out,const Array& A) {
375  out<<A.size()<<" ";
376  for (size_t i=0;i<A.size();i++) {
377  out<<A[i];
378  if (i<A.size()-1) out<<" ";
379  }
380  return out;
381  }
391  friend inline std::istream& operator>>(std::istream& in,Array& A) {
392  size_t n;
393  in>>n;
394  A.setSize(n);
395  for (size_t i=0;i<A.size();i++)
396  in>>A[i];
397  return in;
398  }
399  };
400 
401 
403 }
friend void read(std::istream &in, Array &A)
Binary read.
Definition: Array.h:359
const T & operator[](size_t i) const
Read access.
Definition: Array.h:202
Array clone() const
Cloning.
Definition: Array.h:175
friend std::ostream & operator<<(std::ostream &out, const Array &A)
ASCII write.
Definition: Array.h:374
Array(size_t size)
Constructor (known size).
Definition: Array.h:87
Array of variable size.
Definition: Array.h:20
Array & operator=(const Array &A)
Assignment.
Definition: Array.h:154
virtual ~Array()
Destructor.
Definition: Array.h:133
bool operator==(const Array &A) const
Equality test.
Definition: Array.h:287
size_t size() const
Size.
Definition: Array.h:194
Array()
Empty constructor.
Definition: Array.h:80
bool operator!=(const Array &A) const
Inequality test.
Definition: Array.h:301
friend std::pair< T, T > range(const Array &A)
Range.
Definition: Array.h:312
Array(T *ptr, size_t size, bool handleDelete=false)
Constructor (pre-allocated).
Definition: Array.h:99
friend std::istream & operator>>(std::istream &in, Array &A)
ASCII read.
Definition: Array.h:391
Array(const std::list< T > &L)
Constructor (from list).
Definition: Array.h:125
friend std::pair< T, T > prange(const Array &A)
Pointwise range.
Definition: Array.h:329
const_iterator end() const
End const iterator.
Definition: Array.h:252
const T * data() const
Data pointer (read).
Definition: Array.h:224
T * iterator
Iterator type.
Definition: Array.h:71
void setSize(size_t size)
Change size.
Definition: Array.h:143
T * data()
Data pointer (read/write).
Definition: Array.h:217
bool empty() const
Is empty.
Definition: Array.h:187
Array(const Array &A)
Copy constructor.
Definition: Array.h:106
Array & fill(const T &x)
Filling.
Definition: Array.h:276
Array getSubArray(size_t offset, size_t size) const
Sub array.
Definition: Array.h:262
T & operator[](size_t i)
Write access.
Definition: Array.h:210
iterator begin()
Begin iterator.
Definition: Array.h:231
Imagine++ namespace.
Definition: Array.h:7
const T * const_iterator
Const iterator type.
Definition: Array.h:73
const_iterator begin() const
Begin const iterator.
Definition: Array.h:238
iterator end()
End iterator.
Definition: Array.h:245
Array(const Array< T2 > &A)
Constructor (different type).
Definition: Array.h:114
friend void write(std::ostream &out, const Array &A)
Binary write.
Definition: Array.h:346