Cantera  2.0
vcs_DoubleStarStar.cpp
Go to the documentation of this file.
1 /**
2  * @file vcs_DoubleStarStar.cpp
3  *
4  * Header file for class DoubleStarStar
5  */
7 
8 namespace VCSnonideal
9 {
10 
11 //!Default constructor. Create an empty array.
13  m_nrows(0),
14  m_ncols(0)
15 {
16  m_data.clear();
17  m_colAddr.clear();
18 }
19 
20 /*
21  * Constructor. Create an \c m by \c n array, and initialize
22  * all elements to \c v.
23  */
24 DoubleStarStar::DoubleStarStar(size_t m, size_t n, double v) :
25  m_nrows(n),
26  m_ncols(m)
27 {
28  m_data.resize(n*m);
29  std::fill(m_data.begin(), m_data.end(), v);
30  m_colAddr.resize(m);
31  for (size_t jcol = 0; jcol < m_ncols; jcol++) {
32  m_colAddr[jcol] = &(m_data[jcol*m_nrows]);
33  }
34 }
35 
36 // copy constructor
38 {
39  m_nrows = y.m_nrows;
40  m_ncols = y.m_ncols;
41  m_data.resize(m_nrows*m_ncols);
42  m_data = y.m_data;
43  m_colAddr.resize(m_ncols);
44  if (!m_data.empty()) {
45  for (size_t jcol = 0; jcol < m_ncols; jcol++) {
46  m_colAddr[jcol] = &(m_data[jcol*m_nrows]);
47  }
48  }
49 }
50 
51 // assignment operator
53 {
54  if (&y == this) {
55  return *this;
56  }
57  m_nrows = y.m_nrows;
58  m_ncols = y.m_ncols;
59  m_data.resize(m_nrows*m_ncols);
60  m_data = y.m_data;
61  m_colAddr.resize(m_ncols);
62  if (!m_data.empty()) {
63  for (size_t jcol = 0; jcol < m_ncols; jcol++) {
64  m_colAddr[jcol] = &(m_data[jcol*m_nrows]);
65  }
66  }
67  return *this;
68 }
69 
70 
71 // resize the array, and fill the new entries with 'v'
72 /*
73  * @param n This is the number of rows
74  * @param m This is the number of columns in the new matrix
75  * @param v Default fill value -> defaults to zero.
76  */
77 void DoubleStarStar::resize(size_t m, size_t n, double v)
78 {
79  std::vector<double> old_data;
80  bool doCopy = false;
81  if (m_nrows > 0 && m_ncols > 0) {
82  if (m_nrows != n) {
83  doCopy = true;
84  old_data = m_data;
85  }
86  }
87  m_data.resize(n*m, v);
88  if (doCopy) {
89  if (n >= m_nrows && m >= m_ncols) {
90  for (size_t jcol = 0; jcol < m_ncols; jcol++) {
91  for (size_t irow = 0; irow < m_nrows; irow++) {
92  m_data[jcol*n + irow] = old_data[jcol*m_nrows + irow];
93  }
94  for (size_t irow = m_nrows; irow < n; irow++) {
95  m_data[jcol*n + irow] = v;
96  }
97  }
98  for (size_t jcol = m_ncols; jcol < m; jcol++) {
99  for (size_t irow = 0; irow < n; irow++) {
100  m_data[jcol*n + irow] = v;
101  }
102  }
103  } else {
104  std::fill(m_data.begin(), m_data.end(), v);
105  for (size_t jcol = 0; jcol < m_ncols; jcol++) {
106  for (size_t irow = 0; irow < m_nrows; irow++) {
107  m_data[jcol*n + irow] = old_data[jcol*m_nrows + irow];
108  }
109  }
110  }
111  }
112  m_nrows = n;
113  m_ncols = m;
114  m_colAddr.resize(m_ncols);
115  if (!m_data.empty()) {
116  for (size_t jcol = 0; jcol < m_ncols; jcol++) {
117  m_colAddr[jcol] = &(m_data[jcol*m_nrows]);
118  }
119  }
120 }
121 
122 double* DoubleStarStar::operator[](size_t jcol)
123 {
124  return m_colAddr[jcol];
125 }
126 
127 const double* DoubleStarStar::operator[](size_t jcol) const
128 {
129  return (const double*) m_colAddr[jcol];
130 }
131 
133 {
134  return (double* const*) &(m_colAddr[0]);
135 }
136 
137 double const* const* DoubleStarStar::constBaseDataAddr() const
138 {
139  return (double const* const*) &(m_colAddr[0]);
140 }
141 
142 // Number of rows
143 size_t DoubleStarStar::nRows() const
144 {
145  return m_nrows;
146 }
147 
148 // Number of columns
150 {
151  return m_ncols;
152 }
153 
154 }
155