Skip to content

Commit eeddaef

Browse files
Implement moveColumns using std::rotate.
1 parent 5edf70c commit eeddaef

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

mlir/lib/Analysis/Presburger/Matrix.cpp

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,7 @@ void Matrix<T>::fillRow(unsigned row, const T &value) {
255255
}
256256

257257
// moveColumns is implemented by moving the columns adjacent to the source range
258-
// to their final position. When moving right (i.e. dstPos > srcPos), the range
259-
// of the adjacent columns is [srcPos + num, dstPos + num). When moving left
260-
// (i.e. dstPos < srcPos) the range of the adjacent columns is [dstPos, srcPos).
261-
// First, zeroed out columns are inserted in the final positions of the adjacent
262-
// columns. Then, the adjacent columns are moved to their final positions by
263-
// swapping them with the zeroed columns. Finally, the now zeroed adjacent
264-
// columns are deleted.
258+
// to their final position.
265259
template <typename T>
266260
void Matrix<T>::moveColumns(unsigned srcPos, unsigned num, unsigned dstPos) {
267261
if (num == 0)
@@ -276,23 +270,25 @@ void Matrix<T>::moveColumns(unsigned srcPos, unsigned num, unsigned dstPos) {
276270
assert(dstPos + num <= getNumColumns() &&
277271
"move destination range exceeds matrix columns");
278272

279-
unsigned insertCount = offset > 0 ? offset : -offset;
280-
unsigned finalAdjStart = offset > 0 ? srcPos : srcPos + num;
281-
unsigned curAdjStart = offset > 0 ? srcPos + num : dstPos;
282-
// TODO: This can be done using std::rotate.
283-
// Insert new zero columns in the positions where the adjacent columns are to
284-
// be moved.
285-
insertColumns(finalAdjStart, insertCount);
286-
// Update curAdjStart if insertion of new columns invalidates it.
287-
if (finalAdjStart < curAdjStart)
288-
curAdjStart += insertCount;
289-
290-
// Swap the adjacent columns with inserted zero columns.
291-
for (unsigned i = 0; i < insertCount; ++i)
292-
swapColumns(finalAdjStart + i, curAdjStart + i);
293-
294-
// Delete the now redundant zero columns.
295-
removeColumns(curAdjStart, insertCount);
273+
unsigned numRows = getNumRows();
274+
unsigned numCols = getNumReservedColumns();
275+
276+
if (offset > 0) {
277+
// shift the matrix left, see
278+
// https://en.cppreference.com/w/cpp/algorithm/rotate.html.
279+
for (unsigned i = 0; i < numRows; ++i) {
280+
auto begin = data.begin() + i * numCols + srcPos;
281+
auto end = data.begin() + i * numCols + dstPos + num;
282+
std::rotate(begin, begin + num, end);
283+
}
284+
} else {
285+
// shift the matrix right.
286+
for (unsigned i = 0; i < numRows; ++i) {
287+
auto begin = data.begin() + i * numCols + srcPos + num;
288+
auto end = data.begin() + i * numCols + dstPos;
289+
std::rotate(end, begin - num, begin);
290+
}
291+
}
296292
}
297293

298294
template <typename T>

0 commit comments

Comments
 (0)