@Override protected LongIntListMap createMapWithCapacity(int capacity) { return new LongIntListMap(new LongArray(capacity), new IntArray(capacity)); }
/** * @see IntegersFixture#generateRandomIntArray(int, com.almworks.integers.IntegersFixture.SortedStatus, int...) */ public static IntArray generateRandomIntArray(Random random, int maxLength, SortedStatus status, int... minMaxValues) { final IntArray res = new IntArray(maxLength); fillRandomArray(random, new AbstractIntCollector() { public void add(int value) { res.add(value); } }, maxLength, minMaxValues); status.action(res); return res; }
@Override protected LongSet createSet(LongList sortedUniqueList) { LongIntMap map = new LongIntListMap( new LongArray(sortedUniqueList), new IntArray(repeat(0, sortedUniqueList.size()))); return map.keySet(); }
public void testToTableString() { long[][] keysArray = {{}, {-1}, {0, 1, 2, 3}, {10, 20, 30}, {1, 3, 5, 7, 11, 13}}; for (long[] keys : keysArray) { IntArray values = new IntArray(keys.length); for (long key : keys) { values.add((int) (key * key)); } for (T map : createMapsFromLists(new LongArray(keys), values)) { String output; if (map instanceof AbstractWritableLongIntMap) { output = ((AbstractWritableLongIntMap) map).toTableString(); } else if (map instanceof WritableLongIntMapFromLongObjMap) { output = ((WritableLongIntMapFromLongObjMap) map).toTableString(); } else { continue; } int idx = 0, commasCount = 0; for (; idx < output.length() && output.charAt(idx) != '\n'; idx++) { if (output.charAt(idx) == ',') commasCount++; } assertEquals(Math.max(0, keys.length - 1), commasCount); assertFalse(idx == output.length()); idx++; for (; idx < output.length() && output.charAt(idx) != '\n'; idx++) { if (output.charAt(idx) == ',') commasCount--; } assertEquals(0, commasCount); } } }
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)); } }
private static void testRevPerm(IntArray perms) { IntArray rev = new IntArray(perms); IntegersUtils.reversePerm(rev); int n = perms.size(); IntArray e = new IntArray(n); for (int i = 0; i < n; ++i) { e.add(perms.get(rev.get(i))); } CHECK.order(IntProgression.arithmetic(0, n).iterator(), e.iterator()); }
private IntIterator getNextDifferentValueIndex(LongList list) { WritableIntList resultingIndices = new IntArray(); for (int i = 0; i < list.size(); i = list.getNextDifferentValueIndex(i)) { assertTrue("exceeded the list size!", i < list.size()); resultingIndices.add(i); } return resultingIndices.iterator(); }
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)); } } } }
@Override protected List<LongIntListMap> createMapsFromLists(LongList keys, IntList values) { IntArray indices = new IntArray(IntProgression.range(keys.size())); indices.shuffle(myRand); LongIntListMap createdMap = new LongIntListMap(); for (LongIntIterator it : LongIntIterators.pair(keys.get(indices), values.get(indices))) { createdMap.add(it.left(), it.right()); } final LongArray keys0 = new LongArray(keys); final IntArray values0 = new IntArray(values); IntegersUtils.quicksort(keys.size(), LongFunctions.comparator(keys0), new IntIntProcedure() { @Override public void invoke(int a, int b) { keys0.swap(a, b); values0.swap(a, b); } }); return Arrays.asList(new LongIntListMap(keys0, values0), createdMap); }
/** * 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; }
@Override public void testHashCode() { int attemptsCount = 10, shuffleCount = 10; int sizeMax = 600, step = 50; for (int attempt = 0; attempt < attemptsCount; attempt++) { for (int size = step; size < sizeMax; size += step) { LongArray keys = generateRandomLongArray(size, SORTED_UNIQUE); IntArray values = generateRandomIntArray(keys.size(), UNORDERED); int expectedHash = 0; for (int i = 0; i < size; i++) { expectedHash += IntegersUtils.hash(keys.get(i)) + (new Integer(values.get(i))).hashCode(); } for (WritableLongObjMap<Integer> map0 : createObjMapsFromLists(keys, values.toList())) { assertEquals(expectedHash, map0.hashCode()); } IntArray indices = new IntArray(IntProgression.range(size)); for (int i = 0; i < shuffleCount; i++) { map = createMap(); map.putAll(keys.get(indices), values.get(indices)); assertEquals(expectedHash, map.hashCode()); indices.shuffle(myRand); } } } } }
@Override public void testHashCode() { int attemptsCount = 10, shuffleCount = 10; int sizeMax = 600, step = 50; for (int attempt = 0; attempt < attemptsCount; attempt++) { for (int mSize = step; mSize < sizeMax; mSize += step) { LongArray keys = generateRandomLongArray(mSize, SORTED_UNIQUE); int size = keys.size(); IntArray values = generateRandomIntArray(size, UNORDERED); int expectedHash = 0; for (int i = 0; i < size; i++) { expectedHash += IntegersUtils.hash(keys.get(i)) + IntegersUtils.hash(values.get(i)); } for (T map0 : createMapsFromLists(keys, values)) { assertEquals(expectedHash, map0.hashCode()); } IntArray indices = new IntArray(IntProgression.range(size)); for (int i = 0; i < shuffleCount; i++) { map = createMap(); map.putAll(keys.get(indices), values.get(indices)); assertEquals(expectedHash, map.hashCode()); indices.shuffle(myRand); } } } }
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 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 void testRemoveKey() { int size = 400, attempts = 10, maxVal = 1000; for (int attempt = 0; attempt < attempts; attempt++) { LongArray keys = generateRandomLongArray(size, SortedStatus.UNORDERED, maxVal); IntArray values = new IntArray(keys.size()); for (int i = 0; i < keys.size(); i++) { long key = keys.get(i); values.add((int) (-key * key)); } map.putAll(keys, values); keys.sortUnique(); for (int i = 0; i < maxVal; i++) { if (keys.binarySearch(i) >= 0) { assertEquals(-i * i, map.remove(i)); } else { assertEquals(0, map.remove(i)); } } } }
public void testHashCode() { int attemptsCount = 10, shuffleCount = 10; int sizeMax = 600, step = 50; for (int attempt = 0; attempt < attemptsCount; attempt++) { for (int size = step; size < sizeMax; size += step) { LongArray array = generateRandomLongArray(size, SORTED_UNIQUE); int expectedHash = 0; for (LongIterator it : array) { expectedHash += IntegersUtils.hash(it.value()); } for (T set0 : createSets(array)) { assertEquals(expectedHash, set0.hashCode()); } if (isSortedSet()) return; IntArray indices = new IntArray(IntProgression.range(size)); for (int i = 0; i < shuffleCount; i++) { for (T set : createSets(array.get(indices))) { assertEquals(expectedHash, set.hashCode()); indices.shuffle(myRand); } } } } }
/** * 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 LongList getLcsForPermutation(LongList aseq, LongList bseq) { assert aseq.size() == bseq.size() : toBoundedString(aseq) + toBoundedString(bseq); int sz = aseq.size(); LongIntHppcOpenHashMap aseqMap = new LongIntHppcOpenHashMap(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 (LongIterator it : bseq) { ids.add(aseqMap.get(it.value())); } IntList subseq = IntLongestIncreasingSubsequence.getLIS(ids); LongArray res = new LongArray(subseq.size()); for (IntIterator it : subseq) { res.add(aseq.get(it.value())); } return res; }
public void testValueComplexCase() { IntArray keys = new IntArray(IntProgression.arithmetic(0, 1000, 1)); LongArray values0 = new LongArray(LongProgression.arithmetic(0, 1000, 1)); LongArray values = new LongArray(values0.toNativeArray()); values.apply(0, 1000, new LongToLong() { @Override public long invoke(long a) { return a * a - a; } }); res = IntLongIterators.pair(keys.iterator(), values.iterator()); for (int i = 0; i < 1000; i++) { res.next(); int v1 = res.left(); long v2 = res.right(); assertEquals(i, v1); assertEquals(i * i - i, v2); } } }
public void testRemoveKeyValue() { int size = 20, maxKey = size * 2; LongList keys = LongProgression.range(0, maxKey, maxKey / size); IntList values = new IntArray(limit(cycle(0, 1, 2, 3), 20)); for (T map0 : createMapsFromLists(keys, values)) { for (int i = 0; i < maxKey; i++) { assertFalse(map0.remove(i, -1)); if (keys.contains(i)) { int val = (i/2) % 4; if (val == 0) { assertTrue(map0.remove(i, 0)); } else { assertFalse(map0.remove(i, 0)); assertTrue(map0.remove(i, val)); } } else { assertFalse(map0.remove(i, 0)); assertFalse(map0.remove(i, 1)); } } } }
@Override public void checkMap(LongIntListMap actualMap, LongList expectedKeys, IntList expectedValues) { super.checkMap(actualMap, expectedKeys, expectedValues); final LongArray keysArray = new LongArray(expectedKeys); final IntArray valuesArray = new IntArray(expectedValues); IntegersUtils.quicksort(keysArray.size(), LongFunctions.comparator(keysArray), new IntIntProcedure() { @Override public void invoke(int a, int b) { keysArray.swap(a, b); valuesArray.swap(a, b); } }); CHECK.order(keysArray, actualMap.keysAsList()); CHECK.order(LongCollections.asLongList(valuesArray), LongCollections.asLongList(actualMap.valuesAsList())); CHECK.order(actualMap.keysIterator(0, actualMap.size()), keysArray.iterator()); CHECK.order(actualMap.valuesIterator(0, actualMap.size()), valuesArray.iterator()); }