Skip to content

Commit ee6096d

Browse files
committed
add tests
1 parent 5901325 commit ee6096d

File tree

11 files changed

+673
-527
lines changed

11 files changed

+673
-527
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/math/FragmentedRangeBigInteger.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.math.BigInteger;
44
import java.util.Collection;
5+
import java.util.Iterator;
56
import java.util.LinkedList;
67

78
/**
@@ -178,6 +179,52 @@ public BigInteger getMax() {
178179
return getLast().max;
179180
}
180181

182+
/**
183+
* If a range with the exact size exists, it is returned.
184+
* Else, the smaller range greater than the given size is returned.
185+
* If no range can contain the size, null is returned.
186+
*/
187+
public RangeBigInteger removeBestRangeForSize(BigInteger size) {
188+
RangeBigInteger best = null;
189+
BigInteger bestSize = null;
190+
for (Iterator<RangeBigInteger> it = iterator(); it.hasNext(); ) {
191+
RangeBigInteger r = it.next();
192+
BigInteger l = r.getLength();
193+
int c = size.compareTo(l);
194+
if (c == 0) {
195+
it.remove();
196+
return r;
197+
}
198+
if (c > 0) continue;
199+
if (bestSize == null || bestSize.compareTo(l) > 0) {
200+
best = r;
201+
bestSize = l;
202+
}
203+
}
204+
if (best == null) return null;
205+
RangeBigInteger res = new RangeBigInteger(best.min, best.min.add(size).subtract(BigInteger.ONE));
206+
best.min = best.min.add(size);
207+
return res;
208+
}
209+
210+
/** Remove the largest range. */
211+
public RangeBigInteger removeBiggestRange() {
212+
if (isEmpty()) return null;
213+
if (size() == 1) return remove(0);
214+
int biggestIndex = 0;
215+
RangeBigInteger r = get(0);
216+
BigInteger biggestSize = r.getLength();
217+
for (int i = 1; i < size(); ++i) {
218+
r = get(i);
219+
BigInteger l = r.getLength();
220+
if (l.compareTo(biggestSize) > 0) {
221+
biggestSize = l;
222+
biggestIndex = i;
223+
}
224+
}
225+
return remove(biggestIndex);
226+
}
227+
181228
/** Remove and return the first value, or null if empty. */
182229
public BigInteger removeFirstValue() {
183230
if (isEmpty()) return null;
@@ -244,6 +291,20 @@ public void removeValue(BigInteger value) {
244291
removeRange(value, value);
245292
}
246293

294+
/** Return the total size, summing the ranges length. */
295+
public BigInteger getTotalSize() {
296+
BigInteger total = BigInteger.ZERO;
297+
for (RangeBigInteger r : this)
298+
total = total.add(r.getLength());
299+
return total;
300+
}
301+
302+
/** Add the given ranges. */
303+
public void addCopy(Collection<RangeBigInteger> col) {
304+
for (RangeBigInteger r : col)
305+
addRange(r.min, r.max);
306+
}
307+
247308
@Override
248309
public String toString() {
249310
StringBuilder s = new StringBuilder("{");

0 commit comments

Comments
 (0)