PSCF v1.1
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
15namespace Util
16{
17
23 template <typename Data>
24 class DRaggedMatrix : public RaggedMatrix<Data>
25 {
26
27 using RaggedMatrix<Data>::data_;
28 using RaggedMatrix<Data>::rows_;
29 using RaggedMatrix<Data>::capacity1_;
30 using RaggedMatrix<Data>::capacity2_;
31 using RaggedMatrix<Data>::capacity_;
32
33 public:
34
39
46
50 void allocate(DArray<int> const & 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>
130 { return !(data_ == 0); }
131
132}
133#endif
int capacity() const
Return allocated size.
Definition: Array.h:159
Dynamically allocatable contiguous array template.
Definition: DArray.h:32
Dynamically allocated RaggedMatrix.
Definition: DRaggedMatrix.h:25
bool isAllocated() const
Return true iff this DRaggedMatrix has been allocated.
void allocate(DArray< int > const &rowSizes)
Allocate memory for a ragged matrix.
Definition: DRaggedMatrix.h:90
~DRaggedMatrix()
Destructor.
Definition: DRaggedMatrix.h:73
DRaggedMatrix()
Constructor.
Definition: DRaggedMatrix.h:65
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 ** rows_
Array of pointers to rows.
Definition: RaggedMatrix.h:82
int capacity_
Total number of elements.
Definition: RaggedMatrix.h:91
Data * data_
One-dimensional C array of all elements.
Definition: RaggedMatrix.h:79
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
Utility classes for scientific computation.
Definition: accumulators.mod:1