public void emptyIntersection(int[] array1, int[] array2) { array1 = IntCollections.arrayCopy(array1); array2 = IntCollections.arrayCopy(array2); Arrays.sort(array1); Arrays.sort(array2); for (int val : array1) { if (IntCollections.binarySearch(val, array2) >= 0) { Failure failure = createFailure(); failure.actual().setIntArray(array1); failure.expected().setIntArray(array2); failure.fail("Equal elements"); } } }
@Override protected List<WritableLongIntMapProjection> createSets(LongList sortedUniqueList) { IntList repeatContains = repeat(DEFAULT_CONTAINS_VALUE, sortedUniqueList.size()); List<T> maps = createMapsFromLists(sortedUniqueList, repeatContains); List<WritableLongIntMapProjection> sets = new ArrayList(); for (T map0 : maps) { sets.add(new WritableLongIntMapProjection(map0)); } return sets; }
public static IntIntHppcOpenHashMap createFrom(IntIterable keys, IntIterable values) { int keysSize = IntCollections.sizeOfIterable(keys, 0); int valuesSize = IntCollections.sizeOfIterable(values, 0); if (keysSize != valuesSize) { throw new IllegalArgumentException("keys.size() != values.size()"); } else { keysSize = Math.max(keysSize, valuesSize); } float loadFactor = IntIntOpenHashMap.DEFAULT_LOAD_FACTOR; int initialCapacity = (int)(keysSize / loadFactor) + 1; IntIntHppcOpenHashMap map = new IntIntHppcOpenHashMap(initialCapacity); IntIterator keysIt = keys.iterator(); IntIterator valuesIt = values.iterator(); map.putAll(IntIntIterators.pair(keysIt, valuesIt)); if (keysIt.hasNext() || valuesIt.hasNext()) { throw new IllegalArgumentException("keys.size() != values.size()"); } return map; }
public boolean accept(long value, LongList source) { index++; assertEquals(value, ins.get(index)); if (IntCollections.indexOf(index, expectedCopy) >= 0) assertSame(ins, source); else assertSame(ins.getBase(), source); return true; } });
public void order(IntIterator actual, IntIterator expected) { order(IntCollections.toNativeArray(actual), IntCollections.toNativeArray(expected)); }
public void checkMap(T map, LongList keys, IntList values) { assertEquals(keys.size(), map.size()); assertEquals(keys.isEmpty(), map.isEmpty()); for (int i = 0; i < keys.size(); i++) { assertEquals(values.get(i), map.get(keys.get(i))); } LongArray mapKeys = collectIterable(map.size(), map.keysIterator()); CHECK.unordered(mapKeys, keys); IntArray mapValues = IntCollections.collectIterable(map.size(), map.valuesIterator()); CHECK.unordered(mapValues, values); }
/** * contract: aseq shouldn't contain duplicates and bseq should be permutation of aseq. * @param aseq list without duplicates * @param bseq list without duplicates * @return longest common subsequence for {@code aseq} and {@code bseq} */ public static IntList getLcsForPermutation(IntList aseq, IntList bseq) { assert aseq.size() == bseq.size() : toBoundedString(aseq) + toBoundedString(bseq); int sz = aseq.size(); IntIntHppcOpenHashMap aseqMap = new IntIntHppcOpenHashMap(sz); for (int i = 0; i < sz; i++) { assert !aseqMap.containsKey(aseq.get(i)) : "duplicates aren't allowed in aseq: " + aseq.get(i); // check no duplicates in aseq aseqMap.put(aseq.get(i), i); } assert aseqMap.size() == bseq.size() && aseqMap.containsKeys(bseq) : "bseq should be permutation of aseq: " + toBoundedString(aseqMap.keySet()) + " " + toBoundedString(bseq); IntArray ids = new IntArray(); for (IntIterator it : bseq) { ids.add(aseqMap.get(it.value())); } IntList subseq = IntLongestIncreasingSubsequence.getLIS(ids); IntArray res = new IntArray(subseq.size()); for (IntIterator it : subseq) { res.add(aseq.get(it.value())); } return res; }
public void testSimple() { int[][] leftVariants = {{}, {0, 1, 2}, {10, 20, 30}, {0, 2, 4, 6}, {Integer.MIN_VALUE, Integer.MAX_VALUE, 1, 2, 0}}; for (int[] left : leftVariants) { for (int[] right0 : leftVariants) { IntList leftList = new IntArray(left); LongList rightList = LongCollections.asLongList(new IntArray(right0)); IntList leftSingle = new IntList.Single(Integer.MIN_VALUE); LongList rightSingle = new LongList.Single(Long.MIN_VALUE); checkPairIterator(left, right0, leftList, rightList); if (leftList.size() == rightList.size()) { checkPairIterator(left, right0, IntCollections.concatLists(leftList, leftSingle), rightList); checkPairIterator(left, right0, leftList, LongCollections.concatLists(rightList, rightSingle)); } } } }
public void testCompound() { long[][] valuesArray = {{0, 1, 2, 3, 4, 5, 6}, {10, 11, 12, 13, 14, 15, 16}}; int[][] indicesToRemove = {{0, 4, 1, 5}, {2, 6, 1}, {0, 6, 1, 2}}; for (long[] values : valuesArray) { LongArray array = LongArray.create(values); for (int[] indices : indicesToRemove) { IntList removedIndices = LongListRemovingDecorator.prepareUnsortedIndices(indices); LongListRemovingDecorator rem = createFromPrepared(array, removedIndices); LongArray expected = LongArray.copy(values); removedIndices = IntCollections.toSorted(false, new IntArray(indices)); expected.removeAllAtSorted(removedIndices.iterator()); CHECK.order(expected, rem); checkRemovedIndexes(rem, removedIndices.toNativeArray()); assertEquals(indices.length, rem.getRemoveCount()); } } }
public void testIndexOf() { int arrayLength = 1000; int maxValue = Integer.MAX_VALUE; IntArray res = new IntArray(arrayLength); int[] intArr = new int[arrayLength]; for (int i = 0; i < arrayLength; i++) { int val = myRand.nextInt(maxValue); res.add(val); intArr[i] = val; } for (int i = 0; i < arrayLength; i++) { int val = res.get(i); assertEquals(res.indexOf(val), IntCollections.indexOf(val, intArr)); } }
public void order(IntIterator actual, IntIterator expected) { Compare.order(IntCollections.toNativeArray(actual), IntCollections.toNativeArray(expected)); }
public void testIterators() { // assumed that method createMapsFromLists returns variants with all constructors int attemptsCount = 10; int maxSize = 50; // int maxSize = 1000; for (int attempt = 0; attempt < attemptsCount; attempt++) { LongArray keys = generateRandomLongArray(maxSize, SORTED_UNIQUE); IntArray values = generateRandomIntArray(keys.size(), UNORDERED); for (T map : createMapsFromLists(keys, values)) { LongArray actualKeys = LongCollections.collectIterables(map.size(), map.keysIterator()); IntArray actualValues = IntCollections.collectIterable(map.size(), map.valuesIterator()); CHECK.unordered(actualKeys, keys); CHECK.unordered(actualValues, values); actualKeys.clear(); actualValues.clear(); for (LongIntIterator iter : map) { actualKeys.add(iter.left()); actualValues.add(iter.right()); } CHECK.unordered(actualKeys, keys); CHECK.unordered(actualValues, values); } } }
public void emptyIntersection(int[] array1, int[] array2) { array1 = IntCollections.arrayCopy(array1); array2 = IntCollections.arrayCopy(array2); Arrays.sort(array1); Arrays.sort(array2); for (int val : array1) { if (IntCollections.binarySearch(val, array2) >= 0) { Failure failure = createFailure(); failure.actual().setIntArray(array1); failure.expected().setIntArray(array2); failure.fail("Equal elements"); } } }
@Override protected WritableLongIntMapProjection createSet(LongList sortedUniqueList) { T map0 = createMap(); map0.putAll(sortedUniqueList, repeat(DEFAULT_CONTAINS_VALUE, sortedUniqueList.size())); return new WritableLongIntMapProjection(map0); }
public void contains(int val, int[] array) { if (IntCollections.indexOf(val, array) >= 0) return; Failure failure = createFailure(); failure.expected().setElement(val); failure.actual().setIntArray(array); failure.fail("Expected contains"); }
public void order(IntIterator actual, int ... expected) { order(IntCollections.toNativeArray(actual), expected); }
public void testValuesIterator() { IntArray expected = IntArray.create(1, 1, 2, 2, 3, 3); map.putAll(LongArray.create(0, 2, 4, 6, 8, 10), expected); IntArray actual = IntCollections.collectIterable(expected.size(), map.valuesIterator()); actual.sort(); assertEquals(expected, actual); int size = 20, attempts = 10; for (int attempt = 0; attempt < attempts; attempt++) { map.clear(); LongArray keys = generateRandomLongArray(size, SortedStatus.SORTED_UNIQUE, 100, 200); keys.shuffle(myRand); expected = new IntArray(size); for (int i = 0; i < keys.size(); i++) { long key = keys.get(i); expected.add((int) (key * key)); } map.putAll(keys, expected); actual = new IntArray(map.valuesIterator()); actual.sort(); expected.sort(); CHECK.order(actual.toNativeArray(), expected.toNativeArray()); } }
public static IntLongHppcOpenHashMap createFrom(IntIterable keys, LongIterable values) { int keysSize = IntCollections.sizeOfIterable(keys, 0); int valuesSize = LongCollections.sizeOfIterable(values, 0); if (keysSize != valuesSize) { throw new IllegalArgumentException("keys.size() != values.size()"); } else { keysSize = Math.max(keysSize, valuesSize); } float loadFactor = IntLongOpenHashMap.DEFAULT_LOAD_FACTOR; int initialCapacity = (int)(keysSize / loadFactor) + 1; IntLongHppcOpenHashMap map = new IntLongHppcOpenHashMap(initialCapacity); IntIterator keysIt = keys.iterator(); LongIterator valuesIt = values.iterator(); map.putAll(IntLongIterators.pair(keysIt, valuesIt)); if (keysIt.hasNext() || valuesIt.hasNext()) { throw new IllegalArgumentException("keys.size() != values.size()"); } return map; }