Array4D.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 // To be included by Seldon.hxx
22 
23 
24 #ifndef SELDON_FILE_ARRAY4D_HXX
25 
26 #include "../share/Common.hxx"
27 #include "../share/Errors.hxx"
28 #include "../share/Allocator.hxx"
29 
30 namespace Seldon
31 {
32 
33 
35 
38  template <class T, class Allocator>
39  class Array4D
40  {
41  // typdef declarations.
42  public:
43  typedef typename Allocator::value_type value_type;
44  typedef typename Allocator::pointer pointer;
45  typedef typename Allocator::const_pointer const_pointer;
46  typedef typename Allocator::reference reference;
47  typedef typename Allocator::const_reference const_reference;
48 
49  // Attributes.
50  protected:
51  // Length along dimension #1.
52  int length1_;
53  // Length along dimension #2.
54  int length2_;
55  // Length along dimension #3.
56  int length3_;
57  // Length along dimension #3.
58  int length4_;
59 
60  // Size of a slice (i.e. length1_ by length2_).
61  long length34_;
62  // Size of a slice (i.e. length1_ by length2_ by length3_).
63  long length234_;
64 
65  // Pointer to stored elements.
66  pointer data_;
67 
68  // Methods.
69  public:
70  // Constructors.
71  explicit Array4D();
72  explicit Array4D(int i, int j, int k, int l);
74 
75  // Destructor. (inline)
76  ~Array4D();
77 
78  // Basic methods. (inline)
79  int GetLength1() const;
80  int GetLength2() const;
81  int GetLength3() const;
82  int GetLength4() const;
83  long GetSize() const;
84  long GetDataSize() const;
85  pointer GetData() const;
86  pointer GetDataPointer(int i, int j, int k, int l) const;
87 
88  // Memory management.
89  void Reallocate(int i, int j, int k, int l);
90  void Clear();
91 
92  // Element access and affectation. (inline)
93  reference operator() (int i, int j, int k, int l);
94 #ifndef SWIG
95  const_reference operator() (int i, int j, int k, int l) const;
97 #endif
98  void Copy(const Array4D<T, Allocator>& A);
99 
100  // Convenient functions.
101  size_t GetMemorySize() const;
102  void Zero();
103  void Fill();
104  template <class T0>
105  void Fill(const T0& x);
106  void FillRand();
107  void Print() const;
108 
109  // Input/output functions
110  void Write(string FileName, bool with_size = true) const;
111  void Write(ofstream& FileStream, bool with_size = true) const;
112  void Read(string FileName, bool with_size = true);
113  void Read(ifstream& FileStream, bool with_size = true);
114  };
115 
116 
117 #ifndef SWIG
118 
119  template <class T, class Allocator>
120  ostream& operator << (ostream& out,
121  const Array4D<T, Allocator>& A);
122 #endif
123 
124 } // namespace Seldon.
125 
126 
127 #define SELDON_FILE_ARRAY4D_HXX
128 #endif
Seldon::Array4D::Zero
void Zero()
Sets all elements to zero.
Definition: Array4D.cxx:254
Seldon::Array4D::Array4D
Array4D()
Default constructor.
Definition: Array4D.cxx:39
Seldon::Array4D::Copy
void Copy(const Array4D< T, Allocator > &A)
Duplicates a 4D array.
Definition: Array4D_Inline.cxx:221
Seldon::Array4D::operator()
reference operator()(int i, int j, int k, int l)
Access operator.
Definition: Array4D_Inline.cxx:165
Seldon::Array4D::GetLength3
int GetLength3() const
Returns the length in dimension #3.
Definition: Array4D_Inline.cxx:74
Seldon::Array4D::GetLength1
int GetLength1() const
Returns the length in dimension #1.
Definition: Array4D_Inline.cxx:52
Seldon::Array4D::~Array4D
~Array4D()
Destructor.
Definition: Array4D_Inline.cxx:36
Seldon::Array4D::GetDataPointer
pointer GetDataPointer(int i, int j, int k, int l) const
Returns a pointer to an element of data array.
Definition: Array4D_Inline.cxx:143
Seldon::Array4D::Print
void Print() const
Displays the array on the standard output.
Definition: Array4D.cxx:312
Seldon::Array4D::Reallocate
void Reallocate(int i, int j, int k, int l)
Reallocates memory to resize the 4D array.
Definition: Array4D.cxx:139
Seldon::Array4D::Fill
void Fill()
Fills the array.
Definition: Array4D.cxx:267
Seldon::Array4D::operator=
Array4D< T, Allocator > & operator=(const Array4D< T, Allocator > &A)
Duplicates a 4D array (assignment operator).
Definition: Array4D_Inline.cxx:207
Seldon::Array4D::GetMemorySize
size_t GetMemorySize() const
Returns the memory used by the object in bytes.
Definition: Array4D.cxx:242
Seldon::Array4D::GetLength4
int GetLength4() const
Returns the length in dimension #4.
Definition: Array4D_Inline.cxx:85
Seldon::Array4D::GetData
pointer GetData() const
Returns a pointer to the data array.
Definition: Array4D_Inline.cxx:126
Seldon::Array4D::GetLength2
int GetLength2() const
Returns the length in dimension #2.
Definition: Array4D_Inline.cxx:63
Seldon::Array4D
4D array.
Definition: Array4D.hxx:39
Seldon::Array4D::Write
void Write(string FileName, bool with_size=true) const
Writes the 4D array in a file.
Definition: Array4D.cxx:347
Seldon::Array4D::Read
void Read(string FileName, bool with_size=true)
Reads the 4D array from a file.
Definition: Array4D.cxx:420
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::Array4D::Clear
void Clear()
Clears the array.
Definition: Array4D.cxx:196
Seldon::Array4D::FillRand
void FillRand()
Fills the 4D array randomly.
Definition: Array4D.cxx:296
Seldon::Array4D::GetDataSize
long GetDataSize() const
Returns the number of elements stored in memory.
Definition: Array4D_Inline.cxx:112
Seldon::Array4D::GetSize
long GetSize() const
Returns the number of elements in the 4D array.
Definition: Array4D_Inline.cxx:98