|
2 | 2 |
|
3 | 3 | import java.math.BigInteger; |
4 | 4 | import java.util.Collection; |
| 5 | +import java.util.Iterator; |
5 | 6 | import java.util.LinkedList; |
6 | 7 |
|
7 | 8 | /** |
@@ -178,6 +179,52 @@ public BigInteger getMax() { |
178 | 179 | return getLast().max; |
179 | 180 | } |
180 | 181 |
|
| 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 | + |
181 | 228 | /** Remove and return the first value, or null if empty. */ |
182 | 229 | public BigInteger removeFirstValue() { |
183 | 230 | if (isEmpty()) return null; |
@@ -244,6 +291,20 @@ public void removeValue(BigInteger value) { |
244 | 291 | removeRange(value, value); |
245 | 292 | } |
246 | 293 |
|
| 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 | + |
247 | 308 | @Override |
248 | 309 | public String toString() { |
249 | 310 | StringBuilder s = new StringBuilder("{"); |
|
0 commit comments