Imagine++
FArray.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 
17  template <typename T,int S> class FArray {
18 
19  protected:
21  T _data[S];
22 
23  public:
25  typedef T* iterator;
27  typedef const T* const_iterator;
28 
34  FArray(){}
41  explicit FArray(const T& v) {
42  fill(v);
43  }
52  template <typename T2> FArray(const T2 t[S]) {
53  for (int i=0;i<S;i++) (*this)[i] = T(t[i]);
54  }
63  template <typename T2> FArray(const FArray<T2,S>& a) {
64  for (int i=0;i<S;i++) (*this)[i] = T(a[i]);
65  }
73  FArray(T v0, T v1) {
74  assert(S==2);
75  (*this)[0]=v0; (*this)[1]=v1;
76  }
85  FArray(T v0, T v1, T v2) {
86  assert(S==3);
87  (*this)[0]=v0; (*this)[1]=v1; (*this)[2]=v2;
88  }
98  FArray(T v0, T v1, T v2, T v3) {
99  assert(S==4);
100  (*this)[0]=v0; (*this)[1]=v1; (*this)[2]=v2; (*this)[3]=v3;
101  }
109  FArray& fill(const T& v) {
110  std::fill(begin(),end(),v);
111  return *this;
112  }
121  template <typename T2> FArray& copy(const T2 t[S]) {
122  for (int i=0;i<S;i++) (*this)[i] = T(t[i]);
123  return *this;
124  }
133  template <typename T2> FArray& copy(const FArray<T2,S>& b) {
134  for (int i=0;i<S;i++) (*this)[i] = T(b[i]);
135  return *this;
136  }
146  template <typename T2> FArray& operator = (const FArray<T2,S>& b) {
147  return copy(b);
148  }
152  bool empty() const { return false; }
159  int size() const { return S; }
167  const T& operator [] (int i) const {
168  assert(i>=0 && i<S);
169  return _data[i];
170  }
178  T& operator [] (int i) {
179  assert(i>=0 && i<S);
180  return _data[i];
181  }
188  const T* data() const { return _data; }
195  T* data() { return _data; }
202  iterator begin() { return _data; }
209  const_iterator begin() const { return _data; }
216  iterator end() { return _data+S; }
223  const_iterator end() const { return _data+S; }
231  bool operator == (const FArray& b) const {
232  return std::equal(begin(),end(),b.begin());
233  }
241  bool operator != (const FArray& b) const {
242  return !(*this == b);
243  }
251  friend std::pair<T,T> range(const FArray& a)
252  {
253  std::pair<T,T> r(a[0],a[0]);
254  for (const_iterator it = a.begin()+1 ; it != a.end() ; ++it)
255  {
256  r.first = std::min(r.first, *it);
257  r.second = std::max(r.second, *it);
258  }
259  return r;
260  }
269  friend inline void write(std::ostream& out,const FArray& a) {
270  out.write((const char*)a._data,(std::streamsize)S*sizeof(T));
271  }
280  friend inline void read(std::istream& in, FArray& a) {
281  in.read((char*)a._data,(std::streamsize)S*sizeof(T));
282  }
292  friend inline std::ostream& operator<<(std::ostream& out,const FArray& a) {
293  for (const_iterator it = a.begin() ; it != a.end() ; ++it)
294  out<<*it<<' ';
295  return out;
296  }
306  friend inline std::istream& operator>>(std::istream& in, FArray& a) {
307  for (iterator it = a.begin() ; it != a.end() ; ++it)
308  in>>*it;
309  return in;
310  }
311  };
312 
313 
315 }
const_iterator begin() const
Begin const iterator.
Definition: FArray.h:209
T * data()
Data pointer (write).
Definition: FArray.h:195
FArray(const T2 t[S])
Constructor from C array.
Definition: FArray.h:52
T _data[S]
internal storage.
Definition: FArray.h:21
iterator begin()
Begin iterator.
Definition: FArray.h:202
friend void write(std::ostream &out, const FArray &a)
Binary write.
Definition: FArray.h:269
friend std::istream & operator>>(std::istream &in, FArray &a)
ASCII read.
Definition: FArray.h:306
const T & operator[](int i) const
Read access.
Definition: FArray.h:167
bool operator!=(const FArray &b) const
Inequality test.
Definition: FArray.h:241
const T * const_iterator
Const iterator type.
Definition: FArray.h:27
bool empty() const
Is empty.
Definition: FArray.h:152
const_iterator end() const
End const iterator.
Definition: FArray.h:223
FArray()
Empty constructor.
Definition: FArray.h:34
friend std::ostream & operator<<(std::ostream &out, const FArray &a)
ASCII write.
Definition: FArray.h:292
FArray(T v0, T v1)
2D alias.
Definition: FArray.h:73
iterator end()
End iterator.
Definition: FArray.h:216
FArray & operator=(const FArray< T2, S > &b)
Assignment.
Definition: FArray.h:146
Array of fixed size.
Definition: FArray.h:17
FArray(const T &v)
Constructor with constant value.
Definition: FArray.h:41
int size() const
Size.
Definition: FArray.h:159
T * iterator
Iterator type.
Definition: FArray.h:25
bool operator==(const FArray &b) const
Equality test.
Definition: FArray.h:231
Imagine++ namespace.
Definition: Array.h:7
FArray(T v0, T v1, T v2, T v3)
4D alias.
Definition: FArray.h:98
const T * data() const
Data pointer (read).
Definition: FArray.h:188
FArray(const FArray< T2, S > &a)
Copy constructor.
Definition: FArray.h:63
FArray & fill(const T &v)
Filling.
Definition: FArray.h:109
FArray(T v0, T v1, T v2)
3D alias.
Definition: FArray.h:85
FArray & copy(const FArray< T2, S > &b)
Copy.
Definition: FArray.h:133
FArray & copy(const T2 t[S])
C array of different type copy.
Definition: FArray.h:121
friend void read(std::istream &in, FArray &a)
Binary read.
Definition: FArray.h:280
friend std::pair< T, T > range(const FArray &a)
Range.
Definition: FArray.h:251