PSCF v1.1
RaggedMatrix.h
1#ifndef UTIL_RAGGED_MATRIX_H
2#define UTIL_RAGGED_MATRIX_H
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/global.h>
12
13namespace Util
14{
15
28 template <typename Data>
30 {
31
32 public:
33
34 // Protected default constructor, to prohibit direct instantiation.
35
36 // Private copy constructor, to prohibit copy construction.
37
41 virtual ~RaggedMatrix();
42
48 int capacity1();
49
56 int capacity2(int i);
57
65 Data const & operator() (int i, int j) const;
66
74 Data& operator() (int i, int j);
75
76 protected:
77
79 Data* data_;
80
82 Data** rows_;
83
86
89
92
99
100 private:
101
107 RaggedMatrix(RaggedMatrix const & other);
108
109 };
110
111 // Method definitions
112
116 template <typename Data>
118 : data_(0),
119 rows_(0),
120 capacity2_(0),
121 capacity1_(0),
122 capacity_(0)
123 {}
124
125 /*
126 * Destructor.
127 */
128 template <typename Data>
130 {}
131
132 /*
133 * Get number of rows.
134 */
135 template <typename Data>
137 { return capacity1_; }
138
139 /*
140 * Get number of columns in row i.
141 */
142 template <typename Data>
144 { return capacity2_[i]; }
145
146 /*
147 * Return element (i,j) of matrix by const reference.
148 */
149 template <typename Data>
150 inline
151 Data const & RaggedMatrix<Data>::operator() (int i, int j) const
152 {
153 assert(data_ != 0);
154 assert(i >= 0);
155 assert(i < capacity1_);
156 assert(j >= 0);
157 assert(j < capacity2_[i]);
158 return *(rows_[i] + j);
159 }
160
161 /*
162 * Return element (i,j) of matrix by reference.
163 */
164 template <typename Data>
165 inline Data& RaggedMatrix<Data>::operator() (int i, int j)
166 {
167 assert(data_ != 0);
168 assert(i >= 0);
169 assert(i < capacity1_);
170 assert(j >= 0);
171 assert(j < capacity2_[i]);
172 return *(rows_[i] + j);
173 }
174
175}
176#endif
A 2D array in which different rows can have different lengths.
Definition: RaggedMatrix.h:30
int capacity1_
Number of rows (range of first index).
Definition: RaggedMatrix.h:88
int * capacity2_
Array containing number of elements in each row.
Definition: RaggedMatrix.h:85
Data const & operator()(int i, int j) const
Return element (i,j) of matrix by const reference.
Definition: RaggedMatrix.h:151
Data ** rows_
Array of pointers to rows.
Definition: RaggedMatrix.h:82
int capacity_
Total number of elements.
Definition: RaggedMatrix.h:91
RaggedMatrix()
Default constructor.
Definition: RaggedMatrix.h:117
int capacity1()
Get number of rows.
Definition: RaggedMatrix.h:136
int capacity2(int i)
Get number of elements in row number i.
Definition: RaggedMatrix.h:143
virtual ~RaggedMatrix()
Destructor.
Definition: RaggedMatrix.h:129
Data * data_
One-dimensional C array of all elements.
Definition: RaggedMatrix.h:79
File containing preprocessor macros for error handling.
Utility classes for scientific computation.
Definition: accumulators.mod:1