Cantera  2.0
vcs_IntStarStar.cpp
Go to the documentation of this file.
1 /**
2  * @file vcs_IntStarStar.cpp
3  *
4  * Header file for class IntStarStar
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 IntStarStar::IntStarStar(size_t m, size_t n, int 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  if (!m_data.empty()) {
32  for (size_t jcol = 0; jcol < m_ncols; jcol++) {
33  m_colAddr[jcol] = &(m_data[jcol*m_nrows]);
34  }
35  }
36 }
37 
38 // copy constructor
40 {
41  m_nrows = y.m_nrows;
42  m_ncols = y.m_ncols;
43  m_data.resize(m_nrows*m_ncols);
44  m_data = y.m_data;
45  m_colAddr.resize(m_ncols);
46  if (!m_data.empty()) {
47  for (size_t jcol = 0; jcol < m_ncols; jcol++) {
48  m_colAddr[jcol] = &(m_data[jcol*m_nrows]);
49  }
50  }
51 }
52 
53 // assignment operator
55 {
56  if (&y == this) {
57  return *this;
58  }
59  m_nrows = y.m_nrows;
60  m_ncols = y.m_ncols;
61  m_data.resize(m_nrows*m_ncols);
62  m_data = y.m_data;
63  m_colAddr.resize(m_ncols);
64  if (!m_data.empty()) {
65  for (size_t jcol = 0; jcol < m_ncols; jcol++) {
66  m_colAddr[jcol] = &(m_data[jcol*m_nrows]);
67  }
68  }
69  return *this;
70 }
71 
72 
73 //! resize the array, and fill the new entries with 'v'
74 /*!
75  * @param n This is the number of rows
76  * @param m This is the number of columns in the new matrix
77  * @param v Default fill value -> defaults to zero.
78  */
79 void IntStarStar::resize(size_t m, size_t n, int v)
80 {
81  std::vector<int> old_data;
82  bool doCopy = false;
83  if (m_nrows > 0 && m_ncols > 0) {
84  if (m_ncols != m) {
85  doCopy = true;
86  old_data = m_data;
87  }
88  }
89  m_data.resize(n*m, v);
90  if (doCopy) {
91  if (n >= m_nrows && m >= m_ncols) {
92  for (size_t jcol = 0; jcol < m_ncols; jcol++) {
93  for (size_t irow = 0; irow < m_nrows; irow++) {
94  m_data[jcol*m + irow] = old_data[jcol*m_ncols + irow];
95  }
96  for (size_t irow = m_nrows; irow < n; irow++) {
97  m_data[jcol*m + irow] = v;
98  }
99  }
100  for (size_t jcol = m_ncols; jcol < m; jcol++) {
101  for (size_t irow = 0; irow < n; irow++) {
102  m_data[jcol*m + irow] = v;
103  }
104  }
105  } else {
106  std::fill(m_data.begin(), m_data.end(), v);
107  for (size_t jcol = 0; jcol < m_ncols; jcol++) {
108  for (size_t irow = 0; irow < m_nrows; irow++) {
109  m_data[jcol*m + irow] = old_data[jcol*m_ncols + irow];
110  }
111  }
112  }
113  }
114  m_nrows = n;
115  m_ncols = m;
116  m_colAddr.resize(m_ncols);
117  for (size_t jcol = 0; jcol < m_ncols; jcol++) {
118  m_colAddr[jcol] = &(m_data[jcol*m_nrows]);
119  }
120 }
121 
122 int* IntStarStar::operator[](size_t jcol)
123 {
124  return m_colAddr[jcol];
125 }
126 
127 const int* IntStarStar::operator[](size_t jcol) const
128 {
129  return (const int*) m_colAddr[jcol];
130 }
131 
133 {
134  return (int* const*) &(m_colAddr[0]);
135 }
136 
137 /// Number of rows
138 size_t IntStarStar::nRows() const
139 {
140  return m_nrows;
141 }
142 
143 /// Number of columns
144 size_t IntStarStar::nColumns() const
145 {
146  return m_ncols;
147 }
148 
149 }
150 
151