77// ===----------------------------------------------------------------------===//
88//
99// 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.
1211//
1312// ===----------------------------------------------------------------------===//
1413
1514#ifndef MLIR_ANALYSIS_PRESBURGER_MATRIX_H
1615#define MLIR_ANALYSIS_PRESBURGER_MATRIX_H
1716
17+ #include " mlir/Analysis/Presburger/MPInt.h"
1818#include " mlir/Support/LLVM.h"
19- #include " mlir/Analysis/Presburger/Fraction.h"
20- #include " mlir/Analysis/Presburger/Matrix.h"
2119#include " llvm/ADT/ArrayRef.h"
2220#include " llvm/Support/raw_ostream.h"
2321
@@ -34,12 +32,7 @@ namespace presburger {
3432// / (i, j) is stored at data[i*nReservedColumns + j]. The reserved but unused
3533// / columns always have all zero values. The reserved rows are just reserved
3634// / 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>
4135class Matrix {
42- static_assert (std::is_same_v<T,MPInt> || std::is_same_v<T,Fraction>, " T must be MPInt or Fraction." );
4336public:
4437 Matrix () = delete ;
4538
@@ -56,21 +49,21 @@ static_assert(std::is_same_v<T,MPInt> || std::is_same_v<T,Fraction>, "T must be
5649 static Matrix identity (unsigned dimension);
5750
5851 // / Access the element at the specified row and column.
59- T &at (unsigned row, unsigned column) {
52+ MPInt &at (unsigned row, unsigned column) {
6053 assert (row < nRows && " Row outside of range" );
6154 assert (column < nColumns && " Column outside of range" );
6255 return data[row * nReservedColumns + column];
6356 }
6457
65- T at (unsigned row, unsigned column) const {
58+ MPInt at (unsigned row, unsigned column) const {
6659 assert (row < nRows && " Row outside of range" );
6760 assert (column < nColumns && " Column outside of range" );
6861 return data[row * nReservedColumns + column];
6962 }
7063
71- T &operator ()(unsigned row, unsigned column) { return at (row, column); }
64+ MPInt &operator ()(unsigned row, unsigned column) { return at (row, column); }
7265
73- T operator ()(unsigned row, unsigned column) const {
66+ MPInt operator ()(unsigned row, unsigned column) const {
7467 return at (row, column);
7568 }
7669
@@ -94,11 +87,11 @@ static_assert(std::is_same_v<T,MPInt> || std::is_same_v<T,Fraction>, "T must be
9487 void reserveRows (unsigned rows);
9588
9689 // / 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 ;
9992
10093 // / Set the specified row to `elems`.
101- void setRow (unsigned row, ArrayRef<T > elems);
94+ void setRow (unsigned row, ArrayRef<MPInt > elems);
10295
10396 // / Insert columns having positions pos, pos + 1, ... pos + count - 1.
10497 // / 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
132125
133126 void copyRow (unsigned sourceRow, unsigned targetRow);
134127
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)); }
137130
138131 // / 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);
140133 void addToRow (unsigned sourceRow, unsigned targetRow, int64_t scale) {
141- addToRow (sourceRow, targetRow, T (scale));
134+ addToRow (sourceRow, targetRow, MPInt (scale));
142135 }
143136 // / 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);
145138
146139 // / Add `scale` multiples of the source column to the target column.
147140 void addToColumn (unsigned sourceColumn, unsigned targetColumn,
148- const T &scale);
141+ const MPInt &scale);
149142 void addToColumn (unsigned sourceColumn, unsigned targetColumn,
150143 int64_t scale) {
151- addToColumn (sourceColumn, targetColumn, T (scale));
144+ addToColumn (sourceColumn, targetColumn, MPInt (scale));
152145 }
153146
154147 // / 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
159152
160153 // / Divide the first `nCols` of the specified row by their GCD.
161154 // / 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);
163156 // / Divide the columns of the specified row by their GCD.
164157 // / Returns the GCD of the columns of the specified row.
165- T normalizeRow (unsigned row);
158+ MPInt normalizeRow (unsigned row);
166159
167160 // / The given vector is interpreted as a row vector v. Post-multiply v with
168161 // / 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 ;
170163
171164 // / The given vector is interpreted as a column vector v. Pre-multiply v with
172165 // / 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 ;
174167
175168 // / Given the current matrix M, returns the matrices H, U such that H is the
176169 // / 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
199192 unsigned appendExtraRow ();
200193 // / Same as above, but copy the given elements into the row. The length of
201194 // / `elems` must be equal to the number of columns.
202- unsigned appendExtraRow (ArrayRef<T > elems);
195+ unsigned appendExtraRow (ArrayRef<MPInt > elems);
203196
204197 // / Print the matrix.
205198 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
218211
219212 // / Stores the data. data.size() is equal to nRows * nReservedColumns.
220213 // / data.capacity() / nReservedColumns is the number of reserved rows.
221- SmallVector<T , 16 > data;
214+ SmallVector<MPInt , 16 > data;
222215};
223216
224217} // namespace presburger
0 commit comments