/** * {@inheritDoc} * <p> * <strong>Note that this implementation does not guarantee that blocks would be sorted by index.</strong> * </p> */ @Override public Collection<Block> getByResourceId(String resourceId) { ensureSorted(); // prepare resourceId for binary search resourceIds[size] = resourceId; resourceIdsIndex[size] = size; int index = DataUtils.binarySearch(byResourceId); List<Block> result = new ArrayList<>(); int realIndex = resourceIdsIndex[index]; while (index < size && FastStringComparator.INSTANCE.compare(resourceIds[realIndex], resourceId) == 0) { result.add(getBlock(realIndex, resourceId)); index++; realIndex = resourceIdsIndex[index]; } return result; }
private static void quickSort(Sortable data, int low, int high) { if (high - low < 5) { bubbleSort(data, low, high); return; } int i = partition(data, low, high); if (low < i - 1) { quickSort(data, low, i - 1); } if (i < high) { quickSort(data, i, high); } }
public static void sort(Sortable data) { quickSort(data, 0, data.size() - 1); }
public static void sort(Sortable data) { quickSort(data, 0, data.size() - 1); }
private static void quickSort(Sortable data, int low, int high) { if (high - low < 5) { bubbleSort(data, low, high); return; } int i = partition(data, low, high); if (low < i - 1) { quickSort(data, low, i - 1); } if (i < high) { quickSort(data, i, high); } }
/** * {@inheritDoc} */ @Override public Collection<Block> getBySequenceHash(ByteArray sequenceHash) { ensureSorted(); // prepare hash for binary search int[] hash = sequenceHash.toIntArray(); if (hash.length != hashInts) { throw new IllegalArgumentException("Expected " + hashInts + " ints in hash, but got " + hash.length); } int offset = size * blockInts; for (int i = 0; i < hashInts; i++) { blockData[offset++] = hash[i]; } int index = DataUtils.binarySearch(byBlockHash); List<Block> result = new ArrayList<>(); while (index < size && !isLessByHash(size, index)) { // extract block (note that there is no need to extract hash) String resourceId = resourceIds[index]; result.add(createBlock(index, resourceId, sequenceHash)); index++; } return result; }
@Test public void testSort() { int[] expected = new int[200]; int[] actual = new int[expected.length]; for (int i = 0; i < expected.length; i++) { expected[i] = (int) (Math.random() * 900); actual[i] = expected[i]; } Arrays.sort(expected); DataUtils.sort(new SimpleSortable(actual, actual.length)); assertThat(actual, equalTo(expected)); }
@Test public void testSearch() { int[] a = new int[] { 1, 2, 4, 4, 4, 5, 0 }; SimpleSortable sortable = new SimpleSortable(a, a.length - 1); // search 4 a[a.length - 1] = 4; assertThat(DataUtils.binarySearch(sortable), is(2)); // search 5 a[a.length - 1] = 5; assertThat(DataUtils.binarySearch(sortable), is(5)); // search -5 a[a.length - 1] = -5; assertThat(DataUtils.binarySearch(sortable), is(0)); // search 10 a[a.length - 1] = 10; assertThat(DataUtils.binarySearch(sortable), is(6)); // search 3 a[a.length - 1] = 3; assertThat(DataUtils.binarySearch(sortable), is(2)); }
int index = DataUtils.binarySearch(byBlockHash);
resourceIdsIndex[size] = size; int index = DataUtils.binarySearch(byResourceId);