[Android] Add extension to merge sorted lists

Robert Stone git at videolan.org
Wed Nov 20 06:47:53 UTC 2024


vlc-android | branch: master | Robert Stone <rhstone at gmail.com> | Wed Aug  7 21:56:07 2024 -0700| [31077e2b89b230fbf30b74248a966efabccfab2c] | committer: Duncan McNamara

Add extension to merge sorted lists

> https://code.videolan.org/videolan/vlc-android/commit/31077e2b89b230fbf30b74248a966efabccfab2c
---

 .../src/org/videolan/vlc/util/Kextensions.kt       | 41 ++++++++++++++++++++++
 .../test/org/videolan/vlc/util/UtilTests.kt        |  8 +++++
 2 files changed, 49 insertions(+)

diff --git a/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt b/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
index 5fb7dd3249..9ad431b215 100644
--- a/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
+++ b/application/vlc-android/src/org/videolan/vlc/util/Kextensions.kt
@@ -534,3 +534,44 @@ fun ViewPager2.findCurrentFragment(fragmentManager: FragmentManager): Fragment?
 fun ViewPager2.findFragmentAt(fragmentManager: FragmentManager, position: Int): Fragment? {
     return fragmentManager.findFragmentByTag("f$position")
 }
+
+/**
+ * Merges the current sorted mutable list with another sorted list based on a selected property.
+ *
+ * Both lists must be in ascending order using the same property.
+ *
+ * @param <T>       the type of elements in the list
+ * @param <R>       the type of the comparable property extracted by the selector
+ * @param otherList the list to be merged with the current list
+ * @param selector  a function to extract a comparable property from each element in the list
+ */
+fun <T, R : Comparable<R>> MutableList<T>.mergeSorted(otherList: List<T>, selector: (T) -> R?) {
+    mergeSorted(otherList, compareBy(selector))
+}
+/**
+ * Merges the current sorted mutable list with another sorted list using a custom comparator.
+ *
+ * Both lists must be in ascending order using the same sorting order
+ *
+ * @param <T>        the type of elements in the list
+ * @param otherList  the list to be merged with the current list
+ * @param comparator a comparator to determine the sorting order of elements
+ */
+fun <T> MutableList<T>.mergeSorted(otherList: List<T>, comparator: Comparator<T>) {
+    var thisIndex = 0
+    var otherIndex = 0
+
+    while (thisIndex < this.size && otherIndex < otherList.size) {
+        val thisItem = this[thisIndex]
+        val otherItem = otherList[otherIndex]
+
+        if (comparator.compare(thisItem, otherItem) > 0) {
+            this.add(thisIndex, otherItem)
+            otherIndex++
+        }
+        thisIndex++
+    }
+    // Add remaining elements from otherList
+    for (i in otherIndex until otherList.size)
+        this.add(otherList[i])
+}
diff --git a/application/vlc-android/test/org/videolan/vlc/util/UtilTests.kt b/application/vlc-android/test/org/videolan/vlc/util/UtilTests.kt
index 84501b3c62..0c7f3094cb 100644
--- a/application/vlc-android/test/org/videolan/vlc/util/UtilTests.kt
+++ b/application/vlc-android/test/org/videolan/vlc/util/UtilTests.kt
@@ -4,6 +4,7 @@ import org.junit.Assert
 import org.junit.Test
 import org.junit.runner.RunWith
 import org.junit.runners.JUnit4
+import java.util.Comparator
 
 @RunWith(JUnit4::class)
 class ExtensionsTests {
@@ -22,4 +23,11 @@ class ExtensionsTests {
         Assert.assertEquals("SD", generateResolutionClass(712, 480))
         Assert.assertEquals("SD", generateResolutionClass(716, 480))
     }
+    @Test
+    fun mergeLists() {
+        val list1 = mutableListOf(1, 5, 8, 9)
+        val list2 = listOf(3, 4, 7)
+        list1.mergeSorted(list2, Comparator.naturalOrder())
+        Assert.assertEquals(listOf(1, 3, 4, 5, 7, 8, 9), list1)
+    }
 }
\ No newline at end of file



More information about the Android mailing list