1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
|
/*
* Copyright (C) 2002-2024 Sebastiano Vigna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package PACKAGE;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Consumer;
#if KEYS_BYTE_CHAR_SHORT_FLOAT
import WIDENED_PACKAGE.KEY_WIDENED_ITERATOR;
import WIDENED_PACKAGE.WIDENED_ITERATORS;
import WIDENED_PACKAGE.KEY_WIDENED_SPLITERATOR;
import WIDENED_PACKAGE.WIDENED_SPLITERATORS;
#elif ! KEY_CLASS_Boolean
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.IntFunction;
import java.util.function.Supplier;
#endif
#if ! KEYS_REFERENCE
import it.unimi.dsi.fastutil.objects.ObjectArrays;
#else
import java.util.function.Predicate;
#endif
/** A class providing static methods and objects that do useful things with type-specific collections.
*
* @see java.util.Collections
*/
public final class COLLECTIONS {
private COLLECTIONS() {}
#if KEYS_PRIMITIVE && !KEY_TYPE_Boolean
/* Only in the EmptyCollection class, where performance is critical, do we override the
* deprecated, Object based functional methods. For the rest, we just override the
* non-deprecated type-specific method, and let the default method from the interface
* filter into that. This is an extra method call and lambda creation, but it isn't worth
* complexifying the code generation for a case that is already marked as being inefficient.
*/
#endif
/** An immutable class representing an empty type-specific collection.
*
* <p>This class may be useful to implement your own in case you subclass
* a type-specific collection.
*/
public abstract static class EmptyCollection KEY_GENERIC extends ABSTRACT_COLLECTION KEY_GENERIC {
protected EmptyCollection() {}
@Override
public boolean contains(KEY_TYPE k) { return false; }
@Override
public Object[] toArray() { return ObjectArrays.EMPTY_ARRAY; }
@Override
public <T> T[] toArray(T[] array) {
if (array.length > 0) array[0] = null;
return array;
}
@Override
SUPPRESS_WARNINGS_KEY_UNCHECKED
public KEY_BIDI_ITERATOR KEY_GENERIC iterator() { return ITERATORS.EMPTY_ITERATOR; }
@Override
SUPPRESS_WARNINGS_KEY_UNCHECKED
public KEY_SPLITERATOR KEY_GENERIC spliterator() { return SPLITERATORS.EMPTY_SPLITERATOR; }
@Override
public int size() { return 0; }
@Override
public void clear() {}
@Override
public int hashCode() { return 0; }
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (! (o instanceof Collection)) return false;
return ((Collection<?>)o).isEmpty();
}
DEPRECATED_IF_KEYS_PRIMITIVE
@Override
public void forEach(final Consumer<? super KEY_GENERIC_CLASS> action) { }
@Override
public boolean containsAll(final Collection<?> c) { return c.isEmpty(); }
@Override
public boolean addAll(final Collection<? extends KEY_GENERIC_CLASS> c) { throw new UnsupportedOperationException(); }
@Override
public boolean removeAll(final Collection<?> c) { throw new UnsupportedOperationException(); }
@Override
public boolean retainAll(final Collection<?> c) { throw new UnsupportedOperationException(); }
#if KEYS_PRIMITIVE
@Deprecated
#endif
@Override
public boolean removeIf(final java.util.function.Predicate<? super KEY_GENERIC_CLASS> filter) {
Objects.requireNonNull(filter);
return false;
}
#if KEYS_PRIMITIVE
@Override
public KEY_TYPE[] TO_KEY_ARRAY() { return ARRAYS.EMPTY_ARRAY; }
/* {@inheritDoc}
* @deprecated Please use {@code toArray()} instead—this method is redundant and will be removed in the future.
*/
@Deprecated
@Override
public KEY_TYPE[] TO_KEY_ARRAY(KEY_TYPE[] a) { return a; }
@Override
public void forEach(final METHOD_ARG_KEY_CONSUMER action) { }
@Override
public boolean containsAll(final COLLECTION c) { return c.isEmpty(); }
@Override
public boolean addAll(final COLLECTION c) { throw new UnsupportedOperationException(); }
@Override
public boolean removeAll(final COLLECTION c) { throw new UnsupportedOperationException(); }
@Override
public boolean retainAll(final COLLECTION c) { throw new UnsupportedOperationException(); }
@Override
public boolean removeIf(final METHOD_ARG_PREDICATE filter) {
Objects.requireNonNull(filter);
return false;
}
#if KEYS_BYTE_CHAR_SHORT_FLOAT
@Override
public KEY_WIDENED_ITERATOR KEY_WIDENED_ITERATOR_METHOD() { return WIDENED_ITERATORS.EMPTY_ITERATOR; }
@Override
public KEY_WIDENED_SPLITERATOR KEY_WIDENED_SPLITERATOR_METHOD() { return WIDENED_SPLITERATORS.EMPTY_SPLITERATOR; }
#endif
#endif
}
/** A synchronized wrapper class for collections. */
static class SynchronizedCollection KEY_GENERIC implements COLLECTION KEY_GENERIC, java.io.Serializable {
private static final long serialVersionUID = -7046029254386353129L;
protected final COLLECTION KEY_GENERIC collection;
protected final Object sync;
protected SynchronizedCollection(final COLLECTION KEY_GENERIC c, final Object sync) {
this.collection = Objects.requireNonNull(c);
this.sync = sync;
}
protected SynchronizedCollection(final COLLECTION KEY_GENERIC c) {
this.collection = Objects.requireNonNull(c);
this.sync = this;
}
@Override
public boolean add(final KEY_GENERIC_TYPE k) { synchronized(sync) { return collection.add(k); } }
@Override
public boolean contains(final KEY_TYPE k) { synchronized(sync) { return collection.contains(k); } }
@Override
public boolean REMOVE(final KEY_TYPE k) { synchronized(sync) { return collection.REMOVE(k); } }
@Override
public int size() { synchronized(sync) { return collection.size(); } }
@Override
public boolean isEmpty() { synchronized(sync) { return collection.isEmpty(); } }
@Override
public KEY_TYPE[] TO_KEY_ARRAY() { synchronized(sync) { return collection.TO_KEY_ARRAY(); } }
#if KEYS_PRIMITIVE
@Override
public Object[] toArray() { synchronized(sync) { return collection.toArray(); } }
/* {@inheritDoc}
* @deprecated Please use {@code toArray()} instead—this method is redundant and will be removed in the future.
*/
@Deprecated
@Override
public KEY_TYPE[] TO_KEY_ARRAY(final KEY_TYPE[] a) { return toArray(a); }
@Override
public KEY_TYPE[] toArray(final KEY_TYPE[] a) { synchronized(sync) { return collection.toArray(a); } }
@Override
public boolean addAll(final COLLECTION c) { synchronized(sync) { return collection.addAll(c); } }
@Override
public boolean containsAll(final COLLECTION c) { synchronized(sync) { return collection.containsAll(c); } }
@Override
public boolean removeAll(final COLLECTION c) { synchronized(sync) { return collection.removeAll(c); } }
@Override
public boolean retainAll(final COLLECTION c) { synchronized(sync) { return collection.retainAll(c); } }
@Override
@Deprecated
public boolean add(final KEY_GENERIC_CLASS k) { synchronized(sync) { return collection.add(k); } }
@Override
@Deprecated
public boolean contains(final Object k) { synchronized(sync) { return collection.contains(k); } }
@Override
@Deprecated
public boolean remove(final Object k) { synchronized(sync) { return collection.remove(k); } }
#if defined JDK_PRIMITIVE_ITERATOR
@Override
public KEY_WIDENED_ITERATOR KEY_WIDENED_ITERATOR_METHOD() { return collection.KEY_WIDENED_ITERATOR_METHOD(); }
@Override
public KEY_WIDENED_SPLITERATOR KEY_WIDENED_SPLITERATOR_METHOD() { return collection.KEY_WIDENED_SPLITERATOR_METHOD(); }
#endif
#if defined JDK_PRIMITIVE_STREAM
@Override
public JDK_PRIMITIVE_STREAM KEY_WIDENED_STREAM_METHOD() { return collection.KEY_WIDENED_STREAM_METHOD(); }
@Override
public JDK_PRIMITIVE_STREAM KEY_WIDENED_PARALLEL_STREAM_METHOD() { return collection.KEY_WIDENED_PARALLEL_STREAM_METHOD(); }
#endif
#endif
@Override
public <T> T[] toArray(final T[] a) { synchronized(sync) { return collection.toArray(a); } }
@Override
public KEY_ITERATOR KEY_GENERIC iterator() { return collection.iterator(); }
@Override
public KEY_SPLITERATOR KEY_GENERIC spliterator() { return collection.spliterator(); }
#if defined JDK_PRIMITIVE_STREAM
@Deprecated
#endif
@Override
public java.util.stream.Stream <KEY_GENERIC_CLASS> stream() { return collection.stream(); }
#if defined JDK_PRIMITIVE_STREAM
@Deprecated
#endif
@Override
public java.util.stream.Stream <KEY_GENERIC_CLASS> parallelStream() { return collection.parallelStream(); }
@Override
public void forEach(final METHOD_ARG_KEY_CONSUMER action) { synchronized(sync) { collection.forEach(action); } }
@Override
public boolean addAll(final Collection<? extends KEY_GENERIC_CLASS> c) { synchronized(sync) { return collection.addAll(c); } }
@Override
public boolean containsAll(final Collection<?> c) { synchronized(sync) { return collection.containsAll(c); } }
@Override
public boolean removeAll(final Collection<?> c) { synchronized(sync) { return collection.removeAll(c); } }
@Override
public boolean retainAll(final Collection<?> c) { synchronized(sync) { return collection.retainAll(c); } }
@Override
public boolean removeIf(final METHOD_ARG_PREDICATE filter) { synchronized(sync) { return collection.removeIf(filter); } }
@Override
public void clear() { synchronized(sync) { collection.clear(); } }
@Override
public String toString() { synchronized(sync) { return collection.toString(); } }
@Override
public int hashCode() { synchronized(sync) { return collection.hashCode(); } }
@Override
public boolean equals(final Object o) { if (o == this) return true; synchronized(sync) { return collection.equals(o); } }
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
synchronized(sync) { s.defaultWriteObject(); }
}
}
/** Returns a synchronized collection backed by the specified collection.
*
* @param c the collection to be wrapped in a synchronized collection.
* @return a synchronized view of the specified collection.
* @see java.util.Collections#synchronizedCollection(Collection)
*/
public static KEY_GENERIC COLLECTION KEY_GENERIC synchronize(final COLLECTION KEY_GENERIC c) { return new SynchronizedCollection KEY_GENERIC_DIAMOND(c); }
/** Returns a synchronized collection backed by the specified collection, using an assigned object to synchronize.
*
* @param c the collection to be wrapped in a synchronized collection.
* @param sync an object that will be used to synchronize the list access.
* @return a synchronized view of the specified collection.
* @see java.util.Collections#synchronizedCollection(Collection)
*/
public static KEY_GENERIC COLLECTION KEY_GENERIC synchronize(final COLLECTION KEY_GENERIC c, final Object sync) { return new SynchronizedCollection KEY_GENERIC_DIAMOND(c, sync); }
/** An unmodifiable wrapper class for collections. */
static class UnmodifiableCollection KEY_GENERIC implements COLLECTION KEY_GENERIC, java.io.Serializable {
private static final long serialVersionUID = -7046029254386353129L;
protected final COLLECTION KEY_EXTENDS_GENERIC collection;
protected UnmodifiableCollection(final COLLECTION KEY_EXTENDS_GENERIC c) {
this.collection = Objects.requireNonNull(c);
}
@Override
public boolean add(final KEY_GENERIC_TYPE k) { throw new UnsupportedOperationException(); }
@Override
public boolean REMOVE(final KEY_TYPE k) { throw new UnsupportedOperationException(); }
@Override
public int size() { return collection.size(); }
@Override
public boolean isEmpty() { return collection.isEmpty(); }
@Override
public boolean contains(final KEY_TYPE o) { return collection.contains(o); }
@Override
public KEY_ITERATOR KEY_GENERIC iterator() { return ITERATORS.unmodifiable(collection.iterator()); }
@Override
#if KEYS_PRIMITIVE
public KEY_SPLITERATOR KEY_GENERIC spliterator() { return collection.spliterator(); }
#else
@SuppressWarnings("unchecked")
public KEY_SPLITERATOR KEY_GENERIC spliterator() { return (KEY_SPLITERATOR KEY_GENERIC)collection.spliterator(); }
#endif
#if defined JDK_PRIMITIVE_STREAM
@Deprecated
#endif
@Override
#if KEYS_PRIMITIVE
public java.util.stream.Stream <KEY_GENERIC_CLASS> stream() { return collection.stream(); }
#else
@SuppressWarnings("unchecked")
public java.util.stream.Stream <KEY_GENERIC_CLASS> stream() { return (java.util.stream.Stream <KEY_GENERIC_CLASS>)collection.stream(); }
#endif
#if defined JDK_PRIMITIVE_STREAM
@Deprecated
#endif
@Override
#if KEYS_PRIMITIVE
public java.util.stream.Stream <KEY_GENERIC_CLASS> parallelStream() { return collection.parallelStream(); }
#else
@SuppressWarnings("unchecked")
public java.util.stream.Stream <KEY_GENERIC_CLASS> parallelStream() { return (java.util.stream.Stream <KEY_GENERIC_CLASS>)collection.parallelStream(); }
#endif
@Override
public void clear() { throw new UnsupportedOperationException(); }
@Override
public <T> T[] toArray(final T[] a) { return collection.toArray(a); }
@Override
public Object[] toArray() { return collection.toArray(); }
@Override
public void forEach(final METHOD_ARG_KEY_CONSUMER action) { collection.forEach(action); }
@Override
public boolean containsAll(Collection<?> c) { return collection.containsAll(c); }
@Override
public boolean addAll(Collection<? extends KEY_GENERIC_CLASS> c) { throw new UnsupportedOperationException(); }
@Override
public boolean removeAll(Collection<?> c) { throw new UnsupportedOperationException(); }
@Override
public boolean retainAll(Collection<?> c) { throw new UnsupportedOperationException(); }
@Override
public boolean removeIf(final METHOD_ARG_PREDICATE filter) { throw new UnsupportedOperationException(); }
#if KEYS_PRIMITIVE
@Override
@Deprecated
public boolean add(final KEY_GENERIC_CLASS k) { throw new UnsupportedOperationException(); }
@Override
@Deprecated
public boolean contains(final Object k) { return collection.contains(k); }
@Override
@Deprecated
public boolean remove(final Object k) { throw new UnsupportedOperationException(); }
@Override
public KEY_TYPE[] TO_KEY_ARRAY() { return collection.TO_KEY_ARRAY(); }
/* {@inheritDoc}
* @deprecated Please use {@code toArray()} instead—this method is redundant.
*/
@Deprecated
@Override
public KEY_TYPE[] TO_KEY_ARRAY(final KEY_TYPE[] a) { return toArray(a); }
@Override
public KEY_TYPE[] toArray(final KEY_TYPE[] a) { return collection.toArray(a); }
@Override
public boolean containsAll(COLLECTION c) { return collection.containsAll(c); }
@Override
public boolean addAll(COLLECTION c) { throw new UnsupportedOperationException(); }
@Override
public boolean removeAll(COLLECTION c) { throw new UnsupportedOperationException(); }
@Override
public boolean retainAll(COLLECTION c) { throw new UnsupportedOperationException(); }
#if defined JDK_PRIMITIVE_ITERATOR
@Override
public KEY_WIDENED_ITERATOR KEY_WIDENED_ITERATOR_METHOD() { return collection.KEY_WIDENED_ITERATOR_METHOD(); }
@Override
public KEY_WIDENED_SPLITERATOR KEY_WIDENED_SPLITERATOR_METHOD() { return collection.KEY_WIDENED_SPLITERATOR_METHOD(); }
#endif
#if defined JDK_PRIMITIVE_STREAM
@Override
public JDK_PRIMITIVE_STREAM KEY_WIDENED_STREAM_METHOD() { return collection.KEY_WIDENED_STREAM_METHOD(); }
@Override
public JDK_PRIMITIVE_STREAM KEY_WIDENED_PARALLEL_STREAM_METHOD() { return collection.KEY_WIDENED_PARALLEL_STREAM_METHOD(); }
#endif
#endif
@Override
public String toString() { return collection.toString(); }
@Override
public int hashCode() { return collection.hashCode(); }
@Override
public boolean equals(final Object o) { if (o == this) return true; return collection.equals(o); }
}
/** Returns an unmodifiable collection backed by the specified collection.
*
* @param c the collection to be wrapped in an unmodifiable collection.
* @return an unmodifiable view of the specified collection.
* @see java.util.Collections#unmodifiableCollection(Collection)
*/
public static KEY_GENERIC COLLECTION KEY_GENERIC unmodifiable(final COLLECTION KEY_EXTENDS_GENERIC c) { return new UnmodifiableCollection KEY_GENERIC_DIAMOND(c); }
/** A collection wrapper class for iterables. */
public static class IterableCollection KEY_GENERIC extends ABSTRACT_COLLECTION KEY_GENERIC implements java.io.Serializable {
private static final long serialVersionUID = -7046029254386353129L;
protected final KEY_ITERABLE KEY_GENERIC iterable;
protected IterableCollection(final KEY_ITERABLE KEY_GENERIC iterable) {
this.iterable = Objects.requireNonNull(iterable);
}
@Override
public int size() {
long size = iterable.spliterator().getExactSizeIfKnown();
if (size >= 0) return (int)Math.min(Integer.MAX_VALUE, size);
int c = 0;
final KEY_ITERATOR KEY_GENERIC iterator = iterator();
while(iterator.hasNext()) {
iterator.NEXT_KEY();
c++;
}
return c;
}
@Override
public boolean isEmpty() { return ! iterable.iterator().hasNext(); }
@Override
public KEY_ITERATOR KEY_GENERIC iterator() { return iterable.iterator(); }
@Override
public KEY_SPLITERATOR KEY_GENERIC spliterator() { return iterable.spliterator(); }
#if defined JDK_PRIMITIVE_ITERATOR && KEYS_PRIMITIVE
@Override
public KEY_WIDENED_ITERATOR KEY_WIDENED_ITERATOR_METHOD() { return iterable.KEY_WIDENED_ITERATOR_METHOD() ; }
@Override
public KEY_WIDENED_SPLITERATOR KEY_WIDENED_SPLITERATOR_METHOD() { return iterable.KEY_WIDENED_SPLITERATOR_METHOD() ; }
#endif
}
/** Returns an unmodifiable collection backed by the specified iterable.
*
* @param iterable the iterable object to be wrapped in an unmodifiable collection.
* @return an unmodifiable collection view of the specified iterable.
*/
public static KEY_GENERIC COLLECTION KEY_GENERIC asCollection(final KEY_ITERABLE KEY_GENERIC iterable) {
if (iterable instanceof COLLECTION) return (COLLECTION KEY_GENERIC)iterable;
// TODO test for Collection but not our collection, and make a wrapper for it.
return new IterableCollection KEY_GENERIC_DIAMOND(iterable);
}
#if KEYS_INT_LONG_DOUBLE || KEYS_REFERENCE // Currently used for the collector methods, which only exist for the "first-class" primitives and object types.
/** Helper class for size decreasing size estimation.
*
* <p>Used to implement {@code toXWithExpectedSize} to prevent allocating a data structure big enough for the full stream in every thread, when only one thread
* (typically the first one) is going to need to "see" all of it at some point.
*
* <p>Currently set to always assume a roughly split-by-two strategy from the {@link java.util.Spliterator} backing the
* {@link java.util.stream.Stream} (or primitive equivalent). This may perform worse for {@code Spliterator}s that don't
* (for example the ones that wrap an {@link java.util.Iterator} as a {@code Spliterator}). This is quite rare in practice however.
* But note that even in such cases, this would <em>still</em> perform no worse (modulo some trivial amount) then the default
* "flat size for every list" method of combining.
* A way to detect such cases and use a different size estimation strategy may come in the future.
*
* <p>See the {@linkplain #SizeDecreasingSupplier constructor} for requirements of the {@link #builder} functor.
*/
static class SizeDecreasingSupplier<
#if KEYS_REFERENCE
K,
#endif
C extends COLLECTION KEY_GENERIC> implements Supplier<C> {
static final int RECOMMENDED_MIN_SIZE = 8;
final AtomicInteger suppliedCount = new AtomicInteger(0);
final int expectedFinalSize;
final IntFunction<C> builder;
/**
* Construct this {@link SizeDecreasingSupplier}.
*
* @param expectedFinalSize The expected size of the entire remaining contents of the {@code Stream}
* and thus what size the Collection holding the final results should try to be preallocated to.
* @param builder the builder function.
* <p>The input integer will be the size of the collection to preallocate.
* <p>It is recommended that you allocate a minimum size if the given {@code size} is small.
* {@link #RECOMMENDED_MIN_SIZE} gives a good enough ballpark for most cases.
* <p>The subclass is free to use the default constructor for the Collection construction
* instead of the {@code size} given
* if {@code size} falls below the default size used for the default initial capacity for
* that Collection implementation. Often such default constructors contain additional
* optimizations that giving an explicit initial capacity would not do (even if given
* the default capacity as the initial capacity explicitly).
* <p>{@link Supplier#get() get()} of the {@link IntFunction} must return non-{@code null}.
*/
SizeDecreasingSupplier(int expectedFinalSize, IntFunction<C> builder) {
this.expectedFinalSize = expectedFinalSize;
this.builder = builder;
}
// This method may be worth pulling into a shared superclass.
@Override
public C get() {
// The "correct" splitting (assuming split by two) would be
// expectedFinalSize / floor(ln_2(++suppliedCount)). But that seems too much trouble to be worth it.
// Instead we will take a simple harmonically decreasing ratio (1, 1/2, 1/3, 1/4)
//
// Round up int math (to round up, not down) adapted from https://stackoverflow.com/a/503201.
int expectedNeededNextSize = 1 + ((expectedFinalSize - 1) / suppliedCount.incrementAndGet());
if (expectedNeededNextSize < 0) {
// Overflow (and weird below zero results) failsafe
expectedNeededNextSize = RECOMMENDED_MIN_SIZE;
}
return builder.apply(expectedNeededNextSize);
}
}
#endif // KEYS_INT_LONG_DOUBLE || KEYS_REFERENCE
}
|