@@ -564,22 +564,18 @@ void IntegerRelation::clearAndCopyFrom(const IntegerRelation &other) {
564
564
*this = other;
565
565
}
566
566
567
- // Searches for a constraint with a non-zero coefficient at `colIdx` in
568
- // equality (isEq=true) or inequality (isEq=false) constraints.
569
- // Returns true and sets row found in search in `rowIdx`, false otherwise.
570
- bool IntegerRelation::findConstraintWithNonZeroAt (unsigned colIdx, bool isEq,
571
- unsigned *rowIdx) const {
567
+ std::optional<unsigned >
568
+ IntegerRelation::findConstraintWithNonZeroAt (unsigned colIdx, bool isEq) const {
572
569
assert (colIdx < getNumCols () && " position out of bounds" );
573
570
auto at = [&](unsigned rowIdx) -> DynamicAPInt {
574
571
return isEq ? atEq (rowIdx, colIdx) : atIneq (rowIdx, colIdx);
575
572
};
576
573
unsigned e = isEq ? getNumEqualities () : getNumInequalities ();
577
- for (*rowIdx = 0 ; *rowIdx < e; ++(*rowIdx)) {
578
- if (at (*rowIdx) != 0 ) {
579
- return true ;
580
- }
574
+ for (unsigned rowIdx = 0 ; rowIdx < e; ++rowIdx) {
575
+ if (at (rowIdx) != 0 )
576
+ return rowIdx;
581
577
}
582
- return false ;
578
+ return std::nullopt ;
583
579
}
584
580
585
581
void IntegerRelation::normalizeConstraintsByGCD () {
@@ -1088,31 +1084,30 @@ unsigned IntegerRelation::gaussianEliminateVars(unsigned posStart,
1088
1084
unsigned pivotCol = 0 ;
1089
1085
for (pivotCol = posStart; pivotCol < posLimit; ++pivotCol) {
1090
1086
// Find a row which has a non-zero coefficient in column 'j'.
1091
- unsigned pivotRow;
1092
- if (!findConstraintWithNonZeroAt (pivotCol, /* isEq=*/ true , &pivotRow)) {
1093
- // No pivot row in equalities with non-zero at 'pivotCol'.
1094
- if (!findConstraintWithNonZeroAt (pivotCol, /* isEq=*/ false , &pivotRow)) {
1095
- // If inequalities are also non-zero in 'pivotCol', it can be
1096
- // eliminated.
1097
- continue ;
1098
- }
1099
- break ;
1087
+ std::optional<unsigned > pivotRow =
1088
+ findConstraintWithNonZeroAt (pivotCol, /* isEq=*/ true );
1089
+ // No pivot row in equalities with non-zero at 'pivotCol'.
1090
+ if (!pivotRow) {
1091
+ // If inequalities are also non-zero in 'pivotCol', it can be eliminated.
1092
+ if ((pivotRow = findConstraintWithNonZeroAt (pivotCol, /* isEq=*/ false )))
1093
+ break ;
1094
+ continue ;
1100
1095
}
1101
1096
1102
1097
// Eliminate variable at 'pivotCol' from each equality row.
1103
1098
for (unsigned i = 0 , e = getNumEqualities (); i < e; ++i) {
1104
- eliminateFromConstraint (this , i, pivotRow, pivotCol, posStart,
1099
+ eliminateFromConstraint (this , i, * pivotRow, pivotCol, posStart,
1105
1100
/* isEq=*/ true );
1106
1101
equalities.normalizeRow (i);
1107
1102
}
1108
1103
1109
1104
// Eliminate variable at 'pivotCol' from each inequality row.
1110
1105
for (unsigned i = 0 , e = getNumInequalities (); i < e; ++i) {
1111
- eliminateFromConstraint (this , i, pivotRow, pivotCol, posStart,
1106
+ eliminateFromConstraint (this , i, * pivotRow, pivotCol, posStart,
1112
1107
/* isEq=*/ false );
1113
1108
inequalities.normalizeRow (i);
1114
1109
}
1115
- removeEquality (pivotRow);
1110
+ removeEquality (* pivotRow);
1116
1111
gcdTightenInequalities ();
1117
1112
}
1118
1113
// Update position limit based on number eliminated.
@@ -1125,31 +1120,31 @@ unsigned IntegerRelation::gaussianEliminateVars(unsigned posStart,
1125
1120
bool IntegerRelation::gaussianEliminate () {
1126
1121
gcdTightenInequalities ();
1127
1122
unsigned firstVar = 0 , vars = getNumVars ();
1128
- unsigned nowDone, eqs, pivotRow;
1123
+ unsigned nowDone, eqs;
1124
+ std::optional<unsigned > pivotRow;
1129
1125
for (nowDone = 0 , eqs = getNumEqualities (); nowDone < eqs; ++nowDone) {
1130
1126
// Finds the first non-empty column.
1131
1127
for (; firstVar < vars; ++firstVar) {
1132
- if (!findConstraintWithNonZeroAt (firstVar, true , &pivotRow))
1133
- continue ;
1134
- break ;
1128
+ if ((pivotRow = findConstraintWithNonZeroAt (firstVar, /* isEq=*/ true )))
1129
+ break ;
1135
1130
}
1136
1131
// The matrix has been normalized to row echelon form.
1137
1132
if (firstVar >= vars)
1138
1133
break ;
1139
1134
1140
1135
// The first pivot row found is below where it should currently be placed.
1141
- if (pivotRow > nowDone) {
1142
- equalities.swapRows (pivotRow, nowDone);
1143
- pivotRow = nowDone;
1136
+ if (* pivotRow > nowDone) {
1137
+ equalities.swapRows (* pivotRow, nowDone);
1138
+ * pivotRow = nowDone;
1144
1139
}
1145
1140
1146
1141
// Normalize all lower equations and all inequalities.
1147
1142
for (unsigned i = nowDone + 1 ; i < eqs; ++i) {
1148
- eliminateFromConstraint (this , i, pivotRow, firstVar, 0 , true );
1143
+ eliminateFromConstraint (this , i, * pivotRow, firstVar, 0 , true );
1149
1144
equalities.normalizeRow (i);
1150
1145
}
1151
1146
for (unsigned i = 0 , ineqs = getNumInequalities (); i < ineqs; ++i) {
1152
- eliminateFromConstraint (this , i, pivotRow, firstVar, 0 , false );
1147
+ eliminateFromConstraint (this , i, * pivotRow, firstVar, 0 , false );
1153
1148
inequalities.normalizeRow (i);
1154
1149
}
1155
1150
gcdTightenInequalities ();
@@ -2290,9 +2285,8 @@ IntegerRelation::unionBoundingBox(const IntegerRelation &otherCst) {
2290
2285
}
2291
2286
2292
2287
bool IntegerRelation::isColZero (unsigned pos) const {
2293
- unsigned rowPos;
2294
- return !findConstraintWithNonZeroAt (pos, /* isEq=*/ false , &rowPos) &&
2295
- !findConstraintWithNonZeroAt (pos, /* isEq=*/ true , &rowPos);
2288
+ return !findConstraintWithNonZeroAt (pos, /* isEq=*/ false ) &&
2289
+ !findConstraintWithNonZeroAt (pos, /* isEq=*/ true );
2296
2290
}
2297
2291
2298
2292
// / Find positions of inequalities and equalities that do not have a coefficient
@@ -2600,16 +2594,16 @@ void IntegerRelation::print(raw_ostream &os) const {
2600
2594
for (unsigned j = 0 , f = getNumCols (); j < f; ++j)
2601
2595
updatePrintMetrics<DynamicAPInt>(atIneq (i, j), ptm);
2602
2596
// Print using PrintMetrics.
2603
- unsigned MIN_SPACING = 1 ;
2597
+ constexpr unsigned kMinSpacing = 1 ;
2604
2598
for (unsigned i = 0 , e = getNumEqualities (); i < e; ++i) {
2605
2599
for (unsigned j = 0 , f = getNumCols (); j < f; ++j) {
2606
- printWithPrintMetrics<DynamicAPInt>(os, atEq (i, j), MIN_SPACING , ptm);
2600
+ printWithPrintMetrics<DynamicAPInt>(os, atEq (i, j), kMinSpacing , ptm);
2607
2601
}
2608
2602
os << " = 0\n " ;
2609
2603
}
2610
2604
for (unsigned i = 0 , e = getNumInequalities (); i < e; ++i) {
2611
2605
for (unsigned j = 0 , f = getNumCols (); j < f; ++j) {
2612
- printWithPrintMetrics<DynamicAPInt>(os, atIneq (i, j), MIN_SPACING , ptm);
2606
+ printWithPrintMetrics<DynamicAPInt>(os, atIneq (i, j), kMinSpacing , ptm);
2613
2607
}
2614
2608
os << " >= 0\n " ;
2615
2609
}
0 commit comments