/** * 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 checkToBoundedString(String expected, int lim, LongArray array) { WritableLongSet set = LongTreeSet.createFromSortedUnique(array); LongIterable[] iterables = {array, set, array.iterator(), set.iterator(), LongIterators.unionIterator(array, array.subList(0, 1))}; for (LongIterable iterable : iterables) { assertEquals(iterable.toString(), expected, LongCollections.toBoundedString(iterable, lim)); } }
public void testHasMethods() { int MIN = Integer.MIN_VALUE, MAX = Integer.MAX_VALUE; LongArray someValues = LongArray.create(MIN, MIN + 1, 0, 1, 3, 5, MAX - 1, MAX); for (LongArray first : LongCollections.allSubLists(someValues)) { for (LongArray second : LongCollections.allSubLists(someValues)) { IntegersDebug.println(LongCollections.toBoundedString(first) + " " + LongCollections.toBoundedString(second)); assertEquals(new LongUnionIterator(first, second).hasNext(), hasUnion(first, second)); assertEquals(new LongIntersectionIterator(first, second).hasNext(), hasIntersection(first, second)); assertEquals(new LongMinusIterator(first.iterator(), second.iterator()).hasNext(), hasComplement(first, second)); } } }
IntegersDebug.println(LongCollections.toBoundedString(first) + " " + LongCollections.toBoundedString(second)); checkNewSetCreator(first, second);