diff --git a/primitiveCollections/src/main/java/org/lucares/collections/IntList.java b/primitiveCollections/src/main/java/org/lucares/collections/IntList.java index 33f8d22..af0e586 100644 --- a/primitiveCollections/src/main/java/org/lucares/collections/IntList.java +++ b/primitiveCollections/src/main/java/org/lucares/collections/IntList.java @@ -334,7 +334,12 @@ public final class IntList implements Serializable, Cloneable { size = size - (toIndex - fromIndex); - sorted = size() <= 1 ? true : sorted; // lists of size 1 or smaller are always sorted + if (!sorted) { + sorted = true; + for (int i = 1; i < size && sorted; i++) { + sorted = data[i - 1] <= data[i]; + } + } } /** @@ -384,7 +389,12 @@ public final class IntList implements Serializable, Cloneable { } size = insertPosition; - sorted = size() <= 1 ? true : sorted; // lists of size 1 or smaller are always sorted + if (!sorted) { + sorted = true; + for (int i = 1; i < size && sorted; i++) { + sorted = data[i - 1] <= data[i]; + } + } } /** diff --git a/primitiveCollections/src/test/java/org/lucares/collections/IntListTest.java b/primitiveCollections/src/test/java/org/lucares/collections/IntListTest.java index 7f9c0d0..c56c4df 100644 --- a/primitiveCollections/src/test/java/org/lucares/collections/IntListTest.java +++ b/primitiveCollections/src/test/java/org/lucares/collections/IntListTest.java @@ -1223,6 +1223,30 @@ public class IntListTest { Assert.assertTrue("unsorted list with no elements becomes sorted", list.isSorted()); } + @Test + public void testSortedFlagRemove_unsortedBecomesSorted_emptyList() { + + final IntList list = IntList.of(4, 3, 2, 1); + list.remove(0, 4); // removes all + Assert.assertTrue("empty list is sorted", list.isSorted()); + } + + @Test + public void testSortedFlagRemove_unsortedBecomesSorted_oneElement() { + + final IntList list = IntList.of(4, 3, 2, 1); + list.remove(1, 4); // removes 3,2,1 + Assert.assertTrue("list with one element is", list.isSorted()); + } + + @Test + public void testSortedFlagRemove_unsortedBecomesSorted() { + + final IntList list = IntList.of(1, 2, 777, 4, 5); + list.remove(2, 3); // removes 777 + Assert.assertTrue("list is sorted after remove", list.isSorted()); + } + @Test public void testSortedFlagRemoveAll() { @@ -1253,6 +1277,31 @@ public class IntListTest { Assert.assertTrue("unsorted list with no elements becomes sorted", list.isSorted()); } + @Test + public void testSortedFlagRemoveIf_unsortedBecomesSorted_emptyAfterRemove() { + + final IntList list = IntList.of(1, 3, 2); + list.removeIf((value, index) -> true); // makes the list sorted + Assert.assertTrue("list is empty", list.isEmpty()); + Assert.assertTrue("empty list is sorted", list.isSorted()); + } + + @Test + public void testSortedFlagRemoveIf_unsortedBecomesSorted_oneElementAfterRemove() { + + final IntList list = IntList.of(1, 3, 2); + list.removeIf((value, index) -> value > 1); // makes the list sorted + Assert.assertTrue("list with one element is sorted", list.isSorted()); + } + + @Test + public void testSortedFlagRemoveIf_unsortedBecomesSorted() { + + final IntList list = IntList.of(1, 2, 3, 777, 4, 5); + list.removeIf((value, index) -> value == 777); // makes the list sorted + Assert.assertTrue("unsorted list becomes sorted", list.isSorted()); + } + @Test public void testSortedFlagReplace() {