@Override public long invoke(long a) { return permutation.get((int)a); } });
public long get(int index) { return list.get(index); }
public boolean accept(long value, LongList source) { assertEquals(value, myCollection.get(index)); index++; return true; } }
private long[] calcLCS() { for (int i = 0; i < alen; i++) { for (int j = 0; j < blen; j++) { if (aseq.get(i) == bseq.get(j)) { lens[p(i, j)] = m(i - 1, j - 1) + 1; } else { int i = alen - 1, j = blen - 1; while (i >= 0 && j >= 0 && ri >= 0) { long v = aseq.get(i); if (v == bseq.get(j)) { r[prefixSize + (ri--)] = v; i--;
/** * 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; }
protected void checkGetMethods(long... values) { LongList expected = LongArray.create(values); for (LongList arr : createLongListVariants(values)) { for (int i = 0; i < arr.size(); i++) { assertEquals(expected.get(i), arr.get(i)); } for (int i = 0; i < arr.size(); i++) { for (int j = -1; j < 2; j++) { long value = arr.get(i) + j; assertEquals(expected.indexOf(value), arr.indexOf(value)); assertEquals(expected.contains(value), arr.contains(value)); } } } }
@Override protected List<LongSetBuilder> createSets(LongList sortedUniqueList) { ArrayList<LongSetBuilder> sets = new ArrayList(); LongSetBuilder builder = new LongSetBuilder(); builder.addAll(sortedUniqueList); sets.add(builder); int size = sortedUniqueList.size(); if (size != 0 && size < 300) { int numberOfSets = 100; for (int attempt = 0; attempt < numberOfSets; attempt++) { builder = new LongSetBuilder(); LongArray addMask = generateRandomLongArray(size, UNORDERED, 2); for (int i = 0; i < addMask.size(); i++) { if (addMask.get(i) == 0) builder.add(sortedUniqueList.get(i)); } builder.coalesce(); for (int i = 0; i < addMask.size(); i++) { if (addMask.get(i) == 1) builder.add(sortedUniqueList.get(i)); } sets.add(builder); } } return sets; }
public static void segmentedLongArrayChecker(LongList expected, LongSegmentedArray actual) { assertEquals(expected.size(), actual.size()); for (int i = 0; i < expected.size(); i++) { long val = expected.get(i); assertTrue(val == -1 || val == actual.get(i)); } }
private Pair<LongArray, LongArray> createInters(LongList a, LongList b, float intersRate, int intersLen, int maxElem) { LongArray trueIntersection = new LongArray(intersLen); LongArray withExtra = new LongArray(intersLen); int sza = a.size(); int szb = b.size(); int max = (int)((sza + szb) / intersRate); for (int i = 0; i < intersLen; ++i) { int idx = myRand.nextInt(max); if (idx < sza) { long value = a.get(idx); trueIntersection.add(value); withExtra.add(value); } else if (idx < szb) { long value = b.get(idx - sza); trueIntersection.add(value); withExtra.add(value); } else { long value; do value = myRand.nextInt(maxElem); while (value % 2 == 0 || value % 3 == 0); withExtra.add(value); } } trueIntersection.sortUnique(); withExtra.sortUnique(); return Pair.create(trueIntersection, withExtra); }
public void testGet() { for (int attempt = 0; attempt < 10; attempt++) { long[] values = generateRandomLongArray(1000, UNORDERED).extractHostArray(); for (LongList list: createLongListVariants(values)) { if (!(list instanceof AbstractLongList)) return; IntArray indices = generateRandomIntArray(100, UNORDERED, 0, 1000); AbstractLongList abstractList = (AbstractLongList)list; LongList actual = abstractList.get(indices); for (int j = 0; j < actual.size(); j++) { assertEquals(abstractList.get(indices.get(j)), actual.get(j)); } } } }
@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); }
public void testRemoveAllRandom() { final int N_KEYS = 3000; final int N_REMOVED_STEP = 20; final int N_TRIALS = 100; final int PRIME = 41; LongArray alreadyRemoved = new LongArray(); map.insertAllRo(LongProgression.arithmetic(0, N_KEYS), apply(swap(MOD), PRIME)); for (int i = 0; i < N_TRIALS; ++i) { LongArray toRemove = new LongArray(N_REMOVED_STEP); for (int j = 0; j < N_REMOVED_STEP; ++j) toRemove.add(myRand.nextInt(N_KEYS)); LongList notInMap = map.removeAll(toRemove); for (int j = 0; j < notInMap.size(); ++j) assertTrue(i + " " + j, alreadyRemoved.contains(notInMap.get(j))); checkMapValuesEqualKeysMod(PRIME); alreadyRemoved.addAll(toRemove); assertFalse(map.containsAnyKeys(toRemove)); assertFalse(map.containsAnyKeys(alreadyRemoved)); } }
protected void checkRemovedIndexes(final AbstractLongListRemovingDecorator rem, int... expected) { if (expected == null) expected = IntegersUtils.EMPTY_INTS; CHECK.order(rem.removedIndexIterator(), expected); LongList base = rem.getBase(); for (int i = 0; i < base.size(); i++) { boolean removed = rem.isRemovedAt(i); boolean kept = IntCollections.indexOf(i, expected) < 0; assertTrue(String.valueOf(i), removed != kept); int newIndex = rem.getNewIndex(i); if (removed) assertEquals(-1, newIndex); else { assertTrue(newIndex >= 0); assertEquals(base.get(i), rem.get(newIndex)); } } boolean res = rem.iterate(0, rem.size(), new AbstractLongListDecorator.LongVisitor() { int index = -1; public boolean accept(long value, LongList source) { index++; assertEquals(rem.get(index), value); assertSame(rem.getBase(), source); return true; } }); assertTrue(res); }
protected void checkInsertIndexes(final LongListInsertingDecorator ins, int... expected) { if (expected == null) { expected = IntegersUtils.EMPTY_INTS; } CHECK.order(ins.insertIndexIterator(), expected); LongList base = ins.getBase(); for (int i = 0; i < base.size(); i++) { int newIndex = ins.getNewIndex(i); assertTrue(IntCollections.indexOf(newIndex, expected) < 0); assertEquals(base.get(i), ins.get(newIndex)); } final int[] expectedCopy = expected; boolean res = ins.iterate(0, ins.size(), new AbstractLongListDecorator.LongVisitor() { int index = -1; 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; } }); assertTrue(res); }
public void checkPutIfAbsent(LongList keys) { map.clear(); for (int i = 0; i < keys.size(); i++) { long key = keys.get(i); int idx = keys.subList(0, i).indexOf(key); if (idx >= 0) { assertEquals(idx, map.get(key)); assertFalse(map.putIfAbsent(key, i)); assertEquals(idx, map.get(key)); } else { assertEquals(LongIntMap.DEFAULT_VALUE, map.get(key)); assertTrue(map.putIfAbsent(key, i)); assertEquals(i, map.get(key)); } } }
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); }