7
7
// ===----------------------------------------------------------------------===//
8
8
//
9
9
// This is a simple 2D matrix class that supports reading, writing, resizing,
10
- // swapping rows, and swapping columns. It can hold integers (MPInt) or rational
11
- // numbers (Fraction).
10
+ // swapping rows, and swapping columns.
12
11
//
13
12
// ===----------------------------------------------------------------------===//
14
13
15
14
#ifndef MLIR_ANALYSIS_PRESBURGER_MATRIX_H
16
15
#define MLIR_ANALYSIS_PRESBURGER_MATRIX_H
17
16
17
+ #include " mlir/Analysis/Presburger/MPInt.h"
18
18
#include " mlir/Support/LLVM.h"
19
- #include " mlir/Analysis/Presburger/Fraction.h"
20
- #include " mlir/Analysis/Presburger/Matrix.h"
21
19
#include " llvm/ADT/ArrayRef.h"
22
20
#include " llvm/Support/raw_ostream.h"
23
21
@@ -34,12 +32,7 @@ namespace presburger {
34
32
// / (i, j) is stored at data[i*nReservedColumns + j]. The reserved but unused
35
33
// / columns always have all zero values. The reserved rows are just reserved
36
34
// / space in the underlying SmallVector's capacity.
37
- // / This class only works for the types MPInt and Fraction, since the method
38
- // / implementations are in the Matrix.cpp file. Only these two types have
39
- // / been explicitly instantiated there.
40
- template <typename T>
41
35
class Matrix {
42
- static_assert (std::is_same_v<T,MPInt> || std::is_same_v<T,Fraction>, " T must be MPInt or Fraction." );
43
36
public:
44
37
Matrix () = delete ;
45
38
@@ -56,21 +49,21 @@ static_assert(std::is_same_v<T,MPInt> || std::is_same_v<T,Fraction>, "T must be
56
49
static Matrix identity (unsigned dimension);
57
50
58
51
// / Access the element at the specified row and column.
59
- T &at (unsigned row, unsigned column) {
52
+ MPInt &at (unsigned row, unsigned column) {
60
53
assert (row < nRows && " Row outside of range" );
61
54
assert (column < nColumns && " Column outside of range" );
62
55
return data[row * nReservedColumns + column];
63
56
}
64
57
65
- T at (unsigned row, unsigned column) const {
58
+ MPInt at (unsigned row, unsigned column) const {
66
59
assert (row < nRows && " Row outside of range" );
67
60
assert (column < nColumns && " Column outside of range" );
68
61
return data[row * nReservedColumns + column];
69
62
}
70
63
71
- T &operator ()(unsigned row, unsigned column) { return at (row, column); }
64
+ MPInt &operator ()(unsigned row, unsigned column) { return at (row, column); }
72
65
73
- T operator ()(unsigned row, unsigned column) const {
66
+ MPInt operator ()(unsigned row, unsigned column) const {
74
67
return at (row, column);
75
68
}
76
69
@@ -94,11 +87,11 @@ static_assert(std::is_same_v<T,MPInt> || std::is_same_v<T,Fraction>, "T must be
94
87
void reserveRows (unsigned rows);
95
88
96
89
// / Get a [Mutable]ArrayRef corresponding to the specified row.
97
- MutableArrayRef<T > getRow (unsigned row);
98
- ArrayRef<T > getRow (unsigned row) const ;
90
+ MutableArrayRef<MPInt > getRow (unsigned row);
91
+ ArrayRef<MPInt > getRow (unsigned row) const ;
99
92
100
93
// / Set the specified row to `elems`.
101
- void setRow (unsigned row, ArrayRef<T > elems);
94
+ void setRow (unsigned row, ArrayRef<MPInt > elems);
102
95
103
96
// / Insert columns having positions pos, pos + 1, ... pos + count - 1.
104
97
// / Columns that were at positions 0 to pos - 1 will stay where they are;
@@ -132,23 +125,23 @@ static_assert(std::is_same_v<T,MPInt> || std::is_same_v<T,Fraction>, "T must be
132
125
133
126
void copyRow (unsigned sourceRow, unsigned targetRow);
134
127
135
- void fillRow (unsigned row, const T &value);
136
- void fillRow (unsigned row, int64_t value) { fillRow (row, T (value)); }
128
+ void fillRow (unsigned row, const MPInt &value);
129
+ void fillRow (unsigned row, int64_t value) { fillRow (row, MPInt (value)); }
137
130
138
131
// / Add `scale` multiples of the source row to the target row.
139
- void addToRow (unsigned sourceRow, unsigned targetRow, const T &scale);
132
+ void addToRow (unsigned sourceRow, unsigned targetRow, const MPInt &scale);
140
133
void addToRow (unsigned sourceRow, unsigned targetRow, int64_t scale) {
141
- addToRow (sourceRow, targetRow, T (scale));
134
+ addToRow (sourceRow, targetRow, MPInt (scale));
142
135
}
143
136
// / Add `scale` multiples of the rowVec row to the specified row.
144
- void addToRow (unsigned row, ArrayRef<T > rowVec, const T &scale);
137
+ void addToRow (unsigned row, ArrayRef<MPInt > rowVec, const MPInt &scale);
145
138
146
139
// / Add `scale` multiples of the source column to the target column.
147
140
void addToColumn (unsigned sourceColumn, unsigned targetColumn,
148
- const T &scale);
141
+ const MPInt &scale);
149
142
void addToColumn (unsigned sourceColumn, unsigned targetColumn,
150
143
int64_t scale) {
151
- addToColumn (sourceColumn, targetColumn, T (scale));
144
+ addToColumn (sourceColumn, targetColumn, MPInt (scale));
152
145
}
153
146
154
147
// / Negate the specified column.
@@ -159,18 +152,18 @@ static_assert(std::is_same_v<T,MPInt> || std::is_same_v<T,Fraction>, "T must be
159
152
160
153
// / Divide the first `nCols` of the specified row by their GCD.
161
154
// / Returns the GCD of the first `nCols` of the specified row.
162
- T normalizeRow (unsigned row, unsigned nCols);
155
+ MPInt normalizeRow (unsigned row, unsigned nCols);
163
156
// / Divide the columns of the specified row by their GCD.
164
157
// / Returns the GCD of the columns of the specified row.
165
- T normalizeRow (unsigned row);
158
+ MPInt normalizeRow (unsigned row);
166
159
167
160
// / The given vector is interpreted as a row vector v. Post-multiply v with
168
161
// / this matrix, say M, and return vM.
169
- SmallVector<T , 8 > preMultiplyWithRow (ArrayRef<T > rowVec) const ;
162
+ SmallVector<MPInt , 8 > preMultiplyWithRow (ArrayRef<MPInt > rowVec) const ;
170
163
171
164
// / The given vector is interpreted as a column vector v. Pre-multiply v with
172
165
// / this matrix, say M, and return Mv.
173
- SmallVector<T , 8 > postMultiplyWithColumn (ArrayRef<T > colVec) const ;
166
+ SmallVector<MPInt , 8 > postMultiplyWithColumn (ArrayRef<MPInt > colVec) const ;
174
167
175
168
// / Given the current matrix M, returns the matrices H, U such that H is the
176
169
// / column hermite normal form of M, i.e. H = M * U, where U is unimodular and
@@ -199,7 +192,7 @@ static_assert(std::is_same_v<T,MPInt> || std::is_same_v<T,Fraction>, "T must be
199
192
unsigned appendExtraRow ();
200
193
// / Same as above, but copy the given elements into the row. The length of
201
194
// / `elems` must be equal to the number of columns.
202
- unsigned appendExtraRow (ArrayRef<T > elems);
195
+ unsigned appendExtraRow (ArrayRef<MPInt > elems);
203
196
204
197
// / Print the matrix.
205
198
void print (raw_ostream &os) const ;
@@ -218,7 +211,7 @@ static_assert(std::is_same_v<T,MPInt> || std::is_same_v<T,Fraction>, "T must be
218
211
219
212
// / Stores the data. data.size() is equal to nRows * nReservedColumns.
220
213
// / data.capacity() / nReservedColumns is the number of reserved rows.
221
- SmallVector<T , 16 > data;
214
+ SmallVector<MPInt , 16 > data;
222
215
};
223
216
224
217
} // namespace presburger
0 commit comments