From 078c0273bf7935a84afcd22866ffc370a304b6ad Mon Sep 17 00:00:00 2001 From: andi Date: Mon, 15 Aug 2022 16:18:50 +0000 Subject: [PATCH] Update 'README.md' --- README.md | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 7cb5e7b..4a9dc75 100644 --- a/README.md +++ b/README.md @@ -32,21 +32,25 @@ dependencies { # Examples +## IntList / LongList +An alternative to `int[]`/`long[]` that grows dynamically. The list supports a few features that are either not supported +by arrays or that would be expensive to implement. The most unique feature is that the list knows if it is sorted. + +### isSorted() +The lists keep track of whether they are sorted or not. This makes the call to `isSorted()` very cheap, because we only have to return the value of a field. The drawback is that some operations are much more expensive. This affects `shuffle()`, +as well as `remove(int, int)`, `removeIf()`, `removeAll(IntList)` and 'retainAll()' if the list was not sorted before. + +The fact that we know if a list is sorted allows us to use more efficient algorithms for `union(IntList, IntList)`, `intersection(IntList, IntList)`, `indexOf(int)`, `lastIndexOf()` and `uniq()`. + ```java -import org.lucares.collections.IntList; +final IntList list = IntList.of(1, 3, 5); +System.out.println(list + " is sorted: " + list.isSorted()); -public class Example { - public static void main(final String[] args) { - final IntList list = IntList.of(1, 3, 5); - System.out.println(list + " is sorted: " + list.isSorted()); +list.insert(2, 7); +System.out.println(list + " is sorted: " + list.isSorted()); - list.insert(2, 7); - System.out.println(list + " is sorted: " + list.isSorted()); - - list.sort(); - System.out.println(list + " is sorted: " + list.isSorted()); - } -} +list.sort(); +System.out.println(list + " is sorted: " + list.isSorted()); ``` Running this program gives the following output: @@ -56,3 +60,9 @@ Running this program gives the following output: [1, 3, 7, 5] is sorted: false [1, 3, 5, 7] is sorted: true ``` + +The fact that we know if a list is sorted allows us to use more efficient algorithms for `union(IntList, IntList)`, `intersection(IntList, IntList)`, `indexOf(int)`, `lastIndexOf()` and `uniq()`. + + + +