Skip to content

Commit

Permalink
Remove broken .toArray from Long/CharObjectHashMap entirely (#13884)
Browse files Browse the repository at this point in the history
  • Loading branch information
bugmakerrrrrr authored Oct 11, 2024
1 parent 5f0d1fb commit fa77d97
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 37 deletions.
1 change: 1 addition & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Bug Fixes
---------------------
* GITHUB#13832: Fixed an issue where the DefaultPassageFormatter.format method did not format passages as intended
when they were not sorted by startOffset. (Seunghan Jung)
* GITHUB#13884: Remove broken .toArray from Long/CharObjectHashMap entirely. (Pan Guixin)

Build
---------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,15 +574,6 @@ public Iterator<ObjectCursor<VType>> iterator() {
public int size() {
return CharObjectHashMap.this.size();
}

public VType[] toArray() {
VType[] array = (VType[]) new Object[size()];
int i = 0;
for (ObjectCursor<VType> cursor : this) {
array[i++] = cursor.value;
}
return array;
}
}

/** An iterator over the set of assigned values. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,15 +562,6 @@ public Iterator<ObjectCursor<VType>> iterator() {
public int size() {
return LongObjectHashMap.this.size();
}

public VType[] toArray() {
VType[] array = (VType[]) new Object[size()];
int i = 0;
for (ObjectCursor<VType> cursor : this) {
array[i++] = cursor.value;
}
return array;
}
}

/** An iterator over the set of assigned values. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@

package org.apache.lucene.internal.hppc;

import static org.apache.lucene.internal.hppc.TestIntObjectHashMap.toList;

import com.carrotsearch.randomizedtesting.RandomizedTest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Test;

Expand Down Expand Up @@ -66,13 +70,6 @@ private static void assertSortedListEquals(char[] array, char... elements) {
assertArrayEquals(elements, array);
}

/** Check if the array's content is identical to a given sequence of elements. */
private static void assertSortedListEquals(Object[] array, Object... elements) {
assertEquals(elements.length, array.length);
Arrays.sort(array);
assertArrayEquals(elements, array);
}

private final int value0 = vcast(0);
private final int value1 = vcast(1);
private final int value2 = vcast(2);
Expand Down Expand Up @@ -603,13 +600,15 @@ public void testMapValues() {
map.put(key1, value3);
map.put(key2, value2);
map.put(key3, value1);
assertSortedListEquals(map.values().toArray(), value1, value2, value3);
MatcherAssert.assertThat(
toList(map.values()), Matchers.containsInAnyOrder(value1, value2, value3));

map.clear();
map.put(key1, value1);
map.put(key2, value2);
map.put(key3, value2);
assertSortedListEquals(map.values().toArray(), value1, value2, value2);
MatcherAssert.assertThat(
toList(map.values()), Matchers.containsInAnyOrder(value1, value2, value2));
}

/* */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ public void testMapValues() {
assertSortedListEquals(toList(map.values()), value1, value2, value2);
}

private static <T> List<T> toList(Iterable<ObjectCursor<T>> values) {
static <T> List<T> toList(Iterable<ObjectCursor<T>> values) {
ArrayList<T> list = new ArrayList<>();
for (var c : values) {
list.add(c.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@

package org.apache.lucene.internal.hppc;

import static org.apache.lucene.internal.hppc.TestIntObjectHashMap.toList;

import com.carrotsearch.randomizedtesting.RandomizedTest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
Expand Down Expand Up @@ -65,13 +69,6 @@ private static void assertSortedListEquals(long[] array, long... elements) {
assertArrayEquals(elements, array);
}

/** Check if the array's content is identical to a given sequence of elements. */
private static void assertSortedListEquals(Object[] array, Object... elements) {
assertEquals(elements.length, array.length);
Arrays.sort(array);
assertArrayEquals(elements, array);
}

private final int value0 = vcast(0);
private final int value1 = vcast(1);
private final int value2 = vcast(2);
Expand Down Expand Up @@ -585,13 +582,15 @@ public void testMapValues() {
map.put(key1, value3);
map.put(key2, value2);
map.put(key3, value1);
assertSortedListEquals(map.values().toArray(), value1, value2, value3);
MatcherAssert.assertThat(
toList(map.values()), Matchers.containsInAnyOrder(value1, value2, value3));

map.clear();
map.put(key1, value1);
map.put(key2, value2);
map.put(key3, value2);
assertSortedListEquals(map.values().toArray(), value1, value2, value2);
MatcherAssert.assertThat(
toList(map.values()), Matchers.containsInAnyOrder(value1, value2, value2));
}

/* */
Expand Down

0 comments on commit fa77d97

Please sign in to comment.