├── .gitignore ├── .settings ├── org.eclipse.jdt.core.prefs └── org.eclipse.jdt.ui.prefs ├── .svnignore ├── CHANGES ├── LICENSE-2.0 ├── README.md ├── REVISION.md ├── TODO ├── bnd └── biz.aQute.bnd-5.2.0.jar ├── build.properties ├── build.xml ├── drv ├── AVLTreeMap.drv ├── AVLTreeSet.drv ├── AbstractBidirectionalIterator.drv ├── AbstractBigList.drv ├── AbstractBigListIterator.drv ├── AbstractCollection.drv ├── AbstractComparator.drv ├── AbstractFunction.drv ├── AbstractIterator.drv ├── AbstractList.drv ├── AbstractListIterator.drv ├── AbstractMap.drv ├── AbstractPriorityQueue.drv ├── AbstractSet.drv ├── AbstractSortedMap.drv ├── AbstractSortedSet.drv ├── AbstractSpliterator.drv ├── AbstractStack.drv ├── ArrayFIFOQueue.drv ├── ArrayFrontCodedBigList.drv ├── ArrayFrontCodedList.drv ├── ArrayIndirectPriorityQueue.drv ├── ArrayList.drv ├── ArrayMap.drv ├── ArrayPriorityQueue.drv ├── ArraySet.drv ├── Arrays.drv ├── BidirectionalIterable.drv ├── BidirectionalIterator.drv ├── BigArrayBigList.drv ├── BigArrays.drv ├── BigArraysCommon.drv ├── BigArraysFragment.drv ├── BigList.drv ├── BigListIterator.drv ├── BigListIterators.drv ├── BigLists.drv ├── BigSpliterators.drv ├── BinIO.drv ├── BinIOFragment.drv ├── BinaryOperator.drv ├── Collection.drv ├── Collections.drv ├── Comparator.drv ├── Comparators.drv ├── Consumer.drv ├── Function.drv ├── Functions.drv ├── Hash.drv ├── HeapIndirectPriorityQueue.drv ├── HeapPriorityQueue.drv ├── HeapSemiIndirectPriorityQueue.drv ├── Heaps.drv ├── ImmutableList.drv ├── ImmutablePair.drv ├── ImmutableSortedPair.drv ├── IndirectHeaps.drv ├── IndirectPriorityQueue.drv ├── Iterable.drv ├── Iterables.drv ├── Iterator.drv ├── Iterators.drv ├── LinkedOpenCustomHashMap.drv ├── LinkedOpenCustomHashSet.drv ├── LinkedOpenHashMap.drv ├── LinkedOpenHashSet.drv ├── List.drv ├── ListIterator.drv ├── Lists.drv ├── Map.drv ├── MappedBigList.drv ├── Maps.drv ├── MutablePair.drv ├── OpenCustomHashMap.drv ├── OpenCustomHashSet.drv ├── OpenHashBigSet.drv ├── OpenHashMap.drv ├── OpenHashSet.drv ├── Pair.drv ├── Predicate.drv ├── PriorityQueue.drv ├── PriorityQueues.drv ├── RBTreeMap.drv ├── RBTreeSet.drv ├── SemiIndirectHeaps.drv ├── Set.drv ├── Sets.drv ├── SortedMap.drv ├── SortedMaps.drv ├── SortedPair.drv ├── SortedSet.drv ├── SortedSets.drv ├── Spliterator.drv ├── Spliterators.drv ├── Stack.drv ├── StripedOpenHashMap.drv ├── TextIO.drv ├── TextIOFragment.drv └── UnaryOperator.drv ├── fastutil-core.bnd ├── fastutil.bnd ├── find-deps.sh ├── gencsource.sh ├── guava └── it │ └── unimi │ └── dsi │ └── fastutil │ ├── ints │ ├── Int2IntOpenHashMapGuavaTest.java │ └── IntSetGuavaTest.java │ └── objects │ ├── Object2ObjectOpenHashMapGuavaTest.java │ ├── Object2ObjectRBTreeMapGuavaTest.java │ └── ObjectOpenHashSetGuavaTest.java ├── ivy.xml ├── lib ├── hamcrest-all-1.3.jar └── junit-4.13.jar ├── makefile ├── pom-core-model.xml ├── pom-model.xml ├── split.sh ├── src ├── it │ └── unimi │ │ └── dsi │ │ └── fastutil │ │ ├── AbstractIndirectPriorityQueue.java │ │ ├── AbstractPriorityQueue.java │ │ ├── AbstractStack.java │ │ ├── Arrays.java │ │ ├── BidirectionalIterator.java │ │ ├── BigList.java │ │ ├── BigListIterator.java │ │ ├── BigSwapper.java │ │ ├── Function.java │ │ ├── Hash.java │ │ ├── HashCommon.java │ │ ├── IndirectPriorityQueue.java │ │ ├── IndirectPriorityQueues.java │ │ ├── Pair.java │ │ ├── PriorityQueue.java │ │ ├── PriorityQueues.java │ │ ├── SafeMath.java │ │ ├── Size64.java │ │ ├── SortedPair.java │ │ ├── Stack.java │ │ ├── Swapper.java │ │ ├── booleans │ │ └── package-info.java │ │ ├── bytes │ │ └── package-info.java │ │ ├── chars │ │ └── package-info.java │ │ ├── doubles │ │ └── package-info.java │ │ ├── floats │ │ └── package-info.java │ │ ├── ints │ │ └── package-info.java │ │ ├── io │ │ ├── FastBufferedInputStream.java │ │ ├── FastBufferedOutputStream.java │ │ ├── FastByteArrayInputStream.java │ │ ├── FastByteArrayOutputStream.java │ │ ├── FastMultiByteArrayInputStream.java │ │ ├── InspectableFileCachedInputStream.java │ │ ├── MeasurableInputStream.java │ │ ├── MeasurableOutputStream.java │ │ ├── MeasurableStream.java │ │ ├── RepositionableStream.java │ │ └── package-info.java │ │ ├── longs │ │ └── package-info.java │ │ ├── objects │ │ └── package-info.java │ │ ├── package-info.java │ │ └── shorts │ │ └── package-info.java └── overview.html ├── test.sh └── test └── it └── unimi └── dsi └── fastutil ├── ArraysTest.java ├── BigArraysTest.java ├── HashCommonTest.java ├── IntIntPairTest.java ├── MainRunner.java ├── PairTest.java ├── bytes ├── ByteArrayFrontCodedBigListTest.java ├── ByteArrayFrontCodedListTest.java └── ByteArraysTest.java ├── chars ├── CharArrayFrontCodedListTest.java └── CharArraysTest.java ├── doubles ├── DoubleArraysTest.java ├── DoubleBigArraysTest.java └── DoubleOpenHashSetTest.java ├── floats ├── FloatArraysTest.java └── FloatOpenHashSetTest.java ├── ints ├── AbstractInt2IntMapTest.java ├── AbstractIntCollectionTest.java ├── Int2IntArrayMapTest.java ├── Int2IntLinkedOpenHashMapTest.java ├── Int2IntMapGenericAVLTest.java ├── Int2IntMapGenericArrayTest.java ├── Int2IntMapGenericDefaultTest.java ├── Int2IntMapGenericLinkedOpenHashTest.java ├── Int2IntMapGenericOpenHashTest.java ├── Int2IntMapGenericRBTest.java ├── Int2IntMapGenericTest.java ├── Int2IntMapsTest.java ├── Int2IntOpenCustomHashMapTest.java ├── Int2IntOpenHashMapTest.java ├── Int2ObjectFunctionTest.java ├── Int2ObjectMapGenericAVLTest.java ├── Int2ObjectMapGenericArrayTest.java ├── Int2ObjectMapGenericDefaultTest.java ├── Int2ObjectMapGenericLinkedOpenHashTest.java ├── Int2ObjectMapGenericOpenHashTest.java ├── Int2ObjectMapGenericRBTest.java ├── Int2ObjectMapGenericTest.java ├── Int2ObjectMapTest.java ├── IntAVLTreeSetTest.java ├── IntArrayFIFOQueueTest.java ├── IntArrayFrontCodedListTest.java ├── IntArrayIndirectPriorityQueueTest.java ├── IntArrayListTest.java ├── IntArrayPriorityQueueTest.java ├── IntArraySetTest.java ├── IntArraysTest.java ├── IntBigArrayBigListTest.java ├── IntBigArraysTest.java ├── IntBigListsTest.java ├── IntCollectionsTest.java ├── IntComparatorTest.java ├── IntHeapPriorityQueueTest.java ├── IntHeapSemiIndirectPriorityQueueTest.java ├── IntImmutableListTest.java ├── IntLinkedOpenCustomHashSetTest.java ├── IntLinkedOpenHashSetTest.java ├── IntListTest.java ├── IntListsTest.java ├── IntMappedBigListTest.java ├── IntOpenCustomHashSetTest.java ├── IntOpenHashBigSetTest.java ├── IntOpenHashSetTest.java ├── IntRBTreeSetTest.java ├── IntSemiIndirectHeapsTest.java ├── IntSetTest.java ├── IntSetsTest.java ├── IntSpliteratorsTest.java └── StripedInt2IntOpenHashMapTest.java ├── io ├── BinIOTest.java ├── FastBufferedInputStreamTest.java ├── FastBufferedOutputStreamTest.java ├── FastByteArrayOutputStreamTest.java ├── FastMultiByteArrayInputStreamTest.java ├── InspectableFileCachedInputStreamTest.java └── TextIOTest.java ├── longs ├── LongArrayFrontCodedListTest.java ├── LongArraysTest.java ├── LongOpenHashBigSetTest.java └── LongSetTest.java ├── objects ├── AbstractObject2IntFunctionTest.java ├── Object2IntArrayMapTest.java ├── Object2IntOpenHashMapTest.java ├── Object2ObjectArrayMapTest.java ├── Object2ObjectOpenHashMapTest.java ├── ObjectAVLTreeSetTest.java ├── ObjectArrayListTest.java ├── ObjectArrayPriorityQueueTest.java ├── ObjectArraySetTest.java ├── ObjectArraysTest.java ├── ObjectBigArrayBigListTest.java ├── ObjectBigArraysTest.java ├── ObjectHeapPriorityQueueTest.java ├── ObjectImmutableListTest.java ├── ObjectListTest.java ├── ObjectOpenCustomHashSetTest.java ├── ObjectOpenHashBigSetTest.java ├── ObjectOpenHashSetTest.java ├── ObjectRBTreeSetTest.java ├── ObjectSetTest.java ├── ObjectSetsTest.java ├── Reference2ReferenceArrayMapTest.java └── ReferenceArraySetTest.java └── shorts ├── ShortArrayFrontCodedListTest.java ├── ShortArrayListTest.java └── ShortArraysTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | src/it/unimi/dsi/fastutil/BigArrays.java 2 | src/it/unimi/dsi/fastutil/*/*.java 3 | src/it/unimi/dsi/fastutil/*/*.c 4 | src/it/unimi/dsi/fastutil/*/*.h 5 | src/it/unimi/dsi/fastutil/*.c 6 | src/it/unimi/dsi/fastutil/*.h 7 | pom.xml 8 | pom.xml.asc 9 | dist 10 | docs/* 11 | build 12 | bin 13 | fastutil-*.jar 14 | fastutil*.txt 15 | pom-*.xml 16 | .classpath 17 | .project 18 | .ant-targets-build.xml 19 | -------------------------------------------------------------------------------- /.svnignore: -------------------------------------------------------------------------------- 1 | build 2 | docs 3 | test 4 | fastutil*.tar.gz 5 | fastutil*.jar 6 | test.err 7 | LinkedOpenCustomHashMap.drv 8 | LinkedOpenCustomHashSet.drv 9 | OpenCustomHashMap.drv 10 | OpenCustomHashSet.drv 11 | LinkedOpenHashMap.drv 12 | LinkedOpenHashSet.drv 13 | -------------------------------------------------------------------------------- /REVISION.md: -------------------------------------------------------------------------------- 1 | Criteria for the revision of fastutil's code base 2 | ================================================= 3 | 4 | General rules 5 | ------------- 6 | 7 | * All methods overriding some other method must sport 8 | the @Override annotation. 9 | 10 | * All implementation of deprecated methods must 11 | propagate the deprecation annotation and documentation. 12 | 13 | * Annotation must be in the order @Deprecated, @Override, 14 | @SuppressWarnings. 15 | 16 | Type-specific methods 17 | --------------------- 18 | 19 | For each new type-specific method, the following must happen: 20 | 21 | * The method documentation must contain only a short 22 | title (more or less copied from the non-specific JDK 23 | documentation) and a `@see` pointing at the corresponding 24 | non-type-specific method. 25 | 26 | * The corresponding non-type-specific method must be 27 | redeclared as @Deprecated with the following documentation 28 | and annotation. 29 | 30 | ```java 31 | /** {@inheritDoc} 32 | * @deprecated Please use the corresponding type-specific method instead. */ 33 | @Deprecated 34 | @Override 35 | ``` 36 | 37 | * Ideally, one should provide an abstract class which implements 38 | the non-type-specific methods by delegating the argument 39 | to the type-specific method, possibly treating separately 40 | the `null` case. Documentation and annotation should be 41 | 42 | ```java 43 | /** {@inheritDoc} 44 | *

Delegates to the corresponding type-specific method. 45 | * @deprecated Please use the corresponding type-specific method instead. */ 46 | @Deprecated 47 | @Override 48 | ``` 49 | 50 | * Ideally, the abstract class should implement the type-specific 51 | methods following the example of the Java Collections Framework. 52 | For example, 53 | ```java 54 | /** {@inheritDoc} 55 | *

This implementation just throws an {@link UnsupportedOperationException}. */ 56 | @Override 57 | ``` 58 | 59 | Obsolete type-specific methods 60 | ------------------------------ 61 | 62 | Obsolete methods such as `intIterator()` should be marked in interfaces 63 | as deprecated and documented with a pointer 64 | ```java 65 | /** Returns a type-specific iterator on this elements of this collection. 66 | * 67 | * @see #iterator() 68 | * @deprecated As of fastutil 5, replaced by {@link #iterator()}. 69 | */ 70 | @Deprecated 71 | ``` 72 | 73 | Whenever these methods are implemented, the deprecation must be propagated. 74 | 75 | ```java 76 | /** @{inheritDoc} 77 | * @deprecated As of fastutil 5, replaced by {@link #iterator()}. */ 78 | @Deprecated 79 | @Override 80 | ``` 81 | 82 | Unnecessary methods 83 | ------------------- 84 | 85 | Over the years, a number of duplicate methods have been implemented 86 | (e.g., standard versions of type-specific methods). For clarity 87 | and simplicity, no unnecessary method should be implemented. In doubt, 88 | follow the example of the JDK. 89 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | - Buffer bulk methods with index to get rid of mapped buffer duplication 2 | - Loops in sorting for big arrays should use real indices, not get/set 3 | - Check that binary insertion sort is maybe better 4 | - radix sort for big arrays 5 | - Navigable sets and maps 6 | - Make digit array in radix sort shorter 7 | - FastIterable interface 8 | - Try tripartite quickSort or TimSort for generic sorting. 9 | - http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html 10 | - http://java-performance.info/large-hashmap-overview-jdk-fastutil-goldman-sachs-hppc-koloboke-trove 11 | - http://shipilev.net/ 12 | - http://java-performance.info/jmh/ 13 | - Big maps 14 | - Cilksort 15 | - Tune threshold for non-recursive sorts 16 | - parallel indirect radix sort of two arrays 17 | - Better remove() from keyset (avoid in all possible cases inheritance from AbstractSet!) 18 | - Eliminate ping-pong implementations (look for "delegates to the corresponding generic method") 19 | - Fix comparator() in SortedSet not being specified in the same way as in SortedMap 20 | - Document the return value of map generic methods (null? default return value?). 21 | - XBigArrays.unstableSort for automatic algorithm selection like XArrays.unstableSort 22 | - BigList.unstableSort method 23 | - addTo() etc. on numeric interfaces 24 | - peek() method for ArrayFIFOQueue. 25 | - Spliterator implementations for RBTreeSet/Map, AVLTreeSet/Map, and ArrayFrontCodedLists 26 | - Implement type-specific Iterator views of Spliterator (aka, Spliterators.iterator(Spliterator)) 27 | - Find a cleaner way to deal with the disambiguation overloads 28 | aka. get rid of the forEachRemaining(it.unimi.dsi.fastutil.ints.IntConsumer) style methods and the SpliteratorDisambiguationMethodsFinalShim style classes 29 | - Primitive collector helper methods, collecting a primitive stream into list or set without boxing/unboxing 30 | - Don't have a N-nested sublist of ArrayList.get go through N layers of get methods 31 | remove and add will have to continue to do this so all parent sublists can adjust their bounds, 32 | but get can just short circuit to the parent list's array if we track bounds correctly 33 | - toBigArray for BigList (or maybe a new BigCollection) 34 | - Make the recursive algorithms of BigArrays and the type specific BigArrays prefer aligning to segment boundaries 35 | This should improve cache locality 36 | - Make constructors consistent across types (make it so all Collection types can accept a Collection, an array, an Iterable, and an Iterator) 37 | - Add array copying constructors (or perhaps static factories) for ArraySet 38 | The current one just uses the array exactly, when in some cases a copy of that array may be desired 39 | We can't change the current one without breaking compatibility 40 | - For classes that have a main() method for testing instead of a JUnit test, either migrate it to JUnit or make a JUnit test that just calls the in class's test method 41 | This should make it easier to get more accurate code coverage data. 42 | - Update all benchmarks with the framework ArraySet and ArrayList have 43 | - Pull `if (n < 0) throw new IllegalArgumentException("Argument must be nonnegative: " + n);` into a helper method, since it is used so much 44 | - Convert the tests in main methods (if TEST=1 is given to make) into real unit tests 45 | - Similarly, pull the benchmarks into their own files 46 | -------------------------------------------------------------------------------- /bnd/biz.aQute.bnd-5.2.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vigna/fastutil/eeca53120ef1e35172c14aed53a643516eeaf3f5/bnd/biz.aQute.bnd-5.2.0.jar -------------------------------------------------------------------------------- /build.properties: -------------------------------------------------------------------------------- 1 | jar.base=/usr/share/java 2 | javadoc.base=/usr/share/javadoc 3 | 4 | build.sysclasspath=ignore 5 | 6 | version=8.5.15 7 | 8 | dist=dist 9 | src=src 10 | drv=drv 11 | test=test 12 | reports=reports 13 | coverage=coverage 14 | checkstyle=checkstyle 15 | docs=docs 16 | build=build 17 | instrumented=instrumented 18 | 19 | remote.j2se.apiurl=https://docs.oracle.com/javase/8/docs/api/ 20 | -------------------------------------------------------------------------------- /drv/AbstractBidirectionalIterator.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2002-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | /** An abstract class facilitating the creation of type-specific {@linkplain it.unimi.dsi.fastutil.BidirectionalIterator bidirectional iterators}. 21 | * 22 | * @deprecated As of {@code fastutil} 8 this class is no longer necessary, as its previous abstract 23 | * methods are now default methods of the type-specific interface. 24 | */ 25 | 26 | @Deprecated 27 | public abstract class KEY_ABSTRACT_BIDI_ITERATOR KEY_GENERIC extends KEY_ABSTRACT_ITERATOR KEY_GENERIC implements KEY_BIDI_ITERATOR KEY_GENERIC { 28 | protected KEY_ABSTRACT_BIDI_ITERATOR() {} 29 | } 30 | -------------------------------------------------------------------------------- /drv/AbstractBigListIterator.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2002-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | /** An abstract class facilitating the creation of type-specific {@linkplain it.unimi.dsi.fastutil.BigListIterator big-list iterators}. 21 | * 22 | * @deprecated As of {@code fastutil} 8 this class is no longer necessary, as its previous abstract 23 | * methods are now default methods of the type-specific interface. 24 | */ 25 | 26 | @Deprecated 27 | public abstract class KEY_ABSTRACT_BIG_LIST_ITERATOR KEY_GENERIC extends KEY_ABSTRACT_BIDI_ITERATOR KEY_GENERIC implements KEY_BIG_LIST_ITERATOR KEY_GENERIC { 28 | protected KEY_ABSTRACT_BIG_LIST_ITERATOR() {} 29 | } 30 | -------------------------------------------------------------------------------- /drv/AbstractComparator.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2002-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | /** An abstract class facilitating the creation of type-specific {@linkplain java.util.Comparator comparators}. 21 | * 22 | * @see java.util.Comparator 23 | * @deprecated As of {@code fastutil} 8 this class is no longer necessary, as its only previous abstract 24 | * method is now a default method of the type-specific interface. 25 | */ 26 | 27 | @Deprecated 28 | public abstract class KEY_ABSTRACT_COMPARATOR KEY_GENERIC implements KEY_COMPARATOR KEY_GENERIC, java.io.Serializable { 29 | private static final long serialVersionUID = 0L; 30 | protected KEY_ABSTRACT_COMPARATOR() {} 31 | } 32 | -------------------------------------------------------------------------------- /drv/AbstractFunction.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2002-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | /** An abstract class providing basic methods for functions implementing a type-specific interface. 21 | * 22 | *

This class handles directly a default return 23 | * value (including {@linkplain #defaultReturnValue() methods to access 24 | * it}). Instances of classes inheriting from this class have just to return 25 | * {@code defRetValue} to denote lack of a key in type-specific methods. The value 26 | * is serialized. 27 | * 28 | *

Implementing subclasses have just to provide type-specific {@code get()}, 29 | * type-specific {@code containsKey()}, and {@code size()} methods. 30 | * 31 | */ 32 | 33 | public abstract class ABSTRACT_FUNCTION KEY_VALUE_GENERIC implements FUNCTION KEY_VALUE_GENERIC, java.io.Serializable { 34 | 35 | private static final long serialVersionUID = -4940583368468432370L; 36 | 37 | protected ABSTRACT_FUNCTION() {} 38 | 39 | /** 40 | * The default return value for {@code get()}, {@code put()} and 41 | * {@code remove()}. 42 | */ 43 | 44 | protected VALUE_GENERIC_TYPE defRetValue; 45 | 46 | @Override 47 | public void defaultReturnValue(final VALUE_GENERIC_TYPE rv) { 48 | defRetValue = rv; 49 | } 50 | 51 | @Override 52 | public VALUE_GENERIC_TYPE defaultReturnValue() { 53 | return defRetValue; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /drv/AbstractIterator.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2002-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | /** An abstract class facilitating the creation of type-specific iterators. 21 | * 22 | * @apiNote Up to version 8.5.0, this class was deprecated as abstract methods were 23 | * turned into default methods of the type-specific interface. Now the class hosts 24 | * finalized versions of default delegating methods such as {@link #forEachRemaining}. 25 | */ 26 | 27 | public abstract class KEY_ABSTRACT_ITERATOR KEY_GENERIC implements KEY_ITERATOR KEY_GENERIC { 28 | protected KEY_ABSTRACT_ITERATOR() {} 29 | 30 | #if KEYS_INT_LONG_DOUBLE 31 | /** {@inheritDoc} 32 | * @implSpec This method just delegates to the interface default method, 33 | * as the default method, but it is final, so it cannot be overridden. 34 | */ 35 | @Override 36 | public final void forEachRemaining(final KEY_CONSUMER action) { 37 | forEachRemaining((JDK_PRIMITIVE_KEY_CONSUMER) action); 38 | } 39 | #endif 40 | } 41 | -------------------------------------------------------------------------------- /drv/AbstractListIterator.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2002-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | /** An abstract class facilitating the creation of type-specific {@linkplain java.util.ListIterator list iterators}. 21 | * 22 | * @deprecated As of {@code fastutil} 8 this class is no longer necessary, as its previous abstract 23 | * methods are now default methods of the type-specific interface. 24 | */ 25 | 26 | @Deprecated 27 | public abstract class KEY_ABSTRACT_LIST_ITERATOR KEY_GENERIC extends KEY_ABSTRACT_BIDI_ITERATOR KEY_GENERIC implements KEY_LIST_ITERATOR KEY_GENERIC { 28 | protected KEY_ABSTRACT_LIST_ITERATOR() {} 29 | } 30 | -------------------------------------------------------------------------------- /drv/AbstractPriorityQueue.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2002-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | /** An abstract class providing basic methods for priority queues implementing a type-specific interface. 21 | * @deprecated As of {@code fastutil} 8 this class is no longer necessary, as its previous abstract 22 | * methods are now default methods of the type-specific interface. 23 | */ 24 | @Deprecated 25 | public abstract class ABSTRACT_PRIORITY_QUEUE KEY_GENERIC extends it.unimi.dsi.fastutil.AbstractPriorityQueue implements java.io.Serializable, PRIORITY_QUEUE KEY_GENERIC { 26 | private static final long serialVersionUID = 1L; 27 | } 28 | -------------------------------------------------------------------------------- /drv/AbstractSet.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2002-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | import java.util.Set; 21 | 22 | /** An abstract class providing basic methods for sets implementing a type-specific interface. 23 | * 24 | *

Note that the type-specific {@link Set} interface adds a type-specific {@code remove()} 25 | * method, as it is no longer harmful for subclasses. Thus, concrete subclasses of this class 26 | * must implement {@code remove()} (the {@code rem()} implementation of this 27 | * class just delegates to {@code remove()}). 28 | */ 29 | 30 | public abstract class ABSTRACT_SET KEY_GENERIC extends ABSTRACT_COLLECTION KEY_GENERIC implements Cloneable, SET KEY_GENERIC { 31 | 32 | protected ABSTRACT_SET() {} 33 | 34 | @Override 35 | public abstract KEY_ITERATOR KEY_GENERIC iterator(); 36 | 37 | @Override 38 | public boolean equals(final Object o) { 39 | if (o == this) return true; 40 | if (!(o instanceof Set)) return false; 41 | 42 | Set s = (Set) o; 43 | if (s.size() != size()) return false; 44 | #if KEYS_PRIMITIVE 45 | if (s instanceof SET) { 46 | return containsAll((SET) s); 47 | } 48 | #endif 49 | return containsAll(s); 50 | } 51 | 52 | 53 | /** Returns a hash code for this set. 54 | * 55 | * The hash code of a set is computed by summing the hash codes of 56 | * its elements. 57 | * 58 | * @return a hash code for this set. 59 | */ 60 | @Override 61 | public int hashCode() { 62 | int h = 0, n = size(); 63 | KEY_ITERATOR KEY_GENERIC i = iterator(); 64 | KEY_GENERIC_TYPE k; 65 | 66 | while(n-- != 0) { 67 | k = i.NEXT_KEY(); // We need k because KEY2JAVAHASH() is a macro with repeated evaluation. 68 | h += KEY2JAVAHASH(k); 69 | } 70 | return h; 71 | } 72 | 73 | 74 | #if KEYS_PRIMITIVE 75 | /** {@inheritDoc} 76 | * Delegates to the type-specific {@code rem()} method 77 | * implemented by type-specific abstract {@link java.util.Collection} superclass. 78 | */ 79 | @Override 80 | public boolean remove(KEY_TYPE k) { 81 | return super.rem(k); 82 | } 83 | 84 | /** {@inheritDoc} 85 | * Delegates to the type-specific {@code remove()} method 86 | * specified in the type-specific {@link Set} interface. 87 | * @deprecated Please use {@code remove()} instead. 88 | */ 89 | @Deprecated 90 | @Override 91 | public boolean rem(KEY_TYPE k) { 92 | return remove(k); 93 | } 94 | #endif 95 | 96 | } 97 | -------------------------------------------------------------------------------- /drv/AbstractSortedSet.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2003-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | /** An abstract class providing basic methods for sorted sets implementing a type-specific interface. */ 21 | 22 | public abstract class ABSTRACT_SORTED_SET KEY_GENERIC extends ABSTRACT_SET KEY_GENERIC implements SORTED_SET KEY_GENERIC { 23 | 24 | protected ABSTRACT_SORTED_SET() {} 25 | 26 | @Override 27 | public abstract KEY_BIDI_ITERATOR KEY_GENERIC iterator(); 28 | } 29 | -------------------------------------------------------------------------------- /drv/AbstractSpliterator.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2020-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | /** An abstract class facilitating the creation of type-specific iterators. 21 | * 22 | * @apiNote Presently the class hosts 23 | * finalized versions of default delegating methods such as {@link #forEachRemaining}. 24 | */ 25 | 26 | public abstract class KEY_ABSTRACT_SPLITERATOR KEY_GENERIC implements KEY_SPLITERATOR KEY_GENERIC { 27 | protected KEY_ABSTRACT_SPLITERATOR() {} 28 | 29 | #if KEYS_INT_LONG_DOUBLE 30 | /** {@inheritDoc} 31 | * @implSpec This method just delegates to the interface default method, 32 | * as the default method, but it is final, so it cannot be overridden. 33 | */ 34 | @Override 35 | public final boolean tryAdvance(final KEY_CONSUMER action) { 36 | // Java won't let delegate to java.util.Spliterator.OfInt 37 | return tryAdvance((JDK_PRIMITIVE_KEY_CONSUMER) action); 38 | } 39 | 40 | /** {@inheritDoc} 41 | * @implSpec This method just delegates to the interface default method, 42 | * as the default method, but it is final, so it cannot be overridden. 43 | */ 44 | @Override 45 | public final void forEachRemaining(final KEY_CONSUMER action) { 46 | forEachRemaining((JDK_PRIMITIVE_KEY_CONSUMER) action); 47 | } 48 | #endif 49 | } 50 | -------------------------------------------------------------------------------- /drv/AbstractStack.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2002-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | /** An abstract class providing basic methods for implementing a type-specific stack interface. 21 | * 22 | * @deprecated As of {@code fastutil} 8 this class is no longer necessary, as its previous abstract 23 | * methods are now default methods of the type-specific interface. 24 | */ 25 | @Deprecated 26 | public abstract class ABSTRACT_STACK KEY_GENERIC extends it.unimi.dsi.fastutil.AbstractStack implements STACK KEY_GENERIC { 27 | protected ABSTRACT_STACK() {} 28 | } 29 | -------------------------------------------------------------------------------- /drv/BidirectionalIterable.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2002-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | /** A type-specific {@link Iterable} that further strengthens the specification of {@link Iterable#iterator()}. 21 | */ 22 | public interface KEY_BIDI_ITERABLE KEY_GENERIC extends KEY_ITERABLE KEY_GENERIC { 23 | 24 | /** Returns a type-specific {@link it.unimi.dsi.fastutil.BidirectionalIterator}. 25 | * 26 | * @return a type-specific bidirectional iterator. 27 | */ 28 | @Override 29 | KEY_BIDI_ITERATOR KEY_GENERIC iterator(); 30 | } 31 | -------------------------------------------------------------------------------- /drv/BidirectionalIterator.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2002-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | import it.unimi.dsi.fastutil.BidirectionalIterator; 21 | #if KEYS_PRIMITIVE 22 | import it.unimi.dsi.fastutil.objects.ObjectBidirectionalIterator; 23 | #endif 24 | 25 | /** A type-specific bidirectional iterator; provides an additional method to avoid (un)boxing, 26 | * and the possibility to skip elements backwards. 27 | * 28 | * @see BidirectionalIterator 29 | */ 30 | 31 | #if KEYS_PRIMITIVE 32 | public interface KEY_BIDI_ITERATOR KEY_GENERIC extends KEY_ITERATOR KEY_GENERIC, ObjectBidirectionalIterator { 33 | #else 34 | public interface KEY_BIDI_ITERATOR KEY_GENERIC extends KEY_ITERATOR KEY_GENERIC, BidirectionalIterator { 35 | #endif 36 | 37 | #if KEYS_PRIMITIVE 38 | 39 | /** 40 | * Returns the previous element as a primitive type. 41 | * 42 | * @return the previous element in the iteration. 43 | * @see java.util.ListIterator#previous() 44 | */ 45 | 46 | KEY_TYPE PREV_KEY(); 47 | 48 | /** {@inheritDoc} 49 | * @deprecated Please use the corresponding type-specific method instead. */ 50 | @Deprecated 51 | @Override 52 | default KEY_GENERIC_CLASS previous() { return KEY_CLASS.valueOf(PREV_KEY()); } 53 | 54 | #endif 55 | 56 | /** Moves back for the given number of elements. 57 | * 58 | *

The effect of this call is exactly the same as that of 59 | * calling {@link #previous()} for {@code n} times (possibly stopping 60 | * if {@link #hasPrevious()} becomes false). 61 | * 62 | * @param n the number of elements to skip back. 63 | * @return the number of elements actually skipped. 64 | * @see #previous() 65 | */ 66 | #if KEYS_PRIMITIVE 67 | @Override 68 | #endif 69 | default int back(final int n) { 70 | int i = n; 71 | while(i-- != 0 && hasPrevious()) PREV_KEY(); 72 | return n - i - 1; 73 | } 74 | 75 | /** {@inheritDoc} */ 76 | @Override 77 | default int skip(final int n) { 78 | return KEY_ITERATOR.super.skip(n); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /drv/BigListIterator.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | import it.unimi.dsi.fastutil.BigListIterator; 21 | import it.unimi.dsi.fastutil.SafeMath; 22 | 23 | /** A type-specific {@link BigListIterator}. 24 | * 25 | * @see BigListIterator 26 | */ 27 | 28 | public interface KEY_BIG_LIST_ITERATOR KEY_GENERIC extends KEY_BIDI_ITERATOR KEY_GENERIC, BigListIterator { 29 | 30 | /** 31 | * Replaces the last element returned by {@link BigListIterator#next() next()} or 32 | * {@link BigListIterator#previous() previous()} with the specified element (optional operation). 33 | * @see java.util.ListIterator#set(Object) 34 | */ 35 | 36 | #if KEYS_REFERENCE 37 | @Override 38 | #endif 39 | default void set(@SuppressWarnings("unused") final KEY_GENERIC_TYPE k) { throw new UnsupportedOperationException(); } 40 | 41 | /** 42 | * Inserts the specified element into the list (optional operation). 43 | * @see java.util.ListIterator#add(Object) 44 | */ 45 | 46 | #if KEYS_REFERENCE 47 | @Override 48 | #endif 49 | default void add(@SuppressWarnings("unused") final KEY_GENERIC_TYPE k) { throw new UnsupportedOperationException(); } 50 | 51 | #if KEYS_PRIMITIVE 52 | 53 | /** Replaces the last element returned by {@link #next()} or {@link #previous()} with the specified element (optional operation). 54 | * @deprecated Please use the corresponding type-specific method instead. */ 55 | @Deprecated 56 | @Override 57 | default void set(final KEY_GENERIC_CLASS k) { set(k.KEY_VALUE()); } 58 | 59 | /** Inserts the specified element into the list (optional operation). 60 | * @deprecated Please use the corresponding type-specific method instead. */ 61 | @Deprecated 62 | @Override 63 | default void add(final KEY_GENERIC_CLASS k) { add(k.KEY_VALUE()); } 64 | 65 | #endif 66 | 67 | 68 | /** Skips the given number of elements. 69 | * 70 | *

The effect of this call is exactly the same as that of 71 | * calling {@link BigListIterator#next() next()} for {@code n} times (possibly stopping 72 | * if {@link #hasNext()} becomes false). 73 | * 74 | * @param n the number of elements to skip. 75 | * @return the number of elements actually skipped. 76 | * @see BigListIterator#next() 77 | */ 78 | default long skip(final long n) { 79 | long i = n; 80 | while(i-- != 0 && hasNext()) NEXT_KEY(); 81 | return n - i - 1; 82 | } 83 | 84 | /** Moves back for the given number of elements. 85 | * 86 | *

The effect of this call is exactly the same as that of 87 | * calling {@link BigListIterator#previous() previous()} for {@code n} times (possibly stopping 88 | * if {@link #hasPrevious()} becomes false). 89 | * 90 | * @param n the number of elements to skip back. 91 | * @return the number of elements actually skipped. 92 | * @see BigListIterator#previous() 93 | */ 94 | default long back(final long n) { 95 | long i = n; 96 | while(i-- != 0 && hasPrevious()) PREV_KEY(); 97 | return n - i - 1; 98 | } 99 | 100 | /** {@inheritDoc} 101 | */ 102 | @Override 103 | default int skip(int n) { 104 | return SafeMath.safeLongToInt(skip((long) n)); 105 | } 106 | } -------------------------------------------------------------------------------- /drv/BinaryOperator.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2020-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | import java.util.function.BinaryOperator; 21 | 22 | /** A type-specific {@link BinaryOperator}; provides methods operating both on objects 23 | * and on primitives. 24 | * 25 | * @see BinaryOperator 26 | * @since 8.5.0 27 | */ 28 | 29 | @FunctionalInterface 30 | #if ! KEY_CLASS_Boolean 31 | public interface KEY_BINARY_OPERATOR KEY_GENERIC extends BinaryOperator, JDK_PRIMITIVE_BINARY_OPERATOR { 32 | #else 33 | public interface KEY_BINARY_OPERATOR KEY_GENERIC extends BinaryOperator { 34 | #endif 35 | 36 | /** 37 | * Computes the operator on the given inputs. 38 | * 39 | * @param x the first input. 40 | * @param y the second input. 41 | * @return the output of the operator on the given inputs. 42 | */ 43 | KEY_TYPE apply(KEY_TYPE x, KEY_TYPE y); 44 | 45 | #if KEYS_INT_LONG_DOUBLE 46 | /** {@inheritDoc} 47 | * 48 | * @implSpec This default implementation delegates to {@link #apply}. 49 | * @deprecated Please use {@link #apply}. 50 | */ 51 | #else 52 | /** {@inheritDoc} 53 | * 54 | * @implSpec This default implementation delegates to 55 | * {@link #apply} after narrowing down the arguments to the 56 | * actual key type, throwing an exception if the arguments cannot be 57 | * represented in the restricted domain. This is done for interoperability 58 | * with the Java 8 function environment. The use of this method discouraged, as 59 | * unexpected errors can occur. 60 | * 61 | * @throws IllegalArgumentException If the given operands are not an element of the key domain. 62 | * @since 8.5.0 63 | * @deprecated Please use {@link #apply}. 64 | */ 65 | #endif 66 | 67 | #if ! KEY_CLASS_Boolean 68 | @Deprecated 69 | @Override 70 | default KEY_TYPE_WIDENED JDK_PRIMITIVE_BINARY_OPERATOR_APPLY(final KEY_TYPE_WIDENED x, final KEY_TYPE_WIDENED y) { 71 | return apply(KEY_NARROWING(x), KEY_NARROWING(y)); 72 | } 73 | #endif 74 | 75 | /** {@inheritDoc} 76 | * @deprecated Please use the corresponding type-specific method instead. */ 77 | @Deprecated 78 | @Override 79 | @SuppressWarnings("boxing") 80 | default KEY_CLASS apply(final KEY_CLASS x, final KEY_CLASS y) { 81 | return apply(x.KEY_VALUE(), y.KEY_VALUE()); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /drv/Hash.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2002-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | import it.unimi.dsi.fastutil.Hash; 21 | 22 | /** A type-specific {@link Hash} interface. 23 | * 24 | * @see Hash 25 | */ 26 | 27 | public interface HASH { 28 | 29 | /** A type-specific hash strategy. 30 | * 31 | * @see it.unimi.dsi.fastutil.Hash.Strategy 32 | */ 33 | 34 | interface Strategy { 35 | 36 | /** Returns the hash code of the specified element with respect to this hash strategy. 37 | * 38 | * @param e an element. 39 | * @return the hash code of the given element with respect to this hash strategy. 40 | */ 41 | 42 | int hashCode(KEY_TYPE e); 43 | 44 | /** Returns true if the given elements are equal with respect to this hash strategy. 45 | * 46 | * @param a an element. 47 | * @param b another element. 48 | * @return true if the two specified elements are equal with respect to this hash strategy. 49 | */ 50 | 51 | boolean equals(KEY_TYPE a, KEY_TYPE b); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /drv/ImmutablePair.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2020-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | /** A type-specific immutable {@link it.unimi.dsi.fastutil.Pair Pair}; provides some additional methods that use polymorphism to avoid (un)boxing. */ 21 | 22 | public class IMMUTABLE_PAIR KEY_VALUE_GENERIC implements PAIR KEY_VALUE_GENERIC, java.io.Serializable { 23 | private static final long serialVersionUID = 0L; 24 | 25 | protected final KEY_GENERIC_TYPE left; 26 | protected final VALUE_GENERIC_TYPE right; 27 | 28 | /** Creates a new type-specific immutable {@link it.unimi.dsi.fastutil.Pair Pair} with given left and right value. 29 | * @param left the left value. 30 | * @param right the right value. 31 | */ 32 | public IMMUTABLE_PAIR(final KEY_GENERIC_TYPE left, final VALUE_GENERIC_TYPE right) { 33 | this.left = left; 34 | this.right = right; 35 | } 36 | 37 | /** Returns a new type-specific immutable {@link it.unimi.dsi.fastutil.Pair Pair} with given left and right value. 38 | * @param left the left value. 39 | * @param right the right value. 40 | * 41 | * @implSpec This factory method delegates to the constructor. 42 | */ 43 | public static KEY_VALUE_GENERIC IMMUTABLE_PAIR KEY_VALUE_GENERIC of(final KEY_GENERIC_TYPE left, final VALUE_GENERIC_TYPE right) { 44 | return new IMMUTABLE_PAIR KEY_VALUE_GENERIC(left, right); 45 | } 46 | 47 | @Override 48 | public KEY_GENERIC_TYPE PAIR_LEFT() { 49 | return left; 50 | } 51 | 52 | @Override 53 | public VALUE_GENERIC_TYPE PAIR_RIGHT() { 54 | return right; 55 | } 56 | 57 | @Override 58 | @SuppressWarnings("rawtypes") 59 | public boolean equals(Object other) { 60 | if (other == null) return false; 61 | 62 | #if KEYS_PRIMITIVE || VALUES_PRIMITIVE 63 | if (other instanceof PAIR) { 64 | return 65 | 66 | #if KEY_CLASS_Object 67 | java.util.Objects.equals(left, ((PAIR)other).PAIR_LEFT()) 68 | #else 69 | left == ((PAIR)other).PAIR_LEFT() 70 | #endif 71 | #if VALUE_CLASS_Object 72 | && java.util.Objects.equals(right, ((PAIR)other).PAIR_RIGHT()); 73 | #else 74 | && right == ((PAIR)other).PAIR_RIGHT(); 75 | #endif 76 | } 77 | #endif 78 | 79 | if (other instanceof it.unimi.dsi.fastutil.Pair) { 80 | return 81 | #if KEYS_USE_REFERENCE_EQUALITY 82 | left == ((it.unimi.dsi.fastutil.Pair)other).left() 83 | #else 84 | java.util.Objects.equals(KEY2OBJ(left), ((it.unimi.dsi.fastutil.Pair)other).left()) 85 | #endif 86 | #if VALUES_USE_REFERENCE_EQUALITY 87 | && right == ((it.unimi.dsi.fastutil.Pair)other).right(); 88 | #else 89 | && java.util.Objects.equals(VALUE2OBJ(right), ((it.unimi.dsi.fastutil.Pair)other).right()); 90 | #endif 91 | } 92 | 93 | return false; 94 | } 95 | 96 | @Override 97 | public int hashCode() { 98 | return KEY2JAVAHASH(left) * 19 + VALUE2JAVAHASH(right); 99 | } 100 | 101 | /** Returns a string representation of this pair in the form <l,r>. 102 | * 103 | * @return a string representation of this pair in the form <l,r>. 104 | */ 105 | @Override 106 | public String toString() { 107 | return "<" + PAIR_LEFT() + "," + PAIR_RIGHT() + ">"; 108 | } 109 | } -------------------------------------------------------------------------------- /drv/ImmutableSortedPair.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2020-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | /** A type-specific immutable {@link it.unimi.dsi.fastutil.SortedPair SortedPair}; provides some additional methods that use polymorphism to avoid (un)boxing. */ 21 | 22 | #if KEYS_PRIMITIVE 23 | public class IMMUTABLE_SORTED_PAIR extends IMMUTABLE_PAIR implements SORTED_PAIR, java.io.Serializable { 24 | #else 25 | public class IMMUTABLE_SORTED_PAIR > extends IMMUTABLE_PAIR implements it.unimi.dsi.fastutil.SortedPair, java.io.Serializable { 26 | #endif 27 | private static final long serialVersionUID = 0L; 28 | 29 | private IMMUTABLE_SORTED_PAIR(final KEY_GENERIC_TYPE left, final KEY_GENERIC_TYPE right) { 30 | super(left, right); 31 | } 32 | 33 | /** Returns a new type-specific immutable {@link it.unimi.dsi.fastutil.SortedPair SortedPair} with given left and right value. 34 | * 35 | *

Note that if {@code left} and {@code right} are in the wrong order, they will be exchanged. 36 | * 37 | * @param left the left value. 38 | * @param right the right value. 39 | * 40 | * @implSpec This factory method delegates to the constructor. 41 | */ 42 | #if KEYS_PRIMITIVE 43 | public static IMMUTABLE_SORTED_PAIR of(final KEY_GENERIC_TYPE left, final KEY_GENERIC_TYPE right) { 44 | if (left <= right) return new IMMUTABLE_SORTED_PAIR(left, right); 45 | else return new IMMUTABLE_SORTED_PAIR(right, left); 46 | #else 47 | public static > IMMUTABLE_SORTED_PAIR of(final KEY_GENERIC_TYPE left, final KEY_GENERIC_TYPE right) { 48 | if (left.compareTo(right) <= 0) return new IMMUTABLE_SORTED_PAIR (left, right); 49 | else return new IMMUTABLE_SORTED_PAIR (right, left); 50 | #endif 51 | } 52 | 53 | @Override 54 | @SuppressWarnings("rawtypes") 55 | public boolean equals(Object other) { 56 | if (other == null) return false; 57 | 58 | #if KEYS_PRIMITIVE || VALUES_PRIMITIVE 59 | if (other instanceof SORTED_PAIR) { 60 | return 61 | 62 | #if KEY_CLASS_Object 63 | java.util.Objects.equals(left, ((SORTED_PAIR)other).PAIR_LEFT()) 64 | #else 65 | left == ((SORTED_PAIR)other).PAIR_LEFT() 66 | #endif 67 | #if VALUE_CLASS_Object 68 | && java.util.Objects.equals(right, ((SORTED_PAIR)other).PAIR_RIGHT()); 69 | #else 70 | && right == ((SORTED_PAIR)other).PAIR_RIGHT(); 71 | #endif 72 | } 73 | #endif 74 | 75 | if (other instanceof it.unimi.dsi.fastutil.SortedPair) { 76 | return 77 | #if KEYS_USE_REFERENCE_EQUALITY 78 | left == ((it.unimi.dsi.fastutil.SortedPair)other).left() 79 | #else 80 | java.util.Objects.equals(KEY2OBJ(left), ((it.unimi.dsi.fastutil.SortedPair)other).left()) 81 | #endif 82 | #if VALUES_USE_REFERENCE_EQUALITY 83 | && right == ((it.unimi.dsi.fastutil.SortedPair)other).right(); 84 | #else 85 | && java.util.Objects.equals(VALUE2OBJ(right), ((it.unimi.dsi.fastutil.SortedPair)other).right()); 86 | #endif 87 | } 88 | 89 | return false; 90 | } 91 | 92 | /** Returns a string representation of this sorted pair in the form {l,r}. 93 | * 94 | * @return a string representation of this pair sorted in the form {l,r}. 95 | */ 96 | @Override 97 | public String toString() { 98 | return "{" + PAIR_LEFT() + "," + PAIR_RIGHT() + "}"; 99 | } 100 | } -------------------------------------------------------------------------------- /drv/IndirectPriorityQueue.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2003-2024 Paolo Boldi and Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | import it.unimi.dsi.fastutil.IndirectPriorityQueue; 21 | 22 | /** A type-specific {@link IndirectPriorityQueue}. 23 | * 24 | *

Additionally, this interface strengthens {@link #comparator()}. 25 | */ 26 | 27 | public interface INDIRECT_PRIORITY_QUEUE extends IndirectPriorityQueue { 28 | 29 | /** Returns the type-specific comparator associated with this queue. 30 | * 31 | * @apiNote Note that this specification strengthens the one given in {@link IndirectPriorityQueue}. 32 | * 33 | * @return the comparator associated with this queue. 34 | * @see IndirectPriorityQueue#comparator() 35 | */ 36 | @Override 37 | KEY_COMPARATOR comparator(); 38 | } 39 | -------------------------------------------------------------------------------- /drv/Iterables.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2020-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | /** A class providing static methods and objects that do useful things with type-specific iterables. 21 | * 22 | * @see Iterable 23 | */ 24 | 25 | public final class ITERABLES { 26 | 27 | private ITERABLES() {} 28 | 29 | /** Counts the number of elements returned by a type-specific iterable. 30 | * 31 | * @param iterable an iterable. 32 | * @return the number of elements returned by {@code iterable}. 33 | */ 34 | public static KEY_GENERIC long size(final STD_KEY_ITERABLE KEY_GENERIC iterable) { 35 | long c = 0; 36 | for (@SuppressWarnings("unused") final KEY_GENERIC_TYPE dummy : iterable) c++; 37 | return c; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /drv/LinkedOpenCustomHashMap.drv: -------------------------------------------------------------------------------- 1 | OpenHashMap.drv -------------------------------------------------------------------------------- /drv/LinkedOpenCustomHashSet.drv: -------------------------------------------------------------------------------- 1 | OpenHashSet.drv -------------------------------------------------------------------------------- /drv/LinkedOpenHashMap.drv: -------------------------------------------------------------------------------- 1 | OpenHashMap.drv -------------------------------------------------------------------------------- /drv/LinkedOpenHashSet.drv: -------------------------------------------------------------------------------- 1 | OpenHashSet.drv -------------------------------------------------------------------------------- /drv/ListIterator.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2002-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | import java.util.ListIterator; 21 | 22 | /** A type-specific bidirectional iterator that is also a {@link ListIterator}. 23 | * 24 | *

This interface merges the methods provided by a {@link ListIterator} and 25 | * a type-specific {@link it.unimi.dsi.fastutil.BidirectionalIterator}. Moreover, it provides 26 | * type-specific versions of {@link ListIterator#add(Object) add()} 27 | * and {@link ListIterator#set(Object) set()}. 28 | * 29 | * @see java.util.ListIterator 30 | * @see it.unimi.dsi.fastutil.BidirectionalIterator 31 | */ 32 | 33 | public interface KEY_LIST_ITERATOR KEY_GENERIC extends KEY_BIDI_ITERATOR KEY_GENERIC, ListIterator { 34 | 35 | /** 36 | * Replaces the last element returned by {@link #next} or 37 | * {@link #previous} with the specified element (optional operation). 38 | * @param k the element used to replace the last element returned. 39 | * 40 | *

This default implementation just throws an {@link UnsupportedOperationException}. 41 | * @see ListIterator#set(Object) 42 | */ 43 | 44 | #if KEYS_REFERENCE 45 | @Override 46 | #endif 47 | default void set(final KEY_GENERIC_TYPE k) { throw new UnsupportedOperationException(); } 48 | 49 | /** 50 | * Inserts the specified element into the list (optional operation). 51 | * 52 | *

This default implementation just throws an {@link UnsupportedOperationException}. 53 | * @param k the element to insert. 54 | * @see ListIterator#add(Object) 55 | */ 56 | 57 | #if KEYS_REFERENCE 58 | @Override 59 | #endif 60 | default void add(final KEY_GENERIC_TYPE k) { throw new UnsupportedOperationException(); } 61 | 62 | /** 63 | * Removes from the underlying collection the last element returned 64 | * by this iterator (optional operation). 65 | * 66 | *

This default implementation just throws an {@link UnsupportedOperationException}. 67 | * @see ListIterator#remove() 68 | */ 69 | 70 | @Override 71 | default void remove() { throw new UnsupportedOperationException(); } 72 | 73 | 74 | 75 | #if KEYS_PRIMITIVE 76 | /** {@inheritDoc} 77 | * @deprecated Please use the corresponding type-specific method instead. */ 78 | @Deprecated 79 | @Override 80 | default void set(final KEY_CLASS k) { set(k.KEY_VALUE()); } 81 | 82 | /** {@inheritDoc} 83 | * @deprecated Please use the corresponding type-specific method instead. */ 84 | @Deprecated 85 | @Override 86 | default void add(final KEY_CLASS k) { add(k.KEY_VALUE()); } 87 | 88 | /** {@inheritDoc} 89 | * @deprecated Please use the corresponding type-specific method instead. */ 90 | @Deprecated 91 | @Override 92 | default KEY_GENERIC_CLASS next() { return KEY_BIDI_ITERATOR.super.next(); } 93 | 94 | /** {@inheritDoc} 95 | * @deprecated Please use the corresponding type-specific method instead. */ 96 | @Deprecated 97 | @Override 98 | default KEY_GENERIC_CLASS previous() { return KEY_BIDI_ITERATOR.super.previous(); } 99 | #endif 100 | 101 | } 102 | -------------------------------------------------------------------------------- /drv/OpenCustomHashMap.drv: -------------------------------------------------------------------------------- 1 | OpenHashMap.drv -------------------------------------------------------------------------------- /drv/OpenCustomHashSet.drv: -------------------------------------------------------------------------------- 1 | OpenHashSet.drv -------------------------------------------------------------------------------- /drv/PriorityQueue.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2003-2024 Paolo Boldi and Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | import java.util.NoSuchElementException; 21 | 22 | import it.unimi.dsi.fastutil.PriorityQueue; 23 | 24 | /** A type-specific {@link PriorityQueue}; provides some additional methods that use polymorphism to avoid (un)boxing. 25 | * 26 | *

Additionally, this interface strengthens {@link #comparator()}. 27 | */ 28 | 29 | public interface PRIORITY_QUEUE extends PriorityQueue { 30 | 31 | /** Enqueues a new element. 32 | * @see PriorityQueue#enqueue(Object) 33 | * @param x the element to enqueue. 34 | */ 35 | 36 | void enqueue(KEY_GENERIC_TYPE x); 37 | 38 | /** Dequeues the {@linkplain #first() first} element from the queue. 39 | * @see #dequeue() 40 | * @return the dequeued element. 41 | * @throws NoSuchElementException if the queue is empty. 42 | */ 43 | 44 | KEY_GENERIC_TYPE DEQUEUE(); 45 | 46 | /** Returns the first element of the queue. 47 | * @see #first() 48 | * @return the first element. 49 | * @throws NoSuchElementException if the queue is empty. 50 | */ 51 | 52 | KEY_GENERIC_TYPE FIRST(); 53 | 54 | /** Returns the last element of the queue, that is, the element the would be dequeued last (optional operation). 55 | *

This default implementation just throws an {@link UnsupportedOperationException}. 56 | * @see #last() 57 | * @return the last element. 58 | * @throws NoSuchElementException if the queue is empty. 59 | */ 60 | 61 | default KEY_GENERIC_TYPE LAST() { throw new UnsupportedOperationException(); } 62 | 63 | /** Returns the comparator associated with this priority queue, or null if it uses its elements' natural ordering. 64 | * 65 | * @apiNote Note that this specification strengthens the one given in {@link PriorityQueue#comparator()}. 66 | * @see PriorityQueue#comparator() 67 | * @return the comparator associated with this priority queue. 68 | */ 69 | @Override 70 | KEY_COMPARATOR comparator(); 71 | 72 | /** {@inheritDoc} 73 | *

This default implementation delegates to the corresponding type-specific method. 74 | * @deprecated Please use the corresponding type-specific method instead. */ 75 | @Deprecated 76 | @Override 77 | default void enqueue(final KEY_GENERIC_CLASS x) { enqueue(x.KEY_VALUE()); } 78 | 79 | /** {@inheritDoc} 80 | *

This default implementation delegates to the corresponding type-specific method. 81 | * @deprecated Please use the corresponding type-specific method instead. */ 82 | @Deprecated 83 | @Override 84 | default KEY_GENERIC_CLASS dequeue() { return KEY2OBJ(DEQUEUE()); } 85 | 86 | /** {@inheritDoc} 87 | *

This default implementation delegates to the corresponding type-specific method. 88 | * @deprecated Please use the corresponding type-specific method instead. */ 89 | @Deprecated 90 | @Override 91 | default KEY_GENERIC_CLASS first() { return KEY2OBJ(FIRST()); } 92 | 93 | /** {@inheritDoc} 94 | *

This default implementation delegates to the corresponding type-specific method. 95 | * @deprecated Please use the corresponding type-specific method instead. */ 96 | @Deprecated 97 | @Override 98 | default KEY_GENERIC_CLASS last() { return KEY2OBJ(LAST()); } 99 | } 100 | -------------------------------------------------------------------------------- /drv/SortedPair.drv: -------------------------------------------------------------------------------- 1 | 2 | 3 | /* 4 | * Copyright (C) 2020-2024 Sebastiano Vigna 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | 20 | package PACKAGE; 21 | 22 | /** A type-specific immutable {@link it.unimi.dsi.fastutil.SortedPair SortedPair}. */ 23 | 24 | #if KEYS_PRIMITIVE 25 | public interface SORTED_PAIR extends PAIR, it.unimi.dsi.fastutil.SortedPair, java.io.Serializable { 26 | #else 27 | public interface SORTED_PAIR > extends PAIR , it.unimi.dsi.fastutil.SortedPair, java.io.Serializable { 28 | #endif 29 | 30 | /** Returns a new type-specific immutable {@link it.unimi.dsi.fastutil.SortedPair SortedPair} with given left and right value. 31 | * 32 | *

Note that if {@code left} and {@code right} are in the wrong order, they will be exchanged. 33 | * 34 | * @param left the left value. 35 | * @param right the right value. 36 | * 37 | * @implSpec This factory method delegates to the factory method of the corresponding immutable implementation. 38 | */ 39 | #if KEYS_PRIMITIVE 40 | public static SORTED_PAIR of(final KEY_GENERIC_TYPE left, final KEY_GENERIC_TYPE right) { 41 | return IMMUTABLE_SORTED_PAIR.of(left, right); 42 | #else 43 | public static > SORTED_PAIR of(final KEY_GENERIC_TYPE left, final KEY_GENERIC_TYPE right) { 44 | return IMMUTABLE_SORTED_PAIR.of(left, right); 45 | #endif 46 | } 47 | 48 | #if KEYS_PRIMITIVE 49 | /** 50 | * Returns true if one of the two elements of this sorted pair is equal to a given element. 51 | * 52 | * @param e an element. 53 | * @return true if one of the two elements of this sorted pair is equal to {@code e}. 54 | * @see it.unimi.dsi.fastutil.SortedPair#contains(Object) 55 | */ 56 | default boolean contains(final KEY_TYPE e) { 57 | return e == PAIR_LEFT() || e == PAIR_RIGHT(); 58 | } 59 | 60 | /** {@inheritDoc} 61 | * @deprecated Please use the corresponding type-specific method instead. 62 | */ 63 | @Deprecated 64 | @Override 65 | default boolean contains(final Object o) { 66 | if (o == null) return false; 67 | return contains(KEY_OBJ2TYPE(o)); 68 | } 69 | 70 | #endif 71 | 72 | 73 | } -------------------------------------------------------------------------------- /drv/Stack.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2002-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package PACKAGE; 19 | 20 | import it.unimi.dsi.fastutil.Stack; 21 | 22 | /** A type-specific {@link Stack}; provides some additional methods that use polymorphism to avoid (un)boxing. */ 23 | 24 | public interface STACK KEY_GENERIC extends Stack { 25 | 26 | /** Pushes the given object on the stack. 27 | * @param k the object to push on the stack. 28 | * @see Stack#push(Object) 29 | */ 30 | void push(KEY_TYPE k); 31 | 32 | /** Pops the top off the stack. 33 | * 34 | * @return the top of the stack. 35 | * @see Stack#pop() 36 | */ 37 | KEY_TYPE POP(); 38 | 39 | /** Peeks at the top of the stack (optional operation). 40 | * @return the top of the stack. 41 | * @see Stack#top() 42 | */ 43 | KEY_TYPE TOP(); 44 | 45 | /** Peeks at an element on the stack (optional operation). 46 | * @param i an index from the stop of the stack (0 represents the top). 47 | * @return the {@code i}-th element on the stack. 48 | * @see Stack#peek(int) 49 | */ 50 | KEY_TYPE PEEK(int i); 51 | 52 | /** {@inheritDoc} 53 | *

This default implementation delegates to the corresponding type-specific method. 54 | * @deprecated Please use the corresponding type-specific method instead. */ 55 | @Deprecated 56 | @Override 57 | default void push(KEY_GENERIC_CLASS o) { 58 | push(o.KEY_VALUE()); 59 | } 60 | 61 | /** {@inheritDoc} 62 | *

This default implementation delegates to the corresponding type-specific method. 63 | * @deprecated Please use the corresponding type-specific method instead. */ 64 | @Deprecated 65 | @Override 66 | default KEY_GENERIC_CLASS pop() { 67 | return KEY_CLASS.valueOf(POP()); 68 | } 69 | 70 | /** {@inheritDoc} 71 | *

This default implementation delegates to the corresponding type-specific method. 72 | * @deprecated Please use the corresponding type-specific method instead. */ 73 | @Deprecated 74 | @Override 75 | default KEY_GENERIC_CLASS top() { 76 | return KEY_CLASS.valueOf(TOP()); 77 | } 78 | 79 | /** {@inheritDoc} 80 | *

This default implementation delegates to the corresponding type-specific method. 81 | * @deprecated Please use the corresponding type-specific method instead. */ 82 | @Deprecated 83 | @Override 84 | default KEY_GENERIC_CLASS peek(final int i) { 85 | return KEY_CLASS.valueOf(PEEK(i)); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /drv/TextIO.drv: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | package it.unimi.dsi.fastutil.io; 19 | 20 | import static it.unimi.dsi.fastutil.BigArrays.SEGMENT_MASK; 21 | import static it.unimi.dsi.fastutil.BigArrays.length; 22 | import static it.unimi.dsi.fastutil.BigArrays.start; 23 | import static it.unimi.dsi.fastutil.BigArrays.segment; 24 | import static it.unimi.dsi.fastutil.BigArrays.ensureOffsetLength; 25 | 26 | import java.io.*; 27 | import java.util.*; 28 | import it.unimi.dsi.fastutil.ints.*; 29 | import it.unimi.dsi.fastutil.longs.*; 30 | import it.unimi.dsi.fastutil.doubles.*; 31 | import it.unimi.dsi.fastutil.booleans.*; 32 | import it.unimi.dsi.fastutil.bytes.*; 33 | import it.unimi.dsi.fastutil.shorts.*; 34 | import it.unimi.dsi.fastutil.floats.*; 35 | 36 | /** Provides static methods to perform easily textual I/O. 37 | * 38 | *

This class fills a gap in the Java API: a natural operation on sequences 39 | * of primitive elements is to load or store them in textual form. This format 40 | * makes files humanly readable. 41 | * 42 | *

For each primitive type, this class provides methods that read elements 43 | * from a {@link BufferedReader} or from a filename (which will be opened 44 | * using a buffer of {@link #BUFFER_SIZE} bytes) into an array. Analogously, 45 | * there are methods that store the content of an array (fragment) or the 46 | * elements returned by an iterator to a {@link PrintStream} or to a given 47 | * filename. 48 | * 49 | *

Finally, there are useful wrapper methods that {@linkplain #asIntIterator(CharSequence) 50 | * exhibit a file as a type-specific iterator}. 51 | * 52 | *

Note that, contrarily to the binary case, there is no way to 53 | * {@linkplain BinIO#loadInts(CharSequence) load from a file without providing an array}. You can 54 | * easily work around the problem as follows: 55 | *

56 |  * array = IntIterators.unwrap(TextIO.asIntIterator("foo"));
57 |  * 
58 | * 59 | * @since 4.4 60 | */ 61 | 62 | public class TextIO { 63 | 64 | private TextIO() {} 65 | 66 | /** The size of the buffer used for all I/O on files. */ 67 | public static final int BUFFER_SIZE = 8 * 1024; 68 | 69 | #include "src/it/unimi/dsi/fastutil/io/IntTextIOFragment.h" 70 | #include "src/it/unimi/dsi/fastutil/io/LongTextIOFragment.h" 71 | #include "src/it/unimi/dsi/fastutil/io/DoubleTextIOFragment.h" 72 | #include "src/it/unimi/dsi/fastutil/io/BooleanTextIOFragment.h" 73 | #include "src/it/unimi/dsi/fastutil/io/ByteTextIOFragment.h" 74 | #include "src/it/unimi/dsi/fastutil/io/ShortTextIOFragment.h" 75 | #include "src/it/unimi/dsi/fastutil/io/FloatTextIOFragment.h" 76 | 77 | } 78 | -------------------------------------------------------------------------------- /fastutil-core.bnd: -------------------------------------------------------------------------------- 1 | Automatic-Module-Name: it.unimi.dsi.fastutil.core 2 | Bundle-Name: it.unimi.dsi.fastutil.core 3 | Bundle-SymbolicName: it.unimi.dsi.fastutil.core 4 | Export-Package: it.unimi.dsi.fastutil.* 5 | Bundle-Version: ${version} 6 | -------------------------------------------------------------------------------- /fastutil.bnd: -------------------------------------------------------------------------------- 1 | Automatic-Module-Name: it.unimi.dsi.fastutil 2 | Bundle-Name: it.unimi.dsi.fastutil 3 | Bundle-SymbolicName: it.unimi.dsi.fastutil 4 | Export-Package: it.unimi.dsi.fastutil.* 5 | Bundle-Version: ${version} 6 | -------------------------------------------------------------------------------- /guava/it/unimi/dsi/fastutil/ints/IntSetGuavaTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import java.lang.reflect.Method; 20 | import java.util.Arrays; 21 | import java.util.HashSet; 22 | import java.util.List; 23 | import java.util.Set; 24 | import java.util.function.Supplier; 25 | 26 | import com.google.common.collect.testing.SampleElements; 27 | import com.google.common.collect.testing.SetTestSuiteBuilder; 28 | import com.google.common.collect.testing.TestSetGenerator; 29 | import com.google.common.collect.testing.features.CollectionFeature; 30 | import com.google.common.collect.testing.features.CollectionSize; 31 | import com.google.common.collect.testing.features.SetFeature; 32 | import com.google.common.collect.testing.testers.CollectionToStringTester; 33 | import com.google.common.collect.testing.testers.MapToStringTester; 34 | 35 | import junit.framework.TestCase; 36 | import junit.framework.TestSuite; 37 | 38 | public class IntSetGuavaTest extends TestCase { 39 | 40 | public static TestSuite suite() { 41 | return suite("IntOpenHashSet", IntOpenHashSet::new); 42 | } 43 | 44 | public static TestSuite suite(final String name, final Supplier> factory) { 45 | final HashSet toStringTests = new HashSet<>(); 46 | toStringTests.addAll(Arrays.asList(MapToStringTester.class.getDeclaredMethods())); 47 | toStringTests.addAll(Arrays.asList(CollectionToStringTester.class.getDeclaredMethods())); 48 | 49 | return SetTestSuiteBuilder.using(new TestSetGenerator() { 50 | @Override 51 | public Integer[] createArray(final int n) { 52 | return new Integer[n]; 53 | } 54 | 55 | @Override 56 | public Iterable order(final List insertionOrder) { 57 | return insertionOrder; 58 | } 59 | 60 | @Override 61 | public Set create(final Object... elements) { 62 | final Set set = factory.get(); 63 | for (final Object e : elements) { 64 | set.add((Integer)e); 65 | } 66 | return set; 67 | } 68 | 69 | @Override 70 | public SampleElements samples() { 71 | return new SampleElements<>(Integer.valueOf(0), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3), Integer.valueOf(4)); 72 | } 73 | }).named(name).withFeatures(CollectionSize.ANY, SetFeature.GENERAL_PURPOSE, CollectionFeature.ALLOWS_NULL_QUERIES, CollectionFeature.RESTRICTS_ELEMENTS, CollectionFeature.SUPPORTS_ADD, CollectionFeature.SUPPORTS_REMOVE, CollectionFeature.SERIALIZABLE, CollectionFeature.REMOVE_OPERATIONS, CollectionFeature.SUPPORTS_ITERATOR_REMOVE).suppressing(toStringTests).createTestSuite(); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /guava/it/unimi/dsi/fastutil/objects/Object2ObjectOpenHashMapGuavaTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.objects; 18 | 19 | import java.lang.reflect.Method; 20 | import java.util.Arrays; 21 | import java.util.HashSet; 22 | import java.util.Map; 23 | import java.util.function.Supplier; 24 | 25 | import com.google.common.collect.testing.MapTestSuiteBuilder; 26 | import com.google.common.collect.testing.TestStringMapGenerator; 27 | import com.google.common.collect.testing.features.CollectionFeature; 28 | import com.google.common.collect.testing.features.CollectionSize; 29 | import com.google.common.collect.testing.features.MapFeature; 30 | import com.google.common.collect.testing.testers.CollectionToStringTester; 31 | import com.google.common.collect.testing.testers.MapComputeIfPresentTester; 32 | import com.google.common.collect.testing.testers.MapMergeTester; 33 | import com.google.common.collect.testing.testers.MapToStringTester; 34 | 35 | import junit.framework.TestCase; 36 | import junit.framework.TestSuite; 37 | 38 | public class Object2ObjectOpenHashMapGuavaTest extends TestCase { 39 | 40 | public static TestSuite suite() throws NoSuchMethodException, SecurityException { 41 | return suite("Object2ObjectOpenHashMap", Object2ObjectOpenHashMap::new); 42 | } 43 | 44 | public static TestSuite suite(final String name, final Supplier> factory) throws NoSuchMethodException, SecurityException { 45 | final HashSet suppressed = new HashSet<>(); 46 | suppressed.addAll(Arrays.asList(MapToStringTester.class.getDeclaredMethods())); 47 | suppressed.addAll(Arrays.asList(CollectionToStringTester.class.getDeclaredMethods())); 48 | // An @apiNote explains that our semantics is slightly different 49 | suppressed.add(MapMergeTester.class.getDeclaredMethod("testMappedToNull")); 50 | suppressed.add(MapComputeIfPresentTester.class.getDeclaredMethod("testComputeIfPresent_nullTreatedAsAbsent")); 51 | 52 | return MapTestSuiteBuilder.using(new TestStringMapGenerator() { 53 | 54 | @Override 55 | protected Map create(final Map.Entry[] entries) { 56 | final Map map = factory.get(); 57 | for (final Map.Entry entry : entries) { 58 | map.put(entry.getKey(), entry.getValue()); 59 | } 60 | return map; 61 | } 62 | }).named(name).withFeatures(CollectionSize.ANY, MapFeature.GENERAL_PURPOSE, MapFeature.SUPPORTS_PUT, MapFeature.SUPPORTS_REMOVE, MapFeature.ALLOWS_NULL_KEYS, MapFeature.ALLOWS_NULL_VALUES, MapFeature.ALLOWS_ANY_NULL_QUERIES, CollectionFeature.SUPPORTS_ITERATOR_REMOVE).suppressing(suppressed).createTestSuite(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /guava/it/unimi/dsi/fastutil/objects/Object2ObjectRBTreeMapGuavaTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.objects; 18 | 19 | import java.lang.reflect.Method; 20 | import java.util.Arrays; 21 | import java.util.HashSet; 22 | import java.util.Map; 23 | import java.util.SortedMap; 24 | import java.util.function.Supplier; 25 | 26 | import com.google.common.collect.testing.MapTestSuiteBuilder; 27 | import com.google.common.collect.testing.TestStringSortedMapGenerator; 28 | import com.google.common.collect.testing.features.CollectionFeature; 29 | import com.google.common.collect.testing.features.CollectionSize; 30 | import com.google.common.collect.testing.features.MapFeature; 31 | import com.google.common.collect.testing.testers.CollectionToStringTester; 32 | import com.google.common.collect.testing.testers.MapComputeIfPresentTester; 33 | import com.google.common.collect.testing.testers.MapMergeTester; 34 | import com.google.common.collect.testing.testers.MapToStringTester; 35 | 36 | import junit.framework.TestCase; 37 | import junit.framework.TestSuite; 38 | 39 | public class Object2ObjectRBTreeMapGuavaTest extends TestCase { 40 | 41 | public static TestSuite suite() throws NoSuchMethodException, SecurityException { 42 | return suite("Object2ObjectRBTreeMap", Object2ObjectRBTreeMap::new); 43 | } 44 | 45 | public static TestSuite suite(final String name, final Supplier> factory) throws NoSuchMethodException, SecurityException { 46 | final HashSet suppressed = new HashSet<>(); 47 | suppressed.addAll(Arrays.asList(MapToStringTester.class.getDeclaredMethods())); 48 | suppressed.addAll(Arrays.asList(CollectionToStringTester.class.getDeclaredMethods())); 49 | // An @apiNote explains that our semantics is slightly different 50 | suppressed.add(MapMergeTester.class.getDeclaredMethod("testMappedToNull")); 51 | suppressed.add(MapComputeIfPresentTester.class.getDeclaredMethod("testComputeIfPresent_nullTreatedAsAbsent")); 52 | 53 | return MapTestSuiteBuilder.using(new TestStringSortedMapGenerator() { 54 | 55 | @Override 56 | protected SortedMap create(final Map.Entry[] entries) { 57 | final Map map = factory.get(); 58 | for (final Map.Entry entry : entries) { 59 | map.put(entry.getKey(), entry.getValue()); 60 | } 61 | return (SortedMap)map; 62 | } 63 | }).named(name).withFeatures(CollectionSize.ANY, CollectionFeature.KNOWN_ORDER, MapFeature.GENERAL_PURPOSE, MapFeature.SUPPORTS_PUT, MapFeature.SUPPORTS_REMOVE, MapFeature.ALLOWS_NULL_VALUES, CollectionFeature.SUPPORTS_ITERATOR_REMOVE).suppressing(suppressed).createTestSuite(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /guava/it/unimi/dsi/fastutil/objects/ObjectOpenHashSetGuavaTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.objects; 18 | 19 | import java.lang.reflect.Method; 20 | import java.util.Arrays; 21 | import java.util.HashSet; 22 | import java.util.Set; 23 | import java.util.function.Supplier; 24 | 25 | import com.google.common.collect.testing.SetTestSuiteBuilder; 26 | import com.google.common.collect.testing.TestStringSetGenerator; 27 | import com.google.common.collect.testing.features.CollectionFeature; 28 | import com.google.common.collect.testing.features.CollectionSize; 29 | import com.google.common.collect.testing.features.SetFeature; 30 | import com.google.common.collect.testing.testers.CollectionToStringTester; 31 | import com.google.common.collect.testing.testers.MapToStringTester; 32 | 33 | import junit.framework.TestCase; 34 | import junit.framework.TestSuite; 35 | 36 | public class ObjectOpenHashSetGuavaTest extends TestCase { 37 | 38 | public static TestSuite suite() { 39 | return suite("ObjectOpenHashSet", ObjectOpenHashSet::new); 40 | } 41 | 42 | public static TestSuite suite(final String name, final Supplier> factory) { 43 | final HashSet toStringTests = new HashSet<>(); 44 | toStringTests.addAll(Arrays.asList(MapToStringTester.class.getDeclaredMethods())); 45 | toStringTests.addAll(Arrays.asList(CollectionToStringTester.class.getDeclaredMethods())); 46 | 47 | return SetTestSuiteBuilder.using(new TestStringSetGenerator() { 48 | 49 | @Override 50 | protected Set create(final String[] elements) { 51 | final Set set = factory.get(); 52 | for (final String e : elements) { 53 | set.add(e); 54 | } 55 | return set; 56 | } 57 | }).named(name).withFeatures(CollectionSize.ANY, SetFeature.GENERAL_PURPOSE, CollectionFeature.ALLOWS_NULL_VALUES, CollectionFeature.ALLOWS_NULL_QUERIES, CollectionFeature.RESTRICTS_ELEMENTS, CollectionFeature.SUPPORTS_ADD, CollectionFeature.SUPPORTS_REMOVE, CollectionFeature.SERIALIZABLE, CollectionFeature.REMOVE_OPERATIONS, CollectionFeature.SUPPORTS_ITERATOR_REMOVE).suppressing(toStringTests).createTestSuite(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /ivy.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /lib/hamcrest-all-1.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vigna/fastutil/eeca53120ef1e35172c14aed53a643516eeaf3f5/lib/hamcrest-all-1.3.jar -------------------------------------------------------------------------------- /lib/junit-4.13.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vigna/fastutil/eeca53120ef1e35172c14aed53a643516eeaf3f5/lib/junit-4.13.jar -------------------------------------------------------------------------------- /pom-core-model.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | it.unimi.dsi 4 | fastutil-core 5 | jar 6 | fastutil 7 | @VERSION@ 8 | fastutil extends the Java Collections Framework by providing type-specific maps, sets, lists, and queues with a small memory footprint and fast operations; it provides also big (64-bit) arrays, sets, and lists, sorting algorithms, fast, practical I/O classes for binary and text files, and facilities for memory mapping large files. This jar (fastutil-core.jar) contains data structures based on integers, longs, doubles, and objects, only; fastutil.jar contains all classes. If you have both jars in your dependencies, this jar should be excluded. 9 | http://fastutil.di.unimi.it/ 10 | 11 | 12 | Apache License, Version 2.0 13 | http://www.apache.org/licenses/LICENSE-2.0.html 14 | repo 15 | 16 | 17 | 18 | scm:git://github.com/vigna/fastutil.git 19 | https://github.com/vigna/fastutil 20 | 21 | 22 | 23 | vigna 24 | Sebastiano Vigna 25 | sebastiano.vigna@unimi.it 26 | 27 | 28 | 29 | 1.8 30 | 1.8 31 | 32 | 33 | 34 | junit 35 | junit 36 | 4.12 37 | test 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /pom-model.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | it.unimi.dsi 4 | fastutil 5 | jar 6 | fastutil 7 | @VERSION@ 8 | fastutil extends the Java Collections Framework by providing type-specific maps, sets, lists, and queues with a small memory footprint and fast access and insertion; it provides also big (64-bit) arrays, sets and lists, sorting algorithms, fast, practical I/O classes for binary and text files, and facilities for memory mapping large files. Note that if you have both this jar and fastutil-core.jar in your dependencies, fastutil-core.jar should be excluded. 9 | http://fastutil.di.unimi.it/ 10 | 11 | 12 | Apache License, Version 2.0 13 | http://www.apache.org/licenses/LICENSE-2.0.html 14 | repo 15 | 16 | 17 | 18 | scm:git://github.com/vigna/fastutil.git 19 | https://github.com/vigna/fastutil 20 | 21 | 22 | 23 | vigna 24 | Sebastiano Vigna 25 | sebastiano.vigna@unimi.it 26 | 27 | 28 | 29 | 1.8 30 | 1.8 31 | 32 | 33 | 34 | junit 35 | junit 36 | 4.12 37 | test 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /split.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | TMPFILE=$(mktemp) 4 | 5 | \ls -1 build/it/unimi/dsi/fastutil/*.class build/it/unimi/dsi/fastutil/io/*.class | cut -d/ -f2- >$TMPFILE 6 | find build/it/unimi/dsi/fastutil/* -type f -iname \*.class -not -iname \*Short\* -not -iname \*Char\* -not -iname \*Boolean\* -not -iname \*Byte\* -not -iname \*Float\* -not -iname \*Reference\* | cut -d/ -f2- >>$TMPFILE 7 | 8 | # Expand direct dependencies until stability 9 | 10 | export N=0 11 | while [[ $N != $(sort -u $TMPFILE | wc -l) ]]; do 12 | N=$(sort -u $TMPFILE | wc -l) 13 | jdeps -v -R -e 'it\.unimi\.dsi\.fastutil($|.*)' -classpath build $(sort -u $TMPFILE | while read F; do echo "build/$F"; done) | awk '/build -> build/ {exit} { if (NF == 4) print gensub(/\./, "/", "g", $3) ".class" }' >>$TMPFILE 14 | done 15 | 16 | sort -u $TMPFILE >fastutil-core.txt 17 | 18 | echo "Classes in build:" $(find build -iname \*.class | wc -l) 19 | echo "Classes in core:" $(wc -l fastutil-core.txt) 20 | 21 | while read CLASS; do SOURCE=${CLASS%.class}.java; if [ -f src/$SOURCE ]; then echo $SOURCE; fi; done < fastutil-core.txt > fastutil-src-core.txt 22 | 23 | echo "Source files in src:" $(find src -iname \*.java | wc -l) 24 | echo "Source files in core:" $(wc -l fastutil-src-core.txt) 25 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/AbstractIndirectPriorityQueue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2003-2024 Paolo Boldi and Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil; 18 | 19 | /** An abstract class providing basic methods for implementing the {@link IndirectPriorityQueue} interface. 20 | * 21 | * @deprecated As of {@code fastutil} 8 this class is no longer necessary, as its previous abstract 22 | * methods are now default methods of {@link IndirectPriorityQueue}. 23 | */ 24 | @Deprecated 25 | public abstract class AbstractIndirectPriorityQueue implements IndirectPriorityQueue {} 26 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/AbstractPriorityQueue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2003-2024 Paolo Boldi and Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil; 18 | 19 | /** An abstract class providing basic methods for implementing the {@link PriorityQueue} interface. 20 | * 21 | * @deprecated As of {@code fastutil} 8 this class is no longer necessary, as its previous abstract 22 | * methods are now default methods of {@link PriorityQueue}. 23 | */ 24 | @Deprecated 25 | public abstract class AbstractPriorityQueue implements PriorityQueue {} 26 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/AbstractStack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2002-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil; 18 | 19 | /** An abstract class providing basic methods for implementing the {@link Stack} interface. 20 | * 21 | * @deprecated As of {@code fastutil} 8 this class is no longer necessary, as its previous abstract 22 | * methods are now default methods of {@link Stack}. 23 | */ 24 | @Deprecated 25 | public abstract class AbstractStack implements Stack {} 26 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/BidirectionalIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2002-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil; 18 | 19 | import java.util.Iterator; 20 | import java.util.ListIterator; 21 | 22 | /** A bidirectional {@link Iterator}. 23 | * 24 | *

This kind of iterator is essentially a {@link ListIterator} that 25 | * does not support {@link ListIterator#previousIndex()} and {@link 26 | * ListIterator#nextIndex()}. It is useful for those maps that can easily 27 | * provide bidirectional iteration, but provide no index. 28 | * 29 | *

Note that iterators returned by {@code fastutil} classes are more 30 | * specific, and support skipping. This class serves the purpose of organising 31 | * in a cleaner way the relationships between various iterators. 32 | * 33 | * @see Iterator 34 | * @see ListIterator 35 | */ 36 | 37 | public interface BidirectionalIterator extends Iterator { 38 | 39 | /** Returns the previous element from the collection. 40 | * 41 | * @return the previous element from the collection. 42 | * @see java.util.ListIterator#previous() 43 | */ 44 | 45 | K previous(); 46 | 47 | /** Returns whether there is a previous element. 48 | * 49 | * @return whether there is a previous element. 50 | * @see java.util.ListIterator#hasPrevious() 51 | */ 52 | 53 | boolean hasPrevious(); 54 | } 55 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/BigListIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil; 18 | 19 | import java.util.Iterator; 20 | import java.util.ListIterator; 21 | 22 | /** A list iterator over a {@link BigList}. 23 | * 24 | *

This kind of iterator is essentially a {@link ListIterator} with long indices. 25 | * 26 | * @see Iterator 27 | * @see ListIterator 28 | */ 29 | 30 | public interface BigListIterator extends BidirectionalIterator { 31 | 32 | /** Returns the index of the element that would be returned by a subsequent call to next. 33 | * (Returns list size if the list iterator is at the end of the list.) 34 | * 35 | * @return the index of the element that would be returned by a subsequent call to next, or list 36 | * size if list iterator is at end of list. 37 | * @see ListIterator#nextIndex() 38 | */ 39 | 40 | long nextIndex(); 41 | 42 | /** Returns the index of the element that would be returned by a subsequent call to previous. 43 | * (Returns -1 if the list iterator is at the beginning of the list.) 44 | * 45 | * @return the index of the element that would be returned by a subsequent call to previous, or 46 | * -1 if list iterator is at beginning of list. 47 | * @see ListIterator#previousIndex() 48 | */ 49 | 50 | long previousIndex(); 51 | 52 | /** 53 | * Replaces the last element returned by {@link #next} or 54 | * {@link #previous} with the specified element (optional operation). 55 | * 56 | *

This default implemention just throws an {@link UnsupportedOperationException}. 57 | * 58 | * @param e an element. 59 | * @see ListIterator#set(Object) 60 | */ 61 | default void set(final K e) { 62 | throw new UnsupportedOperationException(); 63 | } 64 | 65 | /** 66 | * Inserts the specified element into the list (optional operation). 67 | * 68 | *

This default implemention just throws an {@link UnsupportedOperationException}. 69 | * 70 | * @param e an element. 71 | * @see ListIterator#add(Object) 72 | */ 73 | default void add(final K e) { 74 | throw new UnsupportedOperationException(); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/BigSwapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil; 18 | 19 | /** An object that can swap elements whose positions are specified by longs. 20 | * 21 | * @see BigArrays#quickSort(long, long, it.unimi.dsi.fastutil.longs.LongComparator, BigSwapper) 22 | */ 23 | @FunctionalInterface 24 | public interface BigSwapper { 25 | /** Swaps the data at the given positions. 26 | * 27 | * @param a the first position to swap. 28 | * @param b the second position to swap. 29 | */ 30 | void swap(long a, long b); 31 | } 32 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/PriorityQueue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2003-2024 Paolo Boldi and Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil; 18 | 19 | import java.util.Comparator; 20 | import java.util.NoSuchElementException; 21 | 22 | /** A priority queue. 23 | * 24 | *

A priority queue provides a way to {@linkplain #enqueue(Object) enqueue} 25 | * elements, and to {@linkplain #dequeue() dequeue} them in some specified 26 | * order. Elements that are smaller in the specified order are 27 | * dequeued first. It is also possible to get the {@linkplain #first() first 28 | * element}, that is, the element that would be dequeued next. 29 | * 30 | *

Additionally, the queue may provide a method to peek at 31 | * element that would be dequeued {@linkplain #last() last}. 32 | * 33 | *

The relative order of the elements enqueued should not change during 34 | * queue operations. Nonetheless, some implementations may give the caller a 35 | * way to notify the queue that the {@linkplain #changed() first element has 36 | * changed its relative position in the order}. 37 | */ 38 | 39 | public interface PriorityQueue { 40 | 41 | /** Enqueues a new element. 42 | * 43 | * @param x the element to enqueue. 44 | */ 45 | 46 | void enqueue(K x); 47 | 48 | /** Dequeues the {@linkplain #first() first} element from the queue. 49 | * 50 | * @return the dequeued element. 51 | * @throws NoSuchElementException if the queue is empty. 52 | */ 53 | 54 | K dequeue(); 55 | 56 | /** Checks whether this queue is empty. 57 | * 58 | *

This default implementation checks whether {@link #size()} is zero. 59 | * @return true if this queue is empty. 60 | */ 61 | 62 | default boolean isEmpty() { 63 | return size() == 0; 64 | } 65 | 66 | /** Returns the number of elements in this queue. 67 | * 68 | * @return the number of elements in this queue. 69 | */ 70 | 71 | int size(); 72 | 73 | /** Removes all elements from this queue. 74 | */ 75 | 76 | void clear(); 77 | 78 | /** Returns the first element of the queue. 79 | * 80 | * @return the first element. 81 | * @throws NoSuchElementException if the queue is empty. 82 | */ 83 | 84 | K first(); 85 | 86 | /** Returns the last element of the queue, that is, the element the would be dequeued last (optional operation). 87 | * 88 | *

This default implementation just throws an {@link UnsupportedOperationException}. 89 | * @return the last element. 90 | * @throws NoSuchElementException if the queue is empty. 91 | */ 92 | 93 | default K last() { throw new UnsupportedOperationException(); } 94 | 95 | /** Notifies the queue that the {@linkplain #first() first} element has changed (optional operation). 96 | *

This default implementation just throws an {@link UnsupportedOperationException}. 97 | */ 98 | 99 | default void changed() { throw new UnsupportedOperationException(); } 100 | 101 | 102 | /** Returns the comparator associated with this queue, or {@code null} if it uses its elements' natural ordering. 103 | * 104 | * @return the comparator associated with this sorted set, or {@code null} if it uses its elements' natural ordering. 105 | */ 106 | Comparator comparator(); 107 | } 108 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/SafeMath.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil; 18 | 19 | /** 20 | * Static utility method for safe conversions between primitive types. 21 | * 22 | *

23 | * Note that the methods of this class will throw an exception whenever a larger type cannot be 24 | * exactly represented by a smaller type. This includes doubles within the range a float, but with 25 | * too many significant bits. 26 | */ 27 | 28 | public final class SafeMath { 29 | private SafeMath() {} 30 | 31 | public static char safeIntToChar(final int value) { 32 | if (value < Character.MIN_VALUE || Character.MAX_VALUE < value) throw new IllegalArgumentException(value + " can't be represented as char"); 33 | return (char) value; 34 | } 35 | 36 | public static byte safeIntToByte(final int value) { 37 | if (value < Byte.MIN_VALUE || Byte.MAX_VALUE < value) throw new IllegalArgumentException(value + " can't be represented as byte (out of range)"); 38 | return (byte) value; 39 | } 40 | 41 | public static short safeIntToShort(final int value) { 42 | if (value < Short.MIN_VALUE || Short.MAX_VALUE < value) throw new IllegalArgumentException(value + " can't be represented as short (out of range)"); 43 | return (short) value; 44 | } 45 | 46 | public static char safeLongToChar(final long value) { 47 | if (value < Character.MIN_VALUE || Character.MAX_VALUE < value ) throw new IllegalArgumentException(value + " can't be represented as int (out of range)"); 48 | return (char) value; 49 | } 50 | 51 | public static byte safeLongToByte(final long value) { 52 | if (value < Byte.MIN_VALUE || Byte.MAX_VALUE < value ) throw new IllegalArgumentException(value + " can't be represented as int (out of range)"); 53 | return (byte) value; 54 | } 55 | 56 | public static short safeLongToShort(final long value) { 57 | if (value < Short.MIN_VALUE || Short.MAX_VALUE < value ) throw new IllegalArgumentException(value + " can't be represented as int (out of range)"); 58 | return (short) value; 59 | } 60 | 61 | public static int safeLongToInt(final long value) { 62 | if (value < Integer.MIN_VALUE || Integer.MAX_VALUE < value ) throw new IllegalArgumentException(value + " can't be represented as int (out of range)"); 63 | return (int) value; 64 | } 65 | 66 | public static float safeDoubleToFloat(final double value) { 67 | if (Double.isNaN(value)) return Float.NaN; 68 | if (Double.isInfinite(value)) return value < 0.0d ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY; 69 | if (value < -Float.MAX_VALUE || Float.MAX_VALUE < value) throw new IllegalArgumentException(value + " can't be represented as float (out of range)"); 70 | final float floatValue = (float) value; 71 | if (floatValue != value) throw new IllegalArgumentException(value + " can't be represented as float (imprecise)"); 72 | return floatValue; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/Size64.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil; 18 | 19 | import java.util.Collection; 20 | 21 | /** An interface for data structures whose size can exceed {@link Integer#MAX_VALUE}. 22 | * 23 | *

The only methods specified by this interfaces are {@link #size64()}, and 24 | * a deprecated {@link #size()} identical to {@link Collection#size()}, 25 | * but with a default implementation. Implementations 26 | * can work around the type problem of {@link java.util.Collection#size()} 27 | * (e.g., not being able to return more than {@link Integer#MAX_VALUE}) by implementing this 28 | * interface. Callers interested in large structures 29 | * can use a reflective call to {@code instanceof} to check for the presence of {@link #size64()}. 30 | */ 31 | 32 | public interface Size64 { 33 | /** Returns the size of this data structure as a long. 34 | * 35 | * @return the size of this data structure. 36 | */ 37 | long size64(); 38 | 39 | /** Returns the size of this data structure, minimized with {@link Integer#MAX_VALUE}. 40 | * 41 | *

This default implementation follows the definition above, which is compatible 42 | * with {@link Collection#size()}. 43 | * 44 | * @return the size of this data structure, minimized with {@link Integer#MAX_VALUE}. 45 | * @see java.util.Collection#size() 46 | * @deprecated Use {@link #size64()} instead. 47 | */ 48 | @Deprecated 49 | default int size() { 50 | return (int)Math.min(Integer.MAX_VALUE, size64()); 51 | } 52 | 53 | // This is here instead of in one of the Collection classes because this method does 54 | // not change per type-specific, so there is no need to generate type-specific implementations 55 | // of these methods that the JVM has to separately compile. 56 | /** Returns the size for a given {@link Collection} as a {@code long}, using {@link #size64()} 57 | * if applicable, else using {@link Collection#size()}. 58 | * 59 | * @param c the collection whose size to get 60 | * @return the size 61 | */ 62 | public static long sizeOf(final Collection c) { 63 | return c instanceof Size64 ? ((Size64)c).size64() : c.size(); 64 | } 65 | 66 | /** Returns the size for a given {@link java.util.Map} as a {@code long}, using {@link #size64()} 67 | * if applicable, else using {@link java.util.Map#size()}. 68 | * 69 | * @param m the map whose size to get 70 | * @return the size 71 | */ 72 | public static long sizeOf(final java.util.Map m) { 73 | return m instanceof Size64 ? ((Size64)m).size64() : m.size(); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/SortedPair.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2020-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil; 18 | 19 | import java.util.Objects; 20 | 21 | import it.unimi.dsi.fastutil.objects.ObjectObjectImmutablePair; 22 | import it.unimi.dsi.fastutil.objects.ObjectObjectImmutableSortedPair; 23 | 24 | /** 25 | * A pair of sorted elements. 26 | * 27 | *

28 | * This interface strengthen {@link Pair}, without adding methods. It assumes that both elements of 29 | * the pair are of the same type, and that they are primitive or comparable. It guarantees that the 30 | * {@linkplain #left() left element} is smaller than or equal to the {@linkplain #right() right 31 | * element}. 32 | * 33 | *

34 | * Implementations of this class can be used to represent unordered pairs by 35 | * canonicalization. Note that, in particular, if you build a sorted pair using a left and right 36 | * element in the wrong order they will be exchanged. To support this usage, this interface provides 37 | * a {@link #contains(Object)} method that can be used to check whether a given object is equal to 38 | * either element of the sorted pair. 39 | * 40 | * @param the type of the elements. 41 | */ 42 | 43 | public interface SortedPair> extends Pair { 44 | 45 | /** 46 | * Returns a new immutable {@link it.unimi.dsi.fastutil.SortedPair SortedPair} with given left 47 | * and right value. 48 | * 49 | *

50 | * Note that if {@code left} and {@code right} are in the wrong order, they will be exchanged. 51 | * 52 | * @param l the left value. 53 | * @param r the right value. 54 | * 55 | * @implNote This factory method delegates to 56 | * {@link ObjectObjectImmutablePair#of(Object, Object)}. 57 | */ 58 | public static > SortedPair of(final K l, final K r) { 59 | return ObjectObjectImmutableSortedPair.of(l, r); 60 | } 61 | 62 | /** 63 | * Returns true if one of the two elements of this sorted pair is equal to a given object. 64 | * 65 | * @param o an object, or {@code null}- 66 | * @return true if one of the two elements of this sorted pair is equal to {@code o}. 67 | */ 68 | default boolean contains(final Object o) { 69 | return Objects.equals(o, left()) || Objects.equals(o, right()); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/Stack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2002-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil; 18 | 19 | import java.util.NoSuchElementException; 20 | 21 | /** A stack. 22 | * 23 | *

A stack must provide the classical {@link #push(Object)} and 24 | * {@link #pop()} operations, but may be also peekable 25 | * to some extent: it may provide just the {@link #top()} function, 26 | * or even a more powerful {@link #peek(int)} method that provides 27 | * access to all elements on the stack (indexed from the top, which 28 | * has index 0). 29 | */ 30 | 31 | public interface Stack { 32 | 33 | /** Pushes the given object on the stack. 34 | * 35 | * @param o the object that will become the new top of the stack. 36 | */ 37 | 38 | void push(K o); 39 | 40 | /** Pops the top off the stack. 41 | * 42 | * @return the top of the stack. 43 | * @throws NoSuchElementException if the stack is empty. 44 | */ 45 | 46 | K pop(); 47 | 48 | /** Checks whether the stack is empty. 49 | * 50 | * @return true if the stack is empty. 51 | */ 52 | 53 | boolean isEmpty(); 54 | 55 | /** Peeks at the top of the stack (optional operation). 56 | * 57 | *

This default implementation returns {@link #peek(int) peek(0)}. 58 | * 59 | * @return the top of the stack. 60 | * @throws NoSuchElementException if the stack is empty. 61 | */ 62 | 63 | default K top() { 64 | return peek(0); 65 | } 66 | 67 | /** Peeks at an element on the stack (optional operation). 68 | * 69 | *

This default implementation just throws an {@link UnsupportedOperationException}. 70 | * 71 | * @param i an index from the stop of the stack (0 represents the top). 72 | * @return the {@code i}-th element on the stack. 73 | * @throws IndexOutOfBoundsException if the designated element does not exist.. 74 | */ 75 | 76 | default K peek(final int i) { 77 | throw new UnsupportedOperationException(); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/Swapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2010-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil; 18 | 19 | /** An object that can swap elements whose position is specified by integers. 20 | * 21 | * @see Arrays#quickSort(int, int, it.unimi.dsi.fastutil.ints.IntComparator, Swapper) 22 | */ 23 | @FunctionalInterface 24 | public interface Swapper { 25 | /** Swaps the data at the given positions. 26 | * 27 | * @param a the first position to swap. 28 | * @param b the second position to swap. 29 | */ 30 | void swap(int a, int b); 31 | } 32 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/booleans/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Type-specific classes for boolean elements or keys. 3 | * 4 | *

5 | * Not all classes are provided in a boolean-specific version: sorted sets and maps not generated, 6 | * as they are useless. Unsorted sets and maps are kept around for orthogonality, whereas 7 | * {@link it.unimi.dsi.fastutil.booleans.BooleanCollection} is used by maps with boolean values. 8 | */ 9 | package it.unimi.dsi.fastutil.booleans; -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/bytes/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Type-specific classes for byte elements or keys. 3 | */ 4 | package it.unimi.dsi.fastutil.bytes; 5 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/chars/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Type-specific classes for character elements or keys. 3 | */ 4 | package it.unimi.dsi.fastutil.chars; 5 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/doubles/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Type-specific classes for double elements or keys. 3 | */ 4 | package it.unimi.dsi.fastutil.doubles; 5 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/floats/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Type-specific classes for float elements or keys. 3 | */ 4 | package it.unimi.dsi.fastutil.floats; 5 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/ints/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Type-specific classes for integer elements or keys. 3 | */ 4 | package it.unimi.dsi.fastutil.ints; 5 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/io/FastByteArrayOutputStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.io; 18 | 19 | import java.io.IOException; 20 | 21 | import it.unimi.dsi.fastutil.bytes.ByteArrays; 22 | 23 | /** Simple, fast byte-array output stream that exposes the backing array. 24 | * 25 | *

{@link java.io.ByteArrayOutputStream} is nice, but to get its content you 26 | * must generate each time a new object. This doesn't happen here. 27 | * 28 | *

This class will automatically enlarge the backing array, doubling its 29 | * size whenever new space is needed. The {@link #reset()} method will 30 | * mark the content as empty, but will not decrease the capacity: use 31 | * {@link #trim()} for that purpose. 32 | * 33 | * @author Sebastiano Vigna 34 | */ 35 | 36 | public class FastByteArrayOutputStream extends MeasurableOutputStream implements RepositionableStream { 37 | 38 | /** The array backing the output stream. */ 39 | public static final int DEFAULT_INITIAL_CAPACITY = 16; 40 | 41 | /** The array backing the output stream. */ 42 | public byte[] array; 43 | 44 | /** The number of valid bytes in {@link #array}. */ 45 | public int length; 46 | 47 | /** The current writing position. */ 48 | private int position; 49 | 50 | /** Creates a new array output stream with an initial capacity of {@link #DEFAULT_INITIAL_CAPACITY} bytes. */ 51 | public FastByteArrayOutputStream() { 52 | this(DEFAULT_INITIAL_CAPACITY); 53 | } 54 | 55 | /** Creates a new array output stream with a given initial capacity. 56 | * 57 | * @param initialCapacity the initial length of the backing array. 58 | */ 59 | public FastByteArrayOutputStream(final int initialCapacity) { 60 | array = new byte[initialCapacity]; 61 | } 62 | 63 | /** Creates a new array output stream wrapping a given byte array. 64 | * 65 | * @param a the byte array to wrap. 66 | */ 67 | public FastByteArrayOutputStream(final byte[] a) { 68 | array = a; 69 | } 70 | 71 | /** Marks this array output stream as empty. */ 72 | public void reset() { 73 | length = 0; 74 | position = 0; 75 | } 76 | 77 | /** Ensures that the length of the backing array is equal to {@link #length}. */ 78 | public void trim() { 79 | array = ByteArrays.trim(array, length); 80 | } 81 | 82 | @Override 83 | public void write(final int b) { 84 | if (position >= array.length) array = ByteArrays.grow(array, position + 1, length); 85 | array[position++] = (byte)b; 86 | if (length < position) length = position; 87 | } 88 | 89 | @Override 90 | public void write(final byte[] b, final int off, final int len) throws IOException { 91 | ByteArrays.ensureOffsetLength(b, off, len); 92 | if (position + len > array.length) array = ByteArrays.grow(array, position + len, position); 93 | System.arraycopy(b, off, array, position, len); 94 | if (position + len > length) length = position += len; 95 | } 96 | 97 | @Override 98 | public void position(final long newPosition) { 99 | if (position > Integer.MAX_VALUE) throw new IllegalArgumentException("Position too large: " + newPosition); 100 | position = (int)newPosition; 101 | } 102 | 103 | @Override 104 | public long position() { 105 | return position; 106 | } 107 | 108 | @Override 109 | public long length() throws IOException { 110 | return length; 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/io/MeasurableInputStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.io; 18 | 19 | import java.io.InputStream; 20 | 21 | /** An {@link InputStream} that implements also the {@link MeasurableStream} interface. 22 | * 23 | * @since 5.0.4 24 | */ 25 | 26 | public abstract class MeasurableInputStream extends InputStream implements MeasurableStream { 27 | } 28 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/io/MeasurableOutputStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.io; 18 | 19 | import java.io.OutputStream; 20 | 21 | /** An {@link OutputStream} that implements also the {@link MeasurableStream} interface. 22 | * 23 | * @since 6.0.0 24 | */ 25 | 26 | public abstract class MeasurableOutputStream extends OutputStream implements MeasurableStream { 27 | } 28 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/io/MeasurableStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.io; 18 | 19 | import java.io.IOException; 20 | 21 | /** An stream that provides eager access to its length, 22 | * and keeps track of the current position (e.g., the number of bytes read so far, or the current 23 | * position of the file pointer). 24 | * 25 | *

This class has two methods, both specified as optional. This apparently bizarre 26 | * behaviour is necessary because of wrapper classes which use reflection 27 | * to support those methods (see, e.g., {@link MeasurableInputStream}, {@link FastBufferedInputStream} and {@link FastBufferedOutputStream}). 28 | * 29 | * @since 6.0.0 30 | */ 31 | 32 | public interface MeasurableStream { 33 | 34 | /** Returns the overall length of this stream (optional operation). In most cases, this will require the 35 | * stream to perform some extra action, possibly changing the state of the input stream itself (typically, reading 36 | * all the bytes up to the end, or flushing on output stream). 37 | * Implementing classes should always document what state will the input stream be in 38 | * after calling this method, and which kind of exception could be thrown. 39 | */ 40 | long length() throws IOException; 41 | 42 | /** Returns the current position in this stream (optional operation). 43 | * 44 | *

Usually, the position is just the number of bytes read or written 45 | * since the stream was opened, but in the case of a 46 | * {@link it.unimi.dsi.fastutil.io.RepositionableStream} it 47 | * represent the current position. 48 | */ 49 | long position() throws IOException; 50 | } 51 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/io/RepositionableStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2005-2024 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.io; 18 | 19 | /** A basic interface specifying positioning methods for a byte stream. 20 | * 21 | * @author Sebastiano Vigna 22 | * @since 4.4 23 | */ 24 | 25 | public interface RepositionableStream { 26 | 27 | /** Sets the current stream position. 28 | * 29 | * @param newPosition the new stream position. 30 | */ 31 | void position(long newPosition) throws java.io.IOException; 32 | 33 | /** Returns the current stream position. 34 | * 35 | * @return the current stream position. 36 | */ 37 | long position() throws java.io.IOException; 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/io/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes and static methods that make object and primitive-type I/O easier and faster. 3 | * 4 | *

5 | * Classes in this package provide very efficient, unsynchronized buffered 6 | * {@linkplain it.unimi.dsi.fastutil.io.FastBufferedInputStream input} and 7 | * {@linkplain it.unimi.dsi.fastutil.io.FastBufferedOutputStream output} streams (with support for 8 | * {@linkplain it.unimi.dsi.fastutil.io.RepositionableStream repositioning}, too) and 9 | * {@linkplain it.unimi.dsi.fastutil.io.FastByteArrayInputStream fast streams} based on byte arrays. 10 | * 11 | *

12 | * Static containers provide instead a wealth of methods that can be used to 13 | * {@linkplain BinIO#storeObject(Object, CharSequence) serialize} or 14 | * {@linkplain BinIO#loadObject(CharSequence) deserialize} very easily objects and 15 | * {@linkplain BinIO#storeInts(int[], CharSequence) arrays}, even 16 | * {@linkplain TextIO#storeInts(int[], CharSequence) in text form}. 17 | */ 18 | package it.unimi.dsi.fastutil.io; 19 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/longs/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Type-specific classes for long elements or keys. 3 | */ 4 | package it.unimi.dsi.fastutil.longs; 5 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/objects/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Type-specific classes for object elements or keys. 3 | * 4 | *

5 | * Whenever possible and useful, {@code fastutil} provides both typical collections, which compare 6 | * objects using {@code equals()}, and {@linkplain it.unimi.dsi.fastutil.objects.ReferenceSet 7 | * reference-based collections}, which use identity ({@code ==}). See the related comments 8 | * in the overview. 9 | */ 10 | package it.unimi.dsi.fastutil.objects; 11 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Static classes and methods used by all implementations and some non-type-specific classes that do 3 | * not belong to {@link it.unimi.dsi.fastutil.objects}. 4 | */ 5 | package it.unimi.dsi.fastutil; 6 | -------------------------------------------------------------------------------- /src/it/unimi/dsi/fastutil/shorts/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Type-specific classes for short elements or keys. 3 | */ 4 | package it.unimi.dsi.fastutil.shorts; 5 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/BigArraysTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil; 18 | 19 | import static it.unimi.dsi.fastutil.BigArrays.get; 20 | import static it.unimi.dsi.fastutil.BigArrays.length; 21 | import static it.unimi.dsi.fastutil.BigArrays.swap; 22 | import static it.unimi.dsi.fastutil.BigArrays.wrap; 23 | import static org.junit.Assert.assertArrayEquals; 24 | 25 | import java.util.Arrays; 26 | 27 | import org.junit.Test; 28 | 29 | import it.unimi.dsi.fastutil.longs.LongBigArrayBigList; 30 | 31 | public class BigArraysTest { 32 | 33 | @Test 34 | public void testMergeSort() { 35 | final int[] s = new int[] { 2, 1, 5, 2, 1, 0, 9, 1, 4, 2, 4, 6, 8, 9, 10, 12, 1, 7 }; 36 | final int[][] a = wrap(s.clone()); 37 | 38 | Arrays.sort(s); 39 | final int[][] sorted = wrap(s.clone()); 40 | BigArrays.mergeSort(0, length(a), (k1, k2) -> get(a, k1) - get(a, k2), (k1, k2) -> swap(a, k1, k2)); 41 | assertArrayEquals(sorted, a); 42 | 43 | BigArrays.mergeSort(0, length(a), (k1, k2) -> get(a, k1) - get(a, k2), (k1, k2) -> swap(a, k1, k2)); 44 | assertArrayEquals(sorted, a); 45 | 46 | } 47 | 48 | @Test 49 | public void testQuickSort() { 50 | final int[] s = new int[] { 2, 1, 5, 2, 1, 0, 9, 1, 4, 2, 4, 6, 8, 9, 10, 12, 1, 7 }; 51 | 52 | Arrays.sort(s); 53 | final int[][] sorted = wrap(s.clone()); 54 | 55 | final int[][] a = wrap(s.clone()); 56 | BigArrays.quickSort(0, length(a), (k1, k2) -> get(a, k1) - get(a, k2), (k1, k2) -> swap(a, k1, k2)); 57 | assertArrayEquals(sorted, a); 58 | 59 | BigArrays.quickSort(0, length(a), (k1, k2) -> get(a, k1) - get(a, k2), (k1, k2) -> swap(a, k1, k2)); 60 | assertArrayEquals(sorted, a); 61 | } 62 | 63 | @Test 64 | public void testEnsureCapacity() { 65 | final LongBigArrayBigList longList = new LongBigArrayBigList(); 66 | longList.size(4096); 67 | // Never completes! 68 | longList.ensureCapacity(2); 69 | } 70 | 71 | @Test(expected = ArrayIndexOutOfBoundsException.class) 72 | public void testEnsureOffSetLength() { 73 | BigArrays.ensureOffsetLength(42, Long.MAX_VALUE, 10); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/HashCommonTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | import static org.junit.Assert.assertTrue; 21 | 22 | import org.junit.Test; 23 | 24 | public class HashCommonTest { 25 | @Test 26 | public void testMaxFillSmall() { 27 | for(final float f: new float[] { 0.0001f, .25f, .50f, .75f, .9999f }) { 28 | for(int i = 0; i < 16; i++) { 29 | final int n = HashCommon.arraySize(i, f); 30 | final int maxFill = HashCommon.maxFill(n, f); 31 | assertTrue(n + " <= " + maxFill, n > maxFill); 32 | } 33 | 34 | for(long i = 0; i < 16; i++) { 35 | final long n = HashCommon.bigArraySize(i, f); 36 | final long maxFill = HashCommon.maxFill(n, f); 37 | assertTrue(n + " <= " + maxFill, n > maxFill); 38 | } 39 | } 40 | } 41 | 42 | @Test 43 | public void testInverses() { 44 | for(int i = 0 ; i < 1 << 30; i += 10000) { 45 | assertEquals(i, HashCommon.invMix(HashCommon.mix(i))); 46 | assertEquals(i, HashCommon.mix(HashCommon.invMix(i))); 47 | } 48 | for(long i = 0 ; i < 1 << 62; i += 1000000) { 49 | assertEquals(i, HashCommon.invMix(HashCommon.mix(i))); 50 | assertEquals(i, HashCommon.mix(HashCommon.invMix(i))); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/IntIntPairTest.java: -------------------------------------------------------------------------------- 1 | package it.unimi.dsi.fastutil; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.util.Comparator; 6 | 7 | import org.junit.Test; 8 | 9 | import it.unimi.dsi.fastutil.ints.IntIntPair; 10 | 11 | public class IntIntPairTest { 12 | 13 | @Test 14 | public void test() { 15 | final Comparator comparator = IntIntPair.lexComparator(); 16 | assertEquals(0, comparator.compare(IntIntPair.of(0, 1), IntIntPair.of(0, 1))); 17 | assertEquals(-1, comparator.compare(IntIntPair.of(0, 1), IntIntPair.of(0, 2))); 18 | assertEquals(1, comparator.compare(IntIntPair.of(0, 2), IntIntPair.of(0, 1))); 19 | assertEquals(1, comparator.compare(IntIntPair.of(1, 1), IntIntPair.of(0, 1))); 20 | assertEquals(-1, comparator.compare(IntIntPair.of(0, 1), IntIntPair.of(1, 1))); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/MainRunner.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil; 18 | 19 | import java.lang.reflect.Method; 20 | import java.lang.reflect.Modifier; 21 | 22 | /** 23 | * Contains utility methods for running the {@code public static void main(String[]} method of a class if it has one. 24 | * 25 | *

Several classes in fastutils have legacy tests in {@code main} methods that may or may not 26 | * be generated based on code generation flags. This class will query whether the class given has a 27 | * {@code main} method, and if so, calls it with the parameters given. The idea being to ensure 28 | * that these legacy tests get called by the unit tests. 29 | *
It is intended that these tests will be converted into proper unit tests over time, but this 30 | * can help bridge the gap until that is done. 31 | * 32 | *

To have these {@code main} method tests included in the code generation (as well as to generate 33 | * some micro-benchmarks), pass {@code TEST=1} to the {@code make} command. 34 | * 35 | * @author C. Sean Young (csyoung@google.com) 36 | * 37 | */ 38 | public final class MainRunner { 39 | private MainRunner() {} // Static utility class 40 | 41 | private static final Class[] MAIN_PARAM_TYPES = {String[].class}; 42 | 43 | public static void callMainIfExists(Class clazz, String... args) throws ReflectiveOperationException { 44 | if (clazz.getSimpleName().endsWith("Test")) { 45 | throw new IllegalArgumentException( 46 | "Trying to run the main method of a test class " + clazz.getSimpleName() 47 | + ". This isn't what you want. Instead, run the main method of the class under test."); 48 | } 49 | Method mainMethod = null; 50 | try { 51 | mainMethod = clazz.getDeclaredMethod("main", MAIN_PARAM_TYPES); 52 | } catch(NoSuchMethodException unused) { 53 | // Gracefully do nothing if no main method. 54 | } 55 | if (mainMethod == null) { 56 | return; 57 | } 58 | if (!Modifier.isStatic(mainMethod.getModifiers())) { 59 | // Not a static method, so don't try to call it. 60 | return; 61 | } 62 | // The array itself is the argument, so we need to wrap it in another array first. 63 | mainMethod.invoke(null, new Object[] {args}); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/PairTest.java: -------------------------------------------------------------------------------- 1 | package it.unimi.dsi.fastutil; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import java.util.Comparator; 6 | 7 | import org.junit.Test; 8 | 9 | public class PairTest { 10 | 11 | @SuppressWarnings("boxing") 12 | @Test 13 | public void test() { 14 | final Comparator> comparator = Pair.lexComparator(); 15 | assertEquals(0, comparator.compare(Pair.of(0, 1), Pair.of(0, 1))); 16 | assertEquals(-1, comparator.compare(Pair.of(0, 1), Pair.of(0, 2))); 17 | assertEquals(1, comparator.compare(Pair.of(0, 2), Pair.of(0, 1))); 18 | assertEquals(1, comparator.compare(Pair.of(1, 1), Pair.of(0, 1))); 19 | assertEquals(-1, comparator.compare(Pair.of(0, 1), Pair.of(1, 1))); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/doubles/DoubleOpenHashSetTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.doubles; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | import static org.junit.Assert.assertTrue; 21 | 22 | import org.junit.Test; 23 | 24 | import it.unimi.dsi.fastutil.MainRunner; 25 | 26 | public class DoubleOpenHashSetTest { 27 | 28 | @Test 29 | public void testNaNs() { 30 | final DoubleOpenHashSet s = new DoubleOpenHashSet(); 31 | s.add(Double.NaN); 32 | s.add(Double.NaN); 33 | assertEquals(1, s.size()); 34 | } 35 | 36 | @Test 37 | public void testZeros() { 38 | final DoubleOpenHashSet s = new DoubleOpenHashSet(); 39 | assertTrue(s.add(-0.0d)); 40 | assertTrue(s.add(+0.0d)); 41 | assertEquals(2, s.size()); 42 | } 43 | 44 | @Test 45 | public void testLegacyMainMethodTests() throws Exception { 46 | MainRunner.callMainIfExists(DoubleOpenHashSet.class, "test", /*num=*/"500", /*loadFactor=*/"0.75", /*seed=*/"3838474"); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/floats/FloatOpenHashSetTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.floats; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | import static org.junit.Assert.assertTrue; 21 | 22 | import org.junit.Test; 23 | 24 | import it.unimi.dsi.fastutil.MainRunner; 25 | 26 | public class FloatOpenHashSetTest { 27 | 28 | @Test 29 | public void testNaNs() { 30 | final FloatOpenHashSet s = new FloatOpenHashSet(); 31 | s.add(Float.NaN); 32 | s.add(Float.NaN); 33 | assertEquals(1, s.size()); 34 | } 35 | 36 | @Test 37 | public void testZeros() { 38 | final FloatOpenHashSet s = new FloatOpenHashSet(); 39 | assertTrue(s.add(-0.0f)); 40 | assertTrue(s.add(+0.0f)); 41 | assertEquals(2, s.size()); 42 | } 43 | 44 | @Test 45 | public void testLegacyMainMethodTests() throws Exception { 46 | MainRunner.callMainIfExists(FloatOpenHashSet.class, "test", /*num=*/"500", /*loadFactor=*/"0.75", /*seed=*/"3838474"); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/AbstractIntCollectionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | import static org.junit.Assert.assertFalse; 21 | import static org.junit.Assert.assertTrue; 22 | 23 | import java.util.Collection; 24 | 25 | import org.junit.Test; 26 | 27 | public class AbstractIntCollectionTest { 28 | @Test 29 | public void testAllSpecific() { 30 | IntCollection m = new AbstractIntCollection() { 31 | final IntOpenHashSet s = new IntOpenHashSet(new int[] { 0, 4, 5, 6, 9 }); 32 | 33 | @Override 34 | public IntIterator iterator() { 35 | return s.iterator(); 36 | } 37 | 38 | @Override 39 | public boolean add(int k) { 40 | return s.add(k); 41 | } 42 | 43 | @Override 44 | public boolean rem(int k) { 45 | return s.remove(k); 46 | } 47 | 48 | @Override 49 | public int size() { 50 | return s.size(); 51 | } 52 | }; 53 | 54 | m.addAll(new IntOpenHashSet(new int[] { 1, 3 })); 55 | assertEquals(new IntOpenHashSet(new int[] { 0, 1, 3, 4, 5, 6, 9 }), new IntOpenHashSet(m)); 56 | assertTrue(m.containsAll(new IntOpenHashSet(new int[] { 1, 3, 6 }))); 57 | assertFalse(m.containsAll(new IntOpenHashSet(new int[] { 1, 2, 6 }))); 58 | assertTrue(m.removeAll(new IntOpenHashSet(new int[] { 1, 2, 3 }))); 59 | assertEquals(new IntOpenHashSet(new int[] { 0, 4, 5, 6, 9 }), new IntOpenHashSet(m)); 60 | assertTrue(m.retainAll(new IntOpenHashSet(new int[] { -1, 0, 5, 9, 11 }))); 61 | assertEquals(new IntOpenHashSet(new int[] { 0, 5, 9 }), new IntOpenHashSet(m)); 62 | } 63 | 64 | @Test 65 | public void testAllGeneric() { 66 | IntCollection m = new AbstractIntCollection() { 67 | final IntOpenHashSet s = new IntOpenHashSet(new int[] { 0, 4, 5, 6, 9 }); 68 | 69 | @Override 70 | public IntIterator iterator() { 71 | return s.iterator(); 72 | } 73 | 74 | @Override 75 | public boolean add(int k) { 76 | return s.add(k); 77 | } 78 | 79 | @Override 80 | public boolean rem(int k) { 81 | return s.remove(k); 82 | } 83 | 84 | @Override 85 | public int size() { 86 | return s.size(); 87 | } 88 | }; 89 | 90 | m.addAll((Collection)new IntOpenHashSet(new int[] { 1, 3 })); 91 | assertEquals(new IntOpenHashSet(new int[] { 0, 1, 3, 4, 5, 6, 9 }), new IntOpenHashSet(m)); 92 | assertTrue(m.containsAll((Collection)new IntOpenHashSet(new int[] { 1, 3, 6 }))); 93 | assertFalse(m.containsAll((Collection)new IntOpenHashSet(new int[] { 1, 2, 6 }))); 94 | assertTrue(m.removeAll((Collection)new IntOpenHashSet(new int[] { 1, 2, 3 }))); 95 | assertEquals(new IntOpenHashSet(new int[] { 0, 4, 5, 6, 9 }), new IntOpenHashSet(m)); 96 | assertTrue(m.retainAll((Collection)new IntOpenHashSet(new int[] { -1, 0, 5, 9, 11 }))); 97 | assertEquals(new IntOpenHashSet(new int[] { 0, 5, 9 }), new IntOpenHashSet(m)); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/Int2IntMapGenericAVLTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import java.util.Collections; 20 | import java.util.EnumSet; 21 | import java.util.function.Supplier; 22 | 23 | import org.junit.Test; 24 | import org.junit.runners.Parameterized.Parameters; 25 | 26 | import it.unimi.dsi.fastutil.MainRunner; 27 | 28 | public class Int2IntMapGenericAVLTest extends Int2IntMapGenericTest { 29 | @Parameters 30 | public static Iterable data() { 31 | return Collections.singletonList(new Object[] { (Supplier) Int2IntAVLTreeMap::new, EnumSet.allOf(Capability.class) }); 32 | } 33 | 34 | @Test 35 | public void testLegacyMainMethodTests() throws Exception { 36 | MainRunner.callMainIfExists(Int2IntAVLTreeMap.class, "test", /*num=*/"20", /*seed=*/"42342"); 37 | } 38 | } -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/Int2IntMapGenericArrayTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import java.util.Collections; 20 | import java.util.EnumSet; 21 | import java.util.function.Supplier; 22 | 23 | import org.junit.runners.Parameterized.Parameters; 24 | 25 | public class Int2IntMapGenericArrayTest extends Int2IntMapGenericTest { 26 | @Parameters 27 | public static Iterable data() { 28 | final EnumSet capabilities = EnumSet.allOf(Capability.class); 29 | capabilities.remove(Capability.ITERATOR_MODIFY); 30 | return Collections.singletonList(new Object[] { (Supplier) Int2IntArrayMap::new, capabilities }); 31 | } 32 | } -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/Int2IntMapGenericDefaultTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import it.unimi.dsi.fastutil.ints.Int2IntMapGenericDefaultTest.SimpleInt2IntMap; 20 | import it.unimi.dsi.fastutil.objects.ObjectSet; 21 | import java.util.Collections; 22 | import java.util.EnumSet; 23 | import java.util.Map; 24 | import java.util.function.Supplier; 25 | import org.junit.runners.Parameterized.Parameters; 26 | 27 | public class Int2IntMapGenericDefaultTest extends Int2IntMapGenericTest { 28 | @Parameters 29 | public static Iterable data() { 30 | final EnumSet capabilities = EnumSet.allOf(Capability.class); 31 | capabilities.remove(Capability.ITERATOR_MODIFY); 32 | capabilities.remove(Capability.KEY_SET_MODIFY); 33 | return Collections.singletonList(new Object[] { 34 | (Supplier) () -> new SimpleInt2IntMap(new Int2IntArrayMap()), capabilities 35 | }); 36 | } 37 | 38 | static final class SimpleInt2IntMap implements Int2IntMap { 39 | private final Int2IntMap delegate; 40 | 41 | SimpleInt2IntMap(final Int2IntMap delegate) { 42 | this.delegate = delegate; 43 | } 44 | 45 | @Override 46 | public void clear() { 47 | delegate.clear(); 48 | } 49 | 50 | @Override 51 | public boolean containsKey(final int key) { 52 | return delegate.containsKey(key); 53 | } 54 | 55 | @Override 56 | public boolean containsValue(final int value) { 57 | return delegate.containsValue(value); 58 | } 59 | 60 | @Override 61 | public void defaultReturnValue(final int rv) { 62 | delegate.defaultReturnValue(rv); 63 | } 64 | 65 | @Override 66 | public int defaultReturnValue() { 67 | return delegate.defaultReturnValue(); 68 | } 69 | 70 | @Override 71 | public int get(final int key) { 72 | return delegate.get(key); 73 | } 74 | 75 | @Override 76 | public ObjectSet int2IntEntrySet() { 77 | return delegate.int2IntEntrySet(); 78 | } 79 | 80 | @Override 81 | public boolean isEmpty() { 82 | return delegate.isEmpty(); 83 | } 84 | 85 | @Override 86 | public IntSet keySet() { 87 | return delegate.keySet(); 88 | } 89 | 90 | @Override 91 | public int put(final int key, final int value) { 92 | return delegate.put(key, value); 93 | } 94 | 95 | @Override 96 | public void putAll(final Map m) { 97 | delegate.putAll(m); 98 | } 99 | 100 | @Override 101 | public int remove(final int key) { 102 | return delegate.remove(key); 103 | } 104 | 105 | @Override 106 | public int size() { 107 | return delegate.size(); 108 | } 109 | 110 | @Override 111 | public IntCollection values() { 112 | return delegate.values(); 113 | } 114 | 115 | @Override 116 | public boolean equals(final Object o) { 117 | if (this == o) return true; 118 | return delegate.equals(o); 119 | } 120 | 121 | @Override 122 | public int hashCode() { 123 | return delegate.hashCode(); 124 | } 125 | } 126 | } -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/Int2IntMapGenericOpenHashTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | 21 | import java.util.ArrayList; 22 | import java.util.Collection; 23 | import java.util.EnumSet; 24 | import java.util.function.Supplier; 25 | 26 | import org.junit.Test; 27 | import org.junit.runners.Parameterized.Parameter; 28 | import org.junit.runners.Parameterized.Parameters; 29 | 30 | import it.unimi.dsi.fastutil.Hash; 31 | 32 | public class Int2IntMapGenericOpenHashTest extends Int2IntMapGenericTest { 33 | @Parameter(2) 34 | public float loadFactor; 35 | 36 | @SuppressWarnings({ "boxing" }) 37 | @Parameters(name = "{index}: lf {2}") 38 | public static Iterable data() { 39 | final EnumSet capabilities = EnumSet.allOf(Capability.class); 40 | final int defSize = Int2IntOpenHashMap.DEFAULT_INITIAL_SIZE; 41 | final Collection data = new ArrayList<>(); 42 | for (final float loadFactor : new float[] { Hash.DEFAULT_LOAD_FACTOR, Hash.FAST_LOAD_FACTOR, Hash.VERY_FAST_LOAD_FACTOR }) { 43 | data.add(new Object[] {supplier(defSize, loadFactor), capabilities, loadFactor}); 44 | } 45 | return data; 46 | } 47 | 48 | private static Supplier supplier(final int defSize, final float loadFactor) { 49 | return () -> new Int2IntOpenHashMap(defSize, loadFactor); 50 | } 51 | 52 | @Test 53 | public void testAddTo() { 54 | assertEquals(0, m.addTo(0, 2)); 55 | assertEquals(2, m.get(0)); 56 | assertEquals(2, m.addTo(0, 3)); 57 | assertEquals(5, m.get(0)); 58 | m.defaultReturnValue(-1); 59 | assertEquals(-1, m.addTo(1, 1)); 60 | assertEquals(0, m.get(1)); 61 | assertEquals(0, m.addTo(1, 1)); 62 | assertEquals(1, m.get(1)); 63 | assertEquals(1, m.addTo(1, -2)); 64 | assertEquals(-1, m.get(1)); 65 | } 66 | } -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/Int2IntMapGenericRBTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import java.util.Collections; 20 | import java.util.EnumSet; 21 | import java.util.function.Supplier; 22 | 23 | import org.junit.Test; 24 | import org.junit.runners.Parameterized.Parameters; 25 | 26 | import it.unimi.dsi.fastutil.MainRunner; 27 | 28 | public class Int2IntMapGenericRBTest extends Int2IntMapGenericTest { 29 | @Parameters 30 | public static Iterable data() { 31 | return Collections.singletonList(new Object[] { (Supplier) Int2IntRBTreeMap::new, EnumSet.allOf(Capability.class) }); 32 | } 33 | 34 | @Test 35 | public void testLegacyMainMethodTests() throws Exception { 36 | MainRunner.callMainIfExists(Int2IntRBTreeMap.class, "test", /*num=*/"20", /*seed=*/"42342"); 37 | } 38 | } -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/Int2IntMapsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | import static org.junit.Assert.assertFalse; 21 | import static org.junit.Assert.assertNull; 22 | import static org.junit.Assert.assertSame; 23 | 24 | import java.util.Collections; 25 | import java.util.HashSet; 26 | import java.util.Set; 27 | 28 | import org.junit.Test; 29 | 30 | import it.unimi.dsi.fastutil.ints.Int2IntMap.Entry; 31 | import it.unimi.dsi.fastutil.objects.ObjectIterable; 32 | import it.unimi.dsi.fastutil.objects.ObjectIterator; 33 | 34 | public class Int2IntMapsTest { 35 | 36 | @SuppressWarnings("boxing") 37 | @Test 38 | public void testSingletonMapEqualsShouldCheckTheTypeOfParamters() { 39 | final Int2IntMap map = Int2IntMaps.singleton(1, 2); 40 | assertFalse(map.equals(Collections.singletonMap(null, 2))); 41 | assertFalse(map.equals(Collections.singletonMap(1, null))); 42 | assertFalse(map.equals(Collections.singletonMap("foo", 2))); 43 | assertFalse(map.equals(Collections.singletonMap(1, "foo"))); 44 | } 45 | 46 | @Test 47 | public void testToArrayShouldNullElementAfterLastEntry() { 48 | final Int2IntMap map = Int2IntMaps.EMPTY_MAP; 49 | final Object[] values = new Object[] { "test" }; 50 | map.int2IntEntrySet().toArray(values); 51 | assertNull(values[0]); 52 | } 53 | 54 | @Test 55 | public void testReadingDefaultReturnValueFromUnmodifiableMap() { 56 | final Int2IntMap map = Int2IntMaps.unmodifiable(Int2IntMaps.EMPTY_MAP); 57 | assert(map.defaultReturnValue() == 0); 58 | } 59 | 60 | @Test 61 | public void testFastIteratorHelpers() { 62 | final Int2IntMap m = new Int2IntOpenHashMap(); 63 | m.put(0, 0); 64 | m.put(1, 1); 65 | ObjectIterator fastIterator = Int2IntMaps.fastIterator(m); 66 | Entry e = fastIterator.next(); 67 | assertSame(e, fastIterator.next()); 68 | 69 | final ObjectIterable fastIterable = Int2IntMaps.fastIterable(m); 70 | fastIterator = fastIterable.iterator(); 71 | e = fastIterator.next(); 72 | assertSame(e, fastIterator.next()); 73 | 74 | final Set s = new HashSet<>(); 75 | Int2IntMaps.fastIterable(m).forEach(s::add); 76 | assertEquals(1, s.size()); // Should be always the same entry, mutated 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/Int2ObjectFunctionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import static org.junit.Assert.assertSame; 20 | 21 | import org.junit.Test; 22 | 23 | public class Int2ObjectFunctionTest { 24 | 25 | @SuppressWarnings("deprecation") 26 | @Test 27 | public void testDefaultGenericMethods() { 28 | final Integer zero = Integer.valueOf(0); 29 | 30 | @SuppressWarnings("serial") 31 | final Int2ObjectFunction f = new AbstractInt2ObjectFunction() { 32 | @Override 33 | public Object get(final int key) { 34 | return key == 0 ? zero : defRetValue; 35 | } 36 | }; 37 | 38 | final Object drv = new Object(); 39 | f.defaultReturnValue(drv); 40 | assertSame(Integer.valueOf(0), f.get(0)); 41 | assertSame(drv, f.get(1)); 42 | assertSame(drv, f.get(Integer.valueOf(1))); 43 | assertSame(zero, f.get(0)); 44 | assertSame(zero, f.get(Integer.valueOf(0))); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/Int2ObjectMapGenericAVLTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import java.util.Collections; 20 | import java.util.EnumSet; 21 | import java.util.function.Supplier; 22 | 23 | import org.junit.Test; 24 | import org.junit.runners.Parameterized.Parameters; 25 | 26 | import it.unimi.dsi.fastutil.MainRunner; 27 | 28 | public class Int2ObjectMapGenericAVLTest extends Int2ObjectMapGenericTest> { 29 | @Parameters 30 | public static Iterable data() { 31 | return Collections.singletonList(new Object[] {(Supplier>) Int2ObjectAVLTreeMap::new, EnumSet.allOf(Capability.class)}); 32 | } 33 | 34 | @Test 35 | public void testLegacyMainMethodTests() throws Exception { 36 | MainRunner.callMainIfExists(Int2ObjectAVLTreeMap.class, "test", /*num=*/"20", /*seed=*/"42342"); 37 | } 38 | } -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/Int2ObjectMapGenericArrayTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import java.util.Collections; 20 | import java.util.EnumSet; 21 | import java.util.function.Supplier; 22 | 23 | import org.junit.runners.Parameterized.Parameters; 24 | 25 | public class Int2ObjectMapGenericArrayTest extends Int2ObjectMapGenericTest> { 26 | @Parameters 27 | public static Iterable data() { 28 | final EnumSet capabilities = EnumSet.allOf(Capability.class); 29 | capabilities.remove(Capability.ITERATOR_MODIFY); 30 | return Collections.singletonList(new Object[] { (Supplier>) Int2ObjectArrayMap::new, capabilities }); 31 | } 32 | } -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/Int2ObjectMapGenericDefaultTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import it.unimi.dsi.fastutil.ints.Int2ObjectMapGenericDefaultTest.SimpleInt2ObjectMap; 20 | import it.unimi.dsi.fastutil.objects.ObjectCollection; 21 | import it.unimi.dsi.fastutil.objects.ObjectSet; 22 | import java.util.Collections; 23 | import java.util.EnumSet; 24 | import java.util.Map; 25 | import java.util.function.Supplier; 26 | import org.junit.runners.Parameterized.Parameters; 27 | 28 | public class Int2ObjectMapGenericDefaultTest extends Int2ObjectMapGenericTest> { 29 | @Parameters 30 | public static Iterable data() { 31 | final EnumSet capabilities = EnumSet.allOf(Capability.class); 32 | capabilities.remove(Capability.ITERATOR_MODIFY); 33 | capabilities.remove(Capability.KEY_SET_MODIFY); 34 | return Collections.singletonList(new Object[] { 35 | (Supplier>) () -> new SimpleInt2ObjectMap<>(new Int2ObjectArrayMap<>()), capabilities 36 | }); 37 | } 38 | 39 | static final class SimpleInt2ObjectMap implements Int2ObjectMap { 40 | private final Int2ObjectMap delegate; 41 | 42 | SimpleInt2ObjectMap(final Int2ObjectMap delegate) { 43 | this.delegate = delegate; 44 | } 45 | 46 | @Override 47 | public void clear() { 48 | delegate.clear(); 49 | } 50 | 51 | @Override 52 | public boolean containsKey(final int key) { 53 | return delegate.containsKey(key); 54 | } 55 | 56 | @Override 57 | public boolean containsValue(final Object value) { 58 | return delegate.containsValue(value); 59 | } 60 | 61 | @Override 62 | public void defaultReturnValue(final V rv) { 63 | delegate.defaultReturnValue(rv); 64 | } 65 | 66 | @Override 67 | public V defaultReturnValue() { 68 | return delegate.defaultReturnValue(); 69 | } 70 | 71 | @Override 72 | public V get(final int key) { 73 | return delegate.get(key); 74 | } 75 | 76 | @Override 77 | public ObjectSet> int2ObjectEntrySet() { 78 | return delegate.int2ObjectEntrySet(); 79 | } 80 | 81 | @Override 82 | public boolean isEmpty() { 83 | return delegate.isEmpty(); 84 | } 85 | 86 | @Override 87 | public IntSet keySet() { 88 | return delegate.keySet(); 89 | } 90 | 91 | @Override 92 | public V put(final int key, final V value) { 93 | return delegate.put(key, value); 94 | } 95 | 96 | @Override 97 | public void putAll(final Map m) { 98 | delegate.putAll(m); 99 | } 100 | 101 | @Override 102 | public V remove(final int key) { 103 | return delegate.remove(key); 104 | } 105 | 106 | @Override 107 | public int size() { 108 | return delegate.size(); 109 | } 110 | 111 | @Override 112 | public ObjectCollection values() { 113 | return delegate.values(); 114 | } 115 | 116 | @Override 117 | public boolean equals(final Object o) { 118 | if (this == o) return true; 119 | return delegate.equals(o); 120 | } 121 | 122 | @Override 123 | public int hashCode() { 124 | return delegate.hashCode(); 125 | } 126 | } 127 | } -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/Int2ObjectMapGenericOpenHashTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import java.util.ArrayList; 20 | import java.util.Collection; 21 | import java.util.EnumSet; 22 | import java.util.function.Supplier; 23 | 24 | import org.junit.runners.Parameterized.Parameter; 25 | import org.junit.runners.Parameterized.Parameters; 26 | 27 | import it.unimi.dsi.fastutil.Hash; 28 | 29 | public class Int2ObjectMapGenericOpenHashTest extends Int2ObjectMapGenericTest> { 30 | @Parameter(2) 31 | public float loadFactor; 32 | 33 | @SuppressWarnings({ "boxing" }) 34 | @Parameters(name = "{index}: lf {2}") 35 | public static Iterable data() { 36 | final EnumSet capabilities = EnumSet.allOf(Capability.class); 37 | final int defSize = Int2ObjectOpenHashMap.DEFAULT_INITIAL_SIZE; 38 | final Collection data = new ArrayList<>(); 39 | for (final float loadFactor : new float[] {Hash.DEFAULT_LOAD_FACTOR, Hash.FAST_LOAD_FACTOR, Hash.VERY_FAST_LOAD_FACTOR}) { 40 | data.add(new Object[] {(Supplier>) () -> new Int2ObjectOpenHashMap<>(defSize, loadFactor), capabilities, loadFactor}); 41 | } 42 | return data; 43 | } 44 | } -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/Int2ObjectMapGenericRBTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import java.util.Collections; 20 | import java.util.EnumSet; 21 | import java.util.function.Supplier; 22 | 23 | import org.junit.Test; 24 | import org.junit.runners.Parameterized.Parameters; 25 | 26 | import it.unimi.dsi.fastutil.MainRunner; 27 | 28 | public class Int2ObjectMapGenericRBTest extends Int2ObjectMapGenericTest> { 29 | @Parameters 30 | public static Iterable data() { 31 | return Collections.singletonList(new Object[] {(Supplier>) Int2ObjectRBTreeMap::new, EnumSet.allOf(Capability.class)}); 32 | } 33 | 34 | @Test 35 | public void testLegacyMainMethodTests() throws Exception { 36 | MainRunner.callMainIfExists(Int2ObjectRBTreeMap.class, "test", /*num=*/"20", /*seed=*/"42342"); 37 | } 38 | } -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/IntAVLTreeSetTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | import static org.junit.Assert.assertFalse; 21 | import static org.junit.Assert.assertTrue; 22 | 23 | import org.junit.Test; 24 | 25 | import it.unimi.dsi.fastutil.MainRunner; 26 | 27 | public class IntAVLTreeSetTest { 28 | 29 | @Test 30 | public void testGet() { 31 | final IntAVLTreeSet s = new IntAVLTreeSet(); 32 | final int i = 0; 33 | assertTrue(s.isEmpty()); 34 | assertEquals(0, s.size()); 35 | s.add(i); 36 | assertTrue(s.contains(i)); 37 | assertFalse(s.add(i)); 38 | assertEquals(1, s.size()); 39 | } 40 | @Test 41 | public void testLegacyMainMethodTests() throws Exception { 42 | MainRunner.callMainIfExists(IntAVLTreeSet.class, "test", /*num=*/"20", /*seed=*/"423429"); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/IntBigListsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import static org.junit.Assert.assertArrayEquals; 20 | 21 | import org.junit.Test; 22 | 23 | public class IntBigListsTest { 24 | 25 | @Test 26 | public void testSingletonToArray() { 27 | assertArrayEquals(new int[] { 1 }, IntBigLists.singleton(1).toIntArray()); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/IntCollectionsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import static org.junit.Assert.assertFalse; 20 | import static org.junit.Assert.assertTrue; 21 | 22 | import java.util.function.IntPredicate; 23 | import java.util.function.Predicate; 24 | 25 | import org.junit.Test; 26 | 27 | public class IntCollectionsTest { 28 | 29 | @Test 30 | public void testIsNotEmpty() { 31 | final IntCollection test = IntCollections.asCollection(() -> IntSets.singleton(0).iterator()); 32 | 33 | assertFalse(test.isEmpty()); 34 | } 35 | 36 | @Test 37 | public void testEmpty() { 38 | final IntCollection test = IntCollections.asCollection(() -> IntSets.EMPTY_SET.iterator()); 39 | 40 | assertTrue(test.isEmpty()); 41 | } 42 | 43 | @SuppressWarnings("deprecation") 44 | @Test 45 | public void testRemoveIfOverloads() { 46 | final IntOpenHashSet s = IntOpenHashSet.of(1, 2, 3); 47 | s.removeIf(x -> x == 1); 48 | final IntPredicate p = x -> x == 1; 49 | s.removeIf(p); 50 | final it.unimi.dsi.fastutil.ints.IntPredicate q = x -> x == 1; 51 | s.removeIf(q); 52 | @SuppressWarnings("boxing") 53 | final Predicate r = x -> x == 1; 54 | s.removeIf(r); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/IntComparatorTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2003-2024 Barak Ugav and Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | 21 | import org.junit.Test; 22 | 23 | public class IntComparatorTest { 24 | 25 | @Test 26 | public void comparing() { 27 | String[] array = new String[] { "68", "98", "30", "62", "81", "61", "80", "63", "62", "77", "10", "95", "40", 28 | "73", "55", "45", "16", "10", "86", "28", "79", "44", "52", "92", "98", "28", "88", "70", "70", "10" }; 29 | IntComparator c = IntComparator.comparing(i -> array[i]); 30 | for (int i = 0; i < array.length; i++) { 31 | int j = ((i + 29) * 1337) % array.length; 32 | assertEquals(c.compare(i, j), array[i].compareTo(array[j])); 33 | } 34 | } 35 | 36 | @Test 37 | public void comparingInt() { 38 | int[] array = new int[] { 81, 87, 70, 54, 40, 79, 16, 8, 84, 39, 37, 84, 64, 60, 31, 44, 95, 15, 52, 48, 19, 20, 39 | 75, 31, 46, 61, 38, 27, 32, 84 }; 40 | IntComparator c = IntComparator.comparingInt(i -> array[i]); 41 | for (int i = 0; i < array.length; i++) { 42 | int j = ((i + 17) * 1337) % array.length; 43 | assertEquals(c.compare(i, j), Integer.compare(array[i], array[j])); 44 | } 45 | } 46 | 47 | @Test 48 | public void comparingLong() { 49 | long[] array = new long[] { 26, 49, 49, 24, 15, 71, 10, 88, 78, 4, 42, 79, 75, 69, 63, 16, 71, 47, 54, 39, 89, 50 | 10, 64, 37, 38, 59, 81, 59, 58, 33 }; 51 | IntComparator c = IntComparator.comparingLong(i -> array[i]); 52 | for (int i = 0; i < array.length; i++) { 53 | int j = ((i + 19) * 1337) % array.length; 54 | assertEquals(c.compare(i, j), Long.compare(array[i], array[j])); 55 | } 56 | } 57 | 58 | @Test 59 | public void comparingDouble() { 60 | double[] array = new double[] { 0.61, 0.97, 0.97, 0.75, 0.73, 0.36, 0.72, 0.14, 0.93, 0.18, 0.45, 0.03, 0.62, 61 | 0.05, 0.04, 0.05, 0.38, 0.89, 0., 0.93, 0.83, 0.14, 0.21, 0.79, 0.5, 0.17, 0.46, 0.74, 0.88, 0.94 }; 62 | IntComparator c = IntComparator.comparingDouble(i -> array[i]); 63 | for (int i = 0; i < array.length; i++) { 64 | int j = ((i + 23) * 1337) % array.length; 65 | assertEquals(c.compare(i, j), Double.compare(array[i], array[j])); 66 | } 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/IntHeapPriorityQueueTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | 21 | import java.io.File; 22 | import java.io.IOException; 23 | 24 | import org.junit.Test; 25 | 26 | import it.unimi.dsi.fastutil.MainRunner; 27 | import it.unimi.dsi.fastutil.io.BinIO; 28 | 29 | public class IntHeapPriorityQueueTest { 30 | @SuppressWarnings("deprecation") 31 | @Test 32 | public void testSerialize() throws IOException, ClassNotFoundException { 33 | IntHeapPriorityQueue q = new IntHeapPriorityQueue(); 34 | for(int i = 0; i < 100; i++) q.enqueue(i); 35 | 36 | File file = File.createTempFile(getClass().getPackage().getName() + "-", "-tmp"); 37 | file.deleteOnExit(); 38 | BinIO.storeObject(q, file); 39 | IntHeapPriorityQueue r = (IntHeapPriorityQueue)BinIO.loadObject(file); 40 | file.delete(); 41 | for(int i = 0; i < 100; i++) { 42 | assertEquals(q.first(), r.first()); 43 | assertEquals(q.dequeue(), r.dequeue()); 44 | } 45 | } 46 | 47 | @Test 48 | public void testLegacyMainMethodTests() throws Exception { 49 | MainRunner.callMainIfExists(IntHeapPriorityQueue.class, "test", /*num=*/"500", /*seed=*/"9329384"); 50 | } 51 | } 52 | 53 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/IntHeapSemiIndirectPriorityQueueTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import static org.junit.Assert.assertArrayEquals; 20 | 21 | import java.util.Arrays; 22 | 23 | import org.junit.Test; 24 | 25 | import it.unimi.dsi.fastutil.MainRunner; 26 | import junit.framework.TestCase; 27 | 28 | public class IntHeapSemiIndirectPriorityQueueTest extends TestCase { 29 | 30 | public void testTops() { 31 | int refArray[] = { 4, 3, 2, 1, 0, 3, 2, 1, 0, 2, 1, 0, 1, 0, 0 }; 32 | int tops[] = new int[refArray.length]; 33 | final IntHeapSemiIndirectPriorityQueue queue = new IntHeapSemiIndirectPriorityQueue(refArray); 34 | for(int i = refArray.length; i-- != 0;) queue.enqueue(i); 35 | 36 | assertEquals(5, queue.front(tops)); 37 | assertEquals(new IntOpenHashSet(new int[] { 4, 8, 11, 13, 14 }), new IntOpenHashSet(tops, 0, 5)); 38 | for(int i = 4; i-- != 0;) { 39 | queue.dequeue(); 40 | assertEquals(i + 1, queue.front(tops)); 41 | } 42 | queue.dequeue(); 43 | 44 | assertEquals(4, queue.front(tops)); 45 | assertEquals(new IntOpenHashSet(new int[] { 3, 7, 10, 12 }), new IntOpenHashSet(tops, 0, 4)); 46 | for(int i = 3; i-- != 0;) { 47 | queue.dequeue(); 48 | assertEquals(i + 1, queue.front(tops)); 49 | } 50 | queue.dequeue(); 51 | 52 | assertEquals(3, queue.front(tops)); 53 | assertEquals(new IntOpenHashSet(new int[] { 2, 6, 9 }), new IntOpenHashSet(tops, 0, 3)); 54 | for(int i = 2; i-- != 0;) { 55 | queue.dequeue(); 56 | assertEquals(i + 1, queue.front(tops)); 57 | } 58 | queue.dequeue(); 59 | 60 | assertEquals(2, queue.front(tops)); 61 | assertEquals(new IntOpenHashSet(new int[] { 1, 5 }), new IntOpenHashSet(tops, 0, 2)); 62 | queue.dequeue(); 63 | assertEquals(1, queue.front(tops)); 64 | queue.dequeue(); 65 | 66 | assertEquals(1, queue.front(tops)); 67 | } 68 | 69 | @Test 70 | public void testFrontWithComparator() { 71 | final int[] refArray = { 8, 16, 9 }; 72 | 73 | IntComparator comparator = (k1, k2) -> (k1 & 3) - (k2 & 3); 74 | 75 | IntHeapSemiIndirectPriorityQueue queue = new IntHeapSemiIndirectPriorityQueue(refArray, comparator); 76 | queue.enqueue(0); 77 | queue.enqueue(1); 78 | queue.enqueue(2); 79 | final int[] front = new int[2]; 80 | assertEquals(2, queue.front(front)); 81 | Arrays.sort(front); 82 | assertArrayEquals(new int[] { 0, 1 }, front); 83 | } 84 | 85 | @Test 86 | public void testLegacyMainMethodTests() throws Exception { 87 | MainRunner.callMainIfExists(IntHeapSemiIndirectPriorityQueue.class, "test", /*num=*/"500", /*seed=*/"39384"); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/IntLinkedOpenCustomHashSetTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | import static org.junit.Assert.assertFalse; 21 | import static org.junit.Assert.assertTrue; 22 | 23 | import org.junit.Test; 24 | 25 | import it.unimi.dsi.fastutil.MainRunner; 26 | 27 | public class IntLinkedOpenCustomHashSetTest { 28 | 29 | @Test 30 | public void testGetNullKey() { 31 | final IntLinkedOpenCustomHashSet s = new IntLinkedOpenCustomHashSet(new IntHash.Strategy() { 32 | 33 | @Override 34 | public int hashCode(final int o) { 35 | return o % 10; 36 | } 37 | 38 | @Override 39 | public boolean equals(final int a, final int b) { 40 | return (a - b) % 10 == 0; 41 | } 42 | }); 43 | 44 | s.add(3); 45 | s.add(10); 46 | s.add(0); 47 | assertTrue(s.contains(0)); 48 | assertTrue(s.contains(10)); 49 | assertTrue(s.contains(3)); 50 | assertFalse(s.contains(1)); 51 | IntListIterator i = s.iterator(); 52 | assertEquals(3, i.nextInt()); 53 | assertEquals(10, i.nextInt()); 54 | assertFalse(i.hasNext()); 55 | 56 | s.remove(0); 57 | assertFalse(s.contains(0)); 58 | assertFalse(s.contains(10)); 59 | s.add(10); 60 | 61 | i = s.iterator(); 62 | assertEquals(3, i.nextInt()); 63 | assertEquals(10, i.nextInt()); 64 | assertFalse(i.hasNext()); 65 | 66 | } 67 | 68 | @Test 69 | public void testLegacyMainMethodTests() throws Exception { 70 | MainRunner.callMainIfExists(IntLinkedOpenCustomHashSet.class, "test", /*num=*/"500", /*loadFactor=*/"0.75", /*seed=*/"383474"); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/IntListTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | import static org.junit.Assert.assertTrue; 21 | 22 | import org.junit.Test; 23 | 24 | public class IntListTest { 25 | @Test 26 | public void testOf() { 27 | final IntList l = IntList.of(0, 1, 2); 28 | assertEquals(IntArrayList.wrap(new int[] { 0, 1, 2 }), l); 29 | } 30 | 31 | @Test 32 | public void testOfEmpty() { 33 | final IntList l = IntList.of(); 34 | assertTrue(l.isEmpty()); 35 | } 36 | 37 | @Test 38 | public void testOfSingleton() { 39 | final IntList l = IntList.of(0); 40 | assertEquals(IntArrayList.wrap(new int[] { 0 }), l); 41 | } 42 | 43 | @Test 44 | public void testOfPair() { 45 | final IntList l = IntList.of(0, 1); 46 | assertEquals(IntArrayList.wrap(new int[] { 0, 1 }), l); 47 | } 48 | 49 | @Test 50 | public void testOfTriplet() { 51 | final IntList l = IntList.of(0, 1, 2); 52 | assertEquals(IntArrayList.wrap(new int[] { 0, 1, 2 }), l); 53 | } 54 | 55 | @Test(expected = UnsupportedOperationException.class) 56 | public void testOfImmutable() { 57 | final IntList l = IntList.of(0, 1, 2); 58 | l.add(3); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/IntListsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import static org.junit.Assert.assertFalse; 20 | import static org.junit.Assert.assertTrue; 21 | 22 | import java.util.RandomAccess; 23 | 24 | import org.junit.Test; 25 | 26 | import it.unimi.dsi.fastutil.MainRunner; 27 | 28 | public class IntListsTest { 29 | @Test 30 | public void testRandomAccess() { 31 | final IntList fakeList = new AbstractIntList() { 32 | @Override 33 | public int getInt(final int index) { 34 | return 0; 35 | } 36 | @Override 37 | public int size() { 38 | return 1; 39 | } 40 | }; 41 | 42 | assertFalse(IntLists.unmodifiable(fakeList) instanceof RandomAccess); 43 | assertFalse(IntLists.synchronize(fakeList) instanceof RandomAccess); 44 | assertFalse(IntLists.synchronize(fakeList, new Object()) instanceof RandomAccess); 45 | 46 | assertTrue(IntLists.unmodifiable(new IntArrayList()) instanceof RandomAccess); 47 | assertTrue(IntLists.synchronize(new IntArrayList()) instanceof RandomAccess); 48 | assertTrue(IntLists.synchronize(new IntArrayList(), new Object()) instanceof RandomAccess); 49 | } 50 | 51 | @Test 52 | public void testLegacyMainMethodTests() throws Exception { 53 | MainRunner.callMainIfExists(IntLists.class, "int", /*seed=*/"9389487"); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/IntRBTreeSetTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | import static org.junit.Assert.assertFalse; 21 | import static org.junit.Assert.assertTrue; 22 | 23 | import org.junit.Test; 24 | 25 | import it.unimi.dsi.fastutil.MainRunner; 26 | 27 | public class IntRBTreeSetTest { 28 | 29 | @Test 30 | public void testAddAndGet() { 31 | final IntRBTreeSet s = new IntRBTreeSet(); 32 | final int i = 0; 33 | assertTrue(s.isEmpty()); 34 | assertEquals(0, s.size()); 35 | s.add(i); 36 | assertTrue(s.contains(i)); 37 | assertFalse(s.add(i)); 38 | assertEquals(1, s.size()); 39 | } 40 | 41 | @Test 42 | public void testLegacyMainMethodTests() throws Exception { 43 | MainRunner.callMainIfExists(IntRBTreeSet.class, "test", /*num=*/"20", /*seed=*/"423429"); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/IntSemiIndirectHeapsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import static org.junit.Assert.assertArrayEquals; 20 | import static org.junit.Assert.assertEquals; 21 | 22 | import java.util.Arrays; 23 | 24 | import org.junit.Test; 25 | 26 | 27 | public class IntSemiIndirectHeapsTest { 28 | 29 | @Test 30 | public void testFront() { 31 | final int numBits = 20; 32 | int[] refArray = new int[100], heap = new int[100], front = new int[100]; 33 | 34 | for(int i = (1 << numBits) - 1; i-- != 0;) { 35 | for(int j = 0; j < numBits; j++) { 36 | refArray[j] = (i & (1 << j)); 37 | heap[j] = j; 38 | } 39 | 40 | IntSemiIndirectHeaps.makeHeap(refArray, heap, numBits, null); 41 | assertEquals("Heap " + Integer.toBinaryString(i), numBits - Integer.bitCount(i), IntSemiIndirectHeaps.front(refArray, heap, numBits, front)); 42 | } 43 | } 44 | 45 | @Test 46 | public void testFrontWithComparator() { 47 | final int[] refArray = { 8, 16, 9 }; 48 | final int[] heap = { 2, 1, 0 }; 49 | 50 | IntComparator comparator = (k1, k2) -> (k1 & 3) - (k2 & 3); 51 | IntSemiIndirectHeaps.makeHeap(refArray, heap, 3, comparator); 52 | final int[] front = new int[2]; 53 | assertEquals(2, IntSemiIndirectHeaps.front(refArray, heap, 3, front, comparator)); 54 | Arrays.sort(front); 55 | assertArrayEquals(new int[] { 0, 1 }, front); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/IntSetTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | import static org.junit.Assert.assertTrue; 21 | 22 | import org.junit.Test; 23 | 24 | public class IntSetTest { 25 | @Test 26 | public void testOf() { 27 | final IntSet s = IntSet.of(0, 1, 2, 3); 28 | assertEquals(new IntOpenHashSet(new int[] { 0, 1, 2, 3 }), s); 29 | } 30 | 31 | @Test 32 | public void testOfEmpty() { 33 | final IntSet s = IntSet.of(); 34 | assertTrue(s.isEmpty()); 35 | 36 | } 37 | 38 | @Test 39 | public void testOfSingleton() { 40 | final IntSet s = IntSet.of(0); 41 | assertEquals(new IntOpenHashSet(new int[] { 0 }), s); 42 | } 43 | 44 | @Test 45 | public void testOfPair() { 46 | final IntSet s = IntSet.of(0, 1); 47 | assertEquals(new IntOpenHashSet(new int[] { 0, 1 }), s); 48 | } 49 | 50 | @Test 51 | public void testOfTriplet() { 52 | final IntSet s = IntSet.of(0, 1, 2); 53 | assertEquals(new IntOpenHashSet(new int[] { 0, 1, 2 }), s); 54 | } 55 | 56 | @Test(expected = IllegalArgumentException.class) 57 | public void testOfDuplicateThrows() { 58 | IntSet.of(0, 0, 0, 0); 59 | } 60 | 61 | @Test(expected = UnsupportedOperationException.class) 62 | public void testOfImmutable() { 63 | final IntSet l = IntSet.of(0, 1, 2); 64 | l.add(3); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/ints/IntSetsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.ints; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | import static org.junit.Assert.assertFalse; 21 | import static org.junit.Assert.assertNull; 22 | import static org.junit.Assert.assertTrue; 23 | 24 | import org.junit.Test; 25 | 26 | import it.unimi.dsi.fastutil.MainRunner; 27 | 28 | public class IntSetsTest { 29 | @Test 30 | public void testToArrayShouldNullElementAfterLastEntry() { 31 | final IntSet set = IntSets.EMPTY_SET; 32 | final Object[] values = new Object[] { "test" }; 33 | set.toArray(values); 34 | assertNull(values[0]); 35 | } 36 | 37 | @Test 38 | public void testFromTo() { 39 | final IntSet s = IntSets.fromTo(0, 1000); 40 | assertEquals(1000, s.size()); 41 | assertTrue(s.contains(0)); 42 | assertTrue(s.contains(999)); 43 | assertFalse(s.contains(-1)); 44 | assertFalse(s.contains(1000)); 45 | assertEquals(1000, new IntOpenHashSet(s.iterator()).size()); 46 | } 47 | 48 | @Test 49 | public void testTo() { 50 | IntSet s = IntSets.to(0); 51 | assertEquals(Integer.MAX_VALUE, s.size()); 52 | assertTrue(s.contains(-1)); 53 | assertTrue(s.contains(Integer.MIN_VALUE)); 54 | assertFalse(s.contains(0)); 55 | 56 | s = IntSets.to(Integer.MIN_VALUE + 1000); 57 | final IntIterator iterator = s.iterator(); 58 | assertEquals(Integer.MIN_VALUE, iterator.nextInt()); 59 | for (int i = 0; i < 998; i++) iterator.nextInt(); 60 | assertEquals(Integer.MIN_VALUE + 999, iterator.nextInt()); 61 | assertFalse(iterator.hasNext()); 62 | 63 | s = IntSets.to(-2); 64 | assertEquals(Integer.MAX_VALUE - 1, s.size()); 65 | } 66 | 67 | @Test 68 | public void testFrom() { 69 | IntSet s = IntSets.from(0); 70 | assertEquals(Integer.MAX_VALUE, s.size()); 71 | assertFalse(s.contains(-1)); 72 | assertTrue(s.contains(Integer.MAX_VALUE)); 73 | assertTrue(s.contains(0)); 74 | 75 | s = IntSets.from(Integer.MAX_VALUE - 1000); 76 | final IntIterator iterator = s.iterator(); 77 | assertEquals(Integer.MAX_VALUE - 1000, iterator.nextInt()); 78 | for (int i = 0; i < 999; i++) iterator.nextInt(); 79 | assertEquals(Integer.MAX_VALUE, iterator.nextInt()); 80 | assertFalse(iterator.hasNext()); 81 | s = IntSets.from(2); 82 | assertEquals(Integer.MAX_VALUE - 1, s.size()); 83 | 84 | } 85 | 86 | @Test 87 | public void testLegacyMainMethodTests() throws Exception { 88 | MainRunner.callMainIfExists(IntSets.class, "int", /*seed=*/"928374"); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/io/FastByteArrayOutputStreamTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.io; 18 | 19 | 20 | import static org.junit.Assert.assertEquals; 21 | 22 | import java.io.IOException; 23 | 24 | import org.junit.Test; 25 | 26 | public class FastByteArrayOutputStreamTest { 27 | 28 | @SuppressWarnings("resource") 29 | @Test 30 | public void testWrite() { 31 | FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream(); 32 | fbaos.write(1); 33 | fbaos.write(2); 34 | assertEquals(1, fbaos.array[0]); 35 | assertEquals(2, fbaos.array[1]); 36 | assertEquals(2, fbaos.length); 37 | assertEquals(2, fbaos.position()); 38 | fbaos.position(1); 39 | fbaos.write(3); 40 | assertEquals(2, fbaos.position()); 41 | assertEquals(2, fbaos.length); 42 | assertEquals(3, fbaos.array[1]); 43 | fbaos.write(4); 44 | assertEquals(3, fbaos.length); 45 | assertEquals(4, fbaos.array[2]); 46 | 47 | for(int i = 0; i < 14; i++) fbaos.write(i + 10); 48 | assertEquals(17, fbaos.length); 49 | for(int i = 0; i < 14; i++) assertEquals(i + 10, fbaos.array[3 + i]); 50 | } 51 | 52 | @SuppressWarnings("resource") 53 | @Test 54 | public void testWriteArray() throws IOException { 55 | FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream(); 56 | fbaos.write(1); 57 | fbaos.write(2); 58 | fbaos.write(3); 59 | 60 | byte[] a = new byte[14]; 61 | for(int i = 0; i < 14; i++) a[i] = (byte)(i + 10); 62 | fbaos.write(a); 63 | assertEquals(17, fbaos.length); 64 | assertEquals(1, fbaos.array[0]); 65 | assertEquals(2, fbaos.array[1]); 66 | assertEquals(3, fbaos.array[2]); 67 | for(int i = 0; i < 14; i++) assertEquals(i + 10, fbaos.array[3 + i]); 68 | 69 | fbaos.write(a); 70 | assertEquals(31, fbaos.length); 71 | for(int i = 0; i < 14; i++) assertEquals(i + 10, fbaos.array[17 + i]); 72 | 73 | fbaos = new FastByteArrayOutputStream(); 74 | fbaos.write(1); 75 | fbaos.write(2); 76 | fbaos.write(3); 77 | fbaos.position(2); 78 | 79 | fbaos.write(a); 80 | assertEquals(16, fbaos.length); 81 | assertEquals(1, fbaos.array[0]); 82 | assertEquals(2, fbaos.array[1]); 83 | for(int i = 0; i < 14; i++) assertEquals(i + 10, fbaos.array[2 + i]); 84 | 85 | fbaos = new FastByteArrayOutputStream(); 86 | fbaos.write(1); 87 | fbaos.write(2); 88 | fbaos.write(3); 89 | fbaos.write(4); 90 | fbaos.position(3); 91 | 92 | fbaos.write(a); 93 | assertEquals(17, fbaos.length); 94 | assertEquals(1, fbaos.array[0]); 95 | assertEquals(2, fbaos.array[1]); 96 | assertEquals(3, fbaos.array[2]); 97 | for(int i = 0; i < 14; i++) assertEquals(i + 10, fbaos.array[3 + i]); 98 | } 99 | 100 | @SuppressWarnings("resource") 101 | @Test 102 | public void testPositionWrite() { 103 | FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream(); 104 | fbaos.position(1); 105 | fbaos.write(1); 106 | assertEquals(2, fbaos.length); 107 | } 108 | 109 | @SuppressWarnings("resource") 110 | @Test 111 | public void testPositionWrite2() { 112 | FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream(); 113 | fbaos.position(fbaos.array.length + 2); 114 | fbaos.write(1); 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/longs/LongSetTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.longs; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | 21 | import org.junit.Test; 22 | 23 | public class LongSetTest { 24 | 25 | @Test 26 | public void test4() { 27 | assertEquals(new LongOpenHashSet(new long[] { 0, 1, 2, 3, 4 }), LongSet.of(0, 1, 2, 3, 4)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/objects/AbstractObject2IntFunctionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.objects; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | 21 | import org.junit.Test; 22 | 23 | public class AbstractObject2IntFunctionTest { 24 | @SuppressWarnings("deprecation") 25 | @Test 26 | public void testRemove() { 27 | final Object2IntArrayMap a = new Object2IntArrayMap<>(); 28 | final Object key = new Object(); 29 | a.put(key, 1); 30 | assertEquals(Integer.valueOf(1), a.remove(key)); 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/objects/Object2IntArrayMapTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.objects; 18 | 19 | import static org.junit.Assert.assertTrue; 20 | 21 | import org.junit.Test; 22 | 23 | public class Object2IntArrayMapTest { 24 | 25 | @Test 26 | public void testIterator() { 27 | final Object2IntArrayMap testMap = new Object2IntArrayMap<>(); 28 | 29 | testMap.put("A", 10); 30 | testMap.put("B", 10); 31 | final ObjectIterator iterator = testMap.keySet().iterator(); 32 | iterator.next(); 33 | iterator.remove(); 34 | assertTrue(iterator.hasNext()); 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/objects/Object2ObjectOpenHashMapTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.objects; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | import static org.junit.Assert.assertFalse; 21 | import static org.junit.Assert.assertNull; 22 | 23 | import org.junit.Test; 24 | 25 | public class Object2ObjectOpenHashMapTest { 26 | 27 | 28 | @Test 29 | public void testCombinationMethodsWithDefaultValue() { 30 | final Object2ObjectOpenHashMap map = new Object2ObjectOpenHashMap<>(); 31 | map.defaultReturnValue("undefined"); 32 | assertEquals("nak", map.getOrDefault("bar", "nak")); 33 | map.putIfAbsent("foo", "one"); 34 | assertEquals("one", map.get("foo")); 35 | 36 | assertEquals("undefined", map.replace("bar", "two")); 37 | assertFalse(map.containsKey("bar")); 38 | 39 | assertEquals("three", map.computeIfAbsent("qux", s -> "three")); 40 | assertEquals("three", map.get("qux")); 41 | 42 | assertEquals("undefined", map.computeIfPresent("def", (a, b) -> "four")); 43 | assertFalse(map.containsKey("def")); 44 | } 45 | 46 | @Test 47 | public void testCombinationMethodsWithoutDefaultValue() { 48 | final Object2ObjectOpenHashMap map = new Object2ObjectOpenHashMap<>(); 49 | assertEquals("nak", map.getOrDefault("bar", "nak")); 50 | map.putIfAbsent("foo", "one"); 51 | assertEquals("one", map.get("foo")); 52 | 53 | assertNull(map.replace("bar", "two")); 54 | assertFalse(map.containsKey("bar")); 55 | 56 | assertEquals("three", map.computeIfAbsent("qux", s -> "three")); 57 | assertEquals("three", map.get("qux")); 58 | 59 | assertNull(map.computeIfPresent("def", (a, b) -> "four")); 60 | assertFalse(map.containsKey("def")); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/objects/ObjectAVLTreeSetTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.objects; 18 | 19 | 20 | import static org.junit.Assert.assertEquals; 21 | import static org.junit.Assert.assertFalse; 22 | import static org.junit.Assert.assertSame; 23 | import static org.junit.Assert.assertTrue; 24 | 25 | import org.junit.Test; 26 | 27 | import it.unimi.dsi.fastutil.MainRunner; 28 | 29 | public class ObjectAVLTreeSetTest { 30 | 31 | @Test 32 | public void testGet() { 33 | final ObjectAVLTreeSet s = new ObjectAVLTreeSet<>(); 34 | final Integer o = Integer.valueOf(0); 35 | assertTrue(s.isEmpty()); 36 | assertEquals(0, s.size()); 37 | s.add(o); 38 | assertSame(o, s.get(Integer.valueOf(0))); 39 | assertFalse(s.add(o)); 40 | assertEquals(1, s.size()); 41 | } 42 | 43 | @Test 44 | public void testLegacyMainMethodTests() throws Exception { 45 | MainRunner.callMainIfExists(ObjectAVLTreeSet.class, "test", /*num=*/"20", /*seed=*/"423429"); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/objects/ObjectHeapPriorityQueueTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.objects; 18 | 19 | 20 | import static org.junit.Assert.assertEquals; 21 | 22 | import java.io.File; 23 | import java.io.IOException; 24 | 25 | import org.junit.Test; 26 | 27 | import it.unimi.dsi.fastutil.MainRunner; 28 | import it.unimi.dsi.fastutil.io.BinIO; 29 | 30 | public class ObjectHeapPriorityQueueTest { 31 | 32 | @SuppressWarnings({ "unchecked", "boxing" }) 33 | @Test 34 | public void testSerialize() throws IOException, ClassNotFoundException { 35 | ObjectHeapPriorityQueue q = new ObjectHeapPriorityQueue<>(); 36 | for(int i = 0; i < 100; i++) q.enqueue(i); 37 | 38 | File file = File.createTempFile(getClass().getPackage().getName() + "-", "-tmp"); 39 | file.deleteOnExit(); 40 | BinIO.storeObject(q, file); 41 | ObjectHeapPriorityQueue r = (ObjectHeapPriorityQueue)BinIO.loadObject(file); 42 | file.delete(); 43 | for(int i = 0; i < 100; i++) { 44 | assertEquals(q.first(), r.first()); 45 | assertEquals(q.dequeue(), r.dequeue()); 46 | } 47 | } 48 | 49 | @Test 50 | public void testLegacyMainMethodTests() throws Exception { 51 | MainRunner.callMainIfExists(ObjectHeapPriorityQueue.class, "test", /*num=*/"500", /*seed=*/"9329384"); 52 | } 53 | } 54 | 55 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/objects/ObjectImmutableListTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.objects; 18 | 19 | import static org.junit.Assert.assertSame; 20 | 21 | import org.junit.Test; 22 | 23 | public class ObjectImmutableListTest { 24 | @Test 25 | public void testZeroLengthToArray() { 26 | assertSame(ObjectArrays.EMPTY_ARRAY, ObjectImmutableList.of().toArray()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/objects/ObjectListTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.objects; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | import static org.junit.Assert.assertTrue; 21 | 22 | import org.junit.Test; 23 | 24 | public class ObjectListTest { 25 | @Test 26 | public void testOf() { 27 | final ObjectList l = ObjectList.of("0", "1", "2"); 28 | assertEquals(ObjectArrayList.wrap(new String[] { "0", "1", "2" }), l); 29 | } 30 | 31 | @Test 32 | public void testOfEmpty() { 33 | final ObjectList l = ObjectList.of(); 34 | assertTrue(l.isEmpty()); 35 | } 36 | 37 | @Test 38 | public void testOfSingleton() { 39 | final ObjectList l = ObjectList.of("0"); 40 | assertEquals(ObjectArrayList.wrap(new String[] { "0" }), l); 41 | } 42 | 43 | @Test 44 | public void testOfPair() { 45 | final ObjectList l = ObjectList.of("0", "1"); 46 | assertEquals(ObjectArrayList.wrap(new String[] { "0", "1" }), l); 47 | } 48 | 49 | @Test 50 | public void testOfTriplet() { 51 | final ObjectList l = ObjectList.of("0", "1", "2"); 52 | assertEquals(ObjectArrayList.wrap(new String[] { "0", "1", "2" }), l); 53 | } 54 | 55 | @Test(expected = UnsupportedOperationException.class) 56 | public void testOfImmutable() { 57 | final ObjectList l = ObjectList.of("0", "1", "2"); 58 | l.add("3"); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/objects/ObjectOpenCustomHashSetTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.objects; 18 | 19 | 20 | import static org.junit.Assert.assertEquals; 21 | import static org.junit.Assert.assertTrue; 22 | 23 | import java.util.Random; 24 | 25 | import org.junit.Test; 26 | 27 | import it.unimi.dsi.fastutil.Hash; 28 | import it.unimi.dsi.fastutil.MainRunner; 29 | import it.unimi.dsi.fastutil.bytes.ByteArrays; 30 | 31 | public class ObjectOpenCustomHashSetTest { 32 | 33 | @Test 34 | public void testGetNullKey() { 35 | final ObjectOpenCustomHashSet s = new ObjectOpenCustomHashSet<>(new Hash.Strategy() { 36 | 37 | @Override 38 | public int hashCode(Integer o) { 39 | return o == null ? 0 : o.intValue() % 10; 40 | } 41 | 42 | @Override 43 | public boolean equals(Integer a, Integer b) { 44 | if (((a == null) != (b == null)) || a == null) return false; 45 | return ((a.intValue() - b.intValue()) % 10) == 0; 46 | } 47 | }); 48 | 49 | s.add(Integer.valueOf(10)); 50 | assertTrue(s.contains(Integer.valueOf(0))); 51 | assertEquals(10, s.iterator().next().intValue()); 52 | } 53 | 54 | @Test 55 | public void testNullKey() { 56 | Random random = new Random(0); 57 | ObjectOpenCustomHashSet s = new ObjectOpenCustomHashSet<>(ByteArrays.HASH_STRATEGY); 58 | for(int i = 0; i < 1000000; i++) { 59 | byte[] a = new byte[random.nextInt(10)]; 60 | for(int j = a.length; j-- != 0;) a[j] = (byte) random.nextInt(4); 61 | s.add(a); 62 | } 63 | } 64 | 65 | @Test 66 | public void testLegacyMainMethodTests() throws Exception { 67 | MainRunner.callMainIfExists(ObjectOpenCustomHashSet.class, "test", /*num=*/"500", /*loadFactor=*/"0.75", /*seed=*/"3834745"); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/objects/ObjectRBTreeSetTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.objects; 18 | 19 | 20 | import static org.junit.Assert.assertEquals; 21 | import static org.junit.Assert.assertSame; 22 | import static org.junit.Assert.assertFalse; 23 | import static org.junit.Assert.assertTrue; 24 | 25 | import org.junit.Test; 26 | 27 | import it.unimi.dsi.fastutil.MainRunner; 28 | 29 | public class ObjectRBTreeSetTest { 30 | 31 | @Test 32 | public void testAddAndGet() { 33 | final ObjectRBTreeSet s = new ObjectRBTreeSet<>(); 34 | final Integer o = Integer.valueOf(0); 35 | assertTrue(s.isEmpty()); 36 | assertEquals(0, s.size()); 37 | s.add(o); 38 | assertSame(o, s.get(Integer.valueOf(0))); 39 | assertFalse(s.add(o)); 40 | assertEquals(1, s.size()); 41 | } 42 | 43 | @Test 44 | public void testLegacyMainMethodTests() throws Exception { 45 | MainRunner.callMainIfExists(ObjectRBTreeSet.class, "test", /*num=*/"20", /*seed=*/"423429"); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/objects/ObjectSetTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.objects; 18 | 19 | import static org.junit.Assert.assertEquals; 20 | import static org.junit.Assert.assertTrue; 21 | 22 | import org.junit.Test; 23 | 24 | public class ObjectSetTest { 25 | @Test 26 | public void testOf() { 27 | final ObjectSet s = ObjectSet.of("0", "1", "2", "3"); 28 | assertEquals(new ObjectOpenHashSet<>(new String[] { "0", "1", "2", "3" }), s); 29 | } 30 | 31 | @Test 32 | public void testOfEmpty() { 33 | final ObjectSet s = ObjectSet.of(); 34 | assertTrue(s.isEmpty()); 35 | } 36 | 37 | @Test 38 | public void testOfSingleton() { 39 | final ObjectSet s = ObjectSet.of("0"); 40 | assertEquals(new ObjectOpenHashSet<>(new String[] { "0" }), s); 41 | } 42 | 43 | @Test 44 | public void testOfPair() { 45 | final ObjectSet s = ObjectSet.of("0", "1"); 46 | assertEquals(new ObjectOpenHashSet<>(new String[] { "0", "1" }), s); 47 | } 48 | 49 | @Test 50 | public void testOfTriplet() { 51 | final ObjectSet s = ObjectSet.of("0", "1", "2"); 52 | assertEquals(new ObjectOpenHashSet<>(new String[] { "0", "1", "2" }), s); 53 | } 54 | 55 | @Test(expected = IllegalArgumentException.class) 56 | public void testOfDuplicateThrows() { 57 | ObjectSet.of("0", "0", "0", "0"); 58 | } 59 | 60 | @Test(expected = UnsupportedOperationException.class) 61 | public void testOfImmutable() { 62 | final ObjectSet l = ObjectSet.of("0", "1", "2"); 63 | l.add("3"); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/objects/ObjectSetsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.objects; 18 | 19 | 20 | import static org.junit.Assert.assertNull; 21 | 22 | import org.junit.Test; 23 | 24 | import it.unimi.dsi.fastutil.MainRunner; 25 | 26 | public class ObjectSetsTest { 27 | @Test 28 | public void testToArrayShouldNullElementAfterLastEntry() { 29 | ObjectSet set = ObjectSets.EMPTY_SET; 30 | Object[] values = new Object[] { "test" }; 31 | set.toArray(values); 32 | assertNull(values[0]); 33 | } 34 | 35 | @Test 36 | public void testLegacyMainMethodTests() throws Exception { 37 | MainRunner.callMainIfExists(ObjectSets.class, "Object", /*seed=*/"928374"); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /test/it/unimi/dsi/fastutil/objects/ReferenceArraySetTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017-2022 Sebastiano Vigna 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package it.unimi.dsi.fastutil.objects; 18 | 19 | 20 | import static org.junit.Assert.assertEquals; 21 | import static org.junit.Assert.assertFalse; 22 | import static org.junit.Assert.assertTrue; 23 | 24 | import java.io.ByteArrayInputStream; 25 | import java.io.ByteArrayOutputStream; 26 | import java.io.IOException; 27 | import java.io.ObjectOutputStream; 28 | 29 | import org.junit.Test; 30 | 31 | import it.unimi.dsi.fastutil.io.BinIO; 32 | 33 | public class ReferenceArraySetTest { 34 | 35 | @Test 36 | public void testSet() { 37 | for(int i = 0; i <= 2; i++) { 38 | final ReferenceArraySet s = i == 0 ? new ReferenceArraySet<>() : new ReferenceArraySet<>(2); 39 | final Integer one = new Integer(1), two = new Integer(2), three = new Integer(3); 40 | assertTrue(s.add(one)); 41 | assertEquals(1, s.size()); 42 | assertTrue(s.contains(one)); 43 | assertFalse(s.contains(new Integer(1))); 44 | assertTrue(s.add(two)); 45 | assertTrue(s.contains(two)); 46 | assertFalse(s.contains(new Integer(2))); 47 | assertEquals(2, s.size()); 48 | assertFalse(s.add(one)); 49 | assertFalse(s.remove(three)); 50 | assertTrue(s.add(three)); 51 | assertEquals(3, s.size()); 52 | assertTrue(s.contains(one)); 53 | assertTrue(s.contains(two)); 54 | assertTrue(s.contains(three)); 55 | assertEquals(new ReferenceOpenHashSet<>(new Object[] { one, two, three }), new ReferenceOpenHashSet<>(s.iterator())); 56 | assertTrue(s.remove(three)); 57 | assertEquals(2, s.size()); 58 | assertTrue(s.remove(one)); 59 | assertEquals(1, s.size()); 60 | assertFalse(s.contains(one)); 61 | assertTrue(s.remove(two)); 62 | assertEquals(0, s.size()); 63 | assertFalse(s.contains(one)); 64 | } 65 | } 66 | 67 | @Test 68 | public void testClone() { 69 | final ReferenceArraySet s = new ReferenceArraySet<>(); 70 | assertEquals(s, s.clone()); 71 | Integer zero; 72 | s.add(zero = new Integer(0)); 73 | assertEquals(s, s.clone()); 74 | s.add(new Integer(0)); 75 | assertEquals(s, s.clone()); 76 | s.add(new Integer(1)); 77 | assertEquals(s, s.clone()); 78 | s.add(new Integer(2)); 79 | assertEquals(s, s.clone()); 80 | s.remove(zero); 81 | assertEquals(s, s.clone()); 82 | } 83 | 84 | @Test 85 | public void testSerialisation() throws IOException, ClassNotFoundException { 86 | // We can't really test reference maps as equals() doesnt' work 87 | final ObjectArraySet s = new ObjectArraySet<>(); 88 | final ByteArrayOutputStream baos = new ByteArrayOutputStream(); 89 | ObjectOutputStream oos = new ObjectOutputStream(baos); 90 | oos.writeObject(s); 91 | oos.close(); 92 | assertEquals(s, BinIO.loadObject(new ByteArrayInputStream(baos.toByteArray()))); 93 | 94 | s.add(new Integer(0)); 95 | s.add(new Integer(1)); 96 | 97 | baos.reset(); 98 | oos = new ObjectOutputStream(baos); 99 | oos.writeObject(s); 100 | oos.close(); 101 | assertEquals(s, BinIO.loadObject(new ByteArrayInputStream(baos.toByteArray()))); 102 | } 103 | } 104 | --------------------------------------------------------------------------------