@@ -564,22 +564,18 @@ void IntegerRelation::clearAndCopyFrom(const IntegerRelation &other) {
564564 *this = other;
565565}
566566
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 {
572569 assert (colIdx < getNumCols () && " position out of bounds" );
573570 auto at = [&](unsigned rowIdx) -> DynamicAPInt {
574571 return isEq ? atEq (rowIdx, colIdx) : atIneq (rowIdx, colIdx);
575572 };
576573 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;
581577 }
582- return false ;
578+ return std:: nullopt ;
583579}
584580
585581void IntegerRelation::normalizeConstraintsByGCD () {
@@ -1088,31 +1084,29 @@ unsigned IntegerRelation::gaussianEliminateVars(unsigned posStart,
10881084 unsigned pivotCol = 0 ;
10891085 for (pivotCol = posStart; pivotCol < posLimit; ++pivotCol) {
10901086 // Find a row which has a non-zero coefficient in column 'j'.
1091- unsigned pivotRow;
1092- if (!findConstraintWithNonZeroAt (pivotCol, /* isEq=*/ true , &pivotRow)) {
1087+ std::optional<unsigned > pivotRow =
1088+ findConstraintWithNonZeroAt (pivotCol, /* isEq=*/ true );
1089+ if (!pivotRow) {
10931090 // 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 ;
1091+ // If inequalities are also non-zero in 'pivotCol', it can be eliminated.
1092+ if ((pivotRow = findConstraintWithNonZeroAt (pivotCol, /* isEq=*/ false )))
1093+ break ;
11001094 }
11011095
11021096 // Eliminate variable at 'pivotCol' from each equality row.
11031097 for (unsigned i = 0 , e = getNumEqualities (); i < e; ++i) {
1104- eliminateFromConstraint (this , i, pivotRow, pivotCol, posStart,
1098+ eliminateFromConstraint (this , i, * pivotRow, pivotCol, posStart,
11051099 /* isEq=*/ true );
11061100 equalities.normalizeRow (i);
11071101 }
11081102
11091103 // Eliminate variable at 'pivotCol' from each inequality row.
11101104 for (unsigned i = 0 , e = getNumInequalities (); i < e; ++i) {
1111- eliminateFromConstraint (this , i, pivotRow, pivotCol, posStart,
1105+ eliminateFromConstraint (this , i, * pivotRow, pivotCol, posStart,
11121106 /* isEq=*/ false );
11131107 inequalities.normalizeRow (i);
11141108 }
1115- removeEquality (pivotRow);
1109+ removeEquality (* pivotRow);
11161110 gcdTightenInequalities ();
11171111 }
11181112 // Update position limit based on number eliminated.
@@ -1125,31 +1119,31 @@ unsigned IntegerRelation::gaussianEliminateVars(unsigned posStart,
11251119bool IntegerRelation::gaussianEliminate () {
11261120 gcdTightenInequalities ();
11271121 unsigned firstVar = 0 , vars = getNumVars ();
1128- unsigned nowDone, eqs, pivotRow;
1122+ unsigned nowDone, eqs;
1123+ std::optional<unsigned > pivotRow;
11291124 for (nowDone = 0 , eqs = getNumEqualities (); nowDone < eqs; ++nowDone) {
11301125 // Finds the first non-empty column.
11311126 for (; firstVar < vars; ++firstVar) {
1132- if (!findConstraintWithNonZeroAt (firstVar, true , &pivotRow))
1133- continue ;
1134- break ;
1127+ if ((pivotRow = findConstraintWithNonZeroAt (firstVar, /* isEq=*/ true )))
1128+ break ;
11351129 }
11361130 // The matrix has been normalized to row echelon form.
11371131 if (firstVar >= vars)
11381132 break ;
11391133
11401134 // 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;
1135+ if (* pivotRow > nowDone) {
1136+ equalities.swapRows (* pivotRow, nowDone);
1137+ * pivotRow = nowDone;
11441138 }
11451139
11461140 // Normalize all lower equations and all inequalities.
11471141 for (unsigned i = nowDone + 1 ; i < eqs; ++i) {
1148- eliminateFromConstraint (this , i, pivotRow, firstVar, 0 , true );
1142+ eliminateFromConstraint (this , i, * pivotRow, firstVar, 0 , true );
11491143 equalities.normalizeRow (i);
11501144 }
11511145 for (unsigned i = 0 , ineqs = getNumInequalities (); i < ineqs; ++i) {
1152- eliminateFromConstraint (this , i, pivotRow, firstVar, 0 , false );
1146+ eliminateFromConstraint (this , i, * pivotRow, firstVar, 0 , false );
11531147 inequalities.normalizeRow (i);
11541148 }
11551149 gcdTightenInequalities ();
@@ -2290,9 +2284,8 @@ IntegerRelation::unionBoundingBox(const IntegerRelation &otherCst) {
22902284}
22912285
22922286bool IntegerRelation::isColZero (unsigned pos) const {
2293- unsigned rowPos;
2294- return !findConstraintWithNonZeroAt (pos, /* isEq=*/ false , &rowPos) &&
2295- !findConstraintWithNonZeroAt (pos, /* isEq=*/ true , &rowPos);
2287+ return !findConstraintWithNonZeroAt (pos, /* isEq=*/ false ) &&
2288+ !findConstraintWithNonZeroAt (pos, /* isEq=*/ true );
22962289}
22972290
22982291// / Find positions of inequalities and equalities that do not have a coefficient
@@ -2600,16 +2593,16 @@ void IntegerRelation::print(raw_ostream &os) const {
26002593 for (unsigned j = 0 , f = getNumCols (); j < f; ++j)
26012594 updatePrintMetrics<DynamicAPInt>(atIneq (i, j), ptm);
26022595 // Print using PrintMetrics.
2603- unsigned MIN_SPACING = 1 ;
2596+ constexpr unsigned kMinSpacing = 1 ;
26042597 for (unsigned i = 0 , e = getNumEqualities (); i < e; ++i) {
26052598 for (unsigned j = 0 , f = getNumCols (); j < f; ++j) {
2606- printWithPrintMetrics<DynamicAPInt>(os, atEq (i, j), MIN_SPACING , ptm);
2599+ printWithPrintMetrics<DynamicAPInt>(os, atEq (i, j), kMinSpacing , ptm);
26072600 }
26082601 os << " = 0\n " ;
26092602 }
26102603 for (unsigned i = 0 , e = getNumInequalities (); i < e; ++i) {
26112604 for (unsigned j = 0 , f = getNumCols (); j < f; ++j) {
2612- printWithPrintMetrics<DynamicAPInt>(os, atIneq (i, j), MIN_SPACING , ptm);
2605+ printWithPrintMetrics<DynamicAPInt>(os, atIneq (i, j), kMinSpacing , ptm);
26132606 }
26142607 os << " >= 0\n " ;
26152608 }
0 commit comments