private int[] 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) { int 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 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 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 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); }