Array.hxx
1 // Copyright (C) 2010 Lin Wu
2 // Copyright (C) 2001-2009 Vivien Mallet
3 //
4 // This file is part of the linear-algebra library Seldon,
5 // http://seldon.sourceforge.net/.
6 //
7 // Seldon is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 2.1 of the License, or (at your option)
10 // any later version.
11 //
12 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 // more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with Seldon. If not, see http://www.gnu.org/licenses/.
19 
20 
21 #ifndef SELDON_FILE_ARRAY_HXX
22 
23 
24 namespace Seldon
25 {
26 
27 
29 
32  template <class T, int N, class Allocator>
33  class Array
34  {
35  // typdef declarations.
36  public:
37  typedef typename Allocator::value_type value_type;
38  typedef typename Allocator::pointer pointer;
39  typedef typename Allocator::const_pointer const_pointer;
40  typedef typename Allocator::reference reference;
41  typedef typename Allocator::const_reference const_reference;
42 
43  // Attributes.
44  protected:
45 
46  // Length along dimensions.
47  int length_[N];
48  // Sizes of slices for dimension 1, 2, ..., N.
49  long offset_[N];
50  // Pointer to stored elements.
51  pointer data_;
52 
53  // Methods.
54  public:
55  // Constructors.
56  Array();
57  Array(int i);
58  Array(int i, int j, int k);
59  Array(int i, int j, int k, int l);
60  Array(int i, int j, int k, int l, int m);
61  Array(int i, int j, int k, int l, int m, int n);
62  Array(int i, int j, int k, int l, int m, int n, int o);
63  Array(int i, int j, int k, int l, int m, int n, int o, int p);
64  Array(int i, int j, int k, int l, int m, int n, int o, int p, int q);
65  // Array(int* extent);
66  Array(const Array<T, N, Allocator>& A);
67 
68  // Destructor. (inline)
69  ~Array();
70 
71  // Inline methods.
72  int GetLength(int dimension) const;
73  long GetSize() const;
74  long GetDataSize() const;
75  pointer GetData() const;
76 
77  // Memory management.
78  void Reallocate(int i, int j, int k);
79  void Reallocate(int i, int j, int k, int l);
80  void Reallocate(int i, int j, int k, int l, int m);
81  void Reallocate(int i, int j, int k, int l, int m, int n);
82  void Reallocate(int i, int j, int k, int l, int m, int n, int o);
83  void Reallocate(int i, int j, int k, int l, int m, int n, int o, int p);
84  void Reallocate(int i, int j, int k, int l, int m, int n, int o, int p,
85  int q);
86  void Clear();
87 
88  // Element access and affectation. (inline)
89  reference operator() (int i, int j, int k);
90  reference operator() (int i, int j, int k, int l);
91  reference operator() (int i, int j, int k, int l, int m);
92  reference operator() (int i, int j, int k, int l, int m, int n);
93  reference operator() (int i, int j, int k, int l, int m, int n, int o);
94  reference operator() (int i, int j, int k, int l, int m, int n, int o,
95  int p);
96  reference operator() (int i, int j, int k, int l, int m, int n, int o,
97  int p, int q);
98 #ifndef SWIG
99  const_reference operator() (int i, int j, int k) const;
100  const_reference operator() (int i, int j, int k, int l) const;
101  const_reference operator() (int i, int j, int k, int l, int m) const;
102  const_reference operator()
103  (int i, int j, int k, int l, int m, int n) const;
104  const_reference operator()
105  (int i, int j, int k, int l, int m, int n, int o) const;
106  const_reference operator()
107  (int i, int j, int k, int l, int m, int n, int o, int p) const;
108  const_reference operator()
109  (int i, int j, int k, int l, int m, int n, int o, int p, int q) const;
111 #endif
112  void Copy(const Array<T, N, Allocator>& A);
113 
114  // Convenient functions.
115  size_t GetMemorySize() const;
116  void Zero();
117  void Fill();
118  template <class T0>
119  void Fill(const T0& x);
120  void FillRand();
121  void Print() const;
122 
123  // Input/output functions
124  void Write(string FileName, bool with_size = true) const;
125  void Write(ofstream& FileStream, bool with_size = true) const;
126  void Read(string FileName, bool with_size = true);
127  void Read(ifstream& FileStream, bool with_size = true);
128  };
129 
130 
131 #ifndef SWIG
132  template <class T, int N, class Allocator>
133  ostream& operator << (ostream& out,
134  const Array<T, N, Allocator>& A);
135 #endif
136 
137 
138 } // namespace Seldon.
139 
140 
141 #define SELDON_FILE_ARRAY_HXX
142 #endif
Seldon::Array::Clear
void Clear()
Clears the array.
Definition: Array.cxx:1053
Seldon::Array::GetDataSize
long GetDataSize() const
Returns the number of elements stored in memory.
Definition: ArrayInline.cxx:77
Seldon::Array::GetLength
int GetLength(int dimension) const
Returns the length in dimension #1.
Definition: ArrayInline.cxx:50
Seldon::Array::Read
void Read(string FileName, bool with_size=true)
Reads the array from a file.
Definition: Array.cxx:1473
Seldon::Array
Multi-dimensional Array.
Definition: Array.hxx:33
Seldon::Array::GetMemorySize
size_t GetMemorySize() const
Returns the memory used by the object in bytes.
Definition: Array.cxx:1126
Seldon::Array::~Array
~Array()
Destructor.
Definition: ArrayInline.cxx:34
Seldon::Array::operator=
Array< T, N, Allocator > & operator=(const Array< T, N, Allocator > &A)
Duplicates an array (assignment operator).
Definition: ArrayInline.cxx:526
Seldon::Array::Zero
void Zero()
Sets all elements to zero.
Definition: Array.cxx:1140
Seldon::Array::Write
void Write(string FileName, bool with_size=true) const
Writes the array in a file.
Definition: Array.cxx:1406
Seldon::Array::Reallocate
void Reallocate(int i, int j, int k)
Reallocates memory to resize the 3D array.
Definition: Array.cxx:532
Seldon::Array::Array
Array()
Default constructor.
Definition: Array.cxx:37
Seldon::Array::FillRand
void FillRand()
Fills the array randomly.
Definition: Array.cxx:1181
Seldon::Array::Fill
void Fill()
Fills the array.
Definition: Array.cxx:1153
Seldon::Array::Copy
void Copy(const Array< T, N, Allocator > &A)
Duplicates an array.
Definition: Array.cxx:1085
Seldon::Array::Print
void Print() const
Displays the array on the standard output.
Definition: Array.cxx:1196
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::operator<<
ostream & operator<<(ostream &out, const Array< T, N, Allocator > &A)
operator<< overloaded for a 3D array.
Definition: Array.cxx:1617
Seldon::Array::GetData
pointer GetData() const
Returns a pointer to the data array.
Definition: ArrayInline.cxx:91
Seldon::Array::GetSize
long GetSize() const
Returns the number of elements in the 3D array.
Definition: ArrayInline.cxx:63
Seldon::Array::operator()
reference operator()(int i, int j, int k)
Access operator.
Definition: ArrayInline.cxx:112