Skip to content

Commit 071f869

Browse files
multiplesBetween: Speed up handling of extremely large ranges
1 parent c56619b commit 071f869

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

tcases-openapi/src/main/java/org/cornutum/tcases/openapi/InputModeller.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3435,24 +3435,29 @@ inclusive && isMultipleOf( value, multipleOf)
34353435
}
34363436

34373437
/**
3438-
* Return the number of values between the given mininim and maximum (inclusive) that satisfy the given (not-)multiple-of constraints.
3438+
* Returns the number of values between the given mininim and maximum (inclusive) that satisfy the given (not-)multiple-of constraints.
3439+
* Returns null if the number is effectively infinite.
34393440
*/
3440-
private int multiplesBetween( BigDecimal min, BigDecimal max, BigDecimal multipleOf, Set<BigDecimal> notMultipleOfs)
3441+
private Integer multiplesBetween( BigDecimal min, BigDecimal max, BigDecimal multipleOf, Set<BigDecimal> notMultipleOfs)
34413442
{
3442-
int count = 0;
3443+
BigDecimal countMax =
3444+
min.compareTo( max) > 0
3445+
? BigDecimal.ZERO
3446+
: max.subtract( min).divide( multipleOf).add( BigDecimal.ONE);
34433447

3444-
for( BigDecimal nextMultiple = min;
3445-
nextMultiple.compareTo( max) <= 0;
3446-
nextMultiple = nextMultiple.add( multipleOf))
3447-
{
3448-
BigDecimal checkValue = nextMultiple;
3449-
if( notMultipleOfs.stream().allMatch( m -> !isMultipleOf( checkValue, m)))
3450-
{
3451-
count++;
3452-
}
3453-
}
3448+
BigDecimal intMax = new BigDecimal( Integer.MAX_VALUE);
3449+
return
3450+
countMax.compareTo( intMax) > 0?
3451+
null :
34543452

3455-
return count;
3453+
notMultipleOfs.isEmpty()?
3454+
Integer.valueOf( countMax.intValue()) :
3455+
3456+
(int)
3457+
IntStream.range( 0, countMax.intValue())
3458+
.mapToObj( i -> min.add( multipleOf.multiply( new BigDecimal( i))))
3459+
.filter( nextMultiple -> notMultipleOfs.stream().noneMatch( m -> isMultipleOf( nextMultiple, m)))
3460+
.count();
34563461
}
34573462

34583463
/**

0 commit comments

Comments
 (0)