stiffArray.hh

Go to the documentation of this file.
1 
8 #ifndef stiffArray_hh
9 #define stiffArray_hh
10 
11 #include "toolbox/array.hh"
12 #include "basics/debug.hh"
13 
14 #define StiffArrayConstr_D 0
15 #define StiffArrayDestr_D 0
16 
17 namespace concepts {
18 
19  // *********************************************************** StiffArray **
20 
28  template<uint dim, class F>
29  class StiffArray : public virtual OutputOperator {
30  public:
32  StiffArray() : data_(new F[dim]) {}
37  StiffArray(const F& dft) : data_(new F[dim]) { *this = dft; }
42  StiffArray(const Array<F>& dft) : data_(new F[dim]) {
43  memorycpy(data_, (const F*)dft, dim);
44  conceptsAssert(dft.size() == dim, Assertion());
45  }
51  template<class H>
52  StiffArray(const StiffArray<dim,H>& a, F fnc(const H&))
53  : data_(new F[dim]) {
54  F* d = data_; const H* e = (const H*)a;
55  for(uint i = dim; i--;) *d++ = fnc(*e++);
56  }
62  template<class H>
63  StiffArray(const StiffArray<dim,H>& a, const F& fnc(const H&))
64  : data_(new F[dim]) {
65  F* d = data_; const H* e = (const H*)a;
66  for(uint i = dim; i--;) *d++ = fnc(*e++);
67  }
69  StiffArray(const StiffArray<dim,F>& a) : data_(new F[dim]) {
70  std::memcpy(data_, a.data_, dim*sizeof(F));
71  }
73  DEBUGL(StiffArrayDestr_D, "data = " << data_);
74  delete[] data_;
75  DEBUGL(StiffArrayDestr_D, "done.");
76  }
77  uint length() { return dim; }
79  void zeros() { std::memset(data_, 0, dim*sizeof(F)); }
81  operator F*() { return data_; }
83  operator const F*() const { return data_; }
85  F* data() { return data_; }
87  const F* data() const { return data_; }
88 
89 
90  operator Array<F>() const { return Array<F>(data_,dim); }
91 
92  Array<F> array_data() const { return Array<F>(data_,dim); }
93 
95  const F& operator[] (const int i) const {
96  conceptsAssert3(i < (int)dim, Assertion(),
97  "i = " << i << ", dim = " << dim);
98  return data_[i];
99  }
101  F& operator[] (const int i) {
102  conceptsAssert3(i < (int)dim, Assertion(),
103  "i = " << i << ", dim = " << dim);
104  return data_[i];
105  }
106 
109  F* d = data_; for(uint i = dim; i--;) *d++ *= n;
110  return *this;
111  }
114  F* d = data_; for(uint i = dim; i--;) *d++ /= n;
115  return *this;
116  }
119  F* d = data_; for(uint i = dim; i--;) *d++ = n;
120  return *this;
121  }
124  F* d = data_; for(uint i = dim; i--;) *d++ += n;
125  return *this;
126  }
129  F* d = data_; for(uint i = dim; i--;) *d++ -= n;
130  return *this;
131  }
134  F* d = data_; const F* e = (const F*)a;
135  for(uint i = dim; i--;) *d++ *= *e++;
136  return *this;
137  }
140  StiffArray<dim,F>& apply(F& fnc(F&)) {
141  F* d = data_; for(uint i = dim; i--;) *d = fnc(*d++);
142  return *this;
143  }
144  protected:
145  virtual std::ostream& info(std::ostream& os) const;
146  private:
148  F* data_;
149  };
150 
151  template<uint dim, class F>
152  std::ostream& StiffArray<dim, F>::info(std::ostream& os) const {
153  os << concepts::typeOf(*this)<<"([";
154  F* d = data_;
155  for (uint i = dim; i--;)
156  os << *d++ << ((i == 0) ? "" : ", ");
157  return os << "])";
158  }
159 
160  // ****************************************************** StiffArray<1,F> **
161 
162  template<class F>
163  class StiffArray<1,F> : public virtual OutputOperator {
164  public:
171  StiffArray(const F& dft) : data_(dft) {}
176  StiffArray(const Array<F>& dft) {
177  conceptsAssert(dft.size() == 1, Assertion());
178  data_ = dft[0];
179  }
185  template<class H>
186  StiffArray(const StiffArray<1,H>& a, F fnc(const H&)) {
187  data_ = fnc(a);
188  }
192  DEBUGL(StiffArrayDestr_D, "done.");
193  }
194  uint length() { return 1; }
196  void zeros() { data_ = 0; }
198  operator F() const { return data_; }
199 
201  const F& operator[] (const int i) const {
202  conceptsAssert3(i == 0, Assertion(), "i = " << i);
203  return data_;
204  }
206  F& operator[] (const int i) {
207  conceptsAssert3(i == 0, Assertion(), "i = " << i);
208  return data_;
209  }
210 
213  data_ *= n; return *this;
214  }
217  data_ /= n; return *this;
218  }
221  data_ = n; return *this;
222  }
225  data_ += n; return *this;
226  }
229  data_ -= n; return *this;
230  }
233  StiffArray<1,F>& apply(F& fnc(F&)) {
234  data_ = fnc(data_); return *this;
235  }
236  protected:
237  virtual std::ostream& info(std::ostream& os) const;
238  private:
240  F data_;
241  };
242 
243  template<class F>
244  std::ostream& StiffArray<1, F>::info(std::ostream& os) const {
245  return os << concepts::typeOf(*this)<<"([" << data_ << "])";
246  }
247 
248  // ****************************************************** StiffArray<0,F> **
249 
254  template<class F>
255  class StiffArray<0,F> : public virtual OutputOperator {
256  public:
258  (concepts::MissingFeature("makes no sense")); }
259  };
260 
261 
262 } // namespace concepts
263 
264 #endif // stiffArray_hh
uint size() const
Returns the requested size of the array.
Definition: array.hh:259
StiffArray< 1, F > & operator+=(const F n)
Addition operator.
Definition: stiffArray.hh:224
#define conceptsException(exc)
Prepares an exception for throwing.
Definition: exceptions.hh:344
StiffArray(const StiffArray< 1, F > &a)
Copy constructor.
Definition: stiffArray.hh:190
StiffArray< 1, F > & apply(F &fnc(F &))
Application operator to each component, e.g.
Definition: stiffArray.hh:233
#define conceptsAssert(cond, exc)
Assert that a certain condition is fulfilled.
Definition: exceptions.hh:394
An array of objects of fix length, defined by template parameter dim.
Definition: stiffArray.hh:29
StiffArray(const F &dft)
Constructor.
Definition: stiffArray.hh:37
#define StiffArrayDestr_D
Definition: stiffArray.hh:15
void zeros()
Fills the memory with zeros.
Definition: stiffArray.hh:79
#define DEBUGL(doit, msg)
const F * data() const
Returns a pointer to the data in the array.
Definition: stiffArray.hh:87
StiffArray< dim, F > & apply(F &fnc(F &))
Application operator to each component, e.g.
Definition: stiffArray.hh:140
StiffArray< dim, F > & operator*=(const F n)
Scaling operator.
Definition: stiffArray.hh:108
F * data()
Returns a pointer to the data in the array.
Definition: stiffArray.hh:85
An array of objects.
Definition: bilinearForm.hh:23
Exception class for assertions.
Definition: exceptions.hh:258
StiffArray(const StiffArray< 1, H > &a, F fnc(const H &))
Constructor.
Definition: stiffArray.hh:186
StiffArray(const Array< F > &dft)
Constructor.
Definition: stiffArray.hh:176
StiffArray< dim, F > & operator/=(const F n)
Division operator.
Definition: stiffArray.hh:113
StiffArray(const StiffArray< dim, F > &a)
Copy constructor.
Definition: stiffArray.hh:69
StiffArray(const StiffArray< dim, H > &a, F fnc(const H &))
Constructor.
Definition: stiffArray.hh:52
const F & operator[](const int i) const
Index operator.
Definition: stiffArray.hh:95
StiffArray()
Constructor.
Definition: stiffArray.hh:32
StiffArray< dim, F > & operator*=(const StiffArray< dim, F > &a)
Multiplication operator.
Definition: stiffArray.hh:133
StiffArray(const StiffArray< dim, H > &a, const F &fnc(const H &))
Constructor.
Definition: stiffArray.hh:63
void memorycpy(F *dest, const G *src, size_t n)
Copies n entries from src to dest (faster than std::memcpy)
void zeros()
Fills the memory with zeros.
Definition: stiffArray.hh:196
Exception class to express a missing feature.
Definition: exceptions.hh:206
StiffArray< dim, F > & operator+=(const F n)
Addition operator.
Definition: stiffArray.hh:123
StiffArray< 1, F > & operator*=(const F n)
Scaling operator.
Definition: stiffArray.hh:212
StiffArray< 1, F > & operator/=(const F n)
Division operator.
Definition: stiffArray.hh:216
#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
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Definition: stiffArray.hh:152
StiffArray< 1, F > & operator-=(const F n)
Subtraction operator.
Definition: stiffArray.hh:228
Array< F > array_data() const
Definition: stiffArray.hh:92
StiffArray(const Array< F > &dft)
Constructor.
Definition: stiffArray.hh:42
Class providing an output operator.
StiffArray< 1, F > & operator=(const F n)
Assignement operator.
Definition: stiffArray.hh:220
StiffArray(const F &dft)
Constructor.
Definition: stiffArray.hh:171
StiffArray< dim, F > & operator-=(const F n)
Subtraction operator.
Definition: stiffArray.hh:128
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
StiffArray< dim, F > & operator=(const F n)
Assignement operator.
Definition: stiffArray.hh:118
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich