Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify ArrayTagSet sort method #1079

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -304,33 +304,16 @@ private void checkForNullValues(String[] ts) {
* tags. With the small size a simple insertion sort works well.
*/
private static void insertionSort(String[] ts, int length) {
if (length == 4) {
// Two key/value pairs, swap if needed
if (ts[0].compareTo(ts[2]) > 0) {
// Swap key
String tmp = ts[0];
ts[0] = ts[2];
ts[2] = tmp;

// Swap value
tmp = ts[1];
ts[1] = ts[3];
ts[3] = tmp;
}
} else if (length > 4) {
// One entry is already sorted. Two entries handled above, for larger arrays
// use insertion sort.
for (int i = 2; i < length; i += 2) {
String k = ts[i];
String v = ts[i + 1];
int j = i - 2;
for (; j >= 0 && ts[j].compareTo(k) > 0; j -= 2) {
ts[j + 2] = ts[j];
ts[j + 3] = ts[j + 1];
}
ts[j + 2] = k;
ts[j + 3] = v;
for (int i = 2; i < length; i += 2) {
String k = ts[i];
String v = ts[i + 1];
int j = i - 2;
for (; j >= 0 && ts[j].compareTo(k) > 0; j -= 2) {
ts[j + 2] = ts[j];
ts[j + 3] = ts[j + 1];
}
ts[j + 2] = k;
ts[j + 3] = v;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,25 @@ public void addAllDuplicates() {
assertDistinct(updated);
}

class TagIterator implements Iterable<Tag> {
@Test
public void testSort() {
Assertions.assertEquals(Arrays.asList("a", "v1"), toList(ArrayTagSet.create("a", "v1")));
Assertions.assertEquals(Arrays.asList("a", "v1", "b", "v2"), toList(ArrayTagSet.create("a", "v1", "b", "v2")));
Assertions.assertEquals(Arrays.asList("a", "v1", "b", "v2"), toList(ArrayTagSet.create("b", "v2", "a", "v1")));
Assertions.assertEquals(Arrays.asList("a", "v1", "b", "v3"), toList(ArrayTagSet.create("b", "v2", "a", "v1", "b", "v3")));
Assertions.assertEquals(Arrays.asList("a", "v1", "b", "v2", "c", "v3"), toList(ArrayTagSet.create("c", "v3", "b", "v2", "a", "v1")));
}

private static List<String> toList(TagList tagList) {
List<String> list = new ArrayList<>(tagList.size() * 2);
tagList.forEach(tag -> {
list.add(tag.key());
list.add(tag.value());
});
return list;
}

static final class TagIterator implements Iterable<Tag> {
String[] tags;
TagIterator(String... tags) {
this.tags = tags;
Expand All @@ -803,7 +821,7 @@ public Tag next() {
}
}

class BadTagList implements TagList {
static final class BadTagList implements TagList {

String[] tags;
BadTagList(String... tags) {
Expand Down