MATH-1286
New "Range" inner class that holds a field with the number of elements of the range. An instance of this class replaces the anonymous class returned by the "range" method.
This commit is contained in:
parent
5f03f0d702
commit
2f782bc259
|
@ -51,6 +51,9 @@ If the output is not quite correct, check for invisible trailing spaces!
|
||||||
</properties>
|
</properties>
|
||||||
<body>
|
<body>
|
||||||
<release version="3.6" date="XXXX-XX-XX" description="">
|
<release version="3.6" date="XXXX-XX-XX" description="">
|
||||||
|
<action dev="erans" type="add" issue="MATH-1286">
|
||||||
|
New "Range" inner class of "o.a.c.m.util.IntegerSequence".
|
||||||
|
</action>
|
||||||
<action dev="oertl" type="fix" issue="MATH-1285" due-to="Pim van der Hoorn ">
|
<action dev="oertl" type="fix" issue="MATH-1285" due-to="Pim van der Hoorn ">
|
||||||
Updated reference in ZipfDistribution's javadoc.
|
Updated reference in ZipfDistribution's javadoc.
|
||||||
</action>
|
</action>
|
||||||
|
|
|
@ -42,8 +42,8 @@ public class IntegerSequence {
|
||||||
* @param end Last value of the range.
|
* @param end Last value of the range.
|
||||||
* @return a range.
|
* @return a range.
|
||||||
*/
|
*/
|
||||||
public static Iterable<Integer> range(int start,
|
public static Range range(int start,
|
||||||
int end) {
|
int end) {
|
||||||
return range(start, end, 1);
|
return range(start, end, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,19 +58,63 @@ public class IntegerSequence {
|
||||||
* @param step Increment.
|
* @param step Increment.
|
||||||
* @return a range.
|
* @return a range.
|
||||||
*/
|
*/
|
||||||
public static Iterable<Integer> range(final int start,
|
public static Range range(final int start,
|
||||||
final int max,
|
final int max,
|
||||||
final int step) {
|
final int step) {
|
||||||
return new Iterable<Integer>() {
|
return new Range(start, max, step);
|
||||||
/** {@inheritDoc} */
|
}
|
||||||
@Override
|
|
||||||
public Iterator<Integer> iterator() {
|
/**
|
||||||
return Incrementor.create()
|
* Generates a sequence of integers.
|
||||||
.withStart(start)
|
*/
|
||||||
.withMaximalCount(max + (step > 0 ? 1 : -1))
|
public static class Range implements Iterable<Integer> {
|
||||||
.withIncrement(step);
|
/** Number of integers contained in this range. */
|
||||||
}
|
private final int size;
|
||||||
};
|
/** First value. */
|
||||||
|
private final int start;
|
||||||
|
/** Final value. */
|
||||||
|
private final int max;
|
||||||
|
/** Increment. */
|
||||||
|
private final int step;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a sequence \( a_i, i < 0 <= n \)
|
||||||
|
* where \( a_i = start + i * step \)
|
||||||
|
* and \( n \) is such that \( a_n <= max \) and \( a_{n+1} > max \).
|
||||||
|
*
|
||||||
|
* @param start First value of the range.
|
||||||
|
* @param max Last value of the range that satisfies the above
|
||||||
|
* construction rule.
|
||||||
|
* @param step Increment.
|
||||||
|
*/
|
||||||
|
public Range(int start,
|
||||||
|
int max,
|
||||||
|
int step) {
|
||||||
|
this.start = start;
|
||||||
|
this.max = max;
|
||||||
|
this.step = step;
|
||||||
|
|
||||||
|
final int s = (max - start) / step + 1;
|
||||||
|
this.size = s < 0 ? 0 : s;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the number of elements contained in the range.
|
||||||
|
*
|
||||||
|
* @return the size of the range.
|
||||||
|
*/
|
||||||
|
public int size() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc} */
|
||||||
|
@Override
|
||||||
|
public Iterator<Integer> iterator() {
|
||||||
|
return Incrementor.create()
|
||||||
|
.withStart(start)
|
||||||
|
.withMaximalCount(max + (step > 0 ? 1 : -1))
|
||||||
|
.withIncrement(step);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -26,6 +26,27 @@ import org.junit.Test;
|
||||||
* Tests for {@link IntegerSequence} and {@link IntegerSequence#Incrementor}.
|
* Tests for {@link IntegerSequence} and {@link IntegerSequence#Incrementor}.
|
||||||
*/
|
*/
|
||||||
public class IntegerSequenceTest {
|
public class IntegerSequenceTest {
|
||||||
|
@Test
|
||||||
|
public void testRangeMultipleIterations() {
|
||||||
|
// Check that we can iterate several times using the same instance.
|
||||||
|
final int start = 1;
|
||||||
|
final int max = 7;
|
||||||
|
final int step = 2;
|
||||||
|
|
||||||
|
final List<Integer> seq = new ArrayList<Integer>();
|
||||||
|
final IntegerSequence.Range r = IntegerSequence.range(start, max, step);
|
||||||
|
|
||||||
|
final int numTimes = 3;
|
||||||
|
for (int n = 0; n < numTimes; n++) {
|
||||||
|
seq.clear();
|
||||||
|
for (Integer i : r) {
|
||||||
|
seq.add(i);
|
||||||
|
}
|
||||||
|
Assert.assertEquals(4, seq.size());
|
||||||
|
Assert.assertEquals(seq.size(), r.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIncreasingRange() {
|
public void testIncreasingRange() {
|
||||||
final int start = 1;
|
final int start = 1;
|
||||||
|
@ -33,11 +54,13 @@ public class IntegerSequenceTest {
|
||||||
final int step = 2;
|
final int step = 2;
|
||||||
|
|
||||||
final List<Integer> seq = new ArrayList<Integer>();
|
final List<Integer> seq = new ArrayList<Integer>();
|
||||||
for (Integer i : IntegerSequence.range(start, max, step)) {
|
final IntegerSequence.Range r = IntegerSequence.range(start, max, step);
|
||||||
|
for (Integer i : r) {
|
||||||
seq.add(i);
|
seq.add(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
Assert.assertEquals(4, seq.size());
|
Assert.assertEquals(4, seq.size());
|
||||||
|
Assert.assertEquals(seq.size(), r.size());
|
||||||
for (int i = 0; i < seq.size(); i++) {
|
for (int i = 0; i < seq.size(); i++) {
|
||||||
Assert.assertEquals(start + i * step, seq.get(i).intValue());
|
Assert.assertEquals(start + i * step, seq.get(i).intValue());
|
||||||
}
|
}
|
||||||
|
@ -50,11 +73,13 @@ public class IntegerSequenceTest {
|
||||||
final int step = 2;
|
final int step = 2;
|
||||||
|
|
||||||
final List<Integer> seq = new ArrayList<Integer>();
|
final List<Integer> seq = new ArrayList<Integer>();
|
||||||
for (Integer i : IntegerSequence.range(start, max, step)) {
|
final IntegerSequence.Range r = IntegerSequence.range(start, max, step);
|
||||||
|
for (Integer i : r) {
|
||||||
seq.add(i);
|
seq.add(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
Assert.assertEquals(5, seq.size());
|
Assert.assertEquals(5, seq.size());
|
||||||
|
Assert.assertEquals(seq.size(), r.size());
|
||||||
for (int i = 0; i < seq.size(); i++) {
|
for (int i = 0; i < seq.size(); i++) {
|
||||||
Assert.assertEquals(start + i * step, seq.get(i).intValue());
|
Assert.assertEquals(start + i * step, seq.get(i).intValue());
|
||||||
}
|
}
|
||||||
|
@ -67,11 +92,13 @@ public class IntegerSequenceTest {
|
||||||
final int step = -3;
|
final int step = -3;
|
||||||
|
|
||||||
final List<Integer> seq = new ArrayList<Integer>();
|
final List<Integer> seq = new ArrayList<Integer>();
|
||||||
for (Integer i : IntegerSequence.range(start, max, step)) {
|
final IntegerSequence.Range r = IntegerSequence.range(start, max, step);
|
||||||
|
for (Integer i : r) {
|
||||||
seq.add(i);
|
seq.add(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
Assert.assertEquals(7, seq.size());
|
Assert.assertEquals(7, seq.size());
|
||||||
|
Assert.assertEquals(seq.size(), r.size());
|
||||||
for (int i = 0; i < seq.size(); i++) {
|
for (int i = 0; i < seq.size(); i++) {
|
||||||
Assert.assertEquals(start + i * step, seq.get(i).intValue());
|
Assert.assertEquals(start + i * step, seq.get(i).intValue());
|
||||||
}
|
}
|
||||||
|
@ -84,11 +111,13 @@ public class IntegerSequenceTest {
|
||||||
final int step = -1;
|
final int step = -1;
|
||||||
|
|
||||||
final List<Integer> seq = new ArrayList<Integer>();
|
final List<Integer> seq = new ArrayList<Integer>();
|
||||||
for (Integer i : IntegerSequence.range(start, max, step)) {
|
final IntegerSequence.Range r = IntegerSequence.range(start, max, step);
|
||||||
|
for (Integer i : r) {
|
||||||
seq.add(i);
|
seq.add(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
Assert.assertEquals(1, seq.size());
|
Assert.assertEquals(1, seq.size());
|
||||||
|
Assert.assertEquals(seq.size(), r.size());
|
||||||
Assert.assertEquals(start, seq.get(0).intValue());
|
Assert.assertEquals(start, seq.get(0).intValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,14 +139,16 @@ public class IntegerSequenceTest {
|
||||||
@Test
|
@Test
|
||||||
public void testEmptyRange() {
|
public void testEmptyRange() {
|
||||||
final int start = 2;
|
final int start = 2;
|
||||||
final int end = 1;
|
final int end = 0;
|
||||||
|
|
||||||
final List<Integer> seq = new ArrayList<Integer>();
|
final List<Integer> seq = new ArrayList<Integer>();
|
||||||
for (Integer i : IntegerSequence.range(start, end)) {
|
final IntegerSequence.Range r = IntegerSequence.range(start, end);
|
||||||
|
for (Integer i : r) {
|
||||||
seq.add(i);
|
seq.add(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
Assert.assertEquals(0, seq.size());
|
Assert.assertEquals(0, seq.size());
|
||||||
|
Assert.assertEquals(seq.size(), r.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -127,11 +158,13 @@ public class IntegerSequenceTest {
|
||||||
final int step = -1;
|
final int step = -1;
|
||||||
|
|
||||||
final List<Integer> seq = new ArrayList<Integer>();
|
final List<Integer> seq = new ArrayList<Integer>();
|
||||||
for (Integer i : IntegerSequence.range(start, max, step)) {
|
final IntegerSequence.Range r = IntegerSequence.range(start, max, step);
|
||||||
|
for (Integer i : r) {
|
||||||
seq.add(i);
|
seq.add(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
Assert.assertEquals(0, seq.size());
|
Assert.assertEquals(0, seq.size());
|
||||||
|
Assert.assertEquals(seq.size(), r.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=MaxCountExceededException.class)
|
@Test(expected=MaxCountExceededException.class)
|
||||||
|
|
Loading…
Reference in New Issue