Simpatico  v1.10
DRaggedMatrix.h
1 #ifndef UTIL_D_RAGGED_MATRIX_H
2 #define UTIL_D_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/containers/RaggedMatrix.h>
12 #include <util/containers/DArray.h>
13 #include <util/global.h>
14 
15 namespace Util
16 {
17 
23  template <typename Data>
24  class DRaggedMatrix : public RaggedMatrix<Data>
25  {
26 
32 
33  public:
34 
38  DRaggedMatrix();
39 
46 
50  void allocate(const DArray<int>& rowSizes);
51 
55  bool isAllocated() const;
56 
57  };
58 
59  // Method definitions
60 
61  /*
62  * Default constructor.
63  */
64  template <typename Data>
66  RaggedMatrix<Data>()
67  {}
68 
69  /*
70  * Destructor.
71  */
72  template <typename Data>
74  {
75  if (data_) {
76  Memory::deallocate<Data>(data_, capacity_);
77  }
78  if (rows_) {
79  Memory::deallocate<Data*>(rows_, capacity1_);
80  }
81  if (capacity2_) {
82  Memory::deallocate<int>(capacity2_, capacity1_);
83  }
84  }
85 
86  /*
87  * Allocate memory and set row sizes.
88  */
89  template <typename Data>
91  {
92  if (data_ != 0)
93  UTIL_THROW("Attempt to re-allocate a RaggedMatrix");
94 
95  if (rowSizes.capacity() <= 0)
96  UTIL_THROW("rowSizes.capacity() must be positive");
97 
98  // Calculate total number of elements (all rows)
99  capacity_ = 0;
100  for (int i = 0; i < rowSizes.capacity(); ++i) {
101  if (rowSizes[i] < 0)
102  UTIL_THROW("rowSizes must all be nonnegative");
103  capacity_ += rowSizes[i];
104  }
105 
106  if (capacity_ == 0)
107  UTIL_THROW("Sum of row sizes must be positive");
108 
109  // Allocate all memory
110  capacity1_ = rowSizes.capacity();
111  Memory::allocate<int>(capacity2_, capacity1_);
112  Memory::allocate<Data*>(rows_, capacity1_);
113  Memory::allocate<Data>(data_, capacity_);
114 
115  // Set row sizes and pointers to rows
116  Data* ptr = data_;
117  for (int i = 0; i < capacity1_; ++i) {
118  capacity2_[i] = rowSizes[i];
119  rows_[i] = ptr;
120  ptr += rowSizes[i];
121  }
122 
123  }
124 
125  /*
126  * Return true if the DRaggedMatrix has been allocated, false otherwise.
127  */
128  template <class Data>
129  inline bool DRaggedMatrix<Data>::isAllocated() const
130  { return !(data_ == 0); }
131 
132 }
133 #endif
Data * data_
One-dimensional C array of all elements.
Definition: RaggedMatrix.h:79
~DRaggedMatrix()
Destructor.
Definition: DRaggedMatrix.h:73
A 2D array in which different rows can have different lengths.
Definition: RaggedMatrix.h:29
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
void allocate(const DArray< int > &rowSizes)
Allocate memory for a ragged matrix.
Definition: DRaggedMatrix.h:90
Utility classes for scientific computation.
Definition: accumulators.mod:1
int * capacity2_
Array containing number of elements in each row.
Definition: RaggedMatrix.h:85
bool isAllocated() const
Return true iff this DRaggedMatrix has been allocated.
int capacity_
Total number of elements.
Definition: RaggedMatrix.h:91
Data ** rows_
Array of pointers to rows.
Definition: RaggedMatrix.h:82
Dynamically allocated RaggedMatrix.
Definition: DRaggedMatrix.h:24
DRaggedMatrix()
Constructor.
Definition: DRaggedMatrix.h:65
int capacity() const
Return allocated size.
Definition: Array.h:153
int capacity1_
Number of rows (range of first index).
Definition: RaggedMatrix.h:88