public static LongList getLCS(LongList aseq, LongList bseq, boolean tryOptimize) { if (aseq == null || bseq == null || aseq.isEmpty() || bseq.isEmpty()) { return LongList.EMPTY; return lcs; int maxsize = Math.min(aseq.size(), bseq.size()); LongList prefix = getPrefix(aseq, bseq, maxsize); if (prefix.size() == maxsize) { return prefix; if (suffix.size() == maxsize) { return suffix; boolean hasPrefix = !prefix.isEmpty(); boolean hasSuffix = !suffix.isEmpty(); if (hasPrefix) { aseq = aseq.subList(prefix.size(), aseq.size()); bseq = bseq.subList(prefix.size(), bseq.size()); aseq = aseq.subList(0, aseq.size() - suffix.size()); bseq = bseq.subList(0, bseq.size() - suffix.size()); long[] r = new LongLongestCommonSubsequence(aseq, bseq, prefix.size(), suffix.size()).calcLCS(); if (hasPrefix) { prefix.toNativeArray(0, r, 0, prefix.size()); suffix.toNativeArray(0, r, r.length - suffix.size(), suffix.size());
public int binSearch(long value) { return list.binarySearch(value); } });
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)); } } }
protected void checkCollection(LongList collection, long... ints) { if (ints == null) ints = IntegersUtils.EMPTY_LONGS; CHECK.order(collection.toNativeArray(), ints); assertEquals(collection.size(), ints.length); CHECK.order(collection.iterator(), ints); LongListIterator it; for (int i = 0; i < ints.length; i++) { it = collection.iterator(); it.move(i); CHECK.order(it, LongCollections.arrayCopy(ints, i, ints.length - i)); it = collection.iterator(); for (int i = 0; i < ints.length; i++) { long anInt = ints[i]; assertTrue(it.hasNext()); assertEquals(anInt, it.nextValue()); assertEquals(anInt, collection.get(i)); assertEquals(ints.length, checker.index); long[] array = new long[ints.length - i + 1]; collection.toNativeArray(i, array, 1, collection.size() - i); CHECK.order(LongCollections.arrayCopy(array, 1, array.length - 1), LongCollections.arrayCopy(ints, i, ints.length - i));
public void unordered(LongList actual, LongList expected) { unordered(actual.toNativeArray(), expected.toNativeArray()); }
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)); } } } }
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); }
public void checkCollect(LongList ... lists) { LongArray expected = new LongArray(); for (LongList array : lists) { expected.addAll(array); } CHECK.order(expected, collectLists(lists)); int attemptsCount = 5; for (int attempt = 0; attempt < attemptsCount; attempt++) { LongIterable[] iterables = new LongIterable[lists.length]; for (int i = 0; i < lists.length; i++) { switch (myRand.nextInt(3)) { case 0: iterables[i] = lists[i]; break; case 1: iterables[i] = lists[i].iterator(); break; case 2: int size2 = lists[i].size() / 2; iterables[i] = new LongConcatIterator(lists[i].subList(0, size2), lists[i].subList(size2, lists[i].size())); break; default: assert false; } } CHECK.order(expected, collectIterables(iterables)); for (int i = 0; i < iterables.length; i++) { if (iterables[i] instanceof LongIterator) { iterables[i] = lists[i].iterator(); } } CHECK.order(expected, collectIterables(expected.size(), iterables)); } }
protected void checkValues(long... values) { for (LongList arr : createLongListVariants(values)) { int length = values.length; assertEquals(length == 0, arr.isEmpty()); assertEquals(LongCollections.isSorted(values, 0, length), arr.isSorted()); boolean res = LongCollections.isSortedUnique(false, values, 0, length) == 0; assertEquals(res, arr.isSortedUnique()); checkCollection(arr, values); } }
public void testGetNextDifferentValueIndex() { for (LongList arr : createLongListVariants(0, 1, 1, 2, 2, 2, 3, 4, 5, 5, 5)) { assertEquals(1, arr.getNextDifferentValueIndex(0)); assertEquals(3, arr.getNextDifferentValueIndex(1)); assertEquals(6, arr.getNextDifferentValueIndex(3)); assertEquals(7, arr.getNextDifferentValueIndex(6)); assertEquals(arr.size(), arr.getNextDifferentValueIndex(8)); } }
private void checkToMethods(long... values) { LongList expected = LongArray.create(values); int length = values.length; long[] tmp = new long[length]; for (LongList arr : createLongListVariants(values)) { CHECK.order(expected.toNativeArray(0, tmp, 0, length), arr.toNativeArray(0, tmp, 0, length)); CHECK.order(expected.toNativeArray(), arr.toNativeArray()); CHECK.order(expected.toList(), arr.toList()); } }
private void checkSetRangeAndRemoveRange(LongList values) { int size = values.size(); for (int from = 0; from < size; from++) { for(int to = from + 1; to < size; to++) { LongList valuesForSetRange = toSortedUnique(values.subList(max(0, from - 1), min(to + 1, size))); for (int val = 0; val < valuesForSetRange.size(); val++) { list = LongSameValuesList.create(values); list.setRange(from, to, val); LongList expected = concatLists(values.subList(0, from), repeat(val, to - from), values.subList(to, size)); CHECK.order(expected, list); assertEquals(getChangeCount(expected), list.getChangeCount()); } list = LongSameValuesList.create(values); list.removeRange(from, to); LongList expected = concatLists(values.subList(0, from), values.subList(to, size)); CHECK.order(expected, list); assertEquals(getChangeCount(expected), list.getChangeCount()); } } }
public void testRepeat() { LongList array = LongCollections.repeat(-5, 3); CHECK.order(array, -5, -5, -5); array = LongCollections.repeat(0, 4); CHECK.order(array, 0, 0, 0, 0); assertEquals(0, LongCollections.repeat(3, 0).size()); long[] expected = new long[10]; Arrays.fill(expected, 3, 10, -1); long[] actual = new long[10]; LongCollections.repeat(-1, 10).toNativeArray(3, actual, 3, 7); CHECK.order(expected, actual); try { LongCollections.repeat(3, -1); fail("must throw IAE"); } catch(IllegalArgumentException ex) { // ok } }
protected List<LongTreeSet> createSets(LongList sortedUniqueList) { set = new LongTreeSet(); set.addAll(sortedUniqueList); LongTreeSet set2 = new LongTreeSet(); set2.addAll(sortedUniqueList.iterator()); return Arrays.asList(set, set2, LongTreeSet.createFromSortedUnique(sortedUniqueList), LongTreeSet.createFromSortedUnique(sortedUniqueList, sortedUniqueList.size(), TO_ADD), LongTreeSet.createFromSortedUnique(sortedUniqueList, sortedUniqueList.size(), TO_REMOVE)); }
public void testRemoveAllValues() { map.insertAllRo(LongProgression.arithmetic(0, 10), apply(swap(MOD), 3)); LongList notInMap; notInMap = map.removeAllValues(LongArray.create(2, 0)); compare.empty(notInMap.toNativeArray()); compare.order(map.getKeys().iterator(), 1, 4, 7); checkMapValuesEqualKeysMod(3); map.removeAllValues(LongArray.create(1)); assertEquals(0, map.size()); map.clear(); map.insertAllRo(LongProgression.arithmetic(0, 10, 2), apply(swap(MOD), 13)); // Important: specify one of the values not in the map notInMap = map.removeAllValues(LongArray.create(6, 7, 8, 9, 10)); compare.order(notInMap.iterator(), 7, 9); compare.order(map.getKeys().iterator(), 0, 2, 4, 12, 14, 16, 18); checkMapValuesEqualKeysMod(13); }