@@ -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,30 @@ 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)) {
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 ;
11001095    }
11011096
11021097    //  Eliminate variable at 'pivotCol' from each equality row.
11031098    for  (unsigned  i = 0 , e = getNumEqualities (); i < e; ++i) {
1104-       eliminateFromConstraint (this , i, pivotRow, pivotCol, posStart,
1099+       eliminateFromConstraint (this , i, * pivotRow, pivotCol, posStart,
11051100                              /* isEq=*/ true );
11061101      equalities.normalizeRow (i);
11071102    }
11081103
11091104    //  Eliminate variable at 'pivotCol' from each inequality row.
11101105    for  (unsigned  i = 0 , e = getNumInequalities (); i < e; ++i) {
1111-       eliminateFromConstraint (this , i, pivotRow, pivotCol, posStart,
1106+       eliminateFromConstraint (this , i, * pivotRow, pivotCol, posStart,
11121107                              /* isEq=*/ false );
11131108      inequalities.normalizeRow (i);
11141109    }
1115-     removeEquality (pivotRow);
1110+     removeEquality (* pivotRow);
11161111    gcdTightenInequalities ();
11171112  }
11181113  //  Update position limit based on number eliminated.
@@ -1125,31 +1120,31 @@ unsigned IntegerRelation::gaussianEliminateVars(unsigned posStart,
11251120bool  IntegerRelation::gaussianEliminate () {
11261121  gcdTightenInequalities ();
11271122  unsigned  firstVar = 0 , vars = getNumVars ();
1128-   unsigned  nowDone, eqs, pivotRow;
1123+   unsigned  nowDone, eqs;
1124+   std::optional<unsigned > pivotRow;
11291125  for  (nowDone = 0 , eqs = getNumEqualities (); nowDone < eqs; ++nowDone) {
11301126    //  Finds the first non-empty column.
11311127    for  (; firstVar < vars; ++firstVar) {
1132-       if  (!findConstraintWithNonZeroAt (firstVar, true , &pivotRow))
1133-         continue ;
1134-       break ;
1128+       if  ((pivotRow = findConstraintWithNonZeroAt (firstVar, /* isEq=*/ true )))
1129+         break ;
11351130    }
11361131    //  The matrix has been normalized to row echelon form.
11371132    if  (firstVar >= vars)
11381133      break ;
11391134
11401135    //  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;
11441139    }
11451140
11461141    //  Normalize all lower equations and all inequalities.
11471142    for  (unsigned  i = nowDone + 1 ; i < eqs; ++i) {
1148-       eliminateFromConstraint (this , i, pivotRow, firstVar, 0 , true );
1143+       eliminateFromConstraint (this , i, * pivotRow, firstVar, 0 , true );
11491144      equalities.normalizeRow (i);
11501145    }
11511146    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 );
11531148      inequalities.normalizeRow (i);
11541149    }
11551150    gcdTightenInequalities ();
@@ -2290,9 +2285,8 @@ IntegerRelation::unionBoundingBox(const IntegerRelation &otherCst) {
22902285}
22912286
22922287bool  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 );
22962290}
22972291
22982292// / Find positions of inequalities and equalities that do not have a coefficient
@@ -2600,16 +2594,16 @@ void IntegerRelation::print(raw_ostream &os) const {
26002594    for  (unsigned  j = 0 , f = getNumCols (); j < f; ++j)
26012595      updatePrintMetrics<DynamicAPInt>(atIneq (i, j), ptm);
26022596  //  Print using PrintMetrics.
2603-   unsigned  MIN_SPACING  = 1 ;
2597+   constexpr   unsigned  kMinSpacing  = 1 ;
26042598  for  (unsigned  i = 0 , e = getNumEqualities (); i < e; ++i) {
26052599    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);
26072601    }
26082602    os << "   = 0\n "  ;
26092603  }
26102604  for  (unsigned  i = 0 , e = getNumInequalities (); i < e; ++i) {
26112605    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);
26132607    }
26142608    os << "  >= 0\n "  ;
26152609  }
0 commit comments