1 //===- Matrix.h - MLIR Matrix Class -----------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This is a simple 2D matrix class that supports reading, writing, resizing, 10 // swapping rows, and swapping columns. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef MLIR_ANALYSIS_PRESBURGER_MATRIX_H 15 #define MLIR_ANALYSIS_PRESBURGER_MATRIX_H 16 17 #include "mlir/Support/LLVM.h" 18 #include "llvm/ADT/ArrayRef.h" 19 #include "llvm/Support/raw_ostream.h" 20 21 #include <cassert> 22 23 namespace mlir { 24 25 /// This is a simple class to represent a resizable matrix. 26 /// 27 /// The data is stored in the form of a vector of vectors. 28 class Matrix { 29 public: 30 Matrix() = delete; 31 32 /// Construct a matrix with the specified number of rows and columns. 33 /// Initially, the values are default initialized. 34 Matrix(unsigned rows, unsigned columns); 35 36 /// Return the identity matrix of the specified dimension. 37 static Matrix identity(unsigned dimension); 38 39 /// Access the element at the specified row and column. 40 int64_t &at(unsigned row, unsigned column); 41 int64_t at(unsigned row, unsigned column) const; 42 int64_t &operator()(unsigned row, unsigned column); 43 int64_t operator()(unsigned row, unsigned column) const; 44 45 /// Swap the given columns. 46 void swapColumns(unsigned column, unsigned otherColumn); 47 48 /// Swap the given rows. 49 void swapRows(unsigned row, unsigned otherRow); 50 51 unsigned getNumRows() const; 52 53 unsigned getNumColumns() const; 54 55 /// Get an ArrayRef corresponding to the specified row. 56 ArrayRef<int64_t> getRow(unsigned row) const; 57 58 /// Add `scale` multiples of the source row to the target row. 59 void addToRow(unsigned sourceRow, unsigned targetRow, int64_t scale); 60 61 /// Resize the matrix to the specified dimensions. If a dimension is smaller, 62 /// the values are truncated; if it is bigger, the new values are default 63 /// initialized. 64 void resizeVertically(unsigned newNRows); 65 66 /// Print the matrix. 67 void print(raw_ostream &os) const; 68 void dump() const; 69 70 private: 71 unsigned nRows, nColumns; 72 73 /// Stores the data. data.size() is equal to nRows * nColumns. 74 SmallVector<int64_t, 64> data; 75 }; 76 77 } // namespace mlir 78 79 #endif // MLIR_ANALYSIS_PRESBURGER_MATRIX_H 80