MINT2
TwoDArray.h
Go to the documentation of this file.
1 #ifndef MINT_TWO_D_ARRAY_HH
2 #define MINT_TWO_D_ARRAY_HH
3 
4 #include <iostream>
5 
6 /*
7 MINT::TwoDArray is a simple template class that implements a
8 dynamically allocated array in such a way that it ensures that if you
9 pass it to any function that expects a simple C 2-D array, it will
10 work as such, i.e.
11 
12 MINT::TwoDArray<double> A(N, N);
13 this->mnemat(&A[0][0], A)
14 
15 will work. Note this is not guaranteed with
16 double A[N,N] (when N is not a const)
17 and certainly does not work with
18 std::vector<std::vector<double>>
19  A(N, std::vector<double>(M));
20 
21 Use like this
22 
23 MINT::TwoDArray<double> A(N, M);
24 or
25 MINT::TwoDArray<double> A(N, M, 0);
26 to initialise with 0.
27 Then like a normal array:
28 A[i][j] = 17.0
29 etc.
30 */
31 
32 namespace MINT{
33 
34  template<typename T>
35  class TwoDArray{
36  T* _m;
37  unsigned int _Nrow;
38  unsigned int _Ncol;
39  public:
40  unsigned int Nrow() const{return _Nrow;}
41  unsigned int Ncol() const{return _Ncol;}
42  unsigned int size() const{return _Nrow*_Ncol;}
43 
44  TwoDArray(unsigned int Nrow, unsigned int Ncol)
45  : _m(0), _Nrow(Nrow), _Ncol(Ncol){
46  _m = new T[size()];
47  }
48  TwoDArray(unsigned int Nrow, unsigned int Ncol, T initValue)
49  : _m(0), _Nrow(Nrow), _Ncol(Ncol){
50  _m = new T[size()];
51  for(unsigned int i=0; i < size(); i++) _m[i]=initValue;
52  }
53  TwoDArray(const TwoDArray& other)
54  : _m(0), _Nrow(other._Nrow), _Ncol(other._Ncol){
55  _m = new T[size()];
56  for(unsigned int i = 0; i < size(); i++){
57  _m[i] = other._m[i];
58  }
59  }
60 
61  ~TwoDArray(){if(0 != _m) delete[] _m;}
62 
63  T* operator[](unsigned int row){
64  unsigned int n = row * _Ncol;
65  if(n >= size()){
66  std::cout << "array size error in Mint::TwoDArray"
67  << " row = " << row << ", n = " << n << std::endl;
68  throw "crap";
69  }
70  return &(_m[n]);
71  }
72  const T* operator[](unsigned int row) const{
73  unsigned int n = row * _Ncol;
74  if(n >= size()){
75  std::cout << "array size error in Mint::TwoDArray"
76  << " row = " << row << ", n = " << n << std::endl;
77  throw "crap";
78  }
79  return &(_m[n]);
80  }
81 
82  T* c_array(){
83  return _m;
84  }
85  const T* c_array() const{
86  return _m;
87  }
88  };
89 
90 } // namespace MINT
91 
92 #endif
unsigned int size() const
Definition: TwoDArray.h:42
unsigned int Ncol() const
Definition: TwoDArray.h:41
TwoDArray(unsigned int Nrow, unsigned int Ncol)
Definition: TwoDArray.h:44
const T * c_array() const
Definition: TwoDArray.h:85
unsigned int _Ncol
Definition: TwoDArray.h:38
TwoDArray(const TwoDArray &other)
Definition: TwoDArray.h:53
unsigned int _Nrow
Definition: TwoDArray.h:37
const T * operator[](unsigned int row) const
Definition: TwoDArray.h:72
T * operator[](unsigned int row)
Definition: TwoDArray.h:63
TwoDArray(unsigned int Nrow, unsigned int Ncol, T initValue)
Definition: TwoDArray.h:48
unsigned int Nrow() const
Definition: TwoDArray.h:40