Skip to content

Commit

Permalink
Closes #892: Uses direct formulas to calculate sum(), mean(), and ave…
Browse files Browse the repository at this point in the history
…rage() on IntInterval

Signed-off-by: vmzakharov <zakharov.vladimir.m@gmail.com>
  • Loading branch information
vmzakharov committed May 24, 2020
1 parent 299bc0c commit e5177b7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -790,12 +790,7 @@ public LazyIntIterable asReversed()
@Override
public long sum()
{
long sum = 0L;
for (IntIterator intIterator = this.intIterator(); intIterator.hasNext(); )
{
sum += intIterator.next();
}
return sum;
return (long) this.size() * ((long) this.getFirst() + (long) this.getLast()) / 2L;
}

@Override
Expand Down Expand Up @@ -833,21 +828,14 @@ public int maxIfEmpty(int defaultValue)
@Override
public double average()
{
return (double) this.sum() / (double) this.size();
// for an arithmetic sequence its median and its average are the same
return this.median();
}

@Override
public double median()
{
int[] sortedArray = this.toSortedArray();
int middleIndex = sortedArray.length >> 1;
if (sortedArray.length > 1 && (sortedArray.length & 1) == 0)
{
int first = sortedArray[middleIndex];
int second = sortedArray[middleIndex - 1];
return ((double) first + (double) second) / 2.0;
}
return (double) sortedArray[middleIndex];
return ((double) this.getFirst() + (double) this.getLast()) / 2.0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,19 +560,40 @@ public void maxIfEmpty()
public void sum()
{
Assert.assertEquals(10L, IntInterval.oneTo(4).sum());
Assert.assertEquals(5L, IntInterval.oneToBy(4, 3).sum());
Assert.assertEquals(4L, IntInterval.oneToBy(4, 2).sum());
Assert.assertEquals(-10L, IntInterval.fromTo(-1, -4).sum());
Assert.assertEquals(-15L, IntInterval.fromToBy(-2, -10, -3).sum());

Assert.assertEquals(-7L, IntInterval.fromToBy(-10, 10, 3).sum());

Assert.assertEquals(
3L * ((long) Integer.MAX_VALUE * 2L - 2L) / 2L,
IntInterval.fromTo(Integer.MAX_VALUE - 2, Integer.MAX_VALUE).sum());
}

@Test
public void average()
{
Assert.assertEquals(2.5, IntInterval.oneTo(4).average(), 0.0);
Assert.assertEquals(5.0, IntInterval.oneToBy(9, 2).average(), 0.0);
Assert.assertEquals(5.0, IntInterval.oneToBy(10, 2).average(), 0.0);
Assert.assertEquals(-5.0, IntInterval.fromToBy(-1, -9, -2).average(), 0.0);

Assert.assertEquals((double) Integer.MAX_VALUE - 1.5,
IntInterval.fromTo(Integer.MAX_VALUE - 3, Integer.MAX_VALUE).average(), 0.0);
}

@Test
public void median()
{
Assert.assertEquals(2.5, IntInterval.oneTo(4).median(), 0.0);
Assert.assertEquals(3.0, IntInterval.oneTo(5).median(), 0.0);
Assert.assertEquals(5.0, IntInterval.oneToBy(9, 2).median(), 0.0);
Assert.assertEquals(5.0, IntInterval.oneToBy(10, 2).median(), 0.0);
Assert.assertEquals(-5.0, IntInterval.fromToBy(-1, -9, -2).median(), 0.0);

Assert.assertEquals((double) Integer.MAX_VALUE - 1.5,
IntInterval.fromTo(Integer.MAX_VALUE - 3, Integer.MAX_VALUE).median(), 0.0);
}

@Test
Expand Down

0 comments on commit e5177b7

Please sign in to comment.