File: Array2D.h

package info (click to toggle)
lammps 20220106.git7586adbb6a%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 348,064 kB
  • sloc: cpp: 831,421; python: 24,896; xml: 14,949; f90: 10,845; ansic: 7,967; sh: 4,226; perl: 4,064; fortran: 2,424; makefile: 1,501; objc: 238; lisp: 163; csh: 16; awk: 14; tcl: 6
file content (193 lines) | stat: -rw-r--r-- 4,394 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifndef ARRAY2D_H
#define ARRAY2D_H

#include <cstdlib>
#include <cstdio>
#include <string>
#include <iostream>

#include "Array.h"

// for macros
#include "MatrixDef.h"

namespace ATC_matrix {

  /**
   *  @class  Array2D
   *  @brief  Base class for creating, sizing and operating on 2-D arrays of data
   */

template<typename T>
class Array2D {
public:
   Array2D();
   Array2D(int nrows, int ncols);
   Array2D(const Array2D<T>& A); // copy constructor
  ~Array2D();

   // Resize and reinitalize matrix
   void reset(int nrows, int ncols);
   // Access method to get the (i,j) element:
   T& operator() (int i, int j);
   // Access method to get the i-th col
   AliasArray<T> column(int i) const;
   // Access method to get the (i,j) element:
   const T& operator() (int i, int j) const;
   // Copy operator
   Array2D<T>& operator= (const Array2D<T>& other);
   // assignment operator
   Array2D<T>& operator= (const T other);
   // Get size of Array2D
   int nRows() const;
   int nCols() const;
   // Do I have this element?
   bool has_member(T val) const;
   // print
   void print(std::string name ="") const;
   // Dump templated type to disk; operation not safe for all types
   void write_restart(FILE *f) const;

private:
   int nrows_, ncols_;
   T *data_;
};

template<typename T>
Array2D<T>::Array2D() {
   nrows_ = 0;
   ncols_ = 0;
   data_  = nullptr;
}

template<typename T>
Array2D<T>::Array2D(int nrows, int ncols) {
   nrows_ = nrows;
   ncols_ = ncols;
   data_  = new T[nrows_ * ncols_];
}

template<typename T>
Array2D<T>::Array2D(const Array2D<T>& A) {
   nrows_ = A.nrows_;
   ncols_ = A.ncols_;
   if (A.data_==nullptr)
      data_ = nullptr;
   else {
      data_  = new T[nrows_ * ncols_];
      for(int i=0;i<nrows_*ncols_;i++)
         data_[i] = A.data_[i];
   }
}

template<typename T>
void Array2D<T>::reset(int nrows, int ncols) {
   if (nrows_ == nrows && ncols_ == ncols) { // no size change; don't realloc memory
      return;
   }
   else { // size changed; realloc memory
      nrows_ = nrows;
      ncols_ = ncols;
      if (data_ != nullptr)
         delete [] data_;
      if (ncols_ > 0 && nrows_ > 0)
         data_ = new T[nrows_ * ncols_];
      else {
         data_ = nullptr;
         nrows_ = 0;
         ncols_ = 0;
      }
   }
}

template<typename T>
T& Array2D<T>::operator() (int row, int col) {
   // Array bounds checking
   return data_[col*nrows_ + row];
}

template<typename T>
const T& Array2D<T>::operator() (int row, int col) const {
   // Array bounds checking
   return data_[col*nrows_ + row];
}

template<typename T>
AliasArray<T> Array2D<T>::column(int col) const {
   // Array bounds checking
   return AliasArray<T>(nrows_,&(data_[col*nrows_]));
}

template<typename T>
Array2D<T>& Array2D<T>::operator= (const Array2D<T>& other) {
   if (data_ == nullptr) {  // initialize my internal storage to match LHS
      nrows_ = other.nrows_;
      ncols_ = other.ncols_;
      if (other.data_==nullptr)
         data_ = nullptr;
      else
         data_  = new T[nrows_ * ncols_];
   }
   for(int i=0;i<nrows_*ncols_;i++)
      data_[i] = other.data_[i];
   return *this;
}

template<typename T>
Array2D<T>& Array2D<T>::operator= (const T other) {
  for(int i=0;i<nrows_*ncols_;i++)
     data_[i] = other;
  return *this;
}

template<typename T>
int Array2D<T>::nRows() const {
   return nrows_;
}

template<typename T>
int Array2D<T>::nCols() const {
   return ncols_;
}

template<typename T>
bool Array2D<T>::has_member(T val) const {
   int i;
   bool retval = false;
   for(i=0;i<nrows_*ncols_;i++)
      if (val == data_[i])
         retval = true;
   return(retval);
}

template<typename T>
void Array2D<T>::write_restart(FILE *f) const {
  fwrite(&nrows_,sizeof(int),1,f);
  fwrite(&ncols_,sizeof(int),1,f);
  if (nrows_*ncols_ > 0)
    fwrite(data_,sizeof(T),nrows_*ncols_,f);
}

template<typename T>
Array2D<T>::~Array2D() {
   if (data_ != nullptr)
     delete[] data_;
}

template<typename T>
void Array2D<T>::print(std::string name) const {
  std::cout << "------- Begin "<<name<<" -----------------\n";
  if (data_ != nullptr) {
    for(int col=0;col<ncols_;col++) {
      for(int row=0;row<nrows_;row++) {
       std::cout << data_[col*nrows_ + row] << " ";
      }
      std::cout << "\n";
    }
  }
  std::cout << "\n------- End "<<name<<" -------------------\n";

}

} // end namespace
#endif // Array2D.h