Simpatico  v1.10
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 
13 namespace 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  const Data& 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 
85  int* capacity2_;
86 
89 
91  int capacity_;
92 
98  RaggedMatrix();
99 
100  private:
101 
107  RaggedMatrix(const RaggedMatrix& 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 const Data& RaggedMatrix<Data>::operator() (int i, int j) const
151  {
152  assert(data_ != 0);
153  assert(i >= 0);
154  assert(i < capacity1_);
155  assert(j >= 0);
156  assert(j < capacity2_[i]);
157  return *(rows_[i] + j);
158  }
159 
160  /*
161  * Return element (i,j) of matrix by reference.
162  */
163  template <typename Data>
164  inline Data& RaggedMatrix<Data>::operator() (int i, int j)
165  {
166  assert(data_ != 0);
167  assert(i >= 0);
168  assert(i < capacity1_);
169  assert(j >= 0);
170  assert(j < capacity2_[i]);
171  return *(rows_[i] + j);
172  }
173 
174 }
175 #endif
Data * data_
One-dimensional C array of all elements.
Definition: RaggedMatrix.h:79
virtual ~RaggedMatrix()
Destructor.
Definition: RaggedMatrix.h:129
const Data & operator()(int i, int j) const
Return element (i,j) of matrix by const reference.
Definition: RaggedMatrix.h:150
A 2D array in which different rows can have different lengths.
Definition: RaggedMatrix.h:29
File containing preprocessor macros for error handling.
int capacity2(int i)
Get number of elements in row number i.
Definition: RaggedMatrix.h:143
int capacity1()
Get number of rows.
Definition: RaggedMatrix.h:136
Utility classes for scientific computation.
Definition: accumulators.mod:1
RaggedMatrix()
Default constructor.
Definition: RaggedMatrix.h:117
int * capacity2_
Array containing number of elements in each row.
Definition: RaggedMatrix.h:85
int capacity_
Total number of elements.
Definition: RaggedMatrix.h:91
Data ** rows_
Array of pointers to rows.
Definition: RaggedMatrix.h:82
int capacity1_
Number of rows (range of first index).
Definition: RaggedMatrix.h:88