array.hh

Go to the documentation of this file.
1 
9 #ifndef Array_hh
10 #define Array_hh
11 
12 #include <cstring>
13 #include <memory>
14 #include <iostream>
15 
16 #include "basics/typedefs.hh"
18 #include "basics/exceptions.hh"
19 #include "basics/pointerOutput.hh"
20 
21 // debugging
22 #define ArrayConstr_D 0
23 #define ArrayDestr_D 0
24 
25 namespace concepts {
26 
27  // **************************************** Output operator for unique_ptr's **
28 
31  template<class F>
32  std::ostream& operator<<(std::ostream& os, std::unique_ptr<F>& a) {
33  return os << concepts::typeOf(a);
34  }
35 
36  // ***************************************************************** Array **
37 
45  template<class F>
46  class Array {
47  public:
49  Array(const uint sz = 0) : data_(sz ? new F[sz] : 0),
50  size_(sz), n_(sz) {
51  DEBUGL(ArrayConstr_D, "start: size = " << sz << ", data = " << data_);
52  }
59  Array(const uint sz, const F& dft)
60  : data_(sz ? new F[sz] : 0), size_(sz), n_(sz) {
61  F* d = data_; for(uint i = size_; i--;) *d++ = dft;
62  }
70  Array(const F* dft, const uint sz)
71  : data_(sz ? new F[sz] : 0), size_(sz), n_(sz)
72  {
73  memorycpy(data_, dft, sz);
74  }
84  Array(const uint sz, const F& first, const F& diff) :
85  data_(sz ? new F[sz] : 0), size_(sz), n_(sz) {
86  if (sz > 0) {
87  F* d = data_, *e = data_;
88  *d++ = first;
89  for(uint i = sz - 1; i--;) *d++ = *e++ + diff;
90  }
91  }
97  template<class H>
98  Array(const Array<H>& a, F fnc(const H&)) : data_(0), size_(0), n_(0) {
99  apply(a, fnc);
100  }
101 
103  Array(const Array<F>& a)
104  : data_(a.n_ ? new F[a.n_] : 0), size_(a.n_), n_(a.n_) {
105  std::memcpy((void*)data_, (void*)a.data_, n_*sizeof(F));
106  }
108  template<class H>
109  Array(const Array<H>& a) : data_(0), size_(0), n_(0) { *this = a; }
110 
111  ~Array() {
112  DEBUGL(ArrayDestr_D, "start: size = " << size_ << ", n = " << n_
113  << ", data = " << data_);
114  delete[] data_;
115  DEBUGL(ArrayDestr_D, "done.");
116  }
117 
122  void resize(const uint sz);
126  void resizePreserve(const uint sz);
128  void zeros() { std::memset(data_, 0, n_*sizeof(F)); }
129 
131  operator F*() { return data_; }
133  operator const F*() const { return data_; }
134 
136  const F& operator[] (const int i) const {
137  conceptsAssert3(i < (int)size_, Assertion(),
138  "i = " << i << ", size = " << size_);
139  return data_[i];
140  }
142  F& operator[] (const int i) {
143  conceptsAssert3(i < (int)size_, Assertion(),
144  "i = " << i << ", size = " << size_);
145  return data_[i];
146  }
147 
149  template<class G>
150  Array<F>& operator*=(const G n) {
151  F* d = data_;
152  for(uint i = size_; i--;) *d++ *= n;
153  return *this;
154  }
155 
157  template<class H>
159  conceptsAssert(n_ <= a.size(), Assertion());
160  F* d = data_; const H* e = (const H*)a;
161  for(uint i = n_; i--;) *d++ *= *e++;
162  return *this;
163  }
164 
166  Array<F>& operator=(const F n) {
167  F* d = data_; for(uint i = size_; i--;) *d++ = n;
168  return *this;
169  }
172  resize(a.size());
173  std::memcpy((void*)data_, (void*)a.data_, n_*sizeof(F));
174  return *this;
175  }
177  template<class H>
179  resize(a.size());
180  F* d = data_; const H* e = (const H*)a;
181  for(uint i = size_; i--;) *d++ = *e++;
182  return *this;
183  }
185  Array<F>& operator+=(const F n) {
186  F* d = data_; for(uint i = size_; i--;) *d++ += n;
187  return *this;
188  }
190  template<class H>
192  if (size_ == 0 && a.size() > 0) *this = Array<F>(a.size(), F(0));
193  F* d = data_; const H* e = (const H*)a;
194  for(uint i = size_; i--;) *d++ += *e++;
195  return *this;
196  }
198  Array<F>& operator-=(const F n) {
199  F* d = data_; for(uint i = size_; i--;) *d++ -= n;
200  return *this;
201  }
203  template<class H>
205  if (size_ == 0 && a.size() > 0) *this = Array<F>(a.size(), F(0));
206  F* d = data_; const H* e = (const H*)a;
207  for(uint i = size_; i--;) *d++ -= *e++;
208  return *this;
209  }
211  Array<F> operator-() const {
212  Array<F> a(size_);
213  F* d = (F*)a; const F* e = data_;
214  for(uint i = size_; i--;) *d++ = -*e++;
215  return a;
216  }
219  Array<F>& apply(F& fnc(F&)) {
220  F* d = data_;
221  for(uint i = size_; i--;++d) *d = fnc(*d);
222  return *this;
223  }
232  template<class H>
233  Array<F>& apply(const Array<H>& a, F fnc(const H&)) {
234  resize(a.size());
235  F* d = data_; const H* e = (const H*)a;
236  for(uint i = size_; i--; ++d)
237  *d = fnc(*e++);
238  return *this;
239  }
248  template<class H>
249  Array<F>& apply(const Array<H>& a, F fnc(const H&, const F&)) {
250  resize(a.size());
251  F* d = data_; const H* e = (const H*)a;
252  for(uint i = n_; i--;++d) {
253  *d = fnc(*e++, *d);
254  }
255  return *this;
256  }
257 
259  uint size() const { return n_; }
260 
262  uint cursize() const { return size_;}
263 
265  int memory() const { return size_*sizeof(F); }
266 
269 
270  std::ostream& info(std::ostream& os) const;
271  protected:
273  F* data_;
275  uint size_;
277  uint n_;
278  };
279 
280  template<class F>
281  void Array<F>::resize(const uint sz) {
282  if (sz > size_) {
283  if (size_ > 0) delete[] data_;
284  data_ = new F[size_ = sz];
285  }
286  n_ = sz;
287  }
288 
289  template<class F>
290  void Array<F>::resizePreserve(const uint sz) {
291  if (sz > size_) {
292  F* data = new F[sz];
293  std::memcpy((void*)data, (void*)data_, n_*sizeof(F));
294  delete[] data_;
295  data_ = data;
296  size_ = sz;
297  }
298  n_ = sz;
299  }
300 
301  template<class F>
302  std::ostream& Array<F>::info(std::ostream& os) const {
303  os << "Array<F>(" << n_ << ", [";
304  F* d = data_;
305  for (uint i = n_; i--;)
306  os << *d++ << ((i == 0) ? "" : ", ");
307  return os << "])";
308  }
309 
310  template<class F>
312  F *d = data_, *e = data_ + (n_-1);
313  for(uint i = std::floor(double(n_)/2.0); i--; )
314  std::swap(*d++, *e--);
315  return *this;
316  }
317 
318  template<class F>
319  std::ostream& operator<<(std::ostream& os, const Array<F>& o) {
320 #ifdef DEBUG
321  os << std::flush;
322 #endif
323  return o.info(os);
324  }
325 
326  template <class F>
327  inline bool operator==(const Array<F>& x, const Array<F>& y)
328  {
329  uint n = x.size();
330  if (n != y.size()) return false;
331  const F* d = (const F*)x; const F* e = (const F*)y;
332  for (uint i = n; i--;)
333  if (*d++ != *e++) return false;
334  return true;
335  }
336 
337  template <class F>
338  inline bool operator==(const Array<F>& x, F& y)
339  {
340  uint n = x.size();
341  const F* d = (const F*)x;
342  for (uint i = n; i--;)
343  if (*d++ != y) return false;
344  return true;
345  }
346 
347  template <class F>
348  inline bool operator==(F& y, const Array<F>& x)
349  {
350  return x == y;
351  }
352 
358  template<class F, class G>
360  operator*(const concepts::Array<F>& array, const G& val) {
362  typename Combtype<F,G>::type* r = (typename Combtype<F,G>::type*)result;
363  const F* a = (const F*)array;
364  for(uint i = array.size(); i--;) *r++ = *a++ * val;
365  return result;
366  }
367 
373  template<class F, class G>
374  Array<typename Combtype<F,G>::type>
375  operator*(const G& val, const Array<F>& array) {
376  return array * val;
377  }
378 
379  // ************************************************************** Realtype **
380 
381  template<typename F>
382  struct Realtype<Array<F> > {
383  typedef typename Realtype<F>::type type;
384  };
385 
386  template<class F>
387  void pointerOutput(std::ostream& os, const Array<F>& array) {
388  os << "Array<F>(" << array.size() << ", [";
389  const F* d = (const F*)array;
390  for (uint i = array.size(); i--;) {
391  pointerOutput(os, *d++);
392  os << ((i == 0) ? "" : ", ");
393  }
394  os << "])";
395  }
396 
397 } // namespace concepts
398 
399 #endif // Array_hh
std::ostream & info(std::ostream &os) const
Definition: array.hh:302
uint size() const
Returns the requested size of the array.
Definition: array.hh:259
Array(const Array< F > &a)
Copy constructor.
Definition: array.hh:103
#define ArrayConstr_D
Definition: array.hh:22
Array< F > & operator-=(const F n)
Subtraction operator.
Definition: array.hh:198
uint cursize() const
Returns the size of the allocated memory.
Definition: array.hh:262
int memory() const
Returns the memory usage in bytes.
Definition: array.hh:265
Array< F > & apply(const Array< H > &a, F fnc(const H &, const F &))
Application operator to each component.
Definition: array.hh:249
void pointerOutput(std::ostream &os, const F &val)
Array< F > & operator*=(const G n)
Scaling operator.
Definition: array.hh:150
Array< F > & operator-=(const Array< H > &a)
Subtraction operator.
Definition: array.hh:204
Point< typename Combtype< F, Real >::type, dim > operator*(const Real x, const Point< F, dim > &y)
Realtype< F >::type type
Definition: array.hh:383
#define conceptsAssert(cond, exc)
Assert that a certain condition is fulfilled.
Definition: exceptions.hh:394
Array(const uint sz, const F &first, const F &diff)
Constructor for equidistant values.
Definition: array.hh:84
Array< F > & operator=(const F n)
Assignement operator.
Definition: array.hh:166
uint size_
Current real size of the array.
Definition: array.hh:275
#define DEBUGL(doit, msg)
const F & operator[](const int i) const
Index operator.
Definition: array.hh:136
F * data_
Data.
Definition: array.hh:273
Array< F > & operator=(const Array< H > &a)
Assignement operator.
Definition: array.hh:178
Array(const Array< H > &a)
Type conversion constructor.
Definition: array.hh:109
#define ArrayDestr_D
Definition: array.hh:23
An array of objects.
Definition: bilinearForm.hh:23
Array(const F *dft, const uint sz)
Constructor.
Definition: array.hh:70
Array< F > & operator+=(const Array< H > &a)
Addition operator.
Definition: array.hh:191
Exception class for assertions.
Definition: exceptions.hh:258
std::ostream & operator<<(std::ostream &os, const Level< dim > &c)
Array< F > & operator+=(const F n)
Addition operator.
Definition: array.hh:185
Array(const uint sz=0)
Constructor.
Definition: array.hh:49
Array< F > & apply(const Array< H > &a, F fnc(const H &))
Application operator to each component.
Definition: array.hh:233
Taking for a complex type the appropiate real type and for a real type itself.
Definition: typedefs.hh:100
void memorycpy(F *dest, const G *src, size_t n)
Copies n entries from src to dest (faster than std::memcpy)
Array< F > & apply(F &fnc(F &))
Application operator to each component, e.g.
Definition: array.hh:219
void resize(const uint sz)
Resizes the array.
Definition: array.hh:281
bool operator==(const Point< F, dim > &x, const Point< F, dim > &y)
Array< F > & operator*=(const Array< H > &a)
Multiplication operator.
Definition: array.hh:158
void resizePreserve(const uint sz)
Resizes the array.
Definition: array.hh:290
uint n_
Requested size of the array.
Definition: array.hh:277
Array< F > & operator=(const Array< F > &a)
Assignement operator.
Definition: array.hh:171
Array< F > operator-() const
Negation operator.
Definition: array.hh:211
void zeros()
Fills the memory with zeros.
Definition: array.hh:128
#define conceptsAssert3(cond, exc, msg)
Assert that a certain condition is fulfilled.
Definition: exceptions.hh:442
std::string typeOf(const T &t)
Return the typeid name of a class object.
Definition: output.hh:43
Array< F > & reverse()
Reverse the order of the entries.
Definition: array.hh:311
Array(const uint sz, const F &dft)
Constructor.
Definition: array.hh:59
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
Array(const Array< H > &a, F fnc(const H &))
Constructor by applying a function to another array.
Definition: array.hh:98
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich