use sorted flag in hashcode/equals
This commit is contained in:
@@ -645,10 +645,14 @@ public final class IntList implements Serializable, Cloneable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
|
|
||||||
if (size == 0) {
|
if (size == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* only consider values in the range of 0 to size
|
||||||
|
*/
|
||||||
int result = 1;
|
int result = 1;
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
result = 31 * result + data[i];
|
result = 31 * result + data[i];
|
||||||
@@ -672,7 +676,13 @@ public final class IntList implements Serializable, Cloneable {
|
|||||||
if (size != other.size) {
|
if (size != other.size) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (sorted != other.sorted) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* only consider values in the range of 0 to size
|
||||||
|
*/
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
if (data[i] != other.data[i]) {
|
if (data[i] != other.data[i]) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -587,7 +587,29 @@ public class IntListTest {
|
|||||||
Assert.assertNotEquals(list, clone);
|
Assert.assertNotEquals(list, clone);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO test clone of empty list
|
@Test
|
||||||
|
public void testCloneEmptyList() {
|
||||||
|
final IntList list = new IntList();
|
||||||
|
|
||||||
|
final IntList clone = list.clone();
|
||||||
|
Assert.assertEquals(list, clone);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testClonePreservesSortedFlagOnUnsortedList() {
|
||||||
|
final IntList list = IntList.of(3, 2, 1);
|
||||||
|
|
||||||
|
final IntList clone = list.clone();
|
||||||
|
Assert.assertEquals(list.isSorted(), clone.isSorted());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testClonePreservesSortedFlagOnSortedList() {
|
||||||
|
final IntList list = IntList.of(1, 2, 3);
|
||||||
|
|
||||||
|
final IntList clone = list.clone();
|
||||||
|
Assert.assertEquals(list.isSorted(), clone.isSorted());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testToString() {
|
public void testToString() {
|
||||||
|
|||||||
Reference in New Issue
Block a user