Array3D.hxx
1 // Copyright (C) 2001-2009 Vivien Mallet
2 //
3 // This file is part of the linear-algebra library Seldon,
4 // http://seldon.sourceforge.net/.
5 //
6 // Seldon is free software; you can redistribute it and/or modify it under the
7 // terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 2.1 of the License, or (at your option)
9 // any later version.
10 //
11 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14 // more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with Seldon. If not, see http://www.gnu.org/licenses/.
18 
19 
20 // To be included by Seldon.hxx
21 
22 
23 #ifndef SELDON_FILE_ARRAY3D_HXX
24 
25 #include "../share/Common.hxx"
26 #include "../share/Errors.hxx"
27 #include "../share/Allocator.hxx"
28 
29 namespace Seldon
30 {
31 
32 
34 
37  template <class T, class Allocator>
38  class Array3D
39  {
40  // typdef declarations.
41  public:
42  typedef typename Allocator::value_type value_type;
43  typedef typename Allocator::pointer pointer;
44  typedef typename Allocator::const_pointer const_pointer;
45  typedef typename Allocator::reference reference;
46  typedef typename Allocator::const_reference const_reference;
47 
48  // Attributes.
49  protected:
50  // Length along dimension #1.
51  int length1_;
52  // Length along dimension #2.
53  int length2_;
54  // Length along dimension #3.
55  int length3_;
56 
57  // Size of a slice (i.e. length1_ by length2_).
58  long length23_;
59 
60  // Pointer to stored elements.
61  pointer data_;
62 
63  // Methods.
64  public:
65  // Constructors.
66  Array3D();
67  Array3D(int i, int j, int k);
69 
70  // Destructor. (inline)
71  ~Array3D();
72 
73  // Basic methods. (inline)
74  int GetLength1() const;
75  int GetLength2() const;
76  int GetLength3() const;
77  long GetSize() const;
78  long GetDataSize() const;
79  pointer GetData() const;
80  pointer GetDataPointer(int i, int j, int k) const;
81 
82  // Memory management.
83  void Reallocate(int i, int j, int k);
84  void SetData(int i, int j, int k, pointer data);
85  void Nullify();
86  void Clear();
87 
88  // Element access and affectation. (inline)
89  reference operator() (int i, int j, int k);
90 #ifndef SWIG
91  const_reference operator() (int i, int j, int k) const;
93 #endif
94  void Copy(const Array3D<T, Allocator>& A);
95 
96  // Convenient functions.
97  size_t GetMemorySize() const;
98  void Zero();
99  void Fill();
100  template <class T0>
101  void Fill(const T0& x);
102  void FillRand();
103  void Print() const;
104 
105  // Input/output functions
106  void Write(string FileName, bool with_size = true) const;
107  void Write(ofstream& FileStream, bool with_size = true) const;
108  void Read(string FileName, bool with_size = true);
109  void Read(ifstream& FileStream, bool with_size = true);
110  };
111 
112 
113 #ifndef SWIG
114  template <class T, class Allocator>
115  ostream& operator << (ostream& out,
116  const Array3D<T, Allocator>& A);
117 #endif
118 
119  template <class T0, class T, class Allocator>
120  void MltScalar(const T0& alpha, Array3D<T, Allocator>& A);
121 
122 } // namespace Seldon.
123 
124 
125 #define SELDON_FILE_ARRAY3D_HXX
126 #endif
Seldon::Array3D::Clear
void Clear()
Clears the array.
Definition: Array3D.cxx:228
Seldon::Array3D::Array3D
Array3D()
Default constructor.
Definition: Array3D.cxx:38
Seldon::Array3D::GetLength2
int GetLength2() const
Returns the length in dimension #2.
Definition: Array3D_Inline.cxx:62
Seldon::MltScalar
void MltScalar(const T0 &alpha, Array3D< T, Allocator > &A)
Multiplication of all elements of a 3D array by a scalar.
Definition: Array3D.cxx:539
Seldon::Array3D::SetData
void SetData(int i, int j, int k, pointer data)
Changes the size of the array and sets its data array (low level method).
Definition: Array3D.cxx:192
Seldon::Array3D::Write
void Write(string FileName, bool with_size=true) const
Writes the 3D array in a file.
Definition: Array3D.cxx:373
Seldon::Array3D::Copy
void Copy(const Array3D< T, Allocator > &A)
Duplicates a 3D array.
Definition: Array3D_Inline.cxx:204
Seldon::Array3D::GetLength1
int GetLength1() const
Returns the length in dimension #1.
Definition: Array3D_Inline.cxx:51
Seldon::Array3D::GetDataPointer
pointer GetDataPointer(int i, int j, int k) const
Returns a pointer to an element of data array.
Definition: Array3D_Inline.cxx:130
Seldon::Array3D::~Array3D
~Array3D()
Destructor.
Definition: Array3D_Inline.cxx:35
Seldon::Array3D::GetSize
long GetSize() const
Returns the number of elements in the 3D array.
Definition: Array3D_Inline.cxx:86
Seldon::Array3D::operator()
reference operator()(int i, int j, int k)
Access operator.
Definition: Array3D_Inline.cxx:151
Seldon::Array3D::GetLength3
int GetLength3() const
Returns the length in dimension #3.
Definition: Array3D_Inline.cxx:73
Seldon::Array3D::Print
void Print() const
Displays the array on the standard output.
Definition: Array3D.cxx:342
Seldon::Array3D::Reallocate
void Reallocate(int i, int j, int k)
Reallocates memory to resize the 3D array.
Definition: Array3D.cxx:127
Seldon::Array3D::GetDataSize
long GetDataSize() const
Returns the number of elements stored in memory.
Definition: Array3D_Inline.cxx:100
Seldon::Array3D::Nullify
void Nullify()
Clears the 3D array without releasing memory.
Definition: Array3D.cxx:212
Seldon::Array3D::Zero
void Zero()
Sets all elements to zero.
Definition: Array3D.cxx:283
Seldon::Array3D::GetData
pointer GetData() const
Returns a pointer to the data array.
Definition: Array3D_Inline.cxx:114
Seldon::Array3D::operator=
Array3D< T, Allocator > & operator=(const Array3D< T, Allocator > &A)
Duplicates a 3D array (assignment operator).
Definition: Array3D_Inline.cxx:190
Seldon::Array3D::Fill
void Fill()
Fills the array.
Definition: Array3D.cxx:296
Seldon::Array3D
3D array.
Definition: Array3D.hxx:38
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::Array3D::GetMemorySize
size_t GetMemorySize() const
Returns the memory used by the object in bytes.
Definition: Array3D.cxx:271
Seldon::Array3D::FillRand
void FillRand()
Fills the 3D array randomly.
Definition: Array3D.cxx:326
Seldon::Array3D::Read
void Read(string FileName, bool with_size=true)
Reads the 3D array from a file.
Definition: Array3D.cxx:444