{
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 super U> 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 extends T> container);
27 |
28 | int putAll(Iterable extends IntKTypeCursor extends T>> 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 super KType> 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 super KType> 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 super KType> 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 super KType> 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 KType
s.
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 KType
s.
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 KType
s.
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 extends KType> 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 extends KTypeCursor extends KType>> 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 KType
s.
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 KType
s,
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 KType
s, 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