├── .gitignore ├── CHANGES.txt ├── INSTALL.txt ├── LICENSE-2.0.txt ├── NOTICE.txt ├── README.md ├── hppcrt-benchmarks ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── carrotsearch │ │ └── hppcrt │ │ ├── BenchmarkSuiteRunner.java │ │ ├── BroadWord.java │ │ ├── DistributionGenerator.java │ │ ├── Util.java │ │ ├── implementations │ │ ├── BigramCountingBase.java │ │ ├── EclipseCollIntIntMap.java │ │ ├── EclipseCollObjectIntMap.java │ │ ├── FastUtilIdentityIntMap.java │ │ ├── FastUtilIntIntMap.java │ │ ├── FastUtilObjectIntMap.java │ │ ├── HashMapImplementations.java │ │ ├── HppcIdentityIntMap.java │ │ ├── HppcIntIntMap.java │ │ ├── HppcIntIntScatterMap.java │ │ ├── HppcObjectIntMap.java │ │ ├── HppcObjectIntScatterMap.java │ │ ├── HppcrtIdentityIntMap.java │ │ ├── HppcrtIntIntMap.java │ │ ├── HppcrtObjectIntMap.java │ │ ├── JavaIntIntMap.java │ │ ├── KolobokeIdentityIntMap.java │ │ ├── KolobokeIntIntMap.java │ │ ├── KolobokeObjectIntMap.java │ │ └── MapImplementation.java │ │ ├── jmh │ │ ├── BenchmarkBigramCounting.java │ │ ├── BenchmarkCollectionsSort.java │ │ ├── BenchmarkHashCollisions.java │ │ ├── BenchmarkHashMapBase.java │ │ ├── BenchmarkHashMapContains.java │ │ ├── BenchmarkHashMapExistingKeysPut.java │ │ ├── BenchmarkHashMapPut.java │ │ ├── BenchmarkHashMapRemove.java │ │ ├── BenchmarkPopCnt.java │ │ ├── IterationSpeedBenchmark.java │ │ └── ObjectArrayListBenchmark.java │ │ └── misc │ │ ├── BenchmarkHashContainersSuite.java │ │ ├── FastUtilOomPut.java │ │ ├── HppcArraysBench.java │ │ ├── HppcHeapsSyntheticBench.java │ │ ├── HppcListSyntheticBench.java │ │ ├── HppcMapSyntheticBench.java │ │ ├── HppcMaxHashMapSize.java │ │ ├── HppcOomPut.java │ │ ├── HppcSortSyntheticBench.java │ │ └── SyntheticBenchmarkSuite.java │ └── resources │ └── books-polish.txt ├── hppcrt-template-processor ├── pom.xml └── src │ ├── main │ ├── antlr4 │ │ └── com │ │ │ └── carrotsearch │ │ │ └── hppcrt │ │ │ └── generator │ │ │ └── parser │ │ │ ├── Java7Lexer.g4 │ │ │ └── Java7Parser.g4 │ └── java │ │ └── com │ │ └── carrotsearch │ │ └── hppcrt │ │ └── generator │ │ ├── ImportDirective.java │ │ ├── InlinedMethodDef.java │ │ ├── OutputFile.java │ │ ├── TemplateFile.java │ │ ├── TemplateNullWriter.java │ │ ├── TemplateOptions.java │ │ ├── TemplateProcessor.java │ │ ├── Type.java │ │ └── parser │ │ ├── Channels.java │ │ ├── Replacement.java │ │ ├── ReplacementVisitorBase.java │ │ ├── SignatureProcessor.java │ │ ├── SignatureReplacementVisitor.java │ │ └── TypeBound.java │ └── test │ ├── java │ └── com │ │ └── carrotsearch │ │ └── hppcrt │ │ └── generator │ │ ├── InlinedMethodDefTest.java │ │ ├── TemplateOptionsTest.java │ │ └── parser │ │ └── SignatureProcessorTest.java │ └── resources │ └── test_cases │ ├── CrashParser.txt │ ├── IteratorPool.test │ ├── IteratorPoolAlloc.test │ ├── IteratorPoolAllocGeneric.ok │ ├── IteratorPoolAllocLong.ok │ ├── IteratorPoolGeneric.ok │ ├── IteratorPoolLong.ok │ ├── KTypeArraysClass.test │ ├── KTypePartialSpecializationClass.test │ ├── KTypeVTypeClass.test │ ├── LongArraysClass.ok │ ├── LongIntClass.ok │ ├── LongObjectClass.ok │ ├── LongPartialSpecializationClass.ok │ ├── ObjectArraysClass.ok │ ├── ObjectLongClass.ok │ └── ObjectPartialSpecializationClass.ok ├── hppcrt ├── build.xml ├── pom.xml └── src │ ├── eclipse │ ├── HPPCRT_process_templates_sources.launch │ ├── HPPCRT_process_templates_tests.launch │ ├── HPPCRT_run_tests_generated.launch │ ├── HPPCRT_run_tests_generated_coverage.launch │ ├── HPPCRT_run_tests_templates.launch │ └── HPPCRT_run_tests_templates_coverage.launch │ ├── main │ ├── java │ │ └── com │ │ │ └── carrotsearch │ │ │ └── hppcrt │ │ │ ├── AbstractIterator.java │ │ │ ├── ArraySizingStrategy.java │ │ │ ├── BitUtil.java │ │ │ ├── BoundedProportionalArraySizingStrategy.java │ │ │ ├── BufferAllocationException.java │ │ │ ├── Containers.java │ │ │ ├── Generated.java │ │ │ ├── HashContainers.java │ │ │ ├── IteratorPool.java │ │ │ ├── ObjectFactory.java │ │ │ ├── ObjectPool.java │ │ │ ├── XorShift128P.java │ │ │ ├── cursors │ │ │ └── package.html │ │ │ ├── hash │ │ │ ├── BitMixer.java │ │ │ ├── MurmurHash3.java │ │ │ ├── PhiMix.java │ │ │ └── package.html │ │ │ ├── heaps │ │ │ └── package.html │ │ │ ├── lists │ │ │ └── package.html │ │ │ ├── maps │ │ │ └── package.html │ │ │ ├── predicates │ │ │ └── package.html │ │ │ ├── procedures │ │ │ └── package.html │ │ │ ├── sets │ │ │ └── package.html │ │ │ ├── sorting │ │ │ ├── IndirectComparator.java │ │ │ ├── IndirectSort.java │ │ │ └── package.html │ │ │ └── strategies │ │ │ └── package.html │ └── templates │ │ └── com │ │ └── carrotsearch │ │ └── hppcrt │ │ ├── AbstractKTypeCollection.java │ │ ├── IntKTypeAssociativeContainer.java │ │ ├── IntKTypeMap.java │ │ ├── Intrinsics.java │ │ ├── KTypeArrays.java │ │ ├── KTypeCollection.java │ │ ├── KTypeContainer.java │ │ ├── KTypeDeque.java │ │ ├── KTypeIndexedContainer.java │ │ ├── KTypeLookupContainer.java │ │ ├── KTypePriorityQueue.java │ │ ├── KTypeSet.java │ │ ├── KTypeVTypeAssociativeContainer.java │ │ ├── KTypeVTypeMap.java │ │ ├── VTypeArrays.java │ │ ├── cursors │ │ ├── IntKTypeCursor.java │ │ ├── KTypeCursor.java │ │ └── KTypeVTypeCursor.java │ │ ├── heaps │ │ ├── KTypeHeapPriorityQueue.java │ │ └── KTypeIndexedHeapPriorityQueue.java │ │ ├── lists │ │ ├── KTypeArrayDeque.java │ │ ├── KTypeArrayList.java │ │ └── KTypeLinkedList.java │ │ ├── maps │ │ ├── KTypeVTypeHashMap.java │ │ └── KTypeVTypeIdentityHashMap.java │ │ ├── predicates │ │ ├── IntKTypePredicate.java │ │ ├── KTypePredicate.java │ │ └── KTypeVTypePredicate.java │ │ ├── procedures │ │ ├── IntKTypeProcedure.java │ │ ├── KTypeProcedure.java │ │ └── KTypeVTypeProcedure.java │ │ ├── sets │ │ ├── KTypeHashSet.java │ │ └── KTypeIdentityHashSet.java │ │ ├── sorting │ │ └── KTypeSort.java │ │ └── strategies │ │ ├── KTypeComparator.java │ │ └── KTypeStandardComparator.java │ └── test │ ├── java │ └── com │ │ └── carrotsearch │ │ └── hppcrt │ │ ├── APIExpectationsTest.java │ │ ├── AbstractIteratorTest.java │ │ ├── ArraysTest.java │ │ ├── BoundedProportionalArraySizingStrategyTest.java │ │ ├── BufferAllocationExceptionTest.java │ │ ├── HashContainersTest.java │ │ ├── NaNCornerCaseTest.java │ │ ├── RequireAssertionsRule.java │ │ ├── StringConversionsTest.java │ │ ├── TestUtils.java │ │ ├── XorShiftRandomTest.java │ │ ├── maps │ │ └── ObjectIntOpenHashMapRegressionTest.java │ │ ├── sets │ │ └── HashCollisionsClusteringTest.java │ │ └── sorting │ │ └── IndirectSortTest.java │ └── templates │ └── com │ └── carrotsearch │ └── hppcrt │ ├── AbstractKTypeTest.java │ ├── AbstractKTypeVTypeTest.java │ ├── IntHolder.java │ ├── KTypeArraysTest.java │ ├── KTypeBufferVisualizer.java │ ├── heaps │ ├── KTypeHeapPriorityQueueTest.java │ └── KTypeIndexedHeapPriorityQueueTest.java │ ├── lists │ ├── AbstractKTypeDequeTest.java │ ├── AbstractKTypeIndexedContainerTest.java │ ├── KTypeArrayDequeAsDequeTest.java │ ├── KTypeArrayDequeAsIndexedContainerTest.java │ ├── KTypeArrayDequeTest.java │ ├── KTypeArrayListTest.java │ ├── KTypeLinkedListAsDequeTest.java │ ├── KTypeLinkedListAsIndexedContainerTest.java │ └── KTypeLinkedListTest.java │ ├── maps │ ├── AbstractKTypeVTypeHashMapTest.java │ ├── KTypeVTypeCustomHashMapTest.java │ ├── KTypeVTypeHashMapTest.java │ └── KTypeVTypeIdentityHashMapTest.java │ ├── sets │ ├── AbstractKTypeHashSetTest.java │ ├── KTypeCustomHashSetTest.java │ ├── KTypeHashSetTest.java │ └── KTypeIdentityHashSetTest.java │ └── sorting │ └── KTypeSortTest.java └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | # Mac specific 2 | .DS_Store 3 | 4 | # Maven 5 | target 6 | *.versionsBackup 7 | 8 | # Eclipse 9 | .classpath 10 | .project 11 | .settings 12 | 13 | # External tools. 14 | .clover 15 | 16 | # Locally generated 17 | .builder.classpath 18 | .benchmark.*.db 19 | *.mv.db 20 | -------------------------------------------------------------------------------- /INSTALL.txt: -------------------------------------------------------------------------------- 1 | Hacking HPPC-RT: 2 | ---------------- 3 | The project as a whole requires Java 8, although the final runtime jar is still Java 5 compatible. To assure such 4 | antiquated compatibility, some restrictions must be enforced: 5 | 6 | * hppcrt (sources) : Runtime sources either templates or hand-coded can be up to Java 7 grammar, but have to use Java 5 API at best. 7 | Then, the Retrolambda plugin translates Java <= 7 language constructs to 1.5 language ones, and finally 8 | the strict 1.5 API compatibility is checked by the animal-sniffer plugin before building the runtime jar. 9 | 10 | * hppcrt (tests): templates and hand-coded tests are allowed to be fully Java 8 compatible, APIs included. 11 | 12 | * hppcrt-template-processor: allowed to be fully Java 8 compatible, APIs included. 13 | * hppcrt-benchmarks: allowed to be fully Java 8 compatible, APIs included. 14 | 15 | 16 | Maven shortcuts: 17 | --------------- 18 | 19 | Note: While executing targets 'compile', 'test', 'package' 20 | Maven console ouputs #parse ERRORs by the logger 'org.slf4j.simpleLogger.log.TemplateProcessor.directive.import'. Those errors denote normal operation. 21 | 22 | # Clean everything 23 | mvn clean 24 | 25 | # Generate Eclipse project files (the setup is a bit complex, so use this!) 26 | mvn clean compile eclipse:clean eclipse:eclipse 27 | 28 | # Compile 29 | mvn clean compile 30 | 31 | # Compile all, run all unit tests 32 | mvn clean test 33 | 34 | # Build sources jar. 35 | mvn source:jar 36 | 37 | # Package all JAR files, builds the benchmarks jar, skip the tests execution. 38 | mvn clean package -Pquick 39 | 40 | # Compile all, run all unit tests, package all JAR files, builds the benchmarks jar, install packages in a local repository. 41 | mvn clean install 42 | 43 | # Compile all, package all JAR files, builds the benchmarks jar, skip the tests execution, install packages in a local repository. 44 | mvn clean install -Pquick 45 | 46 | # Try to resolve all dependencies to be able to work off the Net 47 | mvn dependency:go-offline 48 | 49 | # Use -Pquick to skip tests execution, 50 | # and replace -Pquick by -Pquicknodoc to also skip Javadoc generation. 51 | 52 | # Compile all, create benchmark JAR only 53 | mvn clean package -Pquicknodoc -am -pl :hppcrt-benchmarks 54 | 55 | # List available benchmarks 56 | java -jar hppcrt-benchmarks/target/benchmarks.jar -l 57 | 58 | # Run a particular benchmark: 59 | java -cp hppcrt-benchmarks/target/benchmarks.jar com.carrotsearch.hppcrt.jmh.[benchmark class, see list above] --- --warmup=[nb of warmups] --measure=[nb of measures after warmups] 60 | 61 | # Run a particular benchmark, with overriden @Param values. Add one of more arguments to the command line, such as: 62 | param=value1,value2[...etc] 63 | where param is one of @Param name, with value1,value2,... overriding the alternatives hardcoded in benchmark code. 64 | That way, @Param numbers can be changed, truncated, added, without recompiling. @Param enum values cannot be changed, 65 | but you can restrict the execution to a subset of the existing values, still without recompiling. 66 | 67 | Ex: 68 | java -cp hppcrt-benchmarks/target/benchmarks.jar com.carrotsearch.hppcrt.jmh.BenchmarkHashMapPut --- --warmup=10 --measure=10 loadFactor=0.5,0.8 targetSize=1000000,2000000 implementation=HPPCRT_INT_INT,HPPC_INT_INT,HPPC_SCATTER_INT_INT 69 | 70 | -------------------------------------------------------------------------------- /NOTICE.txt: -------------------------------------------------------------------------------- 1 | ACKNOWLEDGEMENT 2 | =============== 3 | HPPC-RT ("Realtime") 4 | Copyright 2013-2019 Vincent Sonnier 5 | 6 | HPPC-RT borrowed code, ideas or both from: 7 | 8 | * HPPC, http://labs.carrotsearch.com/hppc.html, by Carrot Search s.c., Boznicza 11/57, 61-751 Poznan, Poland. 9 | (Apache license) 10 | * Apache Lucene, http://lucene.apache.org/ 11 | (Apache license) 12 | * Fastutil, http://fastutil.di.unimi.it/ 13 | (Apache license) 14 | * Koloboke, https://github.com/OpenHFT/Koloboke 15 | (Apache license) 16 | * Cliff Moon, https://github.com/moonpolysoft for a Robin Hood hashing pull request ( https://github.com/carrotsearch/hppc/pull/3 ) for HPPC. 17 | (Apache license) 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## HPPC-RT: High Performance Primitive Collections "RealTime" 2 | 3 | This is a fork of HPPC from CarrotSearch, see JavaDoc and documentation at: https://github.com/carrotsearch/hppc/wiki for more info. 4 | 5 | IMPORTANT: Developers - read INSTALL.txt file for proper Eclipse setup and requirements. 6 | 7 | --------------------------------------- 8 | 9 | This fork intend to follow the original HPPC as close as possible while 10 | adding, tweaking, and even modifiying some features, for the goal of improving HPPC realtime behaviour. 11 | Most of the internal modifications were done to effectively remove any temporary Objects creation at runtime, provided 12 | the containers are correctly pre-sized initially. 13 | 14 | ### List of changes: 15 | * Tweaked internal implementation to remove any allocation of temporary Objects at runtime. 16 | 17 | * Ability to pre-allocate any container with a strong guarantee to neither reallocate, nor generating garbage as long as the container 18 | have less than capacity() elements. 19 | 20 | * Pooled, recyclable iterators: ability to use iterators the usual way, without creating iterator instances 21 | dynamically at runtime. 22 | That means in particular using the enhanced for loop without any dynamic allocation underneath. 23 | 24 | * Primitive and Objects in-place array sorts, similar to java.util.Arrays with Comparable and Comparator versions. 25 | 26 | * [Object|primitives]Arrays as a complement of java.util.Arrays for in-place buffer manipulation : reverse, rotate...etc. 27 | 28 | * Double-linked lists, supporting all operations common to lists and dequeues, with rich bi-directional iterating methods. 29 | 30 | * Dedicated in-place sort methods for ArrayLists, LinkedLists, and ArrayDeques. 31 | 32 | * Heaps : Priority queues, and Indexed (a.k.a indirect) priority queues. 33 | 34 | * Custom default values for methods returning default values in some circumstances. (see Javadoc) 35 | 36 | ...and some general performance and API tweaks. 37 | 38 | All these new features are heavily Unit tested, so should work well. 39 | 40 | The Jar is compatible with Java 1.5 and have no external dependencies. 41 | 42 | Stable version is available on Maven: 43 | ```` 44 | 45 | com.github.vsonnier 46 | hppcrt 47 | 0.7.5 48 | 49 | ```` 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/Util.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import java.util.Random; 4 | 5 | 6 | public final class Util 7 | { 8 | 9 | /** 10 | * shuffle array contents 11 | * @param array 12 | * @param rnd 13 | * @return 14 | */ 15 | public static int[] shuffle(final int[] array, final Random rnd) 16 | { 17 | for (int i = array.length - 1; i > 0; i--) 18 | { 19 | final int pos = rnd.nextInt(i + 1); 20 | final int t = array[pos]; 21 | array[pos] = array[i]; 22 | array[i] = t; 23 | } 24 | return array; 25 | } 26 | 27 | /** 28 | * shuffle object array contents 29 | * @param array 30 | * @param rnd 31 | * @return 32 | */ 33 | public static T[] shuffle(final T[] array, final Random rnd) 34 | { 35 | for (int i = array.length - 1; i > 0; i--) 36 | { 37 | final int pos = rnd.nextInt(i + 1); 38 | final T t = array[pos]; 39 | array[pos] = array[i]; 40 | array[i] = t; 41 | } 42 | return array; 43 | } 44 | 45 | /** 46 | * shuffle ObjectIndexedContainer contents 47 | * @param array 48 | * @param rnd 49 | * @return 50 | */ 51 | public static ObjectIndexedContainer shuffle(final ObjectIndexedContainer array, final Random rnd) 52 | { 53 | for (int i = array.size() - 1; i > 0; i--) 54 | { 55 | final int pos = rnd.nextInt(i + 1); 56 | final T t = array.get(pos); 57 | array.set(pos, array.get(i)); 58 | array.set(i, t); 59 | } 60 | return array; 61 | } 62 | 63 | /** 64 | * shuffle IntIndexedContainer contents 65 | * @param array 66 | * @param rnd 67 | * @return 68 | */ 69 | public static IntIndexedContainer shuffle(final IntIndexedContainer array, final Random rnd) 70 | { 71 | for (int i = array.size() - 1; i > 0; i--) 72 | { 73 | final int pos = rnd.nextInt(i + 1); 74 | final int t = array.get(pos); 75 | array.set(pos, array.get(i)); 76 | array.set(i, t); 77 | } 78 | return array; 79 | } 80 | 81 | public static void printHeader(final String msg) 82 | { 83 | System.out.println(); 84 | System.out.println("================================================================================"); 85 | System.out.println(msg); 86 | System.out.println("================================================================================"); 87 | System.out.flush(); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/implementations/EclipseCollIntIntMap.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.implementations; 2 | 3 | 4 | import java.util.Arrays; 5 | import java.util.Random; 6 | 7 | import org.eclipse.collections.impl.map.mutable.primitive.IntIntHashMap; 8 | 9 | import com.carrotsearch.hppcrt.Util; 10 | import com.carrotsearch.hppcrt.XorShift128P; 11 | 12 | public class EclipseCollIntIntMap extends MapImplementation 13 | { 14 | private int[] insertKeys; 15 | private int[] containsKeys; 16 | private int[] removedKeys; 17 | private int[] insertValues; 18 | 19 | public EclipseCollIntIntMap(final int size, final float loadFactor) 20 | { 21 | //load factor is fixed to 0.5 ! 22 | super(new IntIntHashMap(size)); 23 | } 24 | 25 | /** 26 | * Setup 27 | */ 28 | @Override 29 | public void setup(final int[] keysToInsert, final MapImplementation.HASH_QUALITY hashQ, final int[] keysForContainsQuery, final int[] keysForRemovalQuery) { 30 | 31 | final Random prng = new XorShift128P(0x122335577L); 32 | 33 | //make a full copy 34 | this.insertKeys = Arrays.copyOf(keysToInsert, keysToInsert.length); 35 | this.containsKeys = Arrays.copyOf(keysForContainsQuery, keysForContainsQuery.length); 36 | this.removedKeys = Arrays.copyOf(keysForRemovalQuery, keysForRemovalQuery.length); 37 | 38 | this.insertValues = new int[keysToInsert.length]; 39 | 40 | for (int i = 0; i < this.insertValues.length; i++) { 41 | 42 | this.insertValues[i] = prng.nextInt(); 43 | } 44 | } 45 | 46 | @Override 47 | public void clear() { 48 | this.instance.clear(); 49 | } 50 | 51 | @Override 52 | public int size() { 53 | 54 | return this.instance.size(); 55 | } 56 | 57 | @Override 58 | public int benchPutAll() { 59 | 60 | final IntIntHashMap instance = this.instance; 61 | 62 | final int[] values = this.insertValues; 63 | 64 | final int count = 0; 65 | 66 | final int[] keys = this.insertKeys; 67 | 68 | for (int i = 0; i < keys.length; i++) { 69 | 70 | //those ones do not return the previous value.... 71 | instance.put(keys[i], values[i]); 72 | } 73 | 74 | return instance.size(); 75 | } 76 | 77 | @Override 78 | public int benchContainKeys() 79 | { 80 | final IntIntHashMap instance = this.instance; 81 | 82 | int count = 0; 83 | 84 | final int[] keys = this.containsKeys; 85 | 86 | for (int i = 0; i < keys.length; i++) { 87 | 88 | count += instance.containsKey(keys[i]) ? 1 : 0; 89 | } 90 | 91 | return count; 92 | } 93 | 94 | @Override 95 | public int benchRemoveKeys() { 96 | 97 | final IntIntHashMap instance = this.instance; 98 | 99 | final int count = 0; 100 | 101 | final int[] keys = this.removedKeys; 102 | 103 | for (int i = 0; i < keys.length; i++) { 104 | 105 | //those ones do not return the previous value.... 106 | instance.remove(keys[i]); 107 | } 108 | 109 | return instance.size(); 110 | } 111 | 112 | @Override 113 | public void setCopyOfInstance(final MapImplementation toCloneFrom) { 114 | 115 | //no clone, but a copy constructor 116 | this.instance = new IntIntHashMap(((IntIntHashMap) toCloneFrom.instance)); 117 | } 118 | 119 | @Override 120 | public void reshuffleInsertedKeys(final Random rand) { 121 | Util.shuffle(this.insertKeys, rand); 122 | 123 | } 124 | 125 | @Override 126 | public void reshuffleInsertedValues(final Random rand) { 127 | Util.shuffle(this.insertValues, rand); 128 | 129 | } 130 | } -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/implementations/EclipseCollObjectIntMap.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.implementations; 2 | 3 | import java.util.Random; 4 | 5 | import org.eclipse.collections.impl.map.mutable.primitive.ObjectIntHashMap; 6 | 7 | import com.carrotsearch.hppcrt.Util; 8 | import com.carrotsearch.hppcrt.XorShift128P; 9 | 10 | public class EclipseCollObjectIntMap extends MapImplementation> 11 | { 12 | 13 | private ComparableInt[] insertKeys; 14 | private ComparableInt[] containsKeys; 15 | private ComparableInt[] removedKeys; 16 | private int[] insertValues; 17 | 18 | protected EclipseCollObjectIntMap(final int size, final float loadFactor) 19 | { 20 | //load factor is fixed to 0.5 ! 21 | super(new ObjectIntHashMap(size)); 22 | } 23 | 24 | /** 25 | * Setup 26 | */ 27 | @Override 28 | public void setup(final int[] keysToInsert, final MapImplementation.HASH_QUALITY hashQ, final int[] keysForContainsQuery, final int[] keysForRemovalQuery) { 29 | 30 | final Random prng = new XorShift128P(0x122335577L); 31 | 32 | this.insertKeys = new ComparableInt[keysToInsert.length]; 33 | 34 | this.containsKeys = new ComparableInt[keysForContainsQuery.length]; 35 | this.removedKeys = new ComparableInt[keysForRemovalQuery.length]; 36 | 37 | this.insertValues = new int[keysToInsert.length]; 38 | 39 | //Auto box into Integers, they must have the same length anyway. 40 | for (int i = 0; i < keysToInsert.length; i++) { 41 | 42 | this.insertKeys[i] = new ComparableInt(keysToInsert[i], hashQ); 43 | 44 | this.insertValues[i] = prng.nextInt(); 45 | } 46 | 47 | //Auto box into Integers 48 | for (int i = 0; i < keysForContainsQuery.length; i++) { 49 | 50 | this.containsKeys[i] = new ComparableInt(keysForContainsQuery[i], hashQ); 51 | } 52 | 53 | //Auto box into Integers 54 | for (int i = 0; i < keysForRemovalQuery.length; i++) { 55 | 56 | this.removedKeys[i] = new ComparableInt(keysForRemovalQuery[i], hashQ); 57 | } 58 | } 59 | 60 | @Override 61 | public void clear() { 62 | this.instance.clear(); 63 | } 64 | 65 | @Override 66 | public int size() { 67 | 68 | return this.instance.size(); 69 | } 70 | 71 | @Override 72 | public int benchPutAll() { 73 | 74 | final ObjectIntHashMap instance = this.instance; 75 | final int[] values = this.insertValues; 76 | 77 | final int count = 0; 78 | 79 | final ComparableInt[] keys = this.insertKeys; 80 | 81 | for (int i = 0; i < keys.length; i++) { 82 | 83 | instance.put(keys[i], values[i]); 84 | } 85 | 86 | return instance.size(); 87 | } 88 | 89 | @Override 90 | public int benchContainKeys() 91 | { 92 | final ObjectIntHashMap instance = this.instance; 93 | 94 | int count = 0; 95 | 96 | final ComparableInt[] keys = this.containsKeys; 97 | 98 | for (int i = 0; i < keys.length; i++) { 99 | 100 | count += instance.containsKey(keys[i]) ? 1 : 0; 101 | } 102 | 103 | return count; 104 | } 105 | 106 | @Override 107 | public int benchRemoveKeys() { 108 | 109 | final ObjectIntHashMap instance = this.instance; 110 | 111 | final int count = 0; 112 | 113 | final ComparableInt[] keys = this.removedKeys; 114 | 115 | for (int i = 0; i < keys.length; i++) { 116 | 117 | instance.remove(keys[i]); 118 | } 119 | 120 | return instance.size(); 121 | } 122 | 123 | @SuppressWarnings("unchecked") 124 | @Override 125 | public void setCopyOfInstance(final MapImplementation toCloneFrom) { 126 | 127 | //copy constructor 128 | this.instance = new ObjectIntHashMap((ObjectIntHashMap) toCloneFrom.instance); 129 | } 130 | 131 | @Override 132 | public void reshuffleInsertedKeys(final Random rand) { 133 | Util.shuffle(this.insertKeys, rand); 134 | 135 | } 136 | 137 | @Override 138 | public void reshuffleInsertedValues(final Random rand) { 139 | Util.shuffle(this.insertValues, rand); 140 | 141 | } 142 | } -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/implementations/FastUtilIdentityIntMap.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.implementations; 2 | 3 | import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap; 4 | 5 | import java.util.Random; 6 | 7 | import com.carrotsearch.hppcrt.Util; 8 | import com.carrotsearch.hppcrt.XorShift128P; 9 | 10 | public class FastUtilIdentityIntMap extends MapImplementation> 11 | { 12 | private ComparableInt[] insertKeys; 13 | private ComparableInt[] containsKeys; 14 | private ComparableInt[] removedKeys; 15 | private int[] insertValues; 16 | 17 | protected FastUtilIdentityIntMap(final int size, final float loadFactor) 18 | { 19 | super(new Reference2IntOpenHashMap(size, loadFactor)); 20 | } 21 | 22 | /** 23 | * Setup 24 | */ 25 | @Override 26 | public void setup(final int[] keysToInsert, final MapImplementation.HASH_QUALITY hashQ, final int[] keysForContainsQuery, final int[] keysForRemovalQuery) { 27 | 28 | final Random prng = new XorShift128P(0x122335577L); 29 | 30 | this.insertKeys = new ComparableInt[keysToInsert.length]; 31 | 32 | this.containsKeys = new ComparableInt[keysForContainsQuery.length]; 33 | this.removedKeys = new ComparableInt[keysForRemovalQuery.length]; 34 | 35 | this.insertValues = new int[keysToInsert.length]; 36 | 37 | //Auto box into Integers, they must have the same length anyway. 38 | for (int ii = 0; ii < keysToInsert.length; ii++) { 39 | 40 | this.insertKeys[ii] = new ComparableInt(keysToInsert[ii], hashQ); 41 | 42 | this.insertValues[ii] = prng.nextInt(); 43 | } 44 | 45 | //Auto box into Integers, they must have the same length anyway. 46 | for (int ii = 0; ii < keysForContainsQuery.length; ii++) { 47 | 48 | this.containsKeys[ii] = new ComparableInt(keysForContainsQuery[ii], hashQ); 49 | } 50 | 51 | //Auto box into Integers, they must have the same length anyway. 52 | for (int ii = 0; ii < keysForRemovalQuery.length; ii++) { 53 | 54 | this.removedKeys[ii] = new ComparableInt(keysForRemovalQuery[ii], hashQ); 55 | } 56 | } 57 | 58 | @Override 59 | public void clear() { 60 | this.instance.clear(); 61 | } 62 | 63 | @Override 64 | public int size() { 65 | 66 | return this.instance.size(); 67 | } 68 | 69 | @Override 70 | public int benchPutAll() { 71 | 72 | final Reference2IntOpenHashMap instance = this.instance; 73 | final int[] values = this.insertValues; 74 | 75 | int count = 0; 76 | 77 | final ComparableInt[] keys = this.insertKeys; 78 | 79 | for (int i = 0; i < keys.length; i++) { 80 | 81 | count += instance.put(keys[i], values[i]); 82 | } 83 | 84 | return count; 85 | } 86 | 87 | @Override 88 | public int benchContainKeys() 89 | { 90 | final Reference2IntOpenHashMap instance = this.instance; 91 | 92 | int count = 0; 93 | 94 | final ComparableInt[] keys = this.containsKeys; 95 | 96 | for (int i = 0; i < keys.length; i++) { 97 | 98 | count += instance.containsKey(keys[i]) ? 1 : 0; 99 | } 100 | 101 | return count; 102 | } 103 | 104 | @Override 105 | public int benchRemoveKeys() { 106 | 107 | final Reference2IntOpenHashMap instance = this.instance; 108 | 109 | int count = 0; 110 | 111 | final ComparableInt[] keys = this.removedKeys; 112 | 113 | for (int i = 0; i < keys.length; i++) { 114 | 115 | count += instance.removeInt(keys[i]); 116 | } 117 | 118 | return count; 119 | } 120 | 121 | @Override 122 | public boolean isIdentityMap() { 123 | 124 | return true; 125 | } 126 | 127 | @SuppressWarnings("unchecked") 128 | @Override 129 | public void setCopyOfInstance(final MapImplementation toCloneFrom) { 130 | 131 | this.instance = ((Reference2IntOpenHashMap) toCloneFrom.instance).clone(); 132 | 133 | } 134 | 135 | @Override 136 | public void reshuffleInsertedKeys(final Random rand) { 137 | Util.shuffle(this.insertKeys, rand); 138 | 139 | } 140 | 141 | @Override 142 | public void reshuffleInsertedValues(final Random rand) { 143 | Util.shuffle(this.insertValues, rand); 144 | 145 | } 146 | 147 | } -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/implementations/FastUtilIntIntMap.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.implementations; 2 | 3 | import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; 4 | 5 | import java.util.Arrays; 6 | import java.util.Random; 7 | 8 | import com.carrotsearch.hppcrt.Util; 9 | import com.carrotsearch.hppcrt.XorShift128P; 10 | 11 | public class FastUtilIntIntMap extends MapImplementation 12 | { 13 | private int[] insertKeys; 14 | private int[] containsKeys; 15 | private int[] removedKeys; 16 | private int[] insertValues; 17 | 18 | protected FastUtilIntIntMap(final int size, final float loadFactor) 19 | { 20 | super(new Int2IntOpenHashMap( 21 | size, 22 | loadFactor)); 23 | } 24 | 25 | /** 26 | * Setup 27 | */ 28 | @Override 29 | public void setup(final int[] keysToInsert, final MapImplementation.HASH_QUALITY hashQ, final int[] keysForContainsQuery, final int[] keysForRemovalQuery) { 30 | 31 | final Random prng = new XorShift128P(0x122335577L); 32 | 33 | //make a full copy 34 | this.insertKeys = Arrays.copyOf(keysToInsert, keysToInsert.length); 35 | this.containsKeys = Arrays.copyOf(keysForContainsQuery, keysForContainsQuery.length); 36 | this.removedKeys = Arrays.copyOf(keysForRemovalQuery, keysForRemovalQuery.length); 37 | 38 | this.insertValues = new int[keysToInsert.length]; 39 | 40 | for (int i = 0; i < this.insertValues.length; i++) { 41 | 42 | this.insertValues[i] = prng.nextInt(); 43 | } 44 | } 45 | 46 | @Override 47 | public void clear() { 48 | this.instance.clear(); 49 | } 50 | 51 | @Override 52 | public int size() { 53 | 54 | return this.instance.size(); 55 | } 56 | 57 | @Override 58 | public void reshuffleInsertedKeys(final Random rand) { 59 | Util.shuffle(this.insertKeys, rand); 60 | 61 | } 62 | 63 | @Override 64 | public void reshuffleInsertedValues(final Random rand) { 65 | Util.shuffle(this.insertValues, rand); 66 | 67 | } 68 | 69 | @Override 70 | public int benchPutAll() { 71 | 72 | final Int2IntOpenHashMap instance = this.instance; 73 | final int[] values = this.insertValues; 74 | 75 | int count = 0; 76 | 77 | final int[] keys = this.insertKeys; 78 | 79 | for (int i = 0; i < keys.length; i++) { 80 | 81 | count += instance.put(keys[i], values[i]); 82 | } 83 | 84 | return count; 85 | } 86 | 87 | @Override 88 | public int benchContainKeys() 89 | { 90 | final Int2IntOpenHashMap instance = this.instance; 91 | 92 | int count = 0; 93 | 94 | final int[] keys = this.containsKeys; 95 | 96 | for (int i = 0; i < keys.length; i++) { 97 | 98 | count += instance.containsKey(keys[i]) ? 1 : 0; 99 | } 100 | 101 | return count; 102 | } 103 | 104 | @Override 105 | public int benchRemoveKeys() { 106 | 107 | final Int2IntOpenHashMap instance = this.instance; 108 | 109 | int count = 0; 110 | 111 | final int[] keys = this.removedKeys; 112 | 113 | for (int i = 0; i < keys.length; i++) { 114 | 115 | count += instance.remove(keys[i]); 116 | } 117 | 118 | return count; 119 | } 120 | 121 | @Override 122 | public void setCopyOfInstance(final MapImplementation toCloneFrom) { 123 | 124 | this.instance = ((Int2IntOpenHashMap) toCloneFrom.instance).clone(); 125 | 126 | } 127 | } -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/implementations/FastUtilObjectIntMap.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.implementations; 2 | 3 | import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; 4 | 5 | import java.util.Random; 6 | 7 | import com.carrotsearch.hppcrt.Util; 8 | import com.carrotsearch.hppcrt.XorShift128P; 9 | 10 | public class FastUtilObjectIntMap extends MapImplementation> 11 | { 12 | 13 | private ComparableInt[] insertKeys; 14 | private ComparableInt[] containsKeys; 15 | private ComparableInt[] removedKeys; 16 | private int[] insertValues; 17 | 18 | protected FastUtilObjectIntMap(final int size, final float loadFactor) 19 | { 20 | super(new Object2IntOpenHashMap(size, loadFactor)); 21 | } 22 | 23 | /** 24 | * Setup 25 | */ 26 | @Override 27 | public void setup(final int[] keysToInsert, final MapImplementation.HASH_QUALITY hashQ, final int[] keysForContainsQuery, final int[] keysForRemovalQuery) { 28 | 29 | final Random prng = new XorShift128P(0x122335577L); 30 | 31 | this.insertKeys = new ComparableInt[keysToInsert.length]; 32 | 33 | this.containsKeys = new ComparableInt[keysForContainsQuery.length]; 34 | this.removedKeys = new ComparableInt[keysForRemovalQuery.length]; 35 | 36 | this.insertValues = new int[keysToInsert.length]; 37 | 38 | //Auto box into Integers, they must have the same length anyway. 39 | for (int i = 0; i < keysToInsert.length; i++) { 40 | 41 | this.insertKeys[i] = new ComparableInt(keysToInsert[i], hashQ); 42 | 43 | this.insertValues[i] = prng.nextInt(); 44 | } 45 | 46 | //Auto box into Integers 47 | for (int i = 0; i < keysForContainsQuery.length; i++) { 48 | 49 | this.containsKeys[i] = new ComparableInt(keysForContainsQuery[i], hashQ); 50 | } 51 | 52 | //Auto box into Integers 53 | for (int i = 0; i < keysForRemovalQuery.length; i++) { 54 | 55 | this.removedKeys[i] = new ComparableInt(keysForRemovalQuery[i], hashQ); 56 | } 57 | } 58 | 59 | @Override 60 | public void clear() { 61 | this.instance.clear(); 62 | } 63 | 64 | @Override 65 | public int size() { 66 | 67 | return this.instance.size(); 68 | } 69 | 70 | @Override 71 | public int benchPutAll() { 72 | 73 | final Object2IntOpenHashMap instance = this.instance; 74 | final int[] values = this.insertValues; 75 | 76 | int count = 0; 77 | 78 | final ComparableInt[] keys = this.insertKeys; 79 | 80 | for (int i = 0; i < keys.length; i++) { 81 | 82 | count += instance.put(keys[i], values[i]); 83 | } 84 | 85 | return count; 86 | } 87 | 88 | @Override 89 | public int benchContainKeys() 90 | { 91 | final Object2IntOpenHashMap instance = this.instance; 92 | 93 | int count = 0; 94 | 95 | final ComparableInt[] keys = this.containsKeys; 96 | 97 | for (int i = 0; i < keys.length; i++) { 98 | 99 | count += instance.containsKey(keys[i]) ? 1 : 0; 100 | } 101 | 102 | return count; 103 | } 104 | 105 | @Override 106 | public int benchRemoveKeys() { 107 | 108 | final Object2IntOpenHashMap instance = this.instance; 109 | 110 | int count = 0; 111 | 112 | final ComparableInt[] keys = this.removedKeys; 113 | 114 | for (int i = 0; i < keys.length; i++) { 115 | 116 | count += instance.removeInt(keys[i]); 117 | } 118 | 119 | return count; 120 | } 121 | 122 | @SuppressWarnings("unchecked") 123 | @Override 124 | public void setCopyOfInstance(final MapImplementation toCloneFrom) { 125 | 126 | this.instance = ((Object2IntOpenHashMap) toCloneFrom.instance).clone(); 127 | 128 | } 129 | 130 | @Override 131 | public void reshuffleInsertedKeys(final Random rand) { 132 | Util.shuffle(this.insertKeys, rand); 133 | 134 | } 135 | 136 | @Override 137 | public void reshuffleInsertedValues(final Random rand) { 138 | Util.shuffle(this.insertValues, rand); 139 | 140 | } 141 | } -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/implementations/HppcIdentityIntMap.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.implementations; 2 | 3 | import java.util.Random; 4 | 5 | import com.carrotsearch.hppc.ObjectIntIdentityHashMap; 6 | import com.carrotsearch.hppcrt.Util; 7 | import com.carrotsearch.hppcrt.XorShift128P; 8 | 9 | public class HppcIdentityIntMap extends MapImplementation> 10 | { 11 | private ComparableInt[] insertKeys; 12 | private ComparableInt[] containsKeys; 13 | private ComparableInt[] removedKeys; 14 | private int[] insertValues; 15 | 16 | protected HppcIdentityIntMap(final int size, final float loadFactor) 17 | { 18 | super(new ObjectIntIdentityHashMap(size, loadFactor)); 19 | } 20 | 21 | /** 22 | * Setup 23 | */ 24 | @Override 25 | public void setup(final int[] keysToInsert, final MapImplementation.HASH_QUALITY hashQ, final int[] keysForContainsQuery, final int[] keysForRemovalQuery) { 26 | 27 | final Random prng = new XorShift128P(0x122335577L); 28 | 29 | this.insertKeys = new ComparableInt[keysToInsert.length]; 30 | 31 | this.containsKeys = new ComparableInt[keysForContainsQuery.length]; 32 | this.removedKeys = new ComparableInt[keysForRemovalQuery.length]; 33 | 34 | this.insertValues = new int[keysToInsert.length]; 35 | 36 | //Auto box into Integers, they must have the same length anyway. 37 | for (int ii = 0; ii < keysToInsert.length; ii++) { 38 | 39 | this.insertKeys[ii] = new ComparableInt(keysToInsert[ii], hashQ); 40 | 41 | this.insertValues[ii] = prng.nextInt(); 42 | } 43 | 44 | //Auto box into Integers, they must have the same length anyway. 45 | for (int ii = 0; ii < keysForContainsQuery.length; ii++) { 46 | 47 | this.containsKeys[ii] = new ComparableInt(keysForContainsQuery[ii], hashQ); 48 | } 49 | 50 | //Auto box into Integers, they must have the same length anyway. 51 | for (int ii = 0; ii < keysForRemovalQuery.length; ii++) { 52 | 53 | this.removedKeys[ii] = new ComparableInt(keysForRemovalQuery[ii], hashQ); 54 | } 55 | } 56 | 57 | @Override 58 | public void clear() { 59 | this.instance.clear(); 60 | } 61 | 62 | @Override 63 | public int size() { 64 | 65 | return this.instance.size(); 66 | } 67 | 68 | @Override 69 | public int benchPutAll() { 70 | 71 | final ObjectIntIdentityHashMap instance = this.instance; 72 | final int[] values = this.insertValues; 73 | 74 | int count = 0; 75 | 76 | final ComparableInt[] keys = this.insertKeys; 77 | 78 | for (int i = 0; i < keys.length; i++) { 79 | 80 | count += instance.put(keys[i], values[i]); 81 | } 82 | 83 | return count; 84 | } 85 | 86 | @Override 87 | public int benchContainKeys() { 88 | final ObjectIntIdentityHashMap instance = this.instance; 89 | 90 | int count = 0; 91 | 92 | final ComparableInt[] keys = this.containsKeys; 93 | 94 | for (int i = 0; i < keys.length; i++) { 95 | 96 | count += instance.containsKey(keys[i]) ? 1 : 0; 97 | } 98 | 99 | return count; 100 | } 101 | 102 | @Override 103 | public int benchRemoveKeys() { 104 | 105 | final ObjectIntIdentityHashMap instance = this.instance; 106 | 107 | int count = 0; 108 | 109 | final ComparableInt[] keys = this.removedKeys; 110 | 111 | for (int i = 0; i < keys.length; i++) { 112 | 113 | count += instance.remove(keys[i]); 114 | } 115 | 116 | return count; 117 | } 118 | 119 | @Override 120 | public boolean isIdentityMap() { 121 | 122 | return true; 123 | } 124 | 125 | @SuppressWarnings("unchecked") 126 | @Override 127 | public void setCopyOfInstance(final MapImplementation toCloneFrom) { 128 | 129 | this.instance = (ObjectIntIdentityHashMap) ((ObjectIntIdentityHashMap) toCloneFrom.instance).clone(); 130 | 131 | } 132 | 133 | @Override 134 | public void reshuffleInsertedKeys(final Random rand) { 135 | Util.shuffle(this.insertKeys, rand); 136 | 137 | } 138 | 139 | @Override 140 | public void reshuffleInsertedValues(final Random rand) { 141 | Util.shuffle(this.insertValues, rand); 142 | 143 | } 144 | } -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/implementations/HppcIntIntMap.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.implementations; 2 | 3 | import java.util.Arrays; 4 | import java.util.Random; 5 | 6 | import com.carrotsearch.hppc.IntIntHashMap; 7 | import com.carrotsearch.hppcrt.Util; 8 | import com.carrotsearch.hppcrt.XorShift128P; 9 | 10 | public class HppcIntIntMap extends MapImplementation 11 | { 12 | private int[] insertKeys; 13 | private int[] containsKeys; 14 | private int[] removedKeys; 15 | private int[] insertValues; 16 | 17 | protected HppcIntIntMap(final int size, final float loadFactor) { 18 | super(new IntIntHashMap(size, loadFactor)); 19 | } 20 | 21 | /** 22 | * Setup 23 | */ 24 | @Override 25 | public void setup(final int[] keysToInsert, final MapImplementation.HASH_QUALITY hashQ, final int[] keysForContainsQuery, final int[] keysForRemovalQuery) { 26 | 27 | final Random prng = new XorShift128P(0x122335577L); 28 | 29 | //make a full copy 30 | this.insertKeys = Arrays.copyOf(keysToInsert, keysToInsert.length); 31 | this.containsKeys = Arrays.copyOf(keysForContainsQuery, keysForContainsQuery.length); 32 | this.removedKeys = Arrays.copyOf(keysForRemovalQuery, keysForRemovalQuery.length); 33 | 34 | this.insertValues = new int[keysToInsert.length]; 35 | 36 | for (int i = 0; i < this.insertValues.length; i++) { 37 | 38 | this.insertValues[i] = prng.nextInt(); 39 | } 40 | } 41 | 42 | @Override 43 | public void clear() { 44 | this.instance.clear(); 45 | } 46 | 47 | @Override 48 | public int size() { 49 | 50 | return this.instance.size(); 51 | } 52 | 53 | @Override 54 | public int benchPutAll() { 55 | 56 | final IntIntHashMap instance = this.instance; 57 | final int[] values = this.insertValues; 58 | 59 | int count = 0; 60 | 61 | final int[] keys = this.insertKeys; 62 | 63 | for (int i = 0; i < keys.length; i++) { 64 | 65 | count += instance.put(keys[i], values[i]); 66 | } 67 | 68 | return count; 69 | } 70 | 71 | @Override 72 | public int benchContainKeys() { 73 | final IntIntHashMap instance = this.instance; 74 | 75 | int count = 0; 76 | 77 | final int[] keys = this.containsKeys; 78 | 79 | for (int i = 0; i < keys.length; i++) { 80 | 81 | count += instance.containsKey(keys[i]) ? 1 : 0; 82 | } 83 | 84 | return count; 85 | } 86 | 87 | @Override 88 | public int benchRemoveKeys() { 89 | 90 | final IntIntHashMap instance = this.instance; 91 | 92 | int count = 0; 93 | 94 | final int[] keys = this.removedKeys; 95 | 96 | for (int i = 0; i < keys.length; i++) { 97 | 98 | count += instance.remove(keys[i]); 99 | } 100 | 101 | return count; 102 | } 103 | 104 | @Override 105 | public void setCopyOfInstance(final MapImplementation toCloneFrom) { 106 | 107 | this.instance = ((IntIntHashMap) toCloneFrom.instance).clone(); 108 | 109 | } 110 | 111 | @Override 112 | public void reshuffleInsertedKeys(final Random rand) { 113 | Util.shuffle(this.insertKeys, rand); 114 | 115 | } 116 | 117 | @Override 118 | public void reshuffleInsertedValues(final Random rand) { 119 | Util.shuffle(this.insertValues, rand); 120 | 121 | } 122 | } -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/implementations/HppcIntIntScatterMap.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.implementations; 2 | 3 | import java.util.Arrays; 4 | import java.util.Random; 5 | 6 | import com.carrotsearch.hppc.IntIntHashMap; 7 | import com.carrotsearch.hppc.IntIntScatterMap; 8 | import com.carrotsearch.hppcrt.Util; 9 | import com.carrotsearch.hppcrt.XorShift128P; 10 | 11 | public class HppcIntIntScatterMap extends MapImplementation 12 | { 13 | private int[] insertKeys; 14 | private int[] containsKeys; 15 | private int[] removedKeys; 16 | private int[] insertValues; 17 | 18 | protected HppcIntIntScatterMap(final int size, final float loadFactor) { 19 | super(new IntIntScatterMap(size, loadFactor)); 20 | } 21 | 22 | /** 23 | * Setup 24 | */ 25 | @Override 26 | public void setup(final int[] keysToInsert, final MapImplementation.HASH_QUALITY hashQ, final int[] keysForContainsQuery, final int[] keysForRemovalQuery) { 27 | 28 | final Random prng = new XorShift128P(0x122335577L); 29 | 30 | //make a full copy 31 | this.insertKeys = Arrays.copyOf(keysToInsert, keysToInsert.length); 32 | this.containsKeys = Arrays.copyOf(keysForContainsQuery, keysForContainsQuery.length); 33 | this.removedKeys = Arrays.copyOf(keysForRemovalQuery, keysForRemovalQuery.length); 34 | 35 | this.insertValues = new int[keysToInsert.length]; 36 | 37 | for (int i = 0; i < this.insertValues.length; i++) { 38 | 39 | this.insertValues[i] = prng.nextInt(); 40 | } 41 | } 42 | 43 | @Override 44 | public void clear() { 45 | this.instance.clear(); 46 | } 47 | 48 | @Override 49 | public int size() { 50 | 51 | return this.instance.size(); 52 | } 53 | 54 | @Override 55 | public int benchPutAll() { 56 | 57 | final IntIntHashMap instance = this.instance; 58 | final int[] values = this.insertValues; 59 | 60 | int count = 0; 61 | 62 | final int[] keys = this.insertKeys; 63 | 64 | for (int i = 0; i < keys.length; i++) { 65 | 66 | count += instance.put(keys[i], values[i]); 67 | } 68 | 69 | return count; 70 | } 71 | 72 | @Override 73 | public int benchContainKeys() { 74 | final IntIntHashMap instance = this.instance; 75 | 76 | int count = 0; 77 | 78 | final int[] keys = this.containsKeys; 79 | 80 | for (int i = 0; i < keys.length; i++) { 81 | 82 | count += instance.containsKey(keys[i]) ? 1 : 0; 83 | } 84 | 85 | return count; 86 | } 87 | 88 | @Override 89 | public int benchRemoveKeys() { 90 | 91 | final IntIntHashMap instance = this.instance; 92 | 93 | int count = 0; 94 | 95 | final int[] keys = this.removedKeys; 96 | 97 | for (int i = 0; i < keys.length; i++) { 98 | 99 | count += instance.remove(keys[i]); 100 | } 101 | 102 | return count; 103 | } 104 | 105 | @Override 106 | public void setCopyOfInstance(final MapImplementation toCloneFrom) { 107 | 108 | this.instance = (IntIntScatterMap) ((IntIntScatterMap) toCloneFrom.instance).clone(); 109 | 110 | } 111 | 112 | @Override 113 | public void reshuffleInsertedKeys(final Random rand) { 114 | Util.shuffle(this.insertKeys, rand); 115 | 116 | } 117 | 118 | @Override 119 | public void reshuffleInsertedValues(final Random rand) { 120 | Util.shuffle(this.insertValues, rand); 121 | 122 | } 123 | } -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/implementations/HppcObjectIntMap.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.implementations; 2 | 3 | import java.util.Random; 4 | 5 | import com.carrotsearch.hppc.ObjectIntHashMap; 6 | import com.carrotsearch.hppcrt.Util; 7 | import com.carrotsearch.hppcrt.XorShift128P; 8 | 9 | public class HppcObjectIntMap extends MapImplementation> 10 | { 11 | 12 | private ComparableInt[] insertKeys; 13 | private ComparableInt[] containsKeys; 14 | private ComparableInt[] removedKeys; 15 | private int[] insertValues; 16 | 17 | protected HppcObjectIntMap(final int size, final float loadFactor) { 18 | super(new ObjectIntHashMap(size, loadFactor)); 19 | } 20 | 21 | /** 22 | * Setup 23 | */ 24 | @Override 25 | public void setup(final int[] keysToInsert, final MapImplementation.HASH_QUALITY hashQ, final int[] keysForContainsQuery, final int[] keysForRemovalQuery) { 26 | 27 | final Random prng = new XorShift128P(0x122335577L); 28 | 29 | this.insertKeys = new ComparableInt[keysToInsert.length]; 30 | 31 | this.containsKeys = new ComparableInt[keysForContainsQuery.length]; 32 | this.removedKeys = new ComparableInt[keysForRemovalQuery.length]; 33 | 34 | this.insertValues = new int[keysToInsert.length]; 35 | 36 | //Auto box into Integers, they must have the same length anyway. 37 | for (int i = 0; i < keysToInsert.length; i++) { 38 | 39 | this.insertKeys[i] = new ComparableInt(keysToInsert[i], hashQ); 40 | 41 | this.insertValues[i] = prng.nextInt(); 42 | } 43 | 44 | //Auto box into Integers 45 | for (int i = 0; i < keysForContainsQuery.length; i++) { 46 | 47 | this.containsKeys[i] = new ComparableInt(keysForContainsQuery[i], hashQ); 48 | } 49 | 50 | //Auto box into Integers 51 | for (int i = 0; i < keysForRemovalQuery.length; i++) { 52 | 53 | this.removedKeys[i] = new ComparableInt(keysForRemovalQuery[i], hashQ); 54 | } 55 | } 56 | 57 | @Override 58 | public void clear() { 59 | this.instance.clear(); 60 | } 61 | 62 | @Override 63 | public int size() { 64 | 65 | return this.instance.size(); 66 | } 67 | 68 | @Override 69 | public int benchPutAll() { 70 | 71 | final ObjectIntHashMap instance = this.instance; 72 | final int[] values = this.insertValues; 73 | 74 | int count = 0; 75 | 76 | final ComparableInt[] keys = this.insertKeys; 77 | 78 | for (int i = 0; i < keys.length; i++) { 79 | 80 | count += instance.put(keys[i], values[i]); 81 | } 82 | 83 | return count; 84 | } 85 | 86 | @Override 87 | public int benchContainKeys() { 88 | final ObjectIntHashMap instance = this.instance; 89 | 90 | int count = 0; 91 | 92 | final ComparableInt[] keys = this.containsKeys; 93 | 94 | for (int i = 0; i < keys.length; i++) { 95 | 96 | count += instance.containsKey(keys[i]) ? 1 : 0; 97 | } 98 | 99 | return count; 100 | } 101 | 102 | @Override 103 | public int benchRemoveKeys() { 104 | 105 | final ObjectIntHashMap instance = this.instance; 106 | 107 | int count = 0; 108 | 109 | final ComparableInt[] keys = this.removedKeys; 110 | 111 | for (int i = 0; i < keys.length; i++) { 112 | 113 | count += instance.remove(keys[i]); 114 | } 115 | 116 | return count; 117 | } 118 | 119 | @SuppressWarnings("unchecked") 120 | @Override 121 | public void setCopyOfInstance(final MapImplementation toCloneFrom) { 122 | 123 | this.instance = ((ObjectIntHashMap) toCloneFrom.instance).clone(); 124 | 125 | } 126 | 127 | @Override 128 | public void reshuffleInsertedKeys(final Random rand) { 129 | Util.shuffle(this.insertKeys, rand); 130 | 131 | } 132 | 133 | @Override 134 | public void reshuffleInsertedValues(final Random rand) { 135 | Util.shuffle(this.insertValues, rand); 136 | 137 | } 138 | } -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/implementations/HppcObjectIntScatterMap.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.implementations; 2 | 3 | import java.util.Random; 4 | 5 | import com.carrotsearch.hppc.ObjectIntScatterMap; 6 | import com.carrotsearch.hppcrt.Util; 7 | import com.carrotsearch.hppcrt.XorShift128P; 8 | 9 | public class HppcObjectIntScatterMap extends MapImplementation> 10 | { 11 | 12 | private ComparableInt[] insertKeys; 13 | private ComparableInt[] containsKeys; 14 | private ComparableInt[] removedKeys; 15 | private int[] insertValues; 16 | 17 | protected HppcObjectIntScatterMap(final int size, final float loadFactor) { 18 | super(new ObjectIntScatterMap(size, loadFactor)); 19 | } 20 | 21 | /** 22 | * Setup 23 | */ 24 | @Override 25 | public void setup(final int[] keysToInsert, final MapImplementation.HASH_QUALITY hashQ, final int[] keysForContainsQuery, final int[] keysForRemovalQuery) { 26 | 27 | final Random prng = new XorShift128P(0x122335577L); 28 | 29 | this.insertKeys = new ComparableInt[keysToInsert.length]; 30 | 31 | this.containsKeys = new ComparableInt[keysForContainsQuery.length]; 32 | this.removedKeys = new ComparableInt[keysForRemovalQuery.length]; 33 | 34 | this.insertValues = new int[keysToInsert.length]; 35 | 36 | //Auto box into Integers, they must have the same length anyway. 37 | for (int i = 0; i < keysToInsert.length; i++) { 38 | 39 | this.insertKeys[i] = new ComparableInt(keysToInsert[i], hashQ); 40 | 41 | this.insertValues[i] = prng.nextInt(); 42 | } 43 | 44 | //Auto box into Integers 45 | for (int i = 0; i < keysForContainsQuery.length; i++) { 46 | 47 | this.containsKeys[i] = new ComparableInt(keysForContainsQuery[i], hashQ); 48 | } 49 | 50 | //Auto box into Integers 51 | for (int i = 0; i < keysForRemovalQuery.length; i++) { 52 | 53 | this.removedKeys[i] = new ComparableInt(keysForRemovalQuery[i], hashQ); 54 | } 55 | } 56 | 57 | @Override 58 | public void clear() { 59 | this.instance.clear(); 60 | } 61 | 62 | @Override 63 | public int size() { 64 | 65 | return this.instance.size(); 66 | } 67 | 68 | @Override 69 | public int benchPutAll() { 70 | 71 | final ObjectIntScatterMap instance = this.instance; 72 | final int[] values = this.insertValues; 73 | 74 | int count = 0; 75 | 76 | final ComparableInt[] keys = this.insertKeys; 77 | 78 | for (int i = 0; i < keys.length; i++) { 79 | 80 | count += instance.put(keys[i], values[i]); 81 | } 82 | 83 | return count; 84 | } 85 | 86 | @Override 87 | public int benchContainKeys() { 88 | final ObjectIntScatterMap instance = this.instance; 89 | 90 | int count = 0; 91 | 92 | final ComparableInt[] keys = this.containsKeys; 93 | 94 | for (int i = 0; i < keys.length; i++) { 95 | 96 | count += instance.containsKey(keys[i]) ? 1 : 0; 97 | } 98 | 99 | return count; 100 | } 101 | 102 | @Override 103 | public int benchRemoveKeys() { 104 | 105 | final ObjectIntScatterMap instance = this.instance; 106 | 107 | int count = 0; 108 | 109 | final ComparableInt[] keys = this.removedKeys; 110 | 111 | for (int i = 0; i < keys.length; i++) { 112 | 113 | count += instance.remove(keys[i]); 114 | } 115 | 116 | return count; 117 | } 118 | 119 | @SuppressWarnings("unchecked") 120 | @Override 121 | public void setCopyOfInstance(final MapImplementation toCloneFrom) { 122 | 123 | this.instance = (ObjectIntScatterMap) ((ObjectIntScatterMap) toCloneFrom.instance).clone(); 124 | 125 | } 126 | 127 | @Override 128 | public void reshuffleInsertedKeys(final Random rand) { 129 | Util.shuffle(this.insertKeys, rand); 130 | 131 | } 132 | 133 | @Override 134 | public void reshuffleInsertedValues(final Random rand) { 135 | Util.shuffle(this.insertValues, rand); 136 | 137 | } 138 | } -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/implementations/HppcrtIdentityIntMap.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.implementations; 2 | 3 | import java.util.Random; 4 | 5 | import com.carrotsearch.hppcrt.Util; 6 | import com.carrotsearch.hppcrt.XorShift128P; 7 | import com.carrotsearch.hppcrt.maps.ObjectIntIdentityHashMap; 8 | 9 | public class HppcrtIdentityIntMap extends MapImplementation> 10 | { 11 | private ComparableInt[] insertKeys; 12 | private ComparableInt[] containsKeys; 13 | private ComparableInt[] removedKeys; 14 | private int[] insertValues; 15 | 16 | protected HppcrtIdentityIntMap(final int size, final float loadFactor) 17 | { 18 | super(new ObjectIntIdentityHashMap(size, loadFactor)); 19 | } 20 | 21 | /** 22 | * Setup 23 | */ 24 | @Override 25 | public void setup(final int[] keysToInsert, final MapImplementation.HASH_QUALITY hashQ, final int[] keysForContainsQuery, final int[] keysForRemovalQuery) { 26 | 27 | final Random prng = new XorShift128P(0x122335577L); 28 | 29 | this.insertKeys = new ComparableInt[keysToInsert.length]; 30 | 31 | this.containsKeys = new ComparableInt[keysForContainsQuery.length]; 32 | this.removedKeys = new ComparableInt[keysForRemovalQuery.length]; 33 | 34 | this.insertValues = new int[keysToInsert.length]; 35 | 36 | //Auto box into Integers, they must have the same length anyway. 37 | for (int ii = 0; ii < keysToInsert.length; ii++) { 38 | 39 | this.insertKeys[ii] = new ComparableInt(keysToInsert[ii], hashQ); 40 | 41 | this.insertValues[ii] = prng.nextInt(); 42 | } 43 | 44 | //Auto box into Integers, they must have the same length anyway. 45 | for (int ii = 0; ii < keysForContainsQuery.length; ii++) { 46 | 47 | this.containsKeys[ii] = new ComparableInt(keysForContainsQuery[ii], hashQ); 48 | } 49 | 50 | //Auto box into Integers, they must have the same length anyway. 51 | for (int ii = 0; ii < keysForRemovalQuery.length; ii++) { 52 | 53 | this.removedKeys[ii] = new ComparableInt(keysForRemovalQuery[ii], hashQ); 54 | } 55 | } 56 | 57 | @Override 58 | public void clear() { 59 | this.instance.clear(); 60 | } 61 | 62 | @Override 63 | public int size() { 64 | 65 | return this.instance.size(); 66 | } 67 | 68 | @Override 69 | public int benchPutAll() { 70 | 71 | final ObjectIntIdentityHashMap instance = this.instance; 72 | final int[] values = this.insertValues; 73 | 74 | int count = 0; 75 | 76 | final ComparableInt[] keys = this.insertKeys; 77 | 78 | for (int i = 0; i < keys.length; i++) { 79 | 80 | count += instance.put(keys[i], values[i]); 81 | } 82 | 83 | return count; 84 | } 85 | 86 | @Override 87 | public int benchContainKeys() 88 | { 89 | final ObjectIntIdentityHashMap instance = this.instance; 90 | 91 | int count = 0; 92 | 93 | final ComparableInt[] keys = this.containsKeys; 94 | 95 | for (int i = 0; i < keys.length; i++) { 96 | 97 | count += instance.containsKey(keys[i]) ? 1 : 0; 98 | } 99 | 100 | return count; 101 | } 102 | 103 | @Override 104 | public int benchRemoveKeys() { 105 | 106 | final ObjectIntIdentityHashMap instance = this.instance; 107 | 108 | int count = 0; 109 | 110 | final ComparableInt[] keys = this.removedKeys; 111 | 112 | for (int i = 0; i < keys.length; i++) { 113 | 114 | count += instance.remove(keys[i]); 115 | } 116 | 117 | return count; 118 | } 119 | 120 | @Override 121 | public boolean isIdentityMap() { 122 | 123 | return true; 124 | } 125 | 126 | @SuppressWarnings("unchecked") 127 | @Override 128 | public void setCopyOfInstance(final MapImplementation toCloneFrom) { 129 | 130 | this.instance = ((ObjectIntIdentityHashMap) toCloneFrom.instance).clone(); 131 | 132 | } 133 | 134 | @Override 135 | public void reshuffleInsertedKeys(final Random rand) { 136 | Util.shuffle(this.insertKeys, rand); 137 | 138 | } 139 | 140 | @Override 141 | public void reshuffleInsertedValues(final Random rand) { 142 | Util.shuffle(this.insertValues, rand); 143 | 144 | } 145 | } -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/implementations/HppcrtIntIntMap.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.implementations; 2 | 3 | import java.util.Arrays; 4 | import java.util.Random; 5 | 6 | import com.carrotsearch.hppcrt.Util; 7 | import com.carrotsearch.hppcrt.XorShift128P; 8 | import com.carrotsearch.hppcrt.maps.IntIntHashMap; 9 | 10 | public class HppcrtIntIntMap extends MapImplementation 11 | { 12 | private int[] insertKeys; 13 | private int[] containsKeys; 14 | private int[] removedKeys; 15 | private int[] insertValues; 16 | 17 | protected HppcrtIntIntMap(final int size, final float loadFactor) 18 | { 19 | super(new IntIntHashMap(size, loadFactor)); 20 | } 21 | 22 | /** 23 | * Setup 24 | */ 25 | @Override 26 | public void setup(final int[] keysToInsert, final MapImplementation.HASH_QUALITY hashQ, final int[] keysForContainsQuery, final int[] keysForRemovalQuery) { 27 | 28 | final Random prng = new XorShift128P(0x122335577L); 29 | 30 | //make a full copy 31 | this.insertKeys = Arrays.copyOf(keysToInsert, keysToInsert.length); 32 | this.containsKeys = Arrays.copyOf(keysForContainsQuery, keysForContainsQuery.length); 33 | this.removedKeys = Arrays.copyOf(keysForRemovalQuery, keysForRemovalQuery.length); 34 | 35 | this.insertValues = new int[keysToInsert.length]; 36 | 37 | for (int i = 0; i < this.insertValues.length; i++) { 38 | 39 | this.insertValues[i] = prng.nextInt(); 40 | } 41 | } 42 | 43 | @Override 44 | public void clear() { 45 | this.instance.clear(); 46 | } 47 | 48 | @Override 49 | public int size() { 50 | 51 | return this.instance.size(); 52 | } 53 | 54 | @Override 55 | public int benchPutAll() { 56 | 57 | final IntIntHashMap instance = this.instance; 58 | final int[] values = this.insertValues; 59 | 60 | int count = 0; 61 | 62 | final int[] keys = this.insertKeys; 63 | 64 | for (int i = 0; i < keys.length; i++) { 65 | 66 | count += instance.put(keys[i], values[i]); 67 | } 68 | 69 | return count; 70 | } 71 | 72 | @Override 73 | public int benchContainKeys() 74 | { 75 | final IntIntHashMap instance = this.instance; 76 | 77 | int count = 0; 78 | 79 | final int[] keys = this.containsKeys; 80 | 81 | for (int i = 0; i < keys.length; i++) { 82 | 83 | count += instance.containsKey(keys[i]) ? 1 : 0; 84 | } 85 | 86 | return count; 87 | } 88 | 89 | @Override 90 | public int benchRemoveKeys() { 91 | 92 | final IntIntHashMap instance = this.instance; 93 | 94 | int count = 0; 95 | 96 | final int[] keys = this.removedKeys; 97 | 98 | for (int i = 0; i < keys.length; i++) { 99 | 100 | count += instance.remove(keys[i]); 101 | } 102 | 103 | return count; 104 | } 105 | 106 | @Override 107 | public void setCopyOfInstance(final MapImplementation toCloneFrom) { 108 | 109 | this.instance = ((IntIntHashMap) toCloneFrom.instance).clone(); 110 | 111 | } 112 | 113 | @Override 114 | public void reshuffleInsertedKeys(final Random rand) { 115 | Util.shuffle(this.insertKeys, rand); 116 | 117 | } 118 | 119 | @Override 120 | public void reshuffleInsertedValues(final Random rand) { 121 | Util.shuffle(this.insertValues, rand); 122 | 123 | } 124 | } -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/implementations/HppcrtObjectIntMap.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.implementations; 2 | 3 | import java.util.Random; 4 | 5 | import com.carrotsearch.hppcrt.Util; 6 | import com.carrotsearch.hppcrt.XorShift128P; 7 | import com.carrotsearch.hppcrt.maps.ObjectIntHashMap; 8 | 9 | public class HppcrtObjectIntMap extends MapImplementation> 10 | { 11 | 12 | private ComparableInt[] insertKeys; 13 | private ComparableInt[] containsKeys; 14 | private ComparableInt[] removedKeys; 15 | private int[] insertValues; 16 | 17 | protected HppcrtObjectIntMap(final int size, final float loadFactor) 18 | { 19 | super(new ObjectIntHashMap(size, loadFactor)); 20 | } 21 | 22 | /** 23 | * Setup 24 | */ 25 | @Override 26 | public void setup(final int[] keysToInsert, final MapImplementation.HASH_QUALITY hashQ, final int[] keysForContainsQuery, final int[] keysForRemovalQuery) { 27 | 28 | final Random prng = new XorShift128P(0x122335577L); 29 | 30 | this.insertKeys = new ComparableInt[keysToInsert.length]; 31 | 32 | this.containsKeys = new ComparableInt[keysForContainsQuery.length]; 33 | this.removedKeys = new ComparableInt[keysForRemovalQuery.length]; 34 | 35 | this.insertValues = new int[keysToInsert.length]; 36 | 37 | //Auto box into Integers, they must have the same length anyway. 38 | for (int i = 0; i < keysToInsert.length; i++) { 39 | 40 | this.insertKeys[i] = new ComparableInt(keysToInsert[i], hashQ); 41 | 42 | this.insertValues[i] = prng.nextInt(); 43 | } 44 | 45 | //Auto box into Integers 46 | for (int i = 0; i < keysForContainsQuery.length; i++) { 47 | 48 | this.containsKeys[i] = new ComparableInt(keysForContainsQuery[i], hashQ); 49 | } 50 | 51 | //Auto box into Integers 52 | for (int i = 0; i < keysForRemovalQuery.length; i++) { 53 | 54 | this.removedKeys[i] = new ComparableInt(keysForRemovalQuery[i], hashQ); 55 | } 56 | } 57 | 58 | @Override 59 | public void clear() { 60 | this.instance.clear(); 61 | } 62 | 63 | @Override 64 | public int size() { 65 | 66 | return this.instance.size(); 67 | } 68 | 69 | @Override 70 | public int benchPutAll() { 71 | 72 | final ObjectIntHashMap instance = this.instance; 73 | final int[] values = this.insertValues; 74 | 75 | int count = 0; 76 | 77 | final ComparableInt[] keys = this.insertKeys; 78 | 79 | for (int i = 0; i < keys.length; i++) { 80 | 81 | count += instance.put(keys[i], values[i]); 82 | } 83 | 84 | return count; 85 | } 86 | 87 | @Override 88 | public int benchContainKeys() 89 | { 90 | final ObjectIntHashMap instance = this.instance; 91 | 92 | int count = 0; 93 | 94 | final ComparableInt[] keys = this.containsKeys; 95 | 96 | for (int i = 0; i < keys.length; i++) { 97 | 98 | count += instance.containsKey(keys[i]) ? 1 : 0; 99 | } 100 | 101 | return count; 102 | } 103 | 104 | @Override 105 | public int benchRemoveKeys() { 106 | 107 | final ObjectIntHashMap instance = this.instance; 108 | 109 | int count = 0; 110 | 111 | final ComparableInt[] keys = this.removedKeys; 112 | 113 | for (int i = 0; i < keys.length; i++) { 114 | 115 | count += instance.remove(keys[i]); 116 | } 117 | 118 | return count; 119 | } 120 | 121 | @SuppressWarnings("unchecked") 122 | @Override 123 | public void setCopyOfInstance(final MapImplementation toCloneFrom) { 124 | 125 | this.instance = ((ObjectIntHashMap) toCloneFrom.instance).clone(); 126 | 127 | } 128 | 129 | @Override 130 | public void reshuffleInsertedKeys(final Random rand) { 131 | Util.shuffle(this.insertKeys, rand); 132 | 133 | } 134 | 135 | @Override 136 | public void reshuffleInsertedValues(final Random rand) { 137 | Util.shuffle(this.insertValues, rand); 138 | 139 | } 140 | } -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/implementations/JavaIntIntMap.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.implementations; 2 | 3 | import java.util.HashMap; 4 | import java.util.Random; 5 | 6 | import com.carrotsearch.hppcrt.Util; 7 | import com.carrotsearch.hppcrt.XorShift128P; 8 | 9 | public class JavaIntIntMap extends MapImplementation> 10 | { 11 | private Integer[] insertKeys; 12 | private Integer[] insertValues; 13 | private Integer[] containsKeys; 14 | private Integer[] removedKeys; 15 | 16 | public JavaIntIntMap(final int size, final float loadFactor) 17 | { 18 | super(new HashMap(size, loadFactor)); 19 | } 20 | 21 | /** 22 | * Setup 23 | */ 24 | @SuppressWarnings("boxing") 25 | @Override 26 | public void setup(final int[] keysToInsert, final MapImplementation.HASH_QUALITY hashQ, final int[] keysForContainsQuery, final int[] keysForRemovalQuery) { 27 | 28 | final Random prng = new XorShift128P(0x122335577L); 29 | 30 | this.insertKeys = new Integer[keysToInsert.length]; 31 | this.insertValues = new Integer[keysToInsert.length]; 32 | 33 | this.containsKeys = new Integer[keysForContainsQuery.length]; 34 | this.removedKeys = new Integer[keysForRemovalQuery.length]; 35 | 36 | //Auto box into Integers 37 | for (int ii = 0; ii < keysToInsert.length; ii++) { 38 | 39 | //autoboxing occurs here 40 | this.insertKeys[ii] = keysToInsert[ii]; 41 | this.insertValues[ii] = prng.nextInt(); 42 | } 43 | 44 | //Auto box into Integers 45 | for (int ii = 0; ii < keysForContainsQuery.length; ii++) { 46 | //autoboxing occurs here 47 | this.containsKeys[ii] = keysForContainsQuery[ii]; 48 | } 49 | 50 | //Auto box into Integers 51 | for (int ii = 0; ii < keysForRemovalQuery.length; ii++) { 52 | //autoboxing occurs here 53 | this.removedKeys[ii] = keysForRemovalQuery[ii]; 54 | } 55 | } 56 | 57 | @Override 58 | public void clear() { 59 | this.instance.clear(); 60 | } 61 | 62 | @Override 63 | public int size() { 64 | 65 | return this.instance.size(); 66 | } 67 | 68 | @Override 69 | public int benchPutAll() { 70 | 71 | final HashMap instance = this.instance; 72 | 73 | int count = 0; 74 | 75 | final Integer[] keys = this.insertKeys; 76 | final Integer[] values = this.insertValues; 77 | 78 | for (int i = 0; i < keys.length; i++) { 79 | 80 | count += (instance.put(keys[i], values[i]) != null) ? 1 : 0; 81 | } 82 | 83 | return count; 84 | } 85 | 86 | @Override 87 | public int benchContainKeys() 88 | { 89 | final HashMap instance = this.instance; 90 | 91 | int count = 0; 92 | 93 | final Integer[] keys = this.containsKeys; 94 | 95 | for (int i = 0; i < keys.length; i++) { 96 | 97 | count += instance.containsKey(keys[i]) ? 1 : 0; 98 | } 99 | 100 | return count; 101 | } 102 | 103 | @Override 104 | public int benchRemoveKeys() { 105 | 106 | final HashMap instance = this.instance; 107 | 108 | int count = 0; 109 | 110 | final Integer[] keys = this.removedKeys; 111 | 112 | for (int i = 0; i < keys.length; i++) { 113 | 114 | count += (instance.remove(keys[i]) != null) ? 1 : 0; 115 | } 116 | 117 | return count; 118 | } 119 | 120 | @SuppressWarnings("unchecked") 121 | @Override 122 | public void setCopyOfInstance(final MapImplementation toCloneFrom) { 123 | 124 | //copy constructor 125 | this.instance = new HashMap((HashMap) (toCloneFrom.instance)); 126 | 127 | } 128 | 129 | @Override 130 | public void reshuffleInsertedKeys(final Random rand) { 131 | Util.shuffle(this.insertKeys, rand); 132 | 133 | } 134 | 135 | @Override 136 | public void reshuffleInsertedValues(final Random rand) { 137 | Util.shuffle(this.insertValues, rand); 138 | 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/implementations/KolobokeIntIntMap.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.implementations; 2 | 3 | import java.util.Arrays; 4 | import java.util.Random; 5 | 6 | import com.koloboke.collect.hash.HashConfig; 7 | import com.koloboke.collect.map.hash.HashIntIntMap; 8 | import com.koloboke.collect.map.hash.HashIntIntMaps; 9 | import com.koloboke.collect.map.hash.HashObjIntMap; 10 | 11 | import com.carrotsearch.hppcrt.Util; 12 | import com.carrotsearch.hppcrt.XorShift128P; 13 | 14 | public class KolobokeIntIntMap extends MapImplementation 15 | { 16 | private int[] insertKeys; 17 | private int[] containsKeys; 18 | private int[] removedKeys; 19 | private int[] insertValues; 20 | 21 | private final int size; 22 | private final float loadFactor; 23 | 24 | public KolobokeIntIntMap(final int size, final float loadFactor) 25 | { 26 | super(HashIntIntMaps.getDefaultFactory(). 27 | withHashConfig(HashConfig.fromLoads(loadFactor / 2, loadFactor, loadFactor)).newMutableMap(size)); 28 | this.size = size; 29 | this.loadFactor = loadFactor; 30 | } 31 | 32 | /** 33 | * Setup 34 | */ 35 | @Override 36 | public void setup(final int[] keysToInsert, final MapImplementation.HASH_QUALITY hashQ, final int[] keysForContainsQuery, final int[] keysForRemovalQuery) { 37 | 38 | final Random prng = new XorShift128P(0x122335577L); 39 | 40 | //make a full copy 41 | this.insertKeys = Arrays.copyOf(keysToInsert, keysToInsert.length); 42 | this.containsKeys = Arrays.copyOf(keysForContainsQuery, keysForContainsQuery.length); 43 | this.removedKeys = Arrays.copyOf(keysForRemovalQuery, keysForRemovalQuery.length); 44 | 45 | this.insertValues = new int[keysToInsert.length]; 46 | 47 | for (int i = 0; i < this.insertValues.length; i++) { 48 | 49 | this.insertValues[i] = prng.nextInt(); 50 | } 51 | } 52 | 53 | @Override 54 | public void clear() { 55 | this.instance.clear(); 56 | } 57 | 58 | @Override 59 | public int size() { 60 | 61 | return this.instance.size(); 62 | } 63 | 64 | @Override 65 | public int benchPutAll() { 66 | 67 | final HashIntIntMap instance = this.instance; 68 | final int[] values = this.insertValues; 69 | 70 | int count = 0; 71 | 72 | final int[] keys = this.insertKeys; 73 | 74 | for (int i = 0; i < keys.length; i++) { 75 | 76 | count += instance.put(keys[i], values[i]); 77 | } 78 | 79 | return count; 80 | } 81 | 82 | @Override 83 | public int benchContainKeys() 84 | { 85 | final HashIntIntMap instance = this.instance; 86 | 87 | int count = 0; 88 | 89 | final int[] keys = this.containsKeys; 90 | 91 | for (int i = 0; i < keys.length; i++) { 92 | 93 | count += instance.containsKey(keys[i]) ? 1 : 0; 94 | } 95 | 96 | return count; 97 | } 98 | 99 | @Override 100 | public int benchRemoveKeys() { 101 | 102 | final HashIntIntMap instance = this.instance; 103 | 104 | int count = 0; 105 | 106 | final int[] keys = this.removedKeys; 107 | 108 | for (int i = 0; i < keys.length; i++) { 109 | 110 | count += instance.remove(keys[i]); 111 | } 112 | 113 | return count; 114 | } 115 | 116 | @Override 117 | public void setCopyOfInstance(final MapImplementation toCloneFrom) { 118 | 119 | final HashIntIntMap sourceCopy = (HashIntIntMap) (toCloneFrom.instance); 120 | 121 | //copy constructor 122 | this.instance = HashIntIntMaps.getDefaultFactory(). 123 | withHashConfig(HashConfig.fromLoads(this.loadFactor / 2, this.loadFactor, this.loadFactor)).newMutableMap(sourceCopy); 124 | 125 | } 126 | 127 | @Override 128 | public void reshuffleInsertedKeys(final Random rand) { 129 | Util.shuffle(this.insertKeys, rand); 130 | 131 | } 132 | 133 | @Override 134 | public void reshuffleInsertedValues(final Random rand) { 135 | Util.shuffle(this.insertValues, rand); 136 | 137 | } 138 | } -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/jmh/BenchmarkBigramCounting.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.jmh; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | import org.openjdk.jmh.annotations.Benchmark; 6 | import org.openjdk.jmh.annotations.OutputTimeUnit; 7 | import org.openjdk.jmh.annotations.Param; 8 | import org.openjdk.jmh.annotations.Scope; 9 | import org.openjdk.jmh.annotations.Setup; 10 | import org.openjdk.jmh.annotations.State; 11 | import org.openjdk.jmh.runner.RunnerException; 12 | 13 | import com.carrotsearch.hppcrt.BenchmarkSuiteRunner; 14 | import com.carrotsearch.hppcrt.implementations.BigramCountingBase; 15 | 16 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 17 | @State(Scope.Thread) 18 | public class BenchmarkBigramCounting 19 | { 20 | private BigramCountingBase bc; 21 | 22 | @Param 23 | public Library library; 24 | 25 | public static enum Library 26 | { 27 | HPPC, 28 | FASTUTIL_OPEN, 29 | FASTUTIL_LINKED, 30 | JAVA_NAIVE, 31 | JAVA_SMART 32 | } 33 | 34 | @Setup 35 | public void setUp() throws Exception 36 | { 37 | this.bc = new BigramCountingBase(); 38 | this.bc.prepareData(); 39 | } 40 | 41 | @Benchmark 42 | public int timeLibrary() 43 | { 44 | int count = 0; 45 | 46 | switch (this.library) 47 | { 48 | case HPPC: 49 | count += this.bc.hppc(); 50 | break; 51 | case FASTUTIL_LINKED: 52 | this.bc.fastutilLinkedOpenHashMap(); 53 | break; 54 | case FASTUTIL_OPEN: 55 | count += this.bc.fastutilOpenHashMap(); 56 | break; 57 | case JAVA_NAIVE: 58 | count += this.bc.jcfNaive(); 59 | break; 60 | case JAVA_SMART: 61 | count += this.bc.jcfSmarter(); 62 | break; 63 | default: 64 | break; 65 | } 66 | 67 | return count; 68 | } 69 | 70 | public static void main(final String[] args) throws RunnerException 71 | { 72 | BenchmarkSuiteRunner.runJmhBasicBenchmarkWithCommandLine(BenchmarkBigramCounting.class, args, 500, 1000); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/jmh/BenchmarkHashMapContains.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.jmh; 2 | 3 | import java.util.Arrays; 4 | 5 | import org.openjdk.jmh.annotations.Benchmark; 6 | import org.openjdk.jmh.annotations.Param; 7 | import org.openjdk.jmh.annotations.Setup; 8 | import org.openjdk.jmh.runner.RunnerException; 9 | 10 | import com.carrotsearch.hppcrt.BenchmarkSuiteRunner; 11 | import com.carrotsearch.hppcrt.Util; 12 | 13 | /** 14 | * Benchmark putting a given number of integers / Objects into a hashmap. 15 | */ 16 | public class BenchmarkHashMapContains extends BenchmarkHashMapBase 17 | { 18 | public enum MAP_LOOKUP_TEST 19 | { 20 | TRUE, 21 | MIXED, 22 | FALSE 23 | } 24 | 25 | @Param 26 | MAP_LOOKUP_TEST lookupSuccessKind; 27 | 28 | private int[] containsKeys; 29 | 30 | public BenchmarkHashMapContains() { 31 | super(); 32 | } 33 | 34 | /** 35 | * 36 | */ 37 | //Since Get is read-only for the map, it is fine to setup() only once 38 | //this time. 39 | @Setup 40 | public void setUp() throws Exception 41 | { 42 | setUpCommon(); 43 | 44 | //Generate a series of containsKeys 45 | this.containsKeys = Arrays.copyOf(this.pushedKeys, this.pushedKeys.length); 46 | 47 | //Shuffle order, so the contains test order is different from 48 | //the insertion order. 49 | Util.shuffle(this.containsKeys, this.prng); 50 | 51 | for (int ii = 0; ii < this.pushedKeys.length; ii++) 52 | { 53 | final boolean isMixedLookupSucceded = (this.lookupSuccessKind == MAP_LOOKUP_TEST.MIXED) && this.prng.nextBoolean(); 54 | 55 | if (this.lookupSuccessKind == MAP_LOOKUP_TEST.TRUE || isMixedLookupSucceded) 56 | { 57 | //do nothing, this.containsKeys[ii] will succeed. 58 | } 59 | else if (this.lookupSuccessKind == MAP_LOOKUP_TEST.FALSE || !isMixedLookupSucceded) 60 | { 61 | //this element may not be in the set: patch the place with a random value, 62 | //so that it is very unlikely for the key to be in the map. 63 | this.containsKeys[ii] = this.prng.nextInt(); 64 | } 65 | } 66 | 67 | //call setup of impl 68 | this.impl.setup(this.pushedKeys, this.hash_quality, this.containsKeys, this.containsKeys); 69 | 70 | //Fill the map, using the putAll 71 | this.impl.benchPutAll(); 72 | } 73 | 74 | /** 75 | * Time the 'contains' operation. 76 | */ 77 | @Benchmark 78 | public int timeContains() 79 | { 80 | return this.impl.benchContainKeys(); 81 | } 82 | 83 | public static void main(final String[] args) throws RunnerException 84 | { 85 | BenchmarkSuiteRunner.runJmhBasicBenchmarkWithCommandLine(BenchmarkHashMapContains.class, args, 2000, 3000); 86 | } 87 | } -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/jmh/BenchmarkHashMapExistingKeysPut.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.jmh; 2 | 3 | import org.openjdk.jmh.annotations.Benchmark; 4 | import org.openjdk.jmh.annotations.Level; 5 | import org.openjdk.jmh.annotations.Setup; 6 | import org.openjdk.jmh.runner.RunnerException; 7 | 8 | import com.carrotsearch.hppcrt.BenchmarkSuiteRunner; 9 | import com.carrotsearch.hppcrt.Util; 10 | 11 | /** 12 | * Benchmark putting a given number of integers / Objects into a hashmap, 13 | * in the case of the keys already exist in the map : this bench try to measure 14 | * the search-and-replace-value performance. 15 | */ 16 | 17 | public class BenchmarkHashMapExistingKeysPut extends BenchmarkHashMapBase 18 | { 19 | public BenchmarkHashMapExistingKeysPut() { 20 | super(); 21 | } 22 | 23 | //Setup - once here. 24 | @Setup 25 | public void setUp() throws Exception { 26 | setUpCommon(); 27 | 28 | //call setup of impl 29 | this.impl.setup(this.pushedKeys, this.hash_quality, this.pushedKeys, this.pushedKeys); 30 | 31 | //Fill it using an initial PutAll 32 | this.impl.benchPutAll(); 33 | } 34 | 35 | //Per-invocation setup here, because we must re-shuffle the map keys 36 | //at each iteration so better exclude it from measurement. 37 | @Setup(Level.Invocation) 38 | public void setUpPerInvocation() throws Exception { 39 | 40 | //Shuffle order, so the put() (replace value only) test order is different from 41 | //the previous put() order 42 | this.impl.reshuffleInsertedKeys(this.prng); 43 | this.impl.reshuffleInsertedValues(this.prng); 44 | } 45 | 46 | 47 | /** 48 | * Time the 'put' operation, of existing keys, reshuffle each time. 49 | */ 50 | @Benchmark 51 | public int timeExistingKeysPut() { 52 | 53 | return this.impl.benchPutAll(); 54 | } 55 | 56 | public static void main(final String[] args) throws RunnerException { 57 | BenchmarkSuiteRunner.runJmhBasicBenchmarkWithCommandLine(BenchmarkHashMapExistingKeysPut.class, args, 2000, 3000); 58 | } 59 | } -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/jmh/BenchmarkHashMapPut.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.jmh; 2 | 3 | import org.openjdk.jmh.annotations.Benchmark; 4 | import org.openjdk.jmh.annotations.Setup; 5 | import org.openjdk.jmh.runner.RunnerException; 6 | 7 | import com.carrotsearch.hppcrt.BenchmarkSuiteRunner; 8 | 9 | /** 10 | * Benchmark putting a given number of integers / Objects into a hashmap 11 | */ 12 | public class BenchmarkHashMapPut extends BenchmarkHashMapBase 13 | { 14 | 15 | //fine to Setup once here, but don't forget to clear at beginning of timePut() ! 16 | @Setup 17 | public void setUp() throws Exception 18 | { 19 | setUpCommon(); 20 | 21 | //call setup of impl, only put setup is OK. 22 | this.impl.setup(this.pushedKeys, this.hash_quality, this.pushedKeys, this.pushedKeys); 23 | } 24 | 25 | /** 26 | * Time the 'put' operation. 27 | */ 28 | @Benchmark 29 | public int timePut() 30 | { 31 | this.impl.clear(); 32 | 33 | return this.impl.benchPutAll(); 34 | } 35 | 36 | public static void main(final String[] args) throws RunnerException 37 | { 38 | BenchmarkSuiteRunner.runJmhBasicBenchmarkWithCommandLine(BenchmarkHashMapPut.class, args, 2000, 3000); 39 | } 40 | } -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/jmh/BenchmarkHashMapRemove.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.jmh; 2 | 3 | import java.util.Arrays; 4 | 5 | import org.openjdk.jmh.annotations.Benchmark; 6 | import org.openjdk.jmh.annotations.Level; 7 | import org.openjdk.jmh.annotations.Param; 8 | import org.openjdk.jmh.annotations.Setup; 9 | import org.openjdk.jmh.runner.RunnerException; 10 | 11 | import com.carrotsearch.hppcrt.BenchmarkSuiteRunner; 12 | import com.carrotsearch.hppcrt.Util; 13 | import com.carrotsearch.hppcrt.implementations.MapImplementation; 14 | 15 | /** 16 | * Benchmark putting a given number of integers / Objects into a hashmap. 17 | * also the base class for all the other Hash benchmarks. 18 | */ 19 | public class BenchmarkHashMapRemove extends BenchmarkHashMapBase 20 | { 21 | 22 | public enum MAP_LOOKUP_TEST 23 | { 24 | TRUE, 25 | MIXED, 26 | FALSE 27 | } 28 | 29 | @Param 30 | MAP_LOOKUP_TEST lookupSuccessKind; 31 | 32 | private int[] removedKeys; 33 | 34 | public BenchmarkHashMapRemove() { 35 | super(); 36 | } 37 | 38 | // This Setup part is only done once 39 | @Setup 40 | public void initialSetUp() throws Exception 41 | { 42 | setUpCommon(); 43 | 44 | //Generate a series of containsKeys // B) Process by get/contains 45 | this.removedKeys = Arrays.copyOf(this.pushedKeys, this.pushedKeys.length); 46 | 47 | //Shuffle order, so the contains test order is different from 48 | //the insertion order. 49 | Util.shuffle(this.removedKeys, this.prng); 50 | 51 | for (int ii = 0; ii < this.pushedKeys.length; ii++) 52 | { 53 | final boolean isMixedLookupSucceded = (this.lookupSuccessKind == MAP_LOOKUP_TEST.MIXED) && this.prng.nextBoolean(); 54 | 55 | if (this.lookupSuccessKind == MAP_LOOKUP_TEST.TRUE || isMixedLookupSucceded) 56 | { 57 | //do nothing, this.removedKeys[ii] will succeed. 58 | } 59 | else if (this.lookupSuccessKind == MAP_LOOKUP_TEST.FALSE || !isMixedLookupSucceded) 60 | { 61 | //this element may not be in the set: patch the place with a random value, 62 | //so that it is very unlikely for the key to be in the map. 63 | this.removedKeys[ii] = this.prng.nextInt(); 64 | } 65 | } 66 | 67 | //call setup of impl, this is the reference implementation, that will stay inchanged. 68 | this.impl.setup(this.pushedKeys, this.hash_quality, this.removedKeys, this.removedKeys); 69 | 70 | this.impl2.setup(this.pushedKeys, this.hash_quality, this.removedKeys, this.removedKeys); 71 | 72 | //Fill it using PutAll 73 | this.impl.benchPutAll(); 74 | } 75 | 76 | //Per-invocation setup here, because we must re-fill the map 77 | //at each iteration so better exclude it from measurement. 78 | @Setup(Level.Invocation) 79 | public void setUp() throws Exception 80 | { 81 | this.impl2.setCopyOfInstance(this.impl); 82 | } 83 | 84 | /** 85 | * Time the 'remove' operation. 86 | */ 87 | @Benchmark 88 | public int timeRemove() 89 | { 90 | return this.impl2.benchRemoveKeys(); 91 | } 92 | 93 | public static void main(final String[] args) throws RunnerException 94 | { 95 | BenchmarkSuiteRunner.runJmhBasicBenchmarkWithCommandLine(BenchmarkHashMapRemove.class, args, 2000, 3000); 96 | } 97 | } -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/jmh/BenchmarkPopCnt.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.jmh; 2 | 3 | import java.util.Arrays; 4 | import java.util.Random; 5 | import java.util.concurrent.TimeUnit; 6 | 7 | import org.openjdk.jmh.annotations.Benchmark; 8 | import org.openjdk.jmh.annotations.OutputTimeUnit; 9 | import org.openjdk.jmh.annotations.Param; 10 | import org.openjdk.jmh.annotations.Scope; 11 | import org.openjdk.jmh.annotations.Setup; 12 | import org.openjdk.jmh.annotations.State; 13 | import org.openjdk.jmh.runner.RunnerException; 14 | 15 | import com.carrotsearch.hppcrt.BenchmarkSuiteRunner; 16 | import com.carrotsearch.hppcrt.BroadWord; 17 | 18 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 19 | @State(Scope.Thread) 20 | public class BenchmarkPopCnt 21 | { 22 | long[] seq; 23 | 24 | @Param 25 | public Distribution distribution; 26 | 27 | public static enum Distribution 28 | { 29 | ZEROS, 30 | FULL, 31 | RANDOM, 32 | ONEBIT 33 | } 34 | 35 | @Setup 36 | public void setUp() throws Exception 37 | { 38 | this.seq = new long[1000000]; 39 | 40 | final Random rnd = new Random(0xdeadbeef); 41 | switch (this.distribution) { 42 | case ZEROS: 43 | break; 44 | case FULL: 45 | Arrays.fill(this.seq, -1); 46 | break; 47 | case RANDOM: 48 | for (int i = 0; i < this.seq.length; i++) { 49 | this.seq[i] = rnd.nextLong(); 50 | } 51 | break; 52 | case ONEBIT: 53 | for (int i = 0; i < this.seq.length; i++) { 54 | this.seq[i] = 1L << rnd.nextInt(64); 55 | } 56 | break; 57 | } 58 | } 59 | 60 | @Benchmark 61 | public int timeLongBitCount() { 62 | int v = 0; 63 | 64 | for (int j = 0; j < this.seq.length; j++) { 65 | v += Long.bitCount(this.seq[j]); 66 | } 67 | 68 | return v; 69 | } 70 | 71 | @Benchmark 72 | public int timeHdPopCnd() { 73 | int v = 0; 74 | 75 | for (int j = 0; j < this.seq.length; j++) { 76 | v += BenchmarkPopCnt.hdBitCount(this.seq[j]); 77 | } 78 | 79 | return v; 80 | } 81 | 82 | @Benchmark 83 | public int timeRank9() { 84 | int v = 0; 85 | 86 | for (int j = 0; j < this.seq.length; j++) { 87 | v += BenchmarkPopCnt.rank9(this.seq[j]); 88 | } 89 | 90 | return v; 91 | } 92 | 93 | @Benchmark 94 | public int timeNaivePopCnt() { 95 | int v = 0; 96 | 97 | for (int j = 0; j < this.seq.length; j++) { 98 | v += BenchmarkPopCnt.naivePopCnt(this.seq[j]); 99 | } 100 | 101 | return v; 102 | } 103 | 104 | // 105 | 106 | private static int naivePopCnt(long x) { 107 | int cnt = 0; 108 | while (x != 0) { 109 | if (((x >>>= 1) & 1) != 0L) { 110 | cnt++; 111 | } 112 | } 113 | return cnt; 114 | } 115 | 116 | private static int hdBitCount(long i) { 117 | // HD, Figure 5-14 118 | i = i - ((i >>> 1) & 0x5555555555555555L); 119 | i = (i & 0x3333333333333333L) + ((i >>> 2) & 0x3333333333333333L); 120 | i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0fL; 121 | i = i + (i >>> 8); 122 | i = i + (i >>> 16); 123 | i = i + (i >>> 32); 124 | return (int) i & 0x7f; 125 | } 126 | 127 | private static int rank9(long x) { 128 | // Step 0 leaves in each pair of bits the number of ones originally contained in that pair: 129 | x = x - ((x & 0xAAAAAAAAAAAAAAAAL) >>> 1); 130 | // Step 1, idem for each nibble: 131 | x = (x & 0x3333333333333333L) + ((x >>> 2) & 0x3333333333333333L); 132 | // Step 2, idem for each byte: 133 | x = (x + (x >>> 4)) & 0x0F0F0F0F0F0F0F0FL; 134 | // Multiply to sum them all into the high byte, and return the high byte: 135 | return (int) ((x * BroadWord.L8_L) >>> 56); 136 | } 137 | 138 | public static void main(final String[] args) throws RunnerException 139 | { 140 | BenchmarkSuiteRunner.runJmhBasicBenchmarkWithCommandLine(BenchmarkPopCnt.class, args, 500, 1000); 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/jmh/IterationSpeedBenchmark.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.jmh; 2 | 3 | import java.util.Random; 4 | import java.util.concurrent.TimeUnit; 5 | import java.util.concurrent.atomic.AtomicInteger; 6 | 7 | import org.openjdk.jmh.annotations.Benchmark; 8 | import org.openjdk.jmh.annotations.OutputTimeUnit; 9 | import org.openjdk.jmh.annotations.Scope; 10 | import org.openjdk.jmh.annotations.Setup; 11 | import org.openjdk.jmh.annotations.State; 12 | import org.openjdk.jmh.runner.RunnerException; 13 | 14 | import com.carrotsearch.hppcrt.BenchmarkSuiteRunner; 15 | import com.carrotsearch.hppcrt.cursors.IntCursor; 16 | import com.carrotsearch.hppcrt.lists.IntArrayList; 17 | import com.carrotsearch.hppcrt.procedures.IntProcedure; 18 | 19 | /** 20 | * Various iteration approaches on an integer list. 21 | */ 22 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 23 | @State(Scope.Thread) 24 | public class IterationSpeedBenchmark 25 | { 26 | public static final int CELLS = (1024 * 1024) * 50; 27 | private IntArrayList list; 28 | 29 | /* */ 30 | @Setup 31 | public void before() 32 | { 33 | this.list = new IntArrayList(); 34 | this.list.resize(IterationSpeedBenchmark.CELLS); 35 | 36 | //fill with random values 37 | final Random prng = new Random(210654641246431L); 38 | 39 | for (int i = 0; i < this.list.size(); i++) { 40 | 41 | this.list.set(i, prng.nextInt()); 42 | } 43 | } 44 | 45 | /* */ 46 | @Benchmark 47 | public int testSimpleGetLoop() throws Exception 48 | { 49 | int count = 0; 50 | for (int i = 0; i < this.list.size(); i++) 51 | { 52 | count += this.list.get(i); 53 | } 54 | 55 | return count; 56 | } 57 | 58 | /* */ 59 | @Benchmark 60 | public int testDirectBufferLoop() throws Exception 61 | { 62 | final int size = this.list.size(); 63 | final int[] buffer = this.list.buffer; 64 | int count = 0; 65 | for (int i = 0; i < size; i++) 66 | { 67 | count += buffer[i]; 68 | } 69 | return count; 70 | } 71 | 72 | /* */ 73 | @Benchmark 74 | public int testIterableCursor() throws Exception 75 | { 76 | int count = 0; 77 | for (final IntCursor c : this.list) 78 | { 79 | count += c.value; 80 | } 81 | return count; 82 | } 83 | 84 | /* */ 85 | @Benchmark 86 | public int testWithProcedureClosure() 87 | { 88 | final AtomicInteger holder = new AtomicInteger(); 89 | this.list.forEach(new IntProcedure() { 90 | @Override 91 | public void apply(final int v) 92 | { 93 | holder.getAndAdd(v); 94 | } 95 | }); 96 | return holder.get(); 97 | } 98 | 99 | /* */ 100 | @Benchmark 101 | public int testDirectBufferWithNewFor() throws Exception 102 | { 103 | int count = 0; 104 | for (final int c : this.list.buffer) 105 | { 106 | count += c; 107 | } 108 | return count; 109 | } 110 | 111 | public static void main(final String[] args) throws RunnerException 112 | { 113 | BenchmarkSuiteRunner.runJmhBasicBenchmarkWithCommandLine(IterationSpeedBenchmark.class, args, 1000, 2000); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/jmh/ObjectArrayListBenchmark.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.jmh; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | import java.util.concurrent.atomic.AtomicInteger; 5 | 6 | import org.openjdk.jmh.annotations.Benchmark; 7 | import org.openjdk.jmh.annotations.OutputTimeUnit; 8 | import org.openjdk.jmh.annotations.Scope; 9 | import org.openjdk.jmh.annotations.Setup; 10 | import org.openjdk.jmh.annotations.State; 11 | import org.openjdk.jmh.runner.RunnerException; 12 | 13 | import com.carrotsearch.hppcrt.BenchmarkSuiteRunner; 14 | import com.carrotsearch.hppcrt.cursors.ObjectCursor; 15 | import com.carrotsearch.hppcrt.lists.ObjectArrayList; 16 | import com.carrotsearch.hppcrt.procedures.ObjectProcedure; 17 | 18 | /** 19 | * Benchmark tests for {@link ObjectArrayList}. 20 | */ 21 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 22 | @State(Scope.Thread) 23 | public class ObjectArrayListBenchmark 24 | { 25 | public static final int CELLS = (1024 * 1024) * 50; 26 | private ObjectArrayList list; 27 | 28 | private static final Object defValue = null; 29 | 30 | /* */ 31 | @Setup 32 | public void before() 33 | { 34 | this.list = new ObjectArrayList(); 35 | this.list.resize(ObjectArrayListBenchmark.CELLS); 36 | } 37 | 38 | /* */ 39 | @Benchmark 40 | public int testSimpleGetLoop() 41 | { 42 | final ObjectArrayList list = this.list; 43 | final int max = list.size(); 44 | int count = 0; 45 | for (int i = 0; i < max; i++) 46 | { 47 | if (list.get(i) != ObjectArrayListBenchmark.defValue) 48 | count++; 49 | } 50 | 51 | return count; 52 | } 53 | 54 | /* */ 55 | @Benchmark 56 | public int testDirectBufferLoop() 57 | { 58 | final int size = this.list.size(); 59 | final Object[] buffer = this.list.buffer; 60 | int count = 0; 61 | for (int i = 0; i < size; i++) 62 | { 63 | if (buffer[i] != ObjectArrayListBenchmark.defValue) 64 | count++; 65 | } 66 | 67 | return count; 68 | } 69 | 70 | /* */ 71 | @Benchmark 72 | public int testIterableCursor() 73 | { 74 | int count = 0; 75 | for (final ObjectCursor c : this.list) 76 | { 77 | if (c.value != ObjectArrayListBenchmark.defValue) 78 | count++; 79 | } 80 | 81 | return count; 82 | } 83 | 84 | /* */ 85 | @Benchmark 86 | public int testWithProcedureClosure() 87 | { 88 | final AtomicInteger count = new AtomicInteger(); 89 | this.list.forEach(new ObjectProcedure() { 90 | @Override 91 | public void apply(final Object v) 92 | { 93 | if (v != ObjectArrayListBenchmark.defValue) 94 | count.incrementAndGet(); 95 | } 96 | }); 97 | 98 | return count.get(); 99 | } 100 | 101 | /* */ 102 | @Benchmark 103 | public int testDirectBufferWithNewFor() 104 | { 105 | int count = 0; 106 | for (final Object c : this.list.buffer) 107 | { 108 | if (ObjectArrayListBenchmark.defValue != c) 109 | count++; 110 | } 111 | return count; 112 | } 113 | 114 | public static void main(final String[] args) throws RunnerException 115 | { 116 | BenchmarkSuiteRunner.runJmhBasicBenchmarkWithCommandLine(ObjectArrayListBenchmark.class, args, 1000, 2000); 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/misc/BenchmarkHashContainersSuite.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.misc; 2 | 3 | import java.util.Arrays; 4 | 5 | import com.carrotsearch.hppcrt.BenchmarkSuiteRunner; 6 | import com.carrotsearch.hppcrt.jmh.BenchmarkHashCollisions; 7 | import com.carrotsearch.hppcrt.jmh.BenchmarkHashMapContains; 8 | import com.carrotsearch.hppcrt.jmh.BenchmarkHashMapPut; 9 | import com.carrotsearch.hppcrt.jmh.BenchmarkHashMapRemove; 10 | 11 | /** 12 | * Runs the suite of benchmarks about hash containers 13 | */ 14 | public class BenchmarkHashContainersSuite 15 | { 16 | private final static Class[] ALL_BENCHMARKS = new Class[] 17 | { 18 | BenchmarkHashCollisions.class, 19 | BenchmarkHashMapPut.class, 20 | BenchmarkHashMapContains.class, 21 | BenchmarkHashMapRemove.class, 22 | 23 | }; 24 | 25 | public static void main(final String[] args) throws Exception 26 | { 27 | BenchmarkSuiteRunner.runMain(Arrays.asList(BenchmarkHashContainersSuite.ALL_BENCHMARKS), args); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/misc/FastUtilOomPut.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.misc; 2 | 3 | import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; 4 | 5 | import java.lang.reflect.Field; 6 | 7 | public class FastUtilOomPut 8 | { 9 | public static void main(String [] args) 10 | throws Exception 11 | { 12 | Int2IntOpenHashMap map = new Int2IntOpenHashMap(100, 1f); 13 | Field f = map.getClass().getDeclaredField("key"); 14 | f.setAccessible(true); 15 | 16 | boolean hitOOM = false; 17 | for (int i = 0;; i++) { 18 | try { 19 | if (hitOOM) { System.out.println("put(" + i + ")"); } 20 | map.put(i, i); 21 | } catch (OutOfMemoryError e) { 22 | hitOOM = true; 23 | System.out.println("OOM, map: " + map.size() + " " + ((int[])f.get(map)).length); 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/misc/HppcMaxHashMapSize.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.misc; 2 | 3 | import com.carrotsearch.hppcrt.maps.*; 4 | import com.carrotsearch.hppcrt.sets.*; 5 | 6 | /** 7 | * Requires a lot of heap! 8 | */ 9 | public class HppcMaxHashMapSize 10 | { 11 | public static void main(final String[] args) 12 | throws Exception 13 | { 14 | final IntHashSet set = new IntHashSet(0x40000000, 0.75f); 15 | 16 | for (int i = 0;; i++) { 17 | try { 18 | set.add(i); 19 | } 20 | catch (final RuntimeException e) { 21 | System.out.println("Max capacity: " + set.size()); 22 | } 23 | catch (final OutOfMemoryError e) { 24 | System.out.println("OOM hit at size: " + set.size() + " (0x" + Integer.toHexString(set.size())); 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/misc/HppcOomPut.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.misc; 2 | 3 | import java.lang.reflect.Field; 4 | 5 | import com.carrotsearch.hppcrt.maps.*; 6 | 7 | public class HppcOomPut 8 | { 9 | public static void main(final String[] args) 10 | throws Exception 11 | { 12 | final IntIntHashMap map = new IntIntHashMap(100, 1f); 13 | final Field f = map.getClass().getDeclaredField("keys"); 14 | f.setAccessible(true); 15 | 16 | boolean hitOOM = false; 17 | for (int i = 0;; i++) { 18 | try { 19 | if (hitOOM) { 20 | System.out.println("put(" + i + ")"); 21 | } 22 | map.put(i, i); 23 | } 24 | catch (final OutOfMemoryError e) { 25 | hitOOM = true; 26 | System.out.println("OOM, map: " + map.size() + " " + ((int[]) f.get(map)).length); 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /hppcrt-benchmarks/src/main/java/com/carrotsearch/hppcrt/misc/SyntheticBenchmarkSuite.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.misc; 2 | 3 | import java.util.Arrays; 4 | import com.carrotsearch.hppcrt.BenchmarkSuiteRunner; 5 | 6 | /** 7 | * Runs the suite of benchmarks about hash containers 8 | */ 9 | public class SyntheticBenchmarkSuite 10 | { 11 | /** 12 | * List of synthetic benchmarks 13 | */ 14 | private final static Class[] ALL_BENCHMARKS = new Class[] { 15 | 16 | HppcArraysBench.class, 17 | HppcSortSyntheticBench.class, 18 | HppcListSyntheticBench.class, 19 | HppcHeapsSyntheticBench.class, 20 | HppcMapSyntheticBench.class 21 | }; 22 | 23 | public static void main(final String[] args) throws Exception 24 | { 25 | BenchmarkSuiteRunner.runMain(Arrays.asList(SyntheticBenchmarkSuite.ALL_BENCHMARKS), args); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /hppcrt-template-processor/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | 6 | com.github.vsonnier 7 | hppcrt-parent 8 | 0.7.6-SNAPSHOT 9 | ../pom.xml 10 | 11 | 12 | 13 | com.github.vsonnier 14 | hppcrt-template-processor 15 | 0.7.6-SNAPSHOT 16 | jar 17 | 18 | HPPC-RT Template Processor 19 | Template Processor and Code Generation for HPPC-RT. 20 | 21 | 22 | 2.1 23 | 24 | 25 | 26 | 27 | 28 | com.google.guava 29 | guava 30 | ${version.guava} 31 | 32 | 33 | org.antlr 34 | antlr4 35 | ${version.antlr} 36 | 37 | 38 | 39 | org.apache.velocity 40 | velocity-engine-core 41 | ${version.velocity} 42 | 43 | 44 | 45 | junit 46 | junit 47 | ${version.junit} 48 | test 49 | 50 | 51 | 52 | com.carrotsearch.randomizedtesting 53 | randomizedtesting-runner 54 | ${version.randomizedtesting} 55 | test 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | org.antlr 64 | antlr4-maven-plugin 65 | ${version.antlr} 66 | 67 | 68 | antlr4 69 | 70 | antlr4 71 | 72 | 73 | true 74 | 75 | 76 | 77 | 78 | 79 | 80 | com.carrotsearch.randomizedtesting 81 | junit4-maven-plugin 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /hppcrt-template-processor/src/main/java/com/carrotsearch/hppcrt/generator/ImportDirective.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.generator; 2 | 3 | import java.io.IOException; 4 | import java.io.Writer; 5 | 6 | import org.apache.velocity.context.InternalContextAdapter; 7 | import org.apache.velocity.exception.MethodInvocationException; 8 | import org.apache.velocity.exception.ParseErrorException; 9 | import org.apache.velocity.exception.ResourceNotFoundException; 10 | import org.apache.velocity.runtime.directive.Parse; 11 | import org.apache.velocity.runtime.parser.node.Node; 12 | 13 | import com.carrotsearch.hppcrt.generator.TemplateOptions.DoNotGenerateTypeException; 14 | 15 | /** 16 | * This class declares a custom "#import" Velocity directive, 17 | * that does the same thing as "#parse" but /dev/null all outputs, 18 | * so is litterally just "importing" Velocity definitions into the current location. 19 | * ex: #import("com/carrotsearch/hppcrt/Intrinsics.java") 20 | * @author Vincent 21 | * 22 | */ 23 | public class ImportDirective extends Parse 24 | { 25 | public ImportDirective() { 26 | super(); 27 | } 28 | 29 | @Override 30 | public String getName() { 31 | 32 | return "import"; 33 | } 34 | 35 | @Override 36 | public boolean render(final InternalContextAdapter context, final Writer writer, final Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException { 37 | 38 | boolean bSuccess = false; 39 | 40 | try { 41 | // dev/null all outputs... 42 | bSuccess = super.render(context, TemplateNullWriter.NULL_WRITER, node); 43 | } 44 | catch (final MethodInvocationException e) { 45 | 46 | if (e.getCause() instanceof DoNotGenerateTypeException) { 47 | 48 | //do nothing, catch the exception, we have encountered a DoNotGenerateTypeException 49 | //thrown by TemplateOptions purposefully 50 | bSuccess = true; 51 | } 52 | else { 53 | 54 | //rethrow the beast to stop the thing dead. 55 | throw e; 56 | } 57 | } //end MethodInvocationException 58 | 59 | return bSuccess; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /hppcrt-template-processor/src/main/java/com/carrotsearch/hppcrt/generator/OutputFile.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.generator; 2 | 3 | import java.nio.file.Path; 4 | 5 | class OutputFile 6 | { 7 | public final Path path; 8 | public boolean upToDate; 9 | 10 | public OutputFile(final Path target) { 11 | this.path = target.toAbsolutePath().normalize(); 12 | } 13 | } -------------------------------------------------------------------------------- /hppcrt-template-processor/src/main/java/com/carrotsearch/hppcrt/generator/TemplateFile.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.generator; 2 | 3 | import java.nio.file.Path; 4 | 5 | class TemplateFile 6 | { 7 | public final Path path; 8 | 9 | public TemplateFile(final Path path) { 10 | this.path = path; 11 | } 12 | 13 | public String getFileName() { 14 | return this.path.getFileName().toString(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /hppcrt-template-processor/src/main/java/com/carrotsearch/hppcrt/generator/TemplateNullWriter.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.generator; 2 | 3 | import java.io.IOException; 4 | import java.io.Writer; 5 | 6 | /** 7 | * Stub for Velocity when we do not really want to render anything into... 8 | * @author Vincent 9 | * 10 | */ 11 | public class TemplateNullWriter extends Writer 12 | { 13 | public static final TemplateNullWriter NULL_WRITER = new TemplateNullWriter(); 14 | 15 | private TemplateNullWriter() { 16 | // nothing 17 | } 18 | 19 | @Override 20 | public void write(final char[] cbuf, final int off, final int len) throws IOException { 21 | // nothing 22 | } 23 | 24 | @Override 25 | public void flush() throws IOException { 26 | // nothing 27 | 28 | } 29 | 30 | @Override 31 | public void close() throws IOException { 32 | // nothing 33 | 34 | } 35 | 36 | @Override 37 | public void write(final int c) throws IOException { 38 | //nothing 39 | } 40 | 41 | @Override 42 | public void write(final char[] cbuf) throws IOException { 43 | //nothing 44 | } 45 | 46 | @Override 47 | public void write(final String str) throws IOException { 48 | //nothing 49 | } 50 | 51 | @Override 52 | public void write(final String str, final int off, final int len) throws IOException { 53 | //nothing 54 | } 55 | 56 | @Override 57 | public Writer append(final CharSequence csq) throws IOException { 58 | //do nothing useful 59 | return this; 60 | } 61 | 62 | @Override 63 | public Writer append(final CharSequence csq, final int start, final int end) throws IOException { 64 | //do nothing useful 65 | return this; 66 | } 67 | 68 | @Override 69 | public Writer append(final char c) throws IOException { 70 | //do nothing useful 71 | return this; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /hppcrt-template-processor/src/main/java/com/carrotsearch/hppcrt/generator/Type.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.generator; 2 | 3 | public enum Type 4 | { 5 | GENERIC("null"), 6 | BYTE("(byte)0"), 7 | CHAR("\'\\u0000\'"), 8 | SHORT("(short)0"), 9 | INT("0"), 10 | LONG("0L"), 11 | FLOAT("0f"), 12 | DOUBLE("0.0"); 13 | 14 | private static final Type[] VALUES = Type.values(); 15 | 16 | public final String defaultValue; 17 | 18 | private Type(final String defaultVal) { 19 | 20 | this.defaultValue = defaultVal; 21 | } 22 | 23 | /** 24 | * Convenience method replacing Enum.valueOf() that returns null on an unknown 25 | * name for enum instead of throwing an exception. 26 | * @param representation 27 | * @return 28 | */ 29 | public static Type valueOfOrNull(final String representation) { 30 | 31 | Type res = null; 32 | 33 | try { 34 | 35 | res = Type.valueOf(representation.trim()); 36 | 37 | } catch (final IllegalArgumentException e) { 38 | //not matching 39 | } 40 | 41 | return res; 42 | } 43 | 44 | /** 45 | * Retreive Type from Type.toString() i.e. (Object, int float, ...etc) and returns null on an unknown 46 | * representation. 47 | * @param representation 48 | * @return 49 | */ 50 | public static Type fromString(final String representation) { 51 | 52 | for (final Type current : Type.VALUES) { 53 | 54 | if (current.getType().toLowerCase().equals(representation.trim().toLowerCase())) { 55 | 56 | return current; 57 | } 58 | } 59 | 60 | return null; 61 | } 62 | 63 | public String getBoxedType() 64 | { 65 | if (this == GENERIC) { 66 | return "Object"; 67 | } 68 | 69 | final String boxed = name().toLowerCase(); 70 | return Character.toUpperCase(boxed.charAt(0)) + boxed.substring(1); 71 | } 72 | 73 | public String getType() 74 | { 75 | if (this == GENERIC) { 76 | return "Object"; 77 | } 78 | 79 | return name().toLowerCase(); 80 | } 81 | 82 | /** 83 | * Used in Velocity when 2 Type(s) are tested for equality in template. 84 | */ 85 | @Override 86 | public String toString() { 87 | return getType(); 88 | } 89 | 90 | public boolean isGeneric() 91 | { 92 | return this == GENERIC; 93 | } 94 | 95 | public String getDefaultValue() { 96 | 97 | return this.defaultValue; 98 | } 99 | } -------------------------------------------------------------------------------- /hppcrt-template-processor/src/main/java/com/carrotsearch/hppcrt/generator/parser/Channels.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.generator.parser; 2 | 3 | public final class Channels 4 | { 5 | private Channels() { 6 | //nothing 7 | } 8 | 9 | public final static int LINE_COMMENT = 1000; 10 | public final static int BLOCK_COMMENT = 1001; 11 | public final static int JAVADOC_COMMENT = 1002; 12 | } 13 | -------------------------------------------------------------------------------- /hppcrt-template-processor/src/main/java/com/carrotsearch/hppcrt/generator/parser/Replacement.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.generator.parser; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collection; 5 | import java.util.Comparator; 6 | 7 | import org.antlr.v4.runtime.misc.Interval; 8 | import org.antlr.v4.runtime.tree.SyntaxTree; 9 | 10 | /** */ 11 | public final class Replacement 12 | { 13 | public final Interval interval; 14 | public final String source; 15 | public final String replacement; 16 | 17 | public Replacement(final String src, final Interval interval, final String replacement) { 18 | 19 | //source is only useful for debug purposes 20 | this.source = src; 21 | 22 | this.interval = interval; 23 | this.replacement = replacement; 24 | } 25 | 26 | public Replacement(final String source, final SyntaxTree ctx, final String replacement) { 27 | this(source, ctx.getSourceInterval(), replacement); 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return "{'" + this.source + "'(" + this.interval + ") => '" + this.replacement + "'}"; 33 | } 34 | 35 | /** 36 | * Sort a list of replacements in their natural stream of replacement order. 37 | * @param replacements 38 | * @return 39 | */ 40 | public static ArrayList sortList(final Collection replacements) { 41 | 42 | final ArrayList sorted = new ArrayList(replacements); 43 | 44 | sorted.sort(new Comparator() { 45 | @Override 46 | public int compare(final Replacement a, final Replacement b) { 47 | return Integer.compare(a.interval.a, b.interval.b); 48 | } 49 | }); 50 | 51 | return sorted; 52 | } 53 | 54 | /** 55 | * display the list of replacements in their natural replacement order 56 | * @param replacements 57 | * @return 58 | */ 59 | public static String toString(final Collection replacements) { 60 | 61 | final ArrayList sorted = Replacement.sortList(replacements); 62 | 63 | return sorted.toString(); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /hppcrt-template-processor/src/main/java/com/carrotsearch/hppcrt/generator/parser/TypeBound.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.generator.parser; 2 | 3 | import com.carrotsearch.hppcrt.generator.Type; 4 | import com.google.common.base.Preconditions; 5 | 6 | public final class TypeBound 7 | { 8 | private final String originalBound; 9 | private final Type targetType; 10 | 11 | public TypeBound(final Type targetType, final String originalBound) { 12 | this.targetType = targetType; 13 | this.originalBound = originalBound; 14 | } 15 | 16 | public Type templateBound() { 17 | 18 | Preconditions.checkNotNull(this.targetType, "Target not a template bound: " + this.originalBound); 19 | return this.targetType; 20 | } 21 | 22 | public boolean isTemplateType() { 23 | return this.targetType != null; 24 | } 25 | 26 | public String originalBound() { 27 | return this.originalBound; 28 | } 29 | 30 | public String getBoxedType() { 31 | return templateBound().getBoxedType(); 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | 37 | return "{originalBound='" + this.originalBound + "', targetType='" + this.targetType + "'}"; 38 | } 39 | } //end static class TypeBound -------------------------------------------------------------------------------- /hppcrt-template-processor/src/test/java/com/carrotsearch/hppcrt/generator/TemplateOptionsTest.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.generator; 2 | 3 | import org.junit.Test; 4 | 5 | public class TemplateOptionsTest 6 | { 7 | @Test 8 | public void testInlines() { 9 | 10 | final TemplateOptions testInstance = new TemplateOptions(Type.GENERIC, null); 11 | 12 | //nothing for now 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /hppcrt-template-processor/src/test/resources/test_cases/CrashParser.txt: -------------------------------------------------------------------------------- 1 | /* 2 | Cut-n-paste here code causing a Parser crash, for investigation. 3 | */ -------------------------------------------------------------------------------- /hppcrt-template-processor/src/test/resources/test_cases/IteratorPool.test: -------------------------------------------------------------------------------- 1 | public class KTypeFoo 2 | { 3 | private IteratorPool, ValueIterator> valueIteratorPool; 4 | 5 | public KTypeFoo() 6 | { 7 | this.valueIteratorPool = new IteratorPool, ValueIterator>() { 8 | }; 9 | 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /hppcrt-template-processor/src/test/resources/test_cases/IteratorPoolAlloc.test: -------------------------------------------------------------------------------- 1 | public class KTypeHeapPriorityQueue extends AbstractKTypeCollection 2 | implements KTypePriorityQueue, Cloneable 3 | { 4 | 5 | protected final IteratorPool, ValueIterator> valueIteratorPool; 6 | 7 | 8 | public KTypeHeapPriorityQueue() 9 | { 10 | this.valueIteratorPool = new IteratorPool, ValueIterator>( 11 | new ObjectFactory() { 12 | 13 | @Override 14 | public ValueIterator create() 15 | { 16 | return new ValueIterator(); 17 | } 18 | 19 | @Override 20 | public void initialize(final ValueIterator obj) 21 | { 22 | obj.cursor.index = 0; 23 | obj.size = KTypeHeapPriorityQueue.this.size(); 24 | obj.buffer = KTypeHeapPriorityQueue.this.buffer; 25 | } 26 | 27 | @Override 28 | public void reset(final ValueIterator obj) { 29 | // for GC sake 30 | obj.buffer = null; 31 | 32 | } 33 | }); 34 | } 35 | 36 | } 37 | 38 | 39 | -------------------------------------------------------------------------------- /hppcrt-template-processor/src/test/resources/test_cases/IteratorPoolAllocGeneric.ok: -------------------------------------------------------------------------------- 1 | public class ObjectHeapPriorityQueue extends AbstractObjectCollection 2 | implements ObjectPriorityQueue, Cloneable 3 | { 4 | 5 | protected final IteratorPool, ValueIterator> valueIteratorPool; 6 | 7 | 8 | public ObjectHeapPriorityQueue() 9 | { 10 | this.valueIteratorPool = new IteratorPool, ValueIterator>( 11 | new ObjectFactory() { 12 | 13 | @Override 14 | public ValueIterator create() 15 | { 16 | return new ValueIterator(); 17 | } 18 | 19 | @Override 20 | public void initialize(final ValueIterator obj) 21 | { 22 | obj.cursor.index = 0; 23 | obj.size = ObjectHeapPriorityQueue.this.size(); 24 | obj.buffer = ObjectHeapPriorityQueue.this.buffer; 25 | } 26 | 27 | @Override 28 | public void reset(final ValueIterator obj) { 29 | // for GC sake 30 | obj.buffer = null; 31 | 32 | } 33 | }); 34 | } 35 | 36 | } 37 | 38 | 39 | -------------------------------------------------------------------------------- /hppcrt-template-processor/src/test/resources/test_cases/IteratorPoolAllocLong.ok: -------------------------------------------------------------------------------- 1 | public class LongHeapPriorityQueue extends AbstractLongCollection 2 | implements LongPriorityQueue, Cloneable 3 | { 4 | 5 | protected final IteratorPool valueIteratorPool; 6 | 7 | 8 | public LongHeapPriorityQueue() 9 | { 10 | this.valueIteratorPool = new IteratorPool( 11 | new ObjectFactory() { 12 | 13 | @Override 14 | public ValueIterator create() 15 | { 16 | return new ValueIterator(); 17 | } 18 | 19 | @Override 20 | public void initialize(final ValueIterator obj) 21 | { 22 | obj.cursor.index = 0; 23 | obj.size = LongHeapPriorityQueue.this.size(); 24 | obj.buffer = LongHeapPriorityQueue.this.buffer; 25 | } 26 | 27 | @Override 28 | public void reset(final ValueIterator obj) { 29 | // for GC sake 30 | obj.buffer = null; 31 | 32 | } 33 | }); 34 | } 35 | 36 | } 37 | 38 | 39 | -------------------------------------------------------------------------------- /hppcrt-template-processor/src/test/resources/test_cases/IteratorPoolGeneric.ok: -------------------------------------------------------------------------------- 1 | public class ObjectFoo 2 | { 3 | private IteratorPool, ValueIterator> valueIteratorPool; 4 | 5 | public ObjectFoo() 6 | { 7 | this.valueIteratorPool = new IteratorPool, ValueIterator>() { 8 | }; 9 | 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /hppcrt-template-processor/src/test/resources/test_cases/IteratorPoolLong.ok: -------------------------------------------------------------------------------- 1 | public class LongFoo 2 | { 3 | private IteratorPool valueIteratorPool; 4 | 5 | public LongFoo() 6 | { 7 | this.valueIteratorPool = new IteratorPool() { 8 | }; 9 | 10 | } 11 | } -------------------------------------------------------------------------------- /hppcrt-template-processor/src/test/resources/test_cases/KTypeVTypeClass.test: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.generator.parser.test_cases; 2 | 3 | import com.carrotsearch.hppcrt.generator.parser.test_cases.subpackage.KTypeVTypeInterfaceImported; 4 | 5 | /** 6 | * A map of (KTypes, VTypes) pairs. 7 | */ 8 | public class KTypeVTypeClass 9 | extends KTypeVTypeSuper 10 | implements KTypeVTypeInterface, 11 | KTypeVTypeInterfaceImported, 12 | Cloneable 13 | { 14 | KType[] keys; 15 | VType[] values; 16 | 17 | public KTypeVTypeClass() { 18 | } 19 | 20 | public KTypeVTypeClass(final KType[] foo, final VType[] booArray, final VType boo) { 21 | 22 | final KType[] foo2 = (KType[]) foo; 23 | final VType[] booArray2 = (VType[]) booArray; 24 | 25 | final VType boo2 = (VType) boo; 26 | } 27 | 28 | public KTypeVTypeClass(final KTypeVTypeSuper foo) { 29 | } 30 | 31 | public VType foo(final KType key, final VType value) { 32 | 33 | throw new RuntimeException(); 34 | } 35 | 36 | public void foo(final KTypeVTypeSuper foo) { 37 | } 38 | } 39 | 40 | class KTypeVTypeSuper 41 | { 42 | } 43 | 44 | interface KTypeVTypeInterface 45 | { 46 | } -------------------------------------------------------------------------------- /hppcrt-template-processor/src/test/resources/test_cases/LongIntClass.ok: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.generator.parser.test_cases; 2 | 3 | import com.carrotsearch.hppcrt.generator.parser.test_cases.subpackage.LongIntInterfaceImported; 4 | 5 | /** 6 | * A map of (longs, ints) pairs. 7 | */ 8 | public class LongIntClass 9 | extends LongIntSuper 10 | implements LongIntInterface, 11 | LongIntInterfaceImported, 12 | Cloneable 13 | { 14 | long[] keys; 15 | int[] values; 16 | 17 | public LongIntClass() { 18 | } 19 | 20 | public LongIntClass(final long[] foo, final int[] booArray, final int boo) { 21 | 22 | final long[] foo2 = (long[]) foo; 23 | final int[] booArray2 = (int[]) booArray; 24 | 25 | final int boo2 = (int) boo; 26 | } 27 | 28 | public LongIntClass(final LongIntSuper foo) { 29 | } 30 | 31 | public int foo(final long key, final int value) { 32 | 33 | throw new RuntimeException(); 34 | } 35 | 36 | public void foo(final LongIntSuper foo) { 37 | } 38 | } 39 | 40 | class LongIntSuper 41 | { 42 | } 43 | 44 | interface LongIntInterface 45 | { 46 | } -------------------------------------------------------------------------------- /hppcrt-template-processor/src/test/resources/test_cases/LongObjectClass.ok: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.generator.parser.test_cases; 2 | 3 | import com.carrotsearch.hppcrt.generator.parser.test_cases.subpackage.LongObjectInterfaceImported; 4 | 5 | /** 6 | * A map of (longs, Objects) pairs. 7 | */ 8 | public class LongObjectClass 9 | extends LongObjectSuper 10 | implements LongObjectInterface, 11 | LongObjectInterfaceImported, 12 | Cloneable 13 | { 14 | long[] keys; 15 | VType[] values; 16 | 17 | public LongObjectClass() { 18 | } 19 | 20 | public LongObjectClass(final long[] foo, final VType[] booArray, final VType boo) { 21 | 22 | final long[] foo2 = (long[]) foo; 23 | final VType[] booArray2 = (VType[]) booArray; 24 | 25 | final VType boo2 = (VType) boo; 26 | } 27 | 28 | public LongObjectClass(final LongObjectSuper foo) { 29 | } 30 | 31 | public VType foo(final long key, final VType value) { 32 | 33 | throw new RuntimeException(); 34 | } 35 | 36 | public void foo(final LongObjectSuper foo) { 37 | } 38 | } 39 | 40 | class LongObjectSuper 41 | { 42 | } 43 | 44 | interface LongObjectInterface 45 | { 46 | } -------------------------------------------------------------------------------- /hppcrt-template-processor/src/test/resources/test_cases/ObjectLongClass.ok: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.generator.parser.test_cases; 2 | 3 | import com.carrotsearch.hppcrt.generator.parser.test_cases.subpackage.ObjectLongInterfaceImported; 4 | 5 | /** 6 | * A map of (Objects, longs) pairs. 7 | */ 8 | public class ObjectLongClass 9 | extends ObjectLongSuper 10 | implements ObjectLongInterface, 11 | ObjectLongInterfaceImported, 12 | Cloneable 13 | { 14 | KType[] keys; 15 | long[] values; 16 | 17 | public ObjectLongClass() { 18 | } 19 | 20 | public ObjectLongClass(final KType[] foo, final long[] booArray, final long boo) { 21 | 22 | final KType[] foo2 = (KType[]) foo; 23 | final long[] booArray2 = (long[]) booArray; 24 | 25 | final long boo2 = (long) boo; 26 | } 27 | 28 | public ObjectLongClass(final ObjectLongSuper foo) { 29 | } 30 | 31 | public long foo(final KType key, final long value) { 32 | 33 | throw new RuntimeException(); 34 | } 35 | 36 | public void foo(final ObjectLongSuper foo) { 37 | } 38 | } 39 | 40 | class ObjectLongSuper 41 | { 42 | } 43 | 44 | interface ObjectLongInterface 45 | { 46 | } -------------------------------------------------------------------------------- /hppcrt/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 16 | 17 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /hppcrt/src/eclipse/HPPCRT_process_templates_sources.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /hppcrt/src/eclipse/HPPCRT_process_templates_tests.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /hppcrt/src/eclipse/HPPCRT_run_tests_generated.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /hppcrt/src/eclipse/HPPCRT_run_tests_generated_coverage.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /hppcrt/src/eclipse/HPPCRT_run_tests_templates.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /hppcrt/src/eclipse/HPPCRT_run_tests_templates_coverage.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /hppcrt/src/main/java/com/carrotsearch/hppcrt/ArraySizingStrategy.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | /** 4 | * Resizing (growth) strategy for array-backed buffers. 5 | */ 6 | public interface ArraySizingStrategy 7 | { 8 | /** 9 | * @param currentBufferLength Current size of the array (buffer). This number 10 | * should comply with the strategy's policies (it is a result of initial rounding 11 | * or further growths). It can also be zero, indicating the growth from an empty 12 | * buffer. 13 | * 14 | * @param elementsCount Number of elements stored in the buffer. 15 | * 16 | * @param expectedAdditions Expected number of additions (resize hint). 17 | * 18 | * @return Must return a new size at least as big as to hold 19 | * elementsCount + expectedAdditions. 20 | * @throws BufferAllocationException 21 | */ 22 | int grow(int currentBufferLength, int elementsCount, int expectedAdditions) throws BufferAllocationException; 23 | } 24 | -------------------------------------------------------------------------------- /hppcrt/src/main/java/com/carrotsearch/hppcrt/BoundedProportionalArraySizingStrategy.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import java.util.ArrayList; 4 | 5 | /** 6 | * Array resizing proportional to the current buffer size, optionally kept within the 7 | * given minimum and maximum growth limits. Java's {@link ArrayList} uses: 8 | *
 9 |  * minGrow = 1
10 |  * maxGrow = Integer.MAX_VALUE (unbounded)
11 |  * growRatio = 1.5f
12 |  * 
13 | */ 14 | public final class BoundedProportionalArraySizingStrategy 15 | implements ArraySizingStrategy 16 | { 17 | public static final int MAX_ARRAY_LENGTH = Integer.MAX_VALUE - /* aligned array header + slack */32; 18 | 19 | /** Minimum grow count. */ 20 | public final static int DEFAULT_MIN_GROW_COUNT = 10; 21 | 22 | /** Maximum grow count (unbounded). */ 23 | public final static int DEFAULT_MAX_GROW_COUNT = BoundedProportionalArraySizingStrategy.MAX_ARRAY_LENGTH; 24 | 25 | /** Default resize is by half the current buffer's size. */ 26 | public final static float DEFAULT_GROW_RATIO = 1.5f; 27 | 28 | /** Minimum number of elements to grow, if limit exceeded. */ 29 | public final int minGrowCount; 30 | 31 | /** Maximum number of elements to grow, if limit exceeded. */ 32 | public final int maxGrowCount; 33 | 34 | /** 35 | * The current buffer length is multiplied by this ratio to get the 36 | * first estimate for the new size. To double the size of the current 37 | * buffer, for example, set to 2. 38 | */ 39 | public final float growRatio; 40 | 41 | /** 42 | * Create the default sizing strategy. 43 | */ 44 | public BoundedProportionalArraySizingStrategy() 45 | { 46 | this(BoundedProportionalArraySizingStrategy.DEFAULT_MIN_GROW_COUNT, BoundedProportionalArraySizingStrategy.DEFAULT_MAX_GROW_COUNT, BoundedProportionalArraySizingStrategy.DEFAULT_GROW_RATIO); 47 | } 48 | 49 | /** 50 | * Create the sizing strategy with custom policies. 51 | */ 52 | public BoundedProportionalArraySizingStrategy(final int minGrow, final int maxGrow, final float ratio) 53 | { 54 | assert minGrow >= 1 : "Min grow must be >= 1."; 55 | assert maxGrow >= minGrow : "Max grow must be >= min grow."; 56 | assert ratio >= 1f : "Growth ratio must be >= 1 (was " + ratio + ")."; 57 | 58 | this.minGrowCount = minGrow; 59 | this.maxGrowCount = maxGrow; 60 | this.growRatio = ratio - 1.0f; 61 | } 62 | 63 | /** 64 | * Grow according to {@link #growRatio}, {@link #minGrowCount} and {@link #maxGrowCount}. 65 | */ 66 | @SuppressWarnings("boxing") 67 | @Override 68 | public int grow(final int currentBufferLength, final int elementsCount, final int expectedAdditions) 69 | { 70 | long growBy = (long) (currentBufferLength * this.growRatio); 71 | 72 | growBy = Math.max(growBy, this.minGrowCount); 73 | growBy = Math.min(growBy, this.maxGrowCount); 74 | final long growTo = Math.min(BoundedProportionalArraySizingStrategy.MAX_ARRAY_LENGTH, growBy + currentBufferLength); 75 | 76 | final long newSize = Math.max((long) elementsCount + expectedAdditions, growTo); 77 | 78 | if (newSize > BoundedProportionalArraySizingStrategy.MAX_ARRAY_LENGTH) { 79 | 80 | throw new BufferAllocationException( 81 | "Java array size exceeded (current length: %d, elements: %d, expected additions: %d)", 82 | currentBufferLength, 83 | elementsCount, 84 | expectedAdditions); 85 | } 86 | 87 | return (int) newSize; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /hppcrt/src/main/java/com/carrotsearch/hppcrt/BufferAllocationException.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import java.util.IllegalFormatException; 4 | import java.util.Locale; 5 | 6 | @SuppressWarnings("serial") 7 | public class BufferAllocationException extends RuntimeException 8 | { 9 | BufferAllocationException(final String message) { 10 | super(message); 11 | } 12 | 13 | public BufferAllocationException(final String message, final Object... args) { 14 | this(message, null, args); 15 | } 16 | 17 | public BufferAllocationException(final String message, final Throwable t, final Object... args) { 18 | super(BufferAllocationException.formatMessage(message, t, args), t); 19 | } 20 | 21 | private static String formatMessage(final String message, final Throwable t, final Object... args) { 22 | 23 | String formattedMessage = ""; 24 | 25 | try { 26 | 27 | formattedMessage = String.format(message, args); 28 | } 29 | catch (final IllegalFormatException e) { 30 | 31 | //something bad happened , replace by a default message 32 | formattedMessage = "'" + message + "' message has ILLEGAL FORMAT, ARGS SUPPRESSED !"; 33 | 34 | //Problem is, this IllegalFormatException may have masked the originally sent exception t, 35 | //so be it. 36 | //We can't use Throwable.setSuppressed() (Java 1.7+) because we want to continue 37 | //to accommodate Java 1.5+. 38 | } 39 | 40 | return formattedMessage; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /hppcrt/src/main/java/com/carrotsearch/hppcrt/Containers.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import java.security.PrivilegedAction; 4 | import java.util.logging.Level; 5 | import java.util.logging.Logger; 6 | 7 | import com.carrotsearch.hppcrt.hash.BitMixer; 8 | import com.carrotsearch.hppcrt.hash.MurmurHash3; 9 | 10 | public final class Containers 11 | { 12 | public final static int NB_OF_PROCESSORS = Runtime.getRuntime().availableProcessors(); 13 | 14 | /** 15 | * The default number of expected elements for containers. 16 | * (power-of-two to accommodate special containers also) 17 | */ 18 | public final static int DEFAULT_EXPECTED_ELEMENTS = 1 << 3; 19 | 20 | /** 21 | * External initial seed value. 22 | * 23 | * @see #randomSeed64() 24 | */ 25 | private static volatile String testsSeedProperty; 26 | 27 | /** 28 | * Unique marker for {@link #testsSeedProperty}. 29 | */ 30 | private final static String NOT_AVAILABLE = ""; 31 | 32 | /** 33 | * No instances. 34 | */ 35 | private Containers() { 36 | //nothing 37 | } 38 | 39 | /** 40 | * Provides a (possibly) random initial seed for randomized stuff, 64 bit version. 41 | * 42 | * If tests.seed property is available and accessible, the 43 | * returned value will be derived from the value of that property and will be 44 | * constant to ensure reproducibility in presence of the randomized testing 45 | * package, which sets this property. 46 | * Full random values are returned if either tests.seed is not available, or is set to "" (empty String). 47 | * @see "https://github.com/carrotsearch/randomizedtesting" 48 | */ 49 | public static long randomSeed64() { 50 | 51 | if (Containers.testsSeedProperty == null) { 52 | try { 53 | Containers.testsSeedProperty = java.security.AccessController.doPrivileged(new PrivilegedAction() { 54 | @Override 55 | public String run() { 56 | return System.getProperty("tests.seed", Containers.NOT_AVAILABLE); 57 | } 58 | }); 59 | } catch (final SecurityException e) { 60 | // If failed on security exception, don't panic. 61 | Containers.testsSeedProperty = Containers.NOT_AVAILABLE; 62 | Logger.getLogger(Containers.class.getName()).log(Level.INFO, 63 | "Failed to read 'tests.seed' property for initial random seed.", e); 64 | } 65 | } 66 | 67 | long initialSeed; 68 | if (!Containers.testsSeedProperty.equals(Containers.NOT_AVAILABLE)) { 69 | initialSeed = Containers.testsSeedProperty.hashCode(); 70 | } else { 71 | // Mix something that is changing over time (nanoTime) 72 | // ... with something that is thread-local and relatively unique 73 | // even for very short time-spans (new Object's address from a TLAB). 74 | initialSeed = System.nanoTime() ^ System.identityHashCode(new Object()); 75 | } 76 | 77 | return MurmurHash3.mix64(initialSeed); 78 | } 79 | 80 | /** 81 | * Same as {@link #randomSeed64()}, but returns 32 bit 82 | */ 83 | public static int randomSeed32() { 84 | 85 | final long longId = Containers.randomSeed64(); 86 | 87 | //fold to 32 bit, try to preserve maximum information 88 | return (int) ((longId >>> 32) ^ longId); 89 | } 90 | 91 | /** 92 | * Call this in order to make {@link #randomSeed64()} or {@link #randomSeed32()} 93 | * re-read the tests.seed property which control their random number generation. 94 | */ 95 | public static void forceReloadTestProperty() { 96 | 97 | Containers.testsSeedProperty = null; 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /hppcrt/src/main/java/com/carrotsearch/hppcrt/Generated.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import static java.lang.annotation.ElementType.*; 4 | import static java.lang.annotation.RetentionPolicy.*; 5 | 6 | import java.lang.annotation.Documented; 7 | import java.lang.annotation.Retention; 8 | import java.lang.annotation.Target; 9 | 10 | @Documented 11 | @Retention(SOURCE) 12 | @Target({PACKAGE, TYPE, ANNOTATION_TYPE, METHOD, CONSTRUCTOR, FIELD, 13 | LOCAL_VARIABLE, PARAMETER}) 14 | public @interface Generated { 15 | /** 16 | * The value element MUST have the name of the code generator. 17 | * The recommended convention is to use the fully qualified name of the 18 | * code generator. For example: com.acme.generator.CodeGen. 19 | */ 20 | String[] value(); 21 | 22 | /** 23 | * Date when the source was generated. 24 | */ 25 | String date() default ""; 26 | 27 | /** 28 | * A place holder for any comments that the code generator may want to 29 | * include in the generated code. 30 | */ 31 | String comments() default ""; 32 | } 33 | -------------------------------------------------------------------------------- /hppcrt/src/main/java/com/carrotsearch/hppcrt/ObjectFactory.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | /** 4 | * Generic Object factory, returning new instances of objects E, 5 | * with the ability to reinitialize the instances of E. 6 | * @param 7 | */ 8 | public interface ObjectFactory { 9 | 10 | /** 11 | * 12 | * @return a new Object instance E 13 | */ 14 | E create(); 15 | 16 | /** 17 | * Method to initialize/re-initialize the object 18 | * when the object is borrowed from an {@link ObjectPool}. That way, 19 | * any object coming out of a pool is set properly 20 | * in a user-controlled state. 21 | * @param obj 22 | */ 23 | void initialize(E obj); 24 | 25 | /** 26 | * Method to reset the object 27 | * when the object is released, to return to an {@link ObjectPool}. That way, 28 | * any object returning to a pool is properly cleaned-up 29 | * @param obj 30 | */ 31 | void reset(E obj); 32 | } 33 | -------------------------------------------------------------------------------- /hppcrt/src/main/java/com/carrotsearch/hppcrt/XorShift128P.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import java.util.Random; 4 | 5 | import com.carrotsearch.hppcrt.hash.MurmurHash3; 6 | 7 | /** 8 | * A fast pseudo-random number generator. This class is not thread-safe and 9 | * should be used from a single thread only. 10 | * 11 | * @see "http://xorshift.di.unimi.it/" 12 | * @see "http://xorshift.di.unimi.it/xorshift128plus.c" 13 | */ 14 | @SuppressWarnings("serial") 15 | public class XorShift128P extends Random 16 | { 17 | /* 18 | * 128 bits of state. 19 | */ 20 | private long state0, state1; 21 | 22 | public XorShift128P(final long seed) { 23 | 24 | this.state0 = XorShift128P.notZero(MurmurHash3.mix64(seed)); 25 | this.state1 = XorShift128P.notZero(MurmurHash3.mix64(seed + 1)); 26 | } 27 | 28 | public XorShift128P() { 29 | this(Containers.randomSeed64()); 30 | } 31 | 32 | @Override 33 | public long nextLong() { 34 | long s1 = this.state0; 35 | final long s0 = this.state1; 36 | this.state0 = s0; 37 | s1 ^= s1 << 23; 38 | return (this.state1 = (s1 ^ s0 ^ (s1 >>> 17) ^ (s0 >>> 26))) + s0; 39 | } 40 | 41 | @Override 42 | public int nextInt() { 43 | return (int) nextLong(); 44 | } 45 | 46 | @Override 47 | protected int next(final int bits) { 48 | return (int) (nextLong() & ((1L << bits) - 1)); 49 | } 50 | 51 | private static long notZero(final long value) { 52 | return value == 0 ? 0xdeadbeefbabeL : value; 53 | } 54 | 55 | @Override 56 | public int nextInt(final int bound) { 57 | if (bound <= 0) { 58 | throw new IllegalArgumentException(); 59 | } 60 | 61 | int r = (nextInt() >>> 1); 62 | final int m = bound - 1; 63 | if ((bound & m) == 0) { 64 | r = (int) ((bound * (long) r) >> 31); 65 | } else { 66 | for (int u = r; u - (r = u % bound) + m < 0; u = nextInt() >>> 1) { 67 | } 68 | } 69 | 70 | return r; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /hppcrt/src/main/java/com/carrotsearch/hppcrt/cursors/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | High Performance Primitive Collections Realtime (HPPC-RT): cursors 5 | 10 | 11 | 12 | 13 | Cursors are composite holders (data plus index) used in iterators. 14 | 15 | -------------------------------------------------------------------------------- /hppcrt/src/main/java/com/carrotsearch/hppcrt/hash/MurmurHash3.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.hash; 2 | 3 | /** 4 | * Hash-mixing routines for primitive types. The implementation is based on the 5 | * finalization step from Austin Appleby's MurmurHash3. and David 6 | * Stafford variant 9 of 64bit mix function (MH3 finalization step, with 7 | * different shifts and constants) 8 | * 9 | * @see "http://sites.google.com/site/murmurhash/" 10 | */ 11 | public final class MurmurHash3 12 | { 13 | /** 14 | * = hash((int)0) 15 | */ 16 | public static final int HASH_0 = 0; 17 | 18 | /** 19 | * = hash((int)1) 20 | */ 21 | public static final int HASH_1 = 1364076727; 22 | 23 | private static final int MUL1_INT = 0x85ebca6b; 24 | private static final int MUL2_INT = 0xc2b2ae35; 25 | 26 | private static final long MUL1_LONG = 0x4cd6944c5cc20b6dL; 27 | private static final long MUL2_LONG = 0xfc12c5b19d3259e9L; 28 | 29 | private MurmurHash3() { 30 | // no instances. 31 | } 32 | 33 | /** 34 | * Mix a 4-byte sequence (Java int), MH3's plain finalization step. 35 | */ 36 | public static int mix32(int k) { 37 | 38 | k = (k ^ (k >>> 16)) * MurmurHash3.MUL1_INT; 39 | k = (k ^ (k >>> 13)) * MurmurHash3.MUL2_INT; 40 | 41 | return k ^ (k >>> 16); 42 | } 43 | 44 | /** 45 | * Mix an 8-byte sequence (Java long): Computes David Stafford variant 9 of 46 | * 64bit mix function (MH3 finalization step, with different shifts and 47 | * constants). 48 | * 49 | * Variant 9 is picked because it contains two 32-bit shifts which could be 50 | * possibly optimized into better machine code. 51 | * 52 | * @see "http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html" 53 | */ 54 | public static long mix64(long z) { 55 | 56 | z = (z ^ (z >>> 32)) * MurmurHash3.MUL1_LONG; 57 | z = (z ^ (z >>> 29)) * MurmurHash3.MUL2_LONG; 58 | return z ^ (z >>> 32); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /hppcrt/src/main/java/com/carrotsearch/hppcrt/hash/PhiMix.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.hash; 2 | 3 | /** 4 | * Quickly mixes the bits of integers. 5 | *

6 | * Those methods mixes the bits of the argument by multiplying by the golden 7 | * ratio and xorshifting the result. 8 | *

9 | * It is borrowed from Koloboke, and it has slightly 11 | * worse behaviour than {@link MurmurHash3} (in open-addressing hash tables the 12 | * average number of probes is slightly larger), but it's much faster. 13 | *

14 | * Reciprocal mixing functions are borrowed from fastutil. 16 | */ 17 | public final class PhiMix 18 | { 19 | /** 20 | * 232 · φ, φ = (√5 − 1)/2. (package 21 | * visibility) 22 | */ 23 | static final int INT_PHI = 0x9E3779B9; 24 | 25 | /** 26 | * 264 · φ, φ = (√5 − 1)/2. (package 27 | * visibility) 28 | */ 29 | static final long LONG_PHI = 0x9E3779B97F4A7C15L; 30 | 31 | /** 32 | * The reciprocal of {@link #INT_PHI} modulo 232. 33 | */ 34 | private static final int INV_INT_PHI = 0x144cbc89; 35 | 36 | /** 37 | * The reciprocal of {@link #LONG_PHI} modulo 264. 38 | */ 39 | private static final long INV_LONG_PHI = 0xf1de83e19937733dL; 40 | 41 | /** 42 | * = hash((int)0) 43 | */ 44 | public static final int HASH_0 = 0; 45 | 46 | /** 47 | * = hash((int)1) 48 | */ 49 | public static final int HASH_1 = 1640556430; 50 | 51 | private PhiMix() { 52 | // no instances. 53 | } 54 | 55 | /** 56 | * Hashes a 4-byte sequence (Java int). 57 | * 58 | * @param x 59 | * an integer. 60 | * @return a hash value obtained by mixing the bits of {@code x}. 61 | */ 62 | public static int mix32(final int x) { 63 | final int h = x * PhiMix.INT_PHI; 64 | return h ^ (h >> 16); 65 | } 66 | 67 | /** 68 | * The inverse of {@link #mix32(int)}. This method is mainly useful to create 69 | * unit tests. 70 | * 71 | * @param x 72 | * an integer. 73 | * @return a value that passed through {@link #mix32(int)} would give {@code x} 74 | * . 75 | */ 76 | public static int invMix32(final int x) { 77 | return (x ^ x >>> 16) * PhiMix.INV_INT_PHI; 78 | } 79 | 80 | /** 81 | * Hashes an 8-byte sequence (Java long). 82 | * 83 | * @param x 84 | * a long integer. 85 | * @return a hash value obtained by mixing the bits of {@code x}. 86 | */ 87 | public static long mix64(final long x) { 88 | long h = x * PhiMix.LONG_PHI; 89 | h ^= h >> 32; 90 | return h ^ (h >> 16); 91 | } 92 | 93 | /** 94 | * The inverse of {@link #mix64(long)}. This method is mainly useful to create 95 | * unit tests. 96 | * 97 | * @param x 98 | * a long integer. 99 | * @return a value that passed through {@link #mix64(long)} would give 100 | * {@code x}. 101 | */ 102 | public static long invMix64(long x) { 103 | x ^= x >>> 32; 104 | x ^= x >>> 16; 105 | return (x ^ x >>> 32) * PhiMix.INV_LONG_PHI; 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /hppcrt/src/main/java/com/carrotsearch/hppcrt/hash/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | High Performance Primitive Collections Realtime (HPPC-RT): hash functions 5 | 10 | 11 | 12 | 13 | Various hash functions primarily for use in associative containers. 14 | 15 | -------------------------------------------------------------------------------- /hppcrt/src/main/java/com/carrotsearch/hppcrt/heaps/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | High Performance Primitive Collections Realtime (HPPC-RT): heaps 5 | 10 | 11 | 12 | 13 | Heap containers implementing direct or indirect (a.k.a indexed) priority queues. 14 | 15 | -------------------------------------------------------------------------------- /hppcrt/src/main/java/com/carrotsearch/hppcrt/lists/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | High Performance Primitive Collections Realtime (HPPC-RT): lists 5 | 10 | 11 | 12 | 13 | List-like containers with various element sequence implementations: arrays, linked-lists, deques. 14 | 15 | -------------------------------------------------------------------------------- /hppcrt/src/main/java/com/carrotsearch/hppcrt/maps/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | High Performance Primitive Collections Realtime (HPPC-RT): maps 5 | 10 | 11 | 12 | 13 | Various unordered map container implementations: open addressing hash, identity hash. 14 | 15 | -------------------------------------------------------------------------------- /hppcrt/src/main/java/com/carrotsearch/hppcrt/predicates/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | High Performance Primitive Collections Realtime (HPPC-RT): predicates 5 | 10 | 11 | 12 | 13 | Predicates that return a boolean true/ false for 14 | a given value. Useful in pseudo-closures applied to values of containers. 15 | 16 | -------------------------------------------------------------------------------- /hppcrt/src/main/java/com/carrotsearch/hppcrt/procedures/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | High Performance Primitive Collections Realtime (HPPC-RT): procedures 5 | 10 | 11 | 12 | 13 | Procedures apply to keys and/or values in a container. 14 | Useful in pseudo-closures. 15 | 16 | -------------------------------------------------------------------------------- /hppcrt/src/main/java/com/carrotsearch/hppcrt/sets/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | High Performance Primitive Collections Realtime (HPPC-RT): sets 5 | 10 | 11 | 12 | 13 | Various unordered set container implementations: open addressing hash, identity hash. 14 | 15 | -------------------------------------------------------------------------------- /hppcrt/src/main/java/com/carrotsearch/hppcrt/sorting/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | High Performance Primitive Collections Realtime (HPPC-RT): sorting 5 | 10 | 11 | 12 | 13 | Sorting routines and various sorting utilities. 14 | 15 | -------------------------------------------------------------------------------- /hppcrt/src/main/java/com/carrotsearch/hppcrt/strategies/package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | High Performance Primitive Collections Realtime (HPPC-RT): strategies 5 | 10 | 11 | 12 | 13 | Strategies for containers in order to customize comparison (heaps, sorting) behavior towards elements. 14 | 15 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/IntKTypeAssociativeContainer.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import java.util.Iterator; 4 | 5 | import com.carrotsearch.hppcrt.cursors.*; 6 | import com.carrotsearch.hppcrt.predicates.*; 7 | import com.carrotsearch.hppcrt.procedures.*; 8 | 9 | /** 10 | * An associative container (alias: map, dictionary) from keys to (one or possibly more) values. 11 | * Object keys must fulfill the contract of {@link Object#hashCode()} and {@link Object#equals(Object)}. 12 | * This is indeed a placeholder for template compilation, 13 | * and will indeed be replaced by a (int, VType) instantiation 14 | * of KTypeVTypeAssociativeContainer 15 | * 16 | * @see KTypeContainer 17 | */ 18 | /*! ($TemplateOptions.doNotGenerate()) !*/ 19 | public interface IntKTypeAssociativeContainer 20 | extends Iterable> 21 | { 22 | 23 | @Override 24 | Iterator> iterator(); 25 | 26 | boolean containsKey(int key); 27 | 28 | int size(); 29 | 30 | int capacity(); 31 | 32 | boolean isEmpty(); 33 | 34 | int removeAll(IntContainer container); 35 | 36 | int removeAll(IntPredicate predicate); 37 | 38 | public int removeAll(IntKTypePredicate predicate); 39 | 40 | > T forEach(T procedure); 41 | 42 | > T forEach(T predicate); 43 | 44 | void clear(); 45 | 46 | IntCollection keys(); 47 | 48 | KTypeCollection values(); 49 | } 50 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/IntKTypeMap.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import com.carrotsearch.hppcrt.cursors.*; 4 | 5 | /** 6 | * An associative container with unique binding from ints to a single value. 7 | * This is indeed a placeholder for template compilation, 8 | * and will indeed be replaced by a (int, VType) instantiation 9 | * of KTypeVTypeMap 10 | */ 11 | /*! ($TemplateOptions.doNotGenerate()) !*/ 12 | public interface IntKTypeMap 13 | extends IntKTypeAssociativeContainer 14 | { 15 | 16 | T put(int key, T value); 17 | 18 | boolean putIfAbsent(final int key, final T value); 19 | 20 | T putOrAdd(int key, T putValue, T additionValue); 21 | 22 | T addTo(int key, T additionValue); 23 | 24 | T get(int key); 25 | 26 | int putAll(IntKTypeAssociativeContainer container); 27 | 28 | int putAll(Iterable> iterable); 29 | 30 | T remove(int key); 31 | 32 | T getDefaultValue(); 33 | 34 | void setDefaultValue(final T defaultValue); 35 | } 36 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/KTypeCollection.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import com.carrotsearch.hppcrt.predicates.KTypePredicate; 4 | 5 | /** 6 | * A collection allows basic, efficient operations on sets of elements 7 | * (difference and intersection). 8 | */ 9 | /*! ${TemplateOptions.generatedAnnotation} !*/ 10 | public interface KTypeCollection extends KTypeContainer 11 | { 12 | /** 13 | * Removes all occurrences of e from this collection. 14 | * 15 | * @param e Element to be removed from this collection, if present. 16 | * @return The number of removed elements as a result of this call. 17 | */ 18 | int removeAll(KType e); 19 | 20 | /** 21 | * Removes all elements in this collection that are present 22 | * in c. Runs in time proportional to the number 23 | * of elements in this collection. Equivalent of sets difference. 24 | * 25 | * @return Returns the number of removed elements. 26 | */ 27 | int removeAll(KTypeLookupContainer c); 28 | 29 | /** 30 | * Removes all elements in this collection for which the 31 | * given predicate returns true. 32 | * 33 | * @return Returns the number of removed elements. 34 | */ 35 | int removeAll(KTypePredicate predicate); 36 | 37 | /** 38 | * Keeps all elements in this collection that are present 39 | * in c. Runs in time proportional to the number 40 | * of elements in this collection. Equivalent of sets intersection. 41 | * 42 | * @return Returns the number of removed elements. 43 | */ 44 | int retainAll(KTypeLookupContainer c); 45 | 46 | /** 47 | * Keeps all elements in this collection for which the 48 | * given predicate returns true. 49 | * 50 | * @return Returns the number of removed elements. 51 | */ 52 | int retainAll(KTypePredicate predicate); 53 | 54 | /** 55 | * Removes all elements from this collection. 56 | */ 57 | void clear(); 58 | } 59 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/KTypeDeque.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import java.util.Iterator; 4 | import java.util.List; 5 | 6 | import com.carrotsearch.hppcrt.cursors.*; 7 | import com.carrotsearch.hppcrt.predicates.*; 8 | import com.carrotsearch.hppcrt.procedures.*; 9 | 10 | /** 11 | * A double-sided queue of KTypes. 12 | */ 13 | /*! ${TemplateOptions.generatedAnnotation} !*/ 14 | public interface KTypeDeque extends KTypeCollection 15 | { 16 | /** 17 | * Removes the first element that equals e1, returning its 18 | * deleted position or -1 if the element was not found. 19 | */ 20 | public int removeFirst(KType e1); 21 | 22 | /** 23 | * Removes the last element that equals e1, returning its 24 | * deleted position or -1 if the element was not found. 25 | */ 26 | public int removeLast(KType e1); 27 | 28 | /** 29 | * Inserts the specified element at the front of this deque. 30 | * 31 | * @param e1 the element to add 32 | */ 33 | public void addFirst(KType e1); 34 | 35 | /** 36 | * Inserts the specified element at the end of this deque. 37 | * 38 | * @param e1 the element to add 39 | */ 40 | public void addLast(KType e1); 41 | 42 | /** 43 | * Retrieves and removes the first element of this deque. 44 | * Precondition : the deque is not empty ! 45 | * @return the head element of this deque. 46 | * @throws AssertionError if this deque is empty and assertions are enabled. 47 | */ 48 | public KType removeFirst(); 49 | 50 | /** 51 | * Retrieves and removes the last element of this deque. 52 | * Precondition : the deque is not empty ! 53 | * @return the tail of this deque. 54 | * @throws AssertionError if this deque is empty and assertions are enabled. 55 | */ 56 | public KType removeLast(); 57 | 58 | /** 59 | * Retrieves, but does not remove, the first element of this deque. 60 | * Precondition : the deque is not empty ! 61 | * @return the head of this deque. 62 | * @throws AssertionError if this deque is empty and assertions are enabled. 63 | */ 64 | public KType getFirst(); 65 | 66 | /** 67 | * Retrieves, but does not remove, the last element of this deque. 68 | * Precondition : the deque is not empty ! 69 | * @return the tail of this deque. 70 | * @throws AssertionError if this deque is empty and assertions are enabled. 71 | */ 72 | public KType getLast(); 73 | 74 | /** 75 | * @return An iterator over elements in this deque in tail-to-head order. 76 | */ 77 | public Iterator> descendingIterator(); 78 | 79 | /** 80 | * Applies a procedure to all container elements. 81 | */ 82 | public > T descendingForEach(T procedure); 83 | 84 | /** 85 | * Applies a predicate to container elements as long, as the predicate 86 | * returns true. The iteration is interrupted otherwise. 87 | */ 88 | public > T descendingForEach(T predicate); 89 | } 90 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/KTypeIndexedContainer.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import java.util.List; 4 | import java.util.RandomAccess; 5 | 6 | import com.carrotsearch.hppcrt.predicates.KTypePredicate; 7 | import com.carrotsearch.hppcrt.procedures.KTypeProcedure; 8 | 9 | /** 10 | * An indexed container provides random access to elements based on an 11 | * index. Indexes are zero-based. 12 | */ 13 | /*! ${TemplateOptions.generatedAnnotation} !*/ 14 | public interface KTypeIndexedContainer extends KTypeCollection, RandomAccess 15 | { 16 | /** 17 | * Removes the first element that equals e1, returning its 18 | * deleted position or -1 if the element was not found. 19 | */ 20 | int removeFirst(KType e1); 21 | 22 | /** 23 | * Removes the last element that equals e1, returning its 24 | * deleted position or -1 if the element was not found. 25 | */ 26 | int removeLast(KType e1); 27 | 28 | /** 29 | * Returns the index of the first occurrence of the specified element in this list, 30 | * or -1 if this list does not contain the element. 31 | */ 32 | int indexOf(KType e1); 33 | 34 | /** 35 | * Returns the index of the last occurrence of the specified element in this list, 36 | * or -1 if this list does not contain the element. 37 | */ 38 | int lastIndexOf(KType e1); 39 | 40 | /** 41 | * Adds an element to the end of this container (the last index is incremented by one). 42 | */ 43 | void add(KType e1); 44 | 45 | /** 46 | * Inserts the specified element at the specified position in this list. 47 | * 48 | * @param index The index at which the element should be inserted, shifting 49 | * any existing and subsequent elements to the right. 50 | * Precondition : index must be valid ! 51 | */ 52 | void insert(int index, KType e1); 53 | 54 | /** 55 | * Replaces the element at the specified position in this list 56 | * with the specified element. 57 | * Precondition : index must be valid ! 58 | * @return Returns the previous value in the list. 59 | */ 60 | KType set(int index, KType e1); 61 | 62 | /** 63 | * @return Returns the element at index index from the list. 64 | * Precondition : index must be valid ! 65 | */ 66 | public KType get(int index); 67 | 68 | /** 69 | * Removes the element at the specified position in this list and returns it. 70 | * Precondition : index must be valid ! 71 | *

Careful. Do not confuse this method with the overridden signature in 72 | * Java Collections ({@link List#remove(Object)}). Use: {@link #removeAll}, 73 | * {@link #removeFirst} or {@link #removeLast} depending 74 | * on the actual need.

75 | */ 76 | KType remove(int index); 77 | 78 | /** 79 | * Removes from this list all of the elements whose index is between 80 | * fromIndex, inclusive, and toIndex, exclusive. 81 | */ 82 | void removeRange(int fromIndex, int toIndex); 83 | 84 | /** 85 | * Applies procedure to a slice of the container, 86 | * fromIndex, inclusive, to toIndex, exclusive. 87 | */ 88 | > T forEach(final T procedure, final int fromIndex, final int toIndex); 89 | 90 | /** 91 | * Applies predicate to a slice of the container, 92 | * fromIndex, inclusive, to toIndex, 93 | * exclusive, or until predicate returns false. 94 | */ 95 | > T forEach(final T predicate, final int fromIndex, final int toIndex); 96 | 97 | } 98 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/KTypeLookupContainer.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | /** 4 | * Marker interface for containers that can check if they contain a given object in 5 | * at least time O(log n) and ideally in amortized 6 | * constant time O(1). 7 | */ 8 | /*! ${TemplateOptions.generatedAnnotation} !*/ 9 | public interface KTypeLookupContainer extends KTypeContainer 10 | { 11 | public boolean contains(KType e); 12 | } 13 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/KTypePriorityQueue.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | 4 | /** 5 | * A Priority queue of KTypes. 6 | */ 7 | /*! ${TemplateOptions.generatedAnnotation} !*/ 8 | public interface KTypePriorityQueue extends KTypeCollection 9 | { 10 | /** 11 | * Add a k element in the priority queue 12 | * @param k 13 | */ 14 | void add(KType k); 15 | 16 | /** 17 | * Retrieve, but not remove, the top element of the queue, 18 | * i.e. the min element with respect to the comparison criteria 19 | * (implementation defined) 20 | * of the queue. Returns the default value if empty. 21 | */ 22 | KType top(); 23 | 24 | /** 25 | * Retrieve, and remove the top element of the queue, 26 | * i.e. the min element with respect to the comparison criteria 27 | * (implementation defined) Returns the default value if empty. 28 | */ 29 | KType popTop(); 30 | 31 | /** 32 | * Update priorities of all the elements of the queue, to re-establish the correct priorities 33 | * towards the comparison criteria. 34 | */ 35 | void updatePriorities(); 36 | 37 | /** 38 | * Update the priority of the {@link #top()} element, to re-establish its actual priority 39 | * towards the comparison criteria when it may have changed such that it is no longer the 40 | * min element with respect to the comparison criteria. 41 | */ 42 | void updateTopPriority(); 43 | 44 | /** 45 | * Returns the "default value" value used 46 | * in methods returning "default value" 47 | */ 48 | KType getDefaultValue(); 49 | 50 | /** 51 | * Set the "default value" value to be used 52 | * in methods returning "default value" 53 | */ 54 | void setDefaultValue(final KType defaultValue); 55 | 56 | } 57 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/KTypeSet.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import com.carrotsearch.hppcrt.KTypeCollection; 4 | import com.carrotsearch.hppcrt.cursors.KTypeCursor; 5 | 6 | /** 7 | * A set of KTypes. 8 | */ 9 | /*! ${TemplateOptions.generatedAnnotation} !*/ 10 | public interface KTypeSet extends KTypeCollection 11 | { 12 | /** 13 | * Adds k to the set. 14 | * 15 | * @return Returns true if this element was not part of the set before. Returns 16 | * false if an equal element is already part of the set, and leaves the set unchanged. . 17 | */ 18 | boolean add(KType k); 19 | 20 | /** 21 | * Adds all elements from a given container to this set. 22 | * 23 | * @return Returns the number of elements actually added as a result of this 24 | * call (not previously present in the set). 25 | */ 26 | int addAll(final KTypeContainer container); 27 | 28 | /** 29 | * Adds all elements from a given iterable to this set. 30 | * 31 | * @return Returns the number of elements actually added as a result of this 32 | * call (not previously present in the set). 33 | */ 34 | int addAll(final Iterable> iterable); 35 | 36 | /** 37 | * Remove all elements of the set matching key. Returns true 38 | * if key was present in the set and has been successfully removed. 39 | * This is indeed an alias for {@code KTypeCollection.removeAll(key) > 0} 40 | * @see KTypeCollection#removeAll 41 | */ 42 | boolean remove(KType key); 43 | } 44 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/VTypeArrays.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import java.util.Arrays; 4 | 5 | import com.carrotsearch.hppcrt.KTypeArrays; 6 | import com.carrotsearch.hppcrt.KTypeIndexedContainer; 7 | 8 | /** 9 | * Utility class gathering array or KTypeIndexedContainer handling algorithms for KTypes. 10 | * This is indeed a placeholder for template compilation, 11 | * and will indeed be replaced by a (VType) instantiation 12 | * of KTypeArrays. 13 | */ 14 | /*! ${TemplateOptions.doNotGenerate()} !*/ 15 | public final class VTypeArrays 16 | { 17 | private VTypeArrays() { 18 | //nothing 19 | } 20 | 21 | /** 22 | * Rotate utility : 23 | * Transforms the range [[slice_1: from; mid - 1][slice_2: mid, to - 1]] of table, into 24 | * [[slice_2][slice_1]]in place, i.e swap the two slices while keeping their own internal order. 25 | * @param table 26 | * @param from the start range to consider 27 | * @param mid start index of the second slice 28 | * @param to the array end range, exclusive 29 | */ 30 | public static void rotate(final T[] table, final int from, final int mid, final int to) { 31 | 32 | KTypeArrays. rotate(table, from, mid, to); 33 | } 34 | 35 | /** 36 | * Rotate utility : 37 | * Transforms the range [[slice_1: from; mid - 1][slice_2: mid, to - 1]] of KTypeIndexedContainer, into 38 | * [[slice_2][slice_1]] in place, i.e swap the two slices while keeping their own internal order. 39 | * @param table 40 | * @param from the start range to consider 41 | * @param mid start index of the second slice 42 | * @param to the array end range, exclusive 43 | */ 44 | public static void rotate(final KTypeIndexedContainer table, final int from, final int mid, final int to) { 45 | 46 | KTypeArrays. rotate(table, from, mid, to); 47 | } 48 | 49 | /** 50 | * Reverse the elements positions of the specified range of array table : 51 | * @param table 52 | * @param from the start range to consider 53 | * @param to the array end range, exclusive 54 | */ 55 | public static void reverse(final T[] table, final int from, final int to) { 56 | 57 | KTypeArrays. reverse(table, from, to); 58 | } 59 | 60 | /** 61 | * Reverse the elements positions of the specified range of KTypeIndexedContainer table : 62 | * @param table 63 | * @param from the start range to consider 64 | * @param to the array end range, exclusive 65 | */ 66 | public static void reverse(final KTypeIndexedContainer table, final int from, final int to) { 67 | 68 | KTypeArrays. reverse(table, from, to); 69 | 70 | } 71 | 72 | /** 73 | * Method to blank any KType[] array elements to its default value 74 | * from [startIndex; endIndex[, equivalent to {@link Arrays}.fill(objectArray, startIndex, endIndex, 0 or null) 75 | */ 76 | public static void blankArray(final T[] objectArray, final int startIndex, final int endIndex) { 77 | 78 | KTypeArrays. blankArray(objectArray, startIndex, endIndex); 79 | 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/cursors/IntKTypeCursor.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.cursors; 2 | 3 | /*! ($TemplateOptions.doNotGenerate()) !*/ 4 | /** 5 | * Place holder 6 | * @author Vincent 7 | * 8 | */ 9 | public final class IntKTypeCursor 10 | { 11 | public int index; 12 | 13 | public int key; 14 | 15 | public T value; 16 | 17 | @Override 18 | public String toString() 19 | { 20 | return "[cursor, index: " + this.index + ", key: " + this.key + ", value: " + this.value + "]"; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/cursors/KTypeCursor.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.cursors; 2 | 3 | /** 4 | * A cursor over a collection of KTypes. 5 | */ 6 | /*! ${TemplateOptions.generatedAnnotation} !*/ 7 | public final class KTypeCursor 8 | { 9 | /** 10 | * The current value's index in the container this cursor belongs to. The meaning of 11 | * this index is defined by the container (usually it will be an index in the underlying 12 | * storage buffer). 13 | */ 14 | public int index; 15 | 16 | /** 17 | * The current value. 18 | */ 19 | public KType value; 20 | 21 | @Override 22 | public String toString() 23 | { 24 | return "[cursor, index: " + index + ", value: " + value + "]"; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/cursors/KTypeVTypeCursor.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.cursors; 2 | 3 | /** 4 | * A cursor over entries of an associative container (KType keys and VType values). 5 | */ 6 | /*! ${TemplateOptions.generatedAnnotation} !*/ 7 | public final class KTypeVTypeCursor 8 | { 9 | /** 10 | * The current key and value's index in the container this cursor belongs to. The meaning of 11 | * this index is defined by the container (usually it will be an index in the underlying 12 | * storage buffer). 13 | */ 14 | public int index; 15 | 16 | /** 17 | * The current key. 18 | */ 19 | public KType key; 20 | 21 | /** 22 | * The current value. 23 | */ 24 | public VType value; 25 | 26 | @Override 27 | public String toString() 28 | { 29 | return "[cursor, index: " + this.index + ", key: " + this.key + ", value: " + this.value + "]"; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/predicates/IntKTypePredicate.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.predicates; 2 | 3 | /** 4 | * A predicate that applies to int, KType pairs. 5 | * This is indeed a placeholder for template compilation, 6 | * and will indeed be replaced by a (int, VType) instantiation 7 | * of KTypeVTypePredicate 8 | */ 9 | /*! ($TemplateOptions.doNotGenerate()) !*/ 10 | public interface IntKTypePredicate 11 | { 12 | boolean apply(int key, KType value); 13 | } 14 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/predicates/KTypePredicate.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.predicates; 2 | 3 | /** 4 | * A predicate that applies to KType objects. 5 | */ 6 | /*! ${TemplateOptions.generatedAnnotation} !*/ 7 | public interface KTypePredicate 8 | { 9 | boolean apply(KType value); 10 | } 11 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/predicates/KTypeVTypePredicate.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.predicates; 2 | 3 | /** 4 | * A predicate that applies to KType, VType pairs. 5 | */ 6 | /*! ${TemplateOptions.generatedAnnotation} !*/ 7 | public interface KTypeVTypePredicate 8 | { 9 | boolean apply(KType key, VType value); 10 | } 11 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/procedures/IntKTypeProcedure.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.procedures; 2 | 3 | /** 4 | * A procedure that applies to int, KType pairs. 5 | * This is indeed a placeholder for template compilation, 6 | * and will indeed be replaced by a (int, VType) instantiation 7 | * of KTypeVTypeProcedure 8 | */ 9 | /*! ($TemplateOptions.doNotGenerate()) !*/ 10 | public interface IntKTypeProcedure 11 | { 12 | void apply(int key, KType value); 13 | } 14 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/procedures/KTypeProcedure.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.procedures; 2 | 3 | /** 4 | * A procedure that applies to KType objects. 5 | */ 6 | /*! ${TemplateOptions.generatedAnnotation} !*/ 7 | public interface KTypeProcedure 8 | { 9 | void apply(KType value); 10 | } 11 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/procedures/KTypeVTypeProcedure.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.procedures; 2 | 3 | /** 4 | * A procedure that applies to KType, VType pairs. 5 | */ 6 | /*! ${TemplateOptions.generatedAnnotation} !*/ 7 | public interface KTypeVTypeProcedure 8 | { 9 | void apply(KType key, VType value); 10 | } 11 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/sets/KTypeIdentityHashSet.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.sets; 2 | 3 | import com.carrotsearch.hppcrt.*; 4 | import com.carrotsearch.hppcrt.strategies.*; 5 | 6 | /*! ${TemplateOptions.doNotGenerateKType( "BYTE", "CHAR", "SHORT", "INT", "LONG", "FLOAT", "DOUBLE")} !*/ 7 | /** 8 | * An identity hash set of KType types, implemented using open 9 | * addressing with linear probing for collision resolution. 10 | * 11 | * The difference with {@link KTypeHashSet} is that it uses direct Object reference equality for comparison and 12 | * direct "address" {@link System#identityHashCode(Object)} for hashCode(), instead of using 13 | * the built-in {@link #hashCode()} / {@link #equals(Object)}. 14 | * 15 | *

This implementation supports null keys.

16 | * 17 | */ 18 | /*! ${TemplateOptions.generatedAnnotation} !*/ 19 | public class KTypeIdentityHashSet 20 | extends KTypeHashSet 21 | { 22 | /** 23 | * Hash customization to only consider the identity hash code. 24 | */ 25 | @Override 26 | protected int hashKey(final KType key) { 27 | 28 | return System.identityHashCode(key); 29 | } 30 | 31 | /** 32 | * Equality customization to only consider object identity, comparing 33 | * instances directly by ==. 34 | */ 35 | @Override 36 | protected boolean equalKeys(final KType a, final KType b) { 37 | 38 | return (a == b); 39 | } 40 | 41 | /** 42 | * Creates a hash set with the default capacity of {@link Containers#DEFAULT_EXPECTED_ELEMENTS}, 43 | * load factor of {@link HashContainers#DEFAULT_LOAD_FACTOR}. 44 | */ 45 | public KTypeIdentityHashSet() 46 | { 47 | this(Containers.DEFAULT_EXPECTED_ELEMENTS, HashContainers.DEFAULT_LOAD_FACTOR); 48 | } 49 | 50 | /** 51 | * Creates a hash set with the given capacity, 52 | * load factor of {@link HashContainers#DEFAULT_LOAD_FACTOR}. 53 | */ 54 | public KTypeIdentityHashSet(final int initialCapacity) 55 | { 56 | this(initialCapacity, HashContainers.DEFAULT_LOAD_FACTOR); 57 | } 58 | 59 | /** 60 | * Creates a hash set with the given capacity and load factor. 61 | */ 62 | public KTypeIdentityHashSet(final int initialCapacity, final double loadFactor) 63 | { 64 | super(initialCapacity, loadFactor); 65 | } 66 | 67 | /** 68 | * Creates a hash set from elements of another container. Default load factor is used. 69 | */ 70 | public KTypeIdentityHashSet(final KTypeContainer container) 71 | { 72 | this(container.size()); 73 | addAll(container); 74 | } 75 | 76 | @Override 77 | public KTypeIdentityHashSet clone() { 78 | 79 | //clone to size() to prevent eventual exponential growth 80 | final KTypeIdentityHashSet cloned = new KTypeIdentityHashSet(size(), this.loadFactor); 81 | 82 | //We must NOT clone because of the independent perturbation values 83 | cloned.addAll(this); 84 | 85 | return cloned; 86 | } 87 | 88 | /** 89 | * Create a set from a variable number of arguments or an array of KType. 90 | */ 91 | public static KTypeIdentityHashSet from(final KType... elements) 92 | { 93 | final KTypeIdentityHashSet set = new KTypeIdentityHashSet(elements.length); 94 | set.add(elements); 95 | return set; 96 | } 97 | 98 | /** 99 | * Create a set from elements of another container. 100 | */ 101 | public static KTypeIdentityHashSet from(final KTypeContainer container) 102 | { 103 | return new KTypeIdentityHashSet(container); 104 | } 105 | 106 | /** 107 | * Create a new hash set with default parameters (shortcut 108 | * instead of using a constructor). 109 | */ 110 | public static KTypeIdentityHashSet newInstance() 111 | { 112 | return new KTypeIdentityHashSet(); 113 | } 114 | 115 | /** 116 | * Returns a new object of this class with no need to declare generic type (shortcut 117 | * instead of using a constructor). 118 | */ 119 | public static KTypeIdentityHashSet newInstance(final int initialCapacity, final double loadFactor) 120 | { 121 | return new KTypeIdentityHashSet(initialCapacity, loadFactor); 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/strategies/KTypeComparator.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.strategies; 2 | 3 | import java.util.Comparator; 4 | 5 | /** 6 | * Interface to support custom comparisons of KTypes, 7 | * as replacement of either natural ordering or Comparable objects. 8 | */ 9 | /*! ${TemplateOptions.generatedAnnotation} !*/ 10 | public interface KTypeComparator /*! #if ($TemplateOptions.KTypeGeneric) !*/extends Comparator /*! #end !*/ 11 | { 12 | /** 13 | * Defines the relative ordering of e1 and e2: 14 | * @return 0 if e1 is "equal" to e2, or else < 0 if e1 is "smaller" than e2, or else > 0 if e1 is "bigger" than e2 15 | */ 16 | /*! #if ($TemplateOptions.KTypeGeneric) !*/ 17 | @Override 18 | /*! #end !*/ 19 | int compare(KType e1, KType e2); 20 | } -------------------------------------------------------------------------------- /hppcrt/src/main/templates/com/carrotsearch/hppcrt/strategies/KTypeStandardComparator.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.strategies; 2 | 3 | import com.carrotsearch.hppcrt.*; 4 | 5 | /*! #import("com/carrotsearch/hppcrt/Intrinsics.java") !*/ 6 | /** 7 | * Standard {@link KTypeComparator} for KTypes, providing the same behavior as either natural ordering 8 | * for primitives, or Comparable for objects. 9 | */ 10 | /*! ${TemplateOptions.generatedAnnotation} !*/ 11 | public final class KTypeStandardComparator implements KTypeComparator 12 | { 13 | public KTypeStandardComparator() { 14 | // nothing 15 | } 16 | 17 | @Override 18 | public int compare(final KType e1, final KType e2) { 19 | 20 | return Intrinsics. compareUnchecked(e1, e2); 21 | } 22 | 23 | @Override 24 | public boolean equals(final Object o) { 25 | 26 | if (o instanceof KTypeStandardComparator) { 27 | 28 | return true; 29 | } 30 | 31 | return false; 32 | } 33 | 34 | @Override 35 | public int hashCode() { 36 | 37 | return KTypeStandardComparator.class.hashCode(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /hppcrt/src/test/java/com/carrotsearch/hppcrt/AbstractIteratorTest.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.Iterator; 6 | import java.util.List; 7 | import java.util.NoSuchElementException; 8 | 9 | import org.junit.Assert; 10 | import org.junit.Test; 11 | 12 | /** 13 | * 14 | */ 15 | public class AbstractIteratorTest 16 | { 17 | public static class RangeIterator extends AbstractIterator 18 | { 19 | int start; 20 | int count; 21 | 22 | public RangeIterator(final int start, final int count) 23 | { 24 | this.start = start; 25 | this.count = count; 26 | } 27 | 28 | @Override 29 | protected Integer fetch() 30 | { 31 | if (this.count == 0) 32 | { 33 | return done(); 34 | } 35 | 36 | this.count--; 37 | return this.start++; 38 | } 39 | } 40 | 41 | @Test 42 | public void testEmpty() 43 | { 44 | final RangeIterator i = new RangeIterator(1, 0); 45 | Assert.assertFalse(i.hasNext()); 46 | Assert.assertFalse(i.hasNext()); 47 | } 48 | 49 | @Test(expected = NoSuchElementException.class) 50 | public void testEmptyExceptionOnNext() 51 | { 52 | final RangeIterator i = new RangeIterator(1, 0); 53 | i.next(); 54 | } 55 | 56 | @Test 57 | public void testNonEmpty() 58 | { 59 | final RangeIterator i = new RangeIterator(1, 1); 60 | Assert.assertTrue(i.hasNext()); 61 | Assert.assertTrue(i.hasNext()); 62 | i.next(); 63 | Assert.assertFalse(i.hasNext()); 64 | Assert.assertFalse(i.hasNext()); 65 | try 66 | { 67 | i.next(); 68 | Assert.fail(); 69 | } catch (final NoSuchElementException e) 70 | { 71 | // expected. 72 | } 73 | } 74 | 75 | @Test 76 | public void testValuesAllRight() 77 | { 78 | Assert.assertEquals(Arrays.asList(1), AbstractIteratorTest.addAll(new RangeIterator(1, 1))); 79 | Assert.assertEquals(Arrays.asList(1, 2), AbstractIteratorTest.addAll(new RangeIterator(1, 2))); 80 | Assert.assertEquals(Arrays.asList(1, 2, 3), AbstractIteratorTest.addAll(new RangeIterator(1, 3))); 81 | } 82 | 83 | private static List addAll(final Iterator i) 84 | { 85 | final List t = new ArrayList(); 86 | while (i.hasNext()) { 87 | t.add(i.next()); 88 | } 89 | return t; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /hppcrt/src/test/java/com/carrotsearch/hppcrt/ArraysTest.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import org.junit.Test; 4 | 5 | import com.carrotsearch.hppcrt.lists.IntArrayList; 6 | 7 | public class ArraysTest 8 | { 9 | public ArraysTest() { 10 | //nothing 11 | } 12 | 13 | @Test 14 | public void testIntRotate() { 15 | 16 | final IntArrayList testArray = IntArrayList.from(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }); 17 | 18 | IntArrays.rotate(testArray, 0, 6, 14); 19 | 20 | //0 1 21 | System.out.println(testArray.toString()); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /hppcrt/src/test/java/com/carrotsearch/hppcrt/BoundedProportionalArraySizingStrategyTest.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | /** 9 | * Test case for bounded proportional sizing strategy. 10 | */ 11 | public class BoundedProportionalArraySizingStrategyTest 12 | { 13 | private BoundedProportionalArraySizingStrategy resizer; 14 | 15 | @Test(expected = BufferAllocationException.class) 16 | public void testBeyondIntRange() 17 | { 18 | this.resizer = new BoundedProportionalArraySizingStrategy(); 19 | this.resizer.grow( 20 | BoundedProportionalArraySizingStrategy.MAX_ARRAY_LENGTH, 21 | BoundedProportionalArraySizingStrategy.MAX_ARRAY_LENGTH, 1); 22 | } 23 | 24 | @Test 25 | public void testExactIntRange() 26 | { 27 | this.resizer = new BoundedProportionalArraySizingStrategy(); 28 | int size = BoundedProportionalArraySizingStrategy.MAX_ARRAY_LENGTH - 2; 29 | size = this.resizer.grow(size, size, 1); 30 | Assert.assertEquals(BoundedProportionalArraySizingStrategy.MAX_ARRAY_LENGTH, size); 31 | try { 32 | size = this.resizer.grow(size, size, 1); 33 | throw new RuntimeException("Unexpected."); 34 | } catch (final BufferAllocationException e) { 35 | // Expected. 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /hppcrt/src/test/java/com/carrotsearch/hppcrt/BufferAllocationExceptionTest.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | 6 | import com.carrotsearch.randomizedtesting.RandomizedRunner; 7 | import com.carrotsearch.randomizedtesting.RandomizedTest; 8 | 9 | @RunWith(RandomizedRunner.class) 10 | public class BufferAllocationExceptionTest 11 | { 12 | @Test 13 | public void testWrongMessageFormat() 14 | { 15 | try { 16 | try { 17 | 18 | throw new OutOfMemoryError("Simulate an OMM"); 19 | } catch (final OutOfMemoryError oom) { 20 | // Expected OOM, re-dispatched into BufferAllocationException 21 | throw new BufferAllocationException("Wrong format = Cause of the OOM => %d", oom, 0.42); //expect a Integer, give a float ! 22 | } 23 | } catch (final BufferAllocationException bae) { 24 | 25 | bae.printStackTrace(System.err); 26 | } 27 | } 28 | 29 | @Test 30 | public void testMessageFormatOK() 31 | { 32 | try { 33 | try { 34 | 35 | throw new OutOfMemoryError("Simulate an OMM"); 36 | } catch (final OutOfMemoryError oom) { 37 | // Expected OOM, re-dispatched into BufferAllocationException 38 | throw new BufferAllocationException("OK format = Cause of the OOM => %d", oom, 42); //expect a Integer, give an Integer ! 39 | } 40 | } catch (final BufferAllocationException bae) { 41 | 42 | bae.printStackTrace(System.err); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /hppcrt/src/test/java/com/carrotsearch/hppcrt/HashContainersTest.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | 7 | import com.carrotsearch.randomizedtesting.RandomizedRunner; 8 | import com.carrotsearch.randomizedtesting.RandomizedTest; 9 | 10 | @RunWith(RandomizedRunner.class) 11 | public class HashContainersTest 12 | { 13 | /* */ 14 | @Test 15 | public void testCapacityCalculations() { 16 | 17 | Assert.assertEquals(HashContainers.MIN_HASH_ARRAY_LENGTH, HashContainers.minBufferSize(0, 0.5f)); 18 | Assert.assertEquals(HashContainers.MIN_HASH_ARRAY_LENGTH, HashContainers.minBufferSize(1, 0.5f)); 19 | 20 | Assert.assertEquals(0x20, HashContainers.minBufferSize(0x10 - 1, 0.5f)); 21 | Assert.assertEquals(0x40, HashContainers.minBufferSize(0x10, 0.49f)); 22 | 23 | final int maxCapacity = HashContainers.maxElements(HashContainers.MAX_LOAD_FACTOR); 24 | 25 | try { 26 | // This should be impossible because it'd create a negative-sized array. 27 | HashContainers.minBufferSize(maxCapacity + 2, HashContainers.MAX_LOAD_FACTOR); 28 | Assert.fail(); 29 | } catch (final BufferAllocationException e) { 30 | // Expected. 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /hppcrt/src/test/java/com/carrotsearch/hppcrt/RequireAssertionsRule.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import static org.junit.Assert.assertTrue; 4 | 5 | import org.junit.Before; 6 | import org.junit.rules.MethodRule; 7 | import org.junit.runners.model.FrameworkMethod; 8 | import org.junit.runners.model.Statement; 9 | 10 | /** 11 | * A JUnit rule that forces assertions to be enabled. 12 | */ 13 | public class RequireAssertionsRule implements MethodRule 14 | { 15 | public Statement apply(Statement base, 16 | FrameworkMethod paramFrameworkMethod, Object paramObject) 17 | { 18 | checkAssertionsEnabled(); 19 | return base; 20 | } 21 | 22 | /* */ 23 | @Before 24 | public void checkAssertionsEnabled() 25 | { 26 | boolean enabled = true; 27 | try 28 | { 29 | assert false; 30 | enabled = false; 31 | } 32 | catch (AssertionError e) 33 | { 34 | // Expected, fall through. 35 | } 36 | 37 | assertTrue("Enable JVM assertions for testing.", enabled); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /hppcrt/src/test/java/com/carrotsearch/hppcrt/StringConversionsTest.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import com.carrotsearch.hppcrt.lists.ByteArrayList; 7 | import com.carrotsearch.hppcrt.lists.CharArrayList; 8 | import com.carrotsearch.hppcrt.lists.ObjectArrayList; 9 | import com.carrotsearch.hppcrt.maps.ObjectObjectHashMap; 10 | 11 | public class StringConversionsTest 12 | { 13 | @Test 14 | public void testByteList() 15 | { 16 | final ByteArrayList list = new ByteArrayList(); 17 | list.add(new byte[] { 1, 2, 3 }); 18 | Assert.assertEquals("[1, 2, 3]", list.toString()); 19 | } 20 | 21 | @Test 22 | public void testCharList() 23 | { 24 | final CharArrayList list = new CharArrayList(); 25 | list.add(new char[] { 'a', 'b', 'c' }); 26 | Assert.assertEquals("[a, b, c]", list.toString()); 27 | } 28 | 29 | @Test 30 | public void testObjectList() 31 | { 32 | final ObjectArrayList list = new ObjectArrayList(); 33 | list.add("ab", "ac", "ad"); 34 | Assert.assertEquals("[ab, ac, ad]", list.toString()); 35 | } 36 | 37 | @Test 38 | public void testObjectObjectMap() 39 | { 40 | final ObjectObjectHashMap map = 41 | ObjectObjectHashMap.from( 42 | new String[] { "a" }, 43 | new String[] { "b" }); 44 | 45 | Assert.assertEquals("[a=>b]", map.toString()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /hppcrt/src/test/java/com/carrotsearch/hppcrt/XorShiftRandomTest.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import java.util.Random; 4 | 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | public class XorShiftRandomTest 9 | { 10 | /** */ 11 | @Test 12 | public void testApproxEqualBucketFill() { 13 | 14 | checkApproxEqualBucketFill(new XorShift128P(0xdeadbeef)); 15 | } 16 | 17 | @Test 18 | public void testApproxEqualBucketFill128P() { 19 | 20 | checkApproxEqualBucketFill(new XorShift128P(0xdeadbeef)); 21 | } 22 | 23 | @Test 24 | public void testNext() { 25 | 26 | checkNext(new XorShift128P()); 27 | } 28 | 29 | @Test 30 | public void testNext128P() { 31 | 32 | checkNext(new XorShift128P()); 33 | } 34 | 35 | private void checkNext(final Random random) { 36 | 37 | for (int bits = 1; bits <= 32; bits++) { 38 | final long max = (1L << bits) - 1; 39 | long mask = 0; 40 | for (int i = 0; i < 10000; i++) { 41 | 42 | long val = -1L; 43 | 44 | if (random instanceof XorShift128P) { 45 | 46 | val = (((XorShift128P) random).next(bits)) & 0xffffffffL; 47 | } 48 | 49 | mask |= val; 50 | Assert.assertTrue(val + " >= " + max + "?", val <= max); 51 | } 52 | Assert.assertEquals(max, mask); 53 | } 54 | } 55 | 56 | private void checkApproxEqualBucketFill(final Random rnd) { 57 | 58 | final int[] buckets = new int[(1 << 8)]; 59 | final int mask = buckets.length - 1; 60 | 61 | final int hits = 1000000; 62 | 63 | for (int count = hits * buckets.length; --count >= 0;) { 64 | buckets[rnd.nextInt() & mask]++; 65 | } 66 | 67 | // every bucket should get +- 1% * hits 68 | final int limit = hits / 100; 69 | for (final int bucketCount : buckets) { 70 | Assert.assertTrue(Math.abs(bucketCount - hits) + " > " + limit + "?", Math.abs(bucketCount - hits) <= limit); 71 | 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /hppcrt/src/test/java/com/carrotsearch/hppcrt/maps/ObjectIntOpenHashMapRegressionTest.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.maps; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | public class ObjectIntOpenHashMapRegressionTest 7 | { 8 | /** @see "http://issues.carrot2.org/browse/HPPC-32" */ 9 | @Test 10 | public void testEqualsOnObjectKeys() 11 | { 12 | final ObjectIntHashMap map = new ObjectIntHashMap(); 13 | final String key1 = "key1"; 14 | final String key2 = new String("key1"); 15 | 16 | map.put(key1, 1); 17 | Assert.assertEquals(1, map.get(key2)); 18 | Assert.assertEquals(1, map.put(key2, 2)); 19 | Assert.assertEquals(1, map.size()); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /hppcrt/src/test/templates/com/carrotsearch/hppcrt/AbstractKTypeVTypeTest.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | import org.junit.Assert; 4 | 5 | import com.carrotsearch.hppcrt.procedures.*; 6 | 7 | /** 8 | * Unit helpers for KType and VType pair containers 9 | */ 10 | 11 | /*! #import("com/carrotsearch/hppcrt/Intrinsics.java") !*/ 12 | /*! #if ($TemplateOptions.KTypeGeneric) !*/ 13 | @SuppressWarnings("unchecked") 14 | /*! #end !*/ 15 | /* ! ${TemplateOptions.generatedAnnotation} ! */ 16 | public abstract class AbstractKTypeVTypeTest extends AbstractKTypeTest 17 | { 18 | 19 | /** 20 | * valueE is special: its value is == null for generics, and == 0 for primitives. 21 | */ 22 | /*! #if ($TemplateOptions.VTypeGeneric) !*/ 23 | protected VType valueE = null; 24 | /*! #else 25 | protected VType valueE = vcast(0); 26 | #end !*/ 27 | 28 | protected VType value0 = vcast(0); 29 | protected VType value1 = vcast(1); 30 | protected VType value2 = vcast(2); 31 | protected VType value3 = vcast(3); 32 | protected VType value4 = vcast(4); 33 | protected VType value5 = vcast(5); 34 | protected VType value6 = vcast(6); 35 | protected VType value7 = vcast(7); 36 | protected VType value8 = vcast(8); 37 | protected VType value9 = vcast(9); 38 | 39 | public volatile long guard; 40 | 41 | /** 42 | * Convert to VType type from an integer used to test stuff. 43 | */ 44 | protected VType vcast(final int value) 45 | { 46 | /*! #if ($TemplateOptions.VTypePrimitive) 47 | return (VType) value; 48 | #else !*/ 49 | @SuppressWarnings("unchecked") 50 | final VType v = (VType) (Object) value; 51 | return v; 52 | /*! #end !*/ 53 | } 54 | 55 | /** 56 | * Convert a VType to int, (VType being a boxed elementary type or a primitive), else 57 | * returns 0. 58 | */ 59 | protected int vcastType(final VType type) 60 | { 61 | /*! #if ($TemplateOptions.VTypePrimitive) 62 | return (int) type; 63 | #else !*/ 64 | long k = 0L; 65 | 66 | if (type instanceof Character) 67 | { 68 | k = ((Character) type).charValue(); 69 | } 70 | else if (type instanceof Number) 71 | { 72 | k = ((Number) type).longValue(); 73 | } 74 | 75 | return (int) k; 76 | /*! #end !*/ 77 | } 78 | 79 | /** 80 | * Create a new array of a given type and copy the arguments to this array. 81 | */ 82 | protected VType[] newvArray(final VType... elements) 83 | { 84 | final VType[] values = Intrinsics. newArray(elements.length); 85 | 86 | for (int i = 0; i < elements.length; i++) 87 | { 88 | /*! #if ($TemplateOptions.VTypeGeneric) !*/ 89 | if (elements[i] != null) 90 | { 91 | values[i] = elements[i]; 92 | } 93 | else 94 | { 95 | values[i] = null; 96 | } 97 | /*! #else 98 | values[i] = (VType)elements[i]; 99 | #end !*/ 100 | } 101 | 102 | return values; 103 | } 104 | 105 | /** 106 | * Return true if VType is part of the array 107 | */ 108 | public boolean isInVArray(final VType[] values, final VType expected) { 109 | 110 | boolean inArray = false; 111 | 112 | for (int i = 0; i < values.length; i++) 113 | { 114 | if (Intrinsics. equals(values[i], expected)) { 115 | 116 | inArray = true; 117 | break; 118 | } 119 | } 120 | 121 | return inArray; 122 | } 123 | 124 | protected void assertSameMap( 125 | 126 | final KTypeVTypeMap c1, 127 | final KTypeVTypeMap c2) 128 | { 129 | Assert.assertEquals(c1.size(), c2.size()); 130 | 131 | c1.forEach(new KTypeVTypeProcedure() 132 | { 133 | @Override 134 | public void apply(final KType key, final VType value) 135 | { 136 | Assert.assertTrue(c2.containsKey(key)); 137 | TestUtils.assertEquals2(value, c2.get(key)); 138 | } 139 | }); 140 | } 141 | } -------------------------------------------------------------------------------- /hppcrt/src/test/templates/com/carrotsearch/hppcrt/IntHolder.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | /** 4 | * int holder. 5 | */ 6 | public class IntHolder implements Comparable 7 | { 8 | public int value; 9 | 10 | public IntHolder() 11 | { 12 | } 13 | 14 | public IntHolder(final int value) 15 | { 16 | this.value = value; 17 | } 18 | 19 | @Override 20 | public int hashCode() 21 | { 22 | return this.value; 23 | } 24 | 25 | @Override 26 | public boolean equals(final Object other) 27 | { 28 | return (other instanceof IntHolder) && this.value == ((IntHolder) other).value; 29 | } 30 | 31 | @Override 32 | public int compareTo(final IntHolder o) { 33 | 34 | if (this.value < o.value) { 35 | 36 | return -1; 37 | } 38 | else if (this.value > o.value) { 39 | 40 | return 1; 41 | } 42 | 43 | return 0; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /hppcrt/src/test/templates/com/carrotsearch/hppcrt/KTypeBufferVisualizer.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt; 2 | 3 | /*! #import("com/carrotsearch/hppcrt/Intrinsics.java") !*/ 4 | /** 5 | * Reused buffer visualization routines. 6 | */ 7 | public class KTypeBufferVisualizer 8 | { 9 | /** 10 | * Visually depict the distribution of keys. 11 | * 12 | * @param characters 13 | * The number of characters to "squeeze" the entire buffer into. 14 | * @return Returns a sequence of characters where '.' depicts an empty 15 | * fragment of the internal buffer and 'X' depicts full or nearly full 16 | * capacity within the buffer's range and anything between 1 and 9 is between. 17 | */ 18 | public static String visualizeKeyDistribution( 19 | /*! #if ($TemplateOptions.KTypeGeneric) !*/final Object[] /*! #else KType [] #end !*/buffer, 20 | final int characters) { 21 | 22 | final StringBuilder b = new StringBuilder(); 23 | final char[] chars = ".123456789X".toCharArray(); 24 | 25 | final int max = buffer.length - 1; 26 | 27 | for (int i = 1, start = -1; i <= characters; i++) { 28 | 29 | final int end = (int) ((long) i * max / characters); 30 | 31 | if (start + 1 <= end) { 32 | int taken = 0; 33 | int slots = 0; 34 | for (int slot = start + 1; slot <= end; slot++, slots++) { 35 | 36 | if (!Intrinsics. isEmpty(buffer[slot])) { 37 | taken++; 38 | } 39 | } 40 | b.append(chars[Math.min(chars.length - 1, taken * chars.length / slots)]); 41 | start = end; 42 | } 43 | } 44 | while (b.length() < characters) { 45 | b.append(' '); 46 | } 47 | return b.toString(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /hppcrt/src/test/templates/com/carrotsearch/hppcrt/lists/KTypeArrayDequeAsDequeTest.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.lists; 2 | 3 | import org.junit.After; 4 | import org.junit.Assert; 5 | 6 | import com.carrotsearch.hppcrt.Intrinsics; 7 | import com.carrotsearch.hppcrt.KTypeContainer; 8 | import com.carrotsearch.hppcrt.KTypeDeque; 9 | import com.carrotsearch.hppcrt.TestUtils; 10 | 11 | /*! #import("com/carrotsearch/hppcrt/Intrinsics.java") !*/ 12 | /** 13 | * Unit tests for {@link KTypeArrayDeque as KTypeDeque}. 14 | */ 15 | /*! ${TemplateOptions.generatedAnnotation} !*/ 16 | public class KTypeArrayDequeAsDequeTest extends AbstractKTypeDequeTest 17 | { 18 | @Override 19 | protected KTypeDeque createNewInstance(final int initialCapacity) { 20 | 21 | return new KTypeArrayDeque(initialCapacity); 22 | } 23 | 24 | @Override 25 | protected void addFromArray(final KTypeDeque testList, final KType... keys) { 26 | final KTypeArrayDeque concreteClass = (KTypeArrayDeque) (testList); 27 | concreteClass.addLast(keys); 28 | 29 | } 30 | 31 | @Override 32 | protected void addFromContainer(final KTypeDeque testList, final KTypeContainer container) { 33 | final KTypeArrayDeque concreteClass = (KTypeArrayDeque) (testList); 34 | concreteClass.addLast(container); 35 | 36 | } 37 | 38 | @Override 39 | protected KType[] getBuffer(final KTypeDeque testList) { 40 | final KTypeArrayDeque concreteClass = (KTypeArrayDeque) (testList); 41 | return Intrinsics. cast(concreteClass.buffer); 42 | } 43 | 44 | @Override 45 | protected int getDescendingValuePoolSize(final KTypeDeque testList) { 46 | final KTypeArrayDeque concreteClass = (KTypeArrayDeque) (testList); 47 | return concreteClass.descendingValueIteratorPool.size(); 48 | } 49 | 50 | @Override 51 | protected int getDescendingValuePoolCapacity(final KTypeDeque testList) { 52 | final KTypeArrayDeque concreteClass = (KTypeArrayDeque) (testList); 53 | return concreteClass.descendingValueIteratorPool.capacity(); 54 | } 55 | 56 | /** 57 | * Move one index to the right, wrapping around buffer of size modulus 58 | */ 59 | private int oneRight(final int index, final int modulus) 60 | { 61 | return (index + 1 == modulus) ? 0 : index + 1; 62 | } 63 | 64 | @After 65 | public void checkConsistency() 66 | { 67 | final KTypeArrayDeque arrayDeque = (KTypeArrayDeque) this.deque; 68 | 69 | if (arrayDeque != null) 70 | { 71 | int count = 0; 72 | //check access by get() 73 | for (/*! #if ($TemplateOptions.KTypeGeneric) !*/final Object 74 | /*! #else 75 | final KType 76 | #end !*/ 77 | val : arrayDeque.toArray()) { 78 | 79 | /*! #if ($TemplateOptions.KTypeGeneric) !*/ 80 | TestUtils.assertEquals2(val, (Object) arrayDeque.get(count)); 81 | /*! #else 82 | TestUtils.assertEquals2(val, arrayDeque.get(count)); 83 | #end !*/ 84 | count++; 85 | } 86 | 87 | Assert.assertEquals(count, this.deque.size()); 88 | 89 | for (int i = arrayDeque.tail; i < arrayDeque.head; i = oneRight(i, arrayDeque.buffer.length)) 90 | { 91 | /*! #if ($TemplateOptions.KTypeGeneric) !*/ 92 | Assert.assertTrue(Intrinsics. empty() == arrayDeque.buffer[i]); 93 | /*! #end !*/ 94 | } 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /hppcrt/src/test/templates/com/carrotsearch/hppcrt/sets/KTypeHashSetTest.java: -------------------------------------------------------------------------------- 1 | package com.carrotsearch.hppcrt.sets; 2 | 3 | import org.junit.*; 4 | 5 | import com.carrotsearch.hppcrt.*; 6 | 7 | import com.carrotsearch.hppcrt.TestUtils; 8 | 9 | /*! #import("com/carrotsearch/hppcrt/Intrinsics.java") !*/ 10 | /** 11 | * Unit tests for {@link KTypeHashSet}. 12 | */ 13 | /*! ${TemplateOptions.generatedAnnotation} !*/ 14 | public class KTypeHashSetTest extends AbstractKTypeHashSetTest 15 | { 16 | @Override 17 | protected KTypeSet createNewSetInstance(final int initialCapacity, final double loadFactor) { 18 | 19 | if (initialCapacity == 0 && loadFactor == HashContainers.DEFAULT_LOAD_FACTOR) { 20 | 21 | return new KTypeHashSet(); 22 | 23 | } else if (loadFactor == HashContainers.DEFAULT_LOAD_FACTOR) { 24 | 25 | return new KTypeHashSet(initialCapacity); 26 | } 27 | 28 | //generic case 29 | return new KTypeHashSet(initialCapacity, loadFactor); 30 | } 31 | 32 | @Override 33 | protected KType[] getKeys(final KTypeSet testSet) { 34 | final KTypeHashSet concreteClass = (KTypeHashSet) (testSet); 35 | 36 | return Intrinsics. cast(concreteClass.keys); 37 | } 38 | 39 | @Override 40 | protected boolean isAllocatedDefaultKey(final KTypeSet testSet) { 41 | final KTypeHashSet concreteClass = (KTypeHashSet) (testSet); 42 | return concreteClass.allocatedDefaultKey; 43 | } 44 | 45 | @Override 46 | protected KTypeSet getClone(final KTypeSet testSet) { 47 | final KTypeHashSet concreteClass = (KTypeHashSet) (testSet); 48 | return concreteClass.clone(); 49 | } 50 | 51 | @Override 52 | protected KTypeSet getFrom(final KTypeContainer container) { 53 | 54 | return KTypeHashSet.from(container); 55 | } 56 | 57 | @Override 58 | protected KTypeSet getFrom(final KType... elements) { 59 | 60 | return KTypeHashSet.from(elements); 61 | } 62 | 63 | @Override 64 | protected KTypeSet getFromArray(final KType[] keys) { 65 | 66 | return KTypeHashSet.from(keys); 67 | } 68 | 69 | @Override 70 | protected void addFromArray(final KTypeSet testSet, final KType... keys) { 71 | final KTypeHashSet concreteClass = (KTypeHashSet) (testSet); 72 | 73 | for (final KType key : keys) { 74 | 75 | concreteClass.add(key); 76 | } 77 | } 78 | 79 | @Override 80 | protected KTypeSet getCopyConstructor(final KTypeSet testSet) { 81 | final KTypeHashSet concreteClass = (KTypeHashSet) (testSet); 82 | 83 | return new KTypeHashSet(concreteClass); 84 | } 85 | 86 | @Override 87 | protected int getEntryPoolSize(final KTypeSet testSet) { 88 | final KTypeHashSet concreteClass = (KTypeHashSet) (testSet); 89 | return concreteClass.entryIteratorPool.size(); 90 | } 91 | 92 | @Override 93 | protected int getEntryPoolCapacity(final KTypeSet testSet) { 94 | final KTypeHashSet concreteClass = (KTypeHashSet) (testSet); 95 | return concreteClass.entryIteratorPool.capacity(); 96 | } 97 | 98 | ////////////////////////////////////// 99 | /// Implementation-specific tests 100 | ///////////////////////////////////// 101 | 102 | /* */ 103 | @Test 104 | public void testAddVarArgs() 105 | { 106 | final KTypeHashSet testSet = new KTypeHashSet(); 107 | testSet.add(this.keyE, this.key1, this.key2, this.key1, this.keyE); 108 | Assert.assertEquals(3, testSet.size()); 109 | TestUtils.assertSortedListEquals(testSet.toArray(), this.keyE, this.key1, this.key2); 110 | } 111 | 112 | /* */ 113 | @Test 114 | public void testAdd2Elements() { 115 | final KTypeHashSet testSet = new KTypeHashSet(); 116 | 117 | testSet.add(this.key1, this.key1); 118 | Assert.assertEquals(1, testSet.size()); 119 | Assert.assertEquals(1, testSet.add(this.key1, this.key2)); 120 | Assert.assertEquals(2, testSet.size()); 121 | Assert.assertEquals(1, testSet.add(this.keyE, this.key1)); 122 | Assert.assertEquals(3, testSet.size()); 123 | } 124 | } 125 | --------------------------------------------------------------------------------