diff --git a/.idea/kotlinScripting.xml b/.idea/kotlinScripting.xml
deleted file mode 100644
index bc444de..0000000
--- a/.idea/kotlinScripting.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml
index 0fc3113..f8467b4 100644
--- a/.idea/kotlinc.xml
+++ b/.idea/kotlinc.xml
@@ -1,6 +1,6 @@
-
+
\ No newline at end of file
diff --git a/build.gradle.kts b/build.gradle.kts
index ea81d94..e06d79b 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -6,9 +6,9 @@ import org.lwjgl.Lwjgl.Module.jemalloc
import org.lwjgl.lwjgl
plugins {
- kotlin("jvm") version embeddedKotlinVersion
+ embeddedKotlin("jvm")
id("org.lwjgl.plugin") version "0.0.34"
- id("elect86.magik") version "0.3.2"
+ id("elect86.magik") version "0.3.3"
`maven-publish`
// id("com.github.johnrengelman.shadow") version "8.1.1"
}
@@ -19,7 +19,7 @@ repositories {
dependencies {
lwjgl { implementation(jemalloc) }
- testImplementation(kotlin("test"))
+ testImplementation(embeddedKotlin("test"))
}
kotlin.jvmToolchain { languageVersion.set(JavaLanguageVersion.of(8)) }
diff --git a/buildSrc/src/main/kotlin/kool/gen/GenerateCode.kt b/buildSrc/src/main/kotlin/kool/gen/GenerateCode.kt
index b4c58b1..89bb45e 100644
--- a/buildSrc/src/main/kotlin/kool/gen/GenerateCode.kt
+++ b/buildSrc/src/main/kotlin/kool/gen/GenerateCode.kt
@@ -3,6 +3,7 @@ package kool.gen
import kool.*
import org.gradle.api.DefaultTask
import org.gradle.api.file.Directory
+import org.gradle.api.file.FileSystemOperations
import org.gradle.api.file.ProjectLayout
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.OutputDirectory
@@ -14,19 +15,22 @@ abstract class GenerateCode: DefaultTask() {
init {
group = "build"
- description = "Generate glm code"
+ description = "Generate code"
}
@get:Inject
abstract val layout: ProjectLayout
+ @get:Inject
+ abstract val files: FileSystemOperations
+
@get:OutputDirectory
- val targetDir: Provider
- get() = layout.buildDirectory.dir("generated/common")
+ val targetDir: Directory = layout.projectDirectory.dir("src/mainGen/kotlin")
@TaskAction
fun generate() {
- val target = targetDir.get().asFile
+ files.delete { delete(targetDir) }
+ val target = targetDir.asFile
ubuffers(target)
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index d7b400a..a5e31d4 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -3,6 +3,6 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
-distributionSha256Sum=ff7bf6a86f09b9b2c40bb8f48b25fc19cf2b2664fd1d220cd7ab833ec758d0d7
+distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
+distributionSha256Sum=3e1af3ae886920c3ac87f7a91f816c0c7c436f276a6eefdb3da152100fef72ae
diff --git a/settings.gradle.kts b/settings.gradle.kts
index f370dfb..c4799be 100644
--- a/settings.gradle.kts
+++ b/settings.gradle.kts
@@ -10,7 +10,7 @@ pluginManagement {
gradle.rootProject {
group = "kotlin.graphics"
- version = "0.9.77"
+ version = "0.9.78"
}
//includeBuild("../build-logic")
\ No newline at end of file
diff --git a/src/mainGen/kotlin/kool/buffersJvm.kt b/src/mainGen/kotlin/kool/buffersJvm.kt
new file mode 100644
index 0000000..c91d7fe
--- /dev/null
+++ b/src/mainGen/kotlin/kool/buffersJvm.kt
@@ -0,0 +1,12568 @@
+@file:Suppress("NOTHING_TO_INLINE")
+@file:OptIn(kotlin.experimental.ExperimentalTypeInference::class, kotlin.ExperimentalUnsignedTypes::class)
+package kool
+import kool.ubuffers.UByteBuffer
+import java.nio.LongBuffer
+import kool.ubuffers.UShortBuffer
+import kool.ubuffers.ULongBuffer
+import kool.lib.mapCapacity
+import kool.set
+import java.nio.IntBuffer
+import java.nio.ByteBuffer
+import kool.rem
+import org.lwjgl.PointerBuffer
+import kool.iterator
+import java.nio.ShortBuffer
+import java.nio.DoubleBuffer
+import java.nio.FloatBuffer
+import java.nio.CharBuffer
+import kool.lib.collectionSizeOrDefault
+import kool.ubuffers.UIntBuffer
+
+/**
+ * Returns 1st *element* from the buffer.
+ *
+ * If the size of this array is less than 1, throws an [IndexOutOfBoundsException]
+ */
+inline operator fun CharBuffer.component1(): Char = get(0)
+/**
+ * Returns 2nd *element* from the buffer.
+ *
+ * If the size of this array is less than 2, throws an [IndexOutOfBoundsException]
+ */
+inline operator fun CharBuffer.component2(): Char = get(1)
+/**
+ * Returns 3rd *element* from the buffer.
+ *
+ * If the size of this array is less than 3, throws an [IndexOutOfBoundsException]
+ */
+inline operator fun CharBuffer.component3(): Char = get(2)
+/**
+ * Returns 4th *element* from the buffer.
+ *
+ * If the size of this array is less than 4, throws an [IndexOutOfBoundsException]
+ */
+inline operator fun CharBuffer.component4(): Char = get(3)
+/**
+ * Returns 5th *element* from the buffer.
+ *
+ * If the size of this array is less than 5, throws an [IndexOutOfBoundsException]
+ */
+inline operator fun CharBuffer.component5(): Char = get(4)
+/** Returns `true` if [element] is found in the array. */
+operator fun CharBuffer.contains(element: Char): Boolean = indexOf(element) >= 0
+/** Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. */
+inline fun CharBuffer.elementAt(index: Int): Char = get(index)
+/** Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */
+inline fun CharBuffer.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char = if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
+/** Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */
+inline fun CharBuffer.elementAtOrNull(index: Int): Char? = this.getOrNull(index)
+/** Returns the first element matching the given [predicate], or `null` if no such element was found. */
+inline fun CharBuffer.find(predicate: (Char) -> Boolean): Char? = firstOrNull(predicate)
+/** Returns the last element matching the given [predicate], or `null` if no such element was found. */
+inline fun CharBuffer.findLast(predicate: (Char) -> Boolean): Char? = lastOrNull(predicate)
+/**
+ * Returns the first element.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+fun CharBuffer.first(): Char {
+ if (isEmpty())
+ throw NoSuchElementException("Array is empty.")
+ return this[0]
+}
+/**
+ * Returns the first element matching the given [predicate].
+ * @throws [NoSuchElementException] if no such element is found.
+ */
+inline fun CharBuffer.first(predicate: (Char) -> Boolean): Char {
+ for (element in this) if (predicate(element)) return element
+ throw NoSuchElementException("Array contains no element matching the predicate.")
+}
+/** Returns the first element, or `null` if the array is empty. */
+fun CharBuffer.firstOrNull(): Char? = if (isEmpty()) null else this[0]
+/** Returns the first element matching the given [predicate], or `null` if element was not found. */
+inline fun CharBuffer.firstOrNull(predicate: (Char) -> Boolean): Char? {
+ for (element in this) if (predicate(element)) return element
+ return null
+}
+/** Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */
+inline fun CharBuffer.getOrElse(index: Int, defaultValue: (Int) -> Char): Char = if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
+/** Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */
+fun CharBuffer.getOrNull(index: Int): Char? = if (index >= 0 && index <= lastIndex) get(index) else null
+/** Returns first index of [element], or -1 if the array does not contain element. */
+fun CharBuffer.indexOf(element: Char): Int {
+ for (index in indices)
+ if (element == this[index])
+ return index
+ return -1
+}
+/** Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. */
+inline fun CharBuffer.indexOfFirst(predicate: (Char) -> Boolean): Int {
+ for (index in indices)
+ if (predicate(this[index]))
+ return index
+ return -1
+}
+/** Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. */
+inline fun CharBuffer.indexOfLast(predicate: (Char) -> Boolean): Int {
+ for (index in indices.reversed())
+ if (predicate(this[index]))
+ return index
+ return -1
+}
+/**
+ * Returns the last element.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+fun CharBuffer.last(): Char {
+ if (isEmpty())
+ throw NoSuchElementException("Array is empty.")
+ return this[lastIndex]
+}
+/**
+ * Returns the last element matching the given [predicate].
+ *
+ * @throws NoSuchElementException if no such element is found.
+ */
+inline fun CharBuffer.last(predicate: (Char) -> Boolean): Char {
+ for (index in this.indices.reversed()) {
+ val element = this[index]
+ if (predicate(element)) return element
+ }
+ throw NoSuchElementException("Array contains no element matching the predicate.")
+}
+/** Returns last index of [element], or -1 if the array does not contain element. */
+fun CharBuffer.lastIndexOf(element: Char): Int {
+ for (index in indices.reversed())
+ if (element == this[index])
+ return index
+ return -1
+}
+/** Returns the last element, or `null` if the array is empty. */
+fun CharBuffer.lastOrNull(): Char? = if (isEmpty()) null else this[rem - 1]
+/** Returns the last element matching the given [predicate], or `null` if no such element was found. */
+inline fun CharBuffer.lastOrNull(predicate: (Char) -> Boolean): Char? {
+ for (index in this.indices.reversed()) {
+ val element = this[index]
+ if (predicate(element)) return element
+ }
+ return null
+}
+/** Returns the single element, or throws an exception if the array is empty or has more than one element. */
+fun CharBuffer.single(): Char = when (rem) {
+ 0 -> throw NoSuchElementException("Array is empty.")
+ 1 -> this[0]
+ else -> throw IllegalArgumentException("Array has more than one element.")
+}
+/** Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. */
+inline fun CharBuffer.single(predicate: (Char) -> Boolean): Char {
+ var single: Char? = null
+ var found = false
+ for (element in this)
+ if (predicate(element)) {
+ if (found) throw IllegalArgumentException("Array contains more than one matching element.")
+ single = element
+ found = true
+ }
+ if (!found) throw NoSuchElementException("Array contains no element matching the predicate.")
+ @Suppress("UNCHECKED_CAST")
+ return single as Char
+}
+/** Returns single element, or `null` if the array is empty or has more than one element. */
+fun CharBuffer.singleOrNull(): Char? = if (rem == 1) this[0] else null
+/** Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. */
+inline fun CharBuffer.singleOrNull(predicate: (Char) -> Boolean): Char? {
+ var single: Char? = null
+ var found = false
+ for (element in this)
+ if (predicate(element)) {
+ if (found) return null
+ single = element
+ found = true
+ }
+ if (!found) return null
+ return single
+}
+/** Returns a list containing only elements matching the given [predicate]. */
+inline fun CharBuffer.filter(predicate: (Char) -> Boolean): List = filterTo(ArrayList(), predicate)
+/**
+ * Returns a list containing only elements matching the given [predicate].
+ * @param [predicate] function that takes the index of an element and the element itself and returns the result of predicate evaluation on the element.
+ */
+inline fun CharBuffer.filterIndexed(predicate: (index: Int, Char) -> Boolean): List = filterIndexedTo(ArrayList(), predicate)
+/**
+ * Appends all elements matching the given [predicate] to the given [destination].
+ * @param [predicate] function that takes the index of an element and the element itself and returns the result of predicate evaluation on the element.
+ */
+inline fun > CharBuffer.filterIndexedTo(destination: C, predicate: (index: Int, Char) -> Boolean): C {
+ forEachIndexed { index, element ->
+ if (predicate(index, element)) destination.add(element)
+ }
+ return destination
+}
+/** Returns a list containing all elements not matching the given [predicate]. */
+inline fun CharBuffer.filterNot(predicate: (Char) -> Boolean): List = filterNotTo(ArrayList(), predicate)
+/** Appends all elements not matching the given [predicate] to the given [destination]. */
+inline fun > CharBuffer.filterNotTo(destination: C, predicate: (Char) -> Boolean): C {
+ for (element in this) if (!predicate(element)) destination.add(element)
+ return destination
+}
+/** Appends all elements matching the given [predicate] to the given [destination]. */
+inline fun > CharBuffer.filterTo(destination: C, predicate: (Char) -> Boolean): C {
+ for (element in this) if (predicate(element)) destination.add(element)
+ return destination
+}
+/** Returns a list containing elements at specified [indices]. */
+fun CharBuffer.slice(indices: Iterable): List {
+ val size = indices.collectionSizeOrDefault(10)
+ if (size == 0) return emptyList()
+ val list = ArrayList(size)
+ for (index in indices)
+ list.add(get(index))
+ return list
+}
+/** Returns an array containing elements of this array at specified [indices]. */
+fun CharBuffer.sliceArray(indices: Collection): CharArray {
+ val result = CharArray(indices.size)
+ var targetIndex = 0
+ for (sourceIndex in indices)
+ result[targetIndex++] = this[sourceIndex]
+ return result
+}
+/** Sorts elements in the array in-place descending according to their natural sort order. */
+fun CharBuffer.sortDescending() {
+ if (rem > 1) {
+ val array = toCharArray().apply{ sortDescending() }
+ for (i in 0 until rem)
+ this[pos + i] = array[i]
+ }
+}
+/** Returns a list of all elements sorted according to their natural sort order. */
+fun CharBuffer.sorted(): List = toTypedArray().apply { sort() }.asList()
+/** Returns an array with all elements of this array sorted according to their natural sort order. */
+fun CharBuffer.sortedArray(): CharArray = toCharArray().apply { sort() }
+/** Returns an array with all elements of this array sorted descending according to their natural sort order. */
+fun CharBuffer.sortedArrayDescending(): CharArray = toCharArray().apply { sortDescending() }
+/** Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. */
+inline fun > CharBuffer.sortedBy(crossinline selector: (Char) -> R?): List = toCharArray().sortedWith(compareBy(selector))
+/** Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. */
+inline fun > CharBuffer.sortedByDescending(crossinline selector: (Char) -> R?): List = toCharArray().sortedWith(compareByDescending(selector))
+/** Returns a list of all elements sorted descending according to their natural sort order. */
+fun CharBuffer.sortedDescending(): List = toCharArray().apply { sort() }.reversed()
+/** Returns a list of all elements sorted according to the specified [comparator]. */
+fun CharBuffer.sortedWith(comparator: Comparator): List = toTypedArray().apply { sortWith(comparator) }.asList()
+/** Sorts the array in-place. */
+fun CharBuffer.sort() {
+ if (rem > 1) {
+ val array = toCharArray().apply{ sort() }
+ for (i in 0 until rem)
+ this[pos + i] = array[i]
+ }
+}
+/** Returns a string representation of the contents of the specified array as if it is [List]. */
+fun CharBuffer.contentToString(): String {
+ val iMax = lim - 1
+ if (iMax == -1) return "[]"
+ val b = StringBuilder()
+ b.append('[')
+ var i = 0
+ while (true) {
+ b.append(get(i))
+ if (i == iMax) return b.append(']').toString()
+ b.append(", ")
+ i++
+ }
+}
+/**
+ * Fills this array or its subrange with the specified [element] value.
+ *
+ * @param fromIndex the start of the range (inclusive) to fill, 0 by default.
+ * @param toIndex the end of the range (exclusive) to fill, size of this array by default.
+ *
+ * @throws IndexOutOfBoundsException if [fromIndex] is less than zero or [toIndex] is greater than the size of this array.
+ * @throws IllegalArgumentException if [fromIndex] is greater than [toIndex].
+ */
+fun CharBuffer.fill(element: Char, fromIndex: Int = pos, toIndex: Int = rem) {
+ for (i in fromIndex until toIndex)
+ this[i] = element
+}
+/** Returns the range of valid indices for the array. */
+val CharBuffer.indices: IntRange
+ get() = IntRange(0, lastIndex)
+/** Returns `true` if the array is empty. */
+inline fun CharBuffer.isEmpty(): Boolean = rem == 0
+/** Returns `true` if the array is not empty. */
+inline fun CharBuffer.isNotEmpty(): Boolean = !isEmpty()
+/** Returns the last valid index for the array. */
+val CharBuffer.lastIndex: Int
+ get() = rem - 1
+/** Returns an array of Byte containing all of the elements of this generic array. */
+fun CharBuffer.toCharArray(): CharArray = CharArray(rem) { index -> this[index] }
+/** Returns a *typed* object array containing all of the elements of this primitive array. */
+fun CharBuffer.toTypedArray(): Array = Array(rem) { index -> this[index] }
+/**
+ * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
+ *
+ * If any of two pairs would have the same key the last one gets added to the map.
+ *
+ * The returned map preserves the entry iteration order of the original array.
+ */
+inline fun CharBuffer.associate(transform: (Char) -> Pair): Map {
+ val capacity = mapCapacity(rem).coerceAtLeast(16)
+ return associateTo(LinkedHashMap(capacity), transform)
+}
+/**
+ * Returns a [Map] containing the elements from the given array indexed by the key returned from [keySelector] function applied to each element.
+ *
+ * If any two elements would have the same key returned by [keySelector] the last one gets added to the map.
+ *
+ * The returned map preserves the entry iteration order of the original array.
+ */
+inline fun CharBuffer.associateBy(keySelector: (Char) -> K): Map {
+ val capacity = mapCapacity(rem).coerceAtLeast(16)
+ return associateByTo(LinkedHashMap(capacity), keySelector)
+}
+/**
+ * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array.
+ *
+ * If any two elements would have the same key returned by [keySelector] the last one gets added to the map.
+ *
+ * The returned map preserves the entry iteration order of the original array.
+ */
+inline fun CharBuffer.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map {
+ val capacity = mapCapacity(rem).coerceAtLeast(16)
+ return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform)
+}
+/**
+ * Populates and returns the [destination] mutable map with key-value pairs, where key is provided by
+ * the [keySelector] function applied to each element of the given array and value is the element itself.
+ *
+ * If any two elements would have the same key returned by [keySelector] the last one gets added to the map.
+ */
+inline fun > CharBuffer.associateByTo(destination: M, keySelector: (Char) -> K): M {
+ for (element in this)
+ destination.put(keySelector(element), element)
+ return destination
+}
+/**
+ * Populates and returns the [destination] mutable map with key-value pairs, where key is provided by the [keySelector]
+ * function and and value is provided by the [valueTransform] function applied to elements of the given array.
+ *
+ * If any two elements would have the same key returned by [keySelector] the last one gets added to the map.
+ */
+inline fun > CharBuffer.associateByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M {
+ for (element in this)
+ destination.put(keySelector(element), valueTransform(element))
+ return destination
+}
+/**
+ * Populates and returns the [destination] mutable map with key-value pairs provided by [transform] function
+ * applied to each element of the given array.
+ *
+ * If any of two pairs would have the same key the last one gets added to the map.
+ */
+inline fun > CharBuffer.associateTo(destination: M, transform: (Char) -> Pair): M {
+ for (element in this)
+ destination += transform(element)
+ return destination
+}
+/**
+ * Returns a [Map] where keys are elements from the given array and values are produced by the [valueSelector] function applied to each element.
+ *
+ * If any two elements are equal, the last one gets added to the map.
+ *
+ * The returned map preserves the entry iteration order of the original array.
+ */
+inline fun CharBuffer.associateWith(valueSelector: (Char) -> V): Map {
+ val result = LinkedHashMap(mapCapacity(rem).coerceAtLeast(16))
+ return associateWithTo(result, valueSelector)
+}
+/**
+ * Populates and returns the [destination] mutable map with key-value pairs for each element of the given array,
+ * where key is the element itself and value is provided by the [valueSelector] function applied to that key.
+ *
+ * If any two elements are equal, the last one overwrites the former value in the map.
+ */
+inline fun > CharBuffer.associateWithTo(destination: M, valueSelector: (Char) -> V): M {
+ for (element in this)
+ destination.put(element, valueSelector(element))
+ return destination
+}
+/** Appends all elements to the given [destination] collection. */
+fun > CharBuffer.toCollection(destination: C): C {
+ for (item in this)
+ destination.add(item)
+ return destination
+}
+/** Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. */
+inline fun CharBuffer.flatMap(transform: (Char) -> Iterable): List = flatMapTo(ArrayList(), transform)
+/** Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. */
+inline fun > CharBuffer.flatMapTo(destination: C, transform: (Char) -> Iterable): C {
+ for (element in this) {
+ val list = transform(element)
+ destination.addAll(list)
+ }
+ return destination
+}
+/**
+ * Groups elements of the original array by the key returned by the given [keySelector] function applied to
+ * each element and returns a map where each group key is associated with a list of corresponding elements.
+ *
+ * The returned map preserves the entry iteration order of the keys produced from the original array.
+ */
+inline fun CharBuffer.groupBy(keySelector: (Char) -> K): Map> = groupByTo(LinkedHashMap>(), keySelector)
+/**
+ * Groups values returned by the [valueTransform] function applied to each element of the original array
+ * by the key returned by the given [keySelector] function applied to the element
+ * and returns a map where each group key is associated with a list of corresponding values.
+ *
+ * The returned map preserves the entry iteration order of the keys produced from the original array.
+ */
+inline fun CharBuffer.groupBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map> = groupByTo(LinkedHashMap>(), keySelector, valueTransform)
+/**
+ * Groups elements of the original array by the key returned by the given [keySelector] function applied to each
+ * element and puts to the [destination] map each group key associated with a list of corresponding elements.
+ *
+ * @return The [destination] map.
+ */
+inline fun >> CharBuffer.groupByTo(destination: M, keySelector: (Char) -> K): M {
+ for (element in this) {
+ val key = keySelector(element)
+ val list = destination.getOrPut(key) { ArrayList() }
+ list.add(element)
+ }
+ return destination
+}
+/**
+ * Groups values returned by the [valueTransform] function applied to each element of the original array
+ * by the key returned by the given [keySelector] function applied to the element
+ * and puts to the [destination] map each group key associated with a list of corresponding values.
+ *
+ * @return The [destination] map.
+ */
+inline fun >> CharBuffer.groupByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M {
+ for (element in this) {
+ val key = keySelector(element)
+ val list = destination.getOrPut(key) { ArrayList() }
+ list.add(valueTransform(element))
+ }
+ return destination
+}
+/**
+ * Returns a list containing only distinct elements from the given array.
+ *
+ * The elements in the resulting list are in the same order as they were in the source array.
+ */
+fun CharBuffer.distinct(): List = this.toMutableSet().toList()
+/**
+ * Returns a list containing only elements from the given array having distinct keys returned by the given [selector] function.
+ *
+ * The elements in the resulting list are in the same order as they were in the source array.
+ */
+inline fun CharBuffer.distinctBy(selector: (Char) -> K): List {
+ val set = HashSet()
+ val list = ArrayList()
+ for (e in this) {
+ val key = selector(e)
+ if (set.add(key))
+ list.add(e)
+ }
+ return list
+}
+/**
+ * Returns a new [MutableSet] containing all distinct elements from the given array.
+ *
+ * The returned set preserves the element iteration order of the original array.
+ */
+fun CharBuffer.toMutableSet(): MutableSet = toCollection(LinkedHashSet(mapCapacity(rem)))
+/** Returns a list containing the results of applying the given [transform] function to each element in the original array. */
+inline fun CharBuffer.map(transform: (Char) -> R): List = mapTo(ArrayList(rem), transform)
+/**
+ * Returns a list containing the results of applying the given [transform] function to each element and its index in the original array.
+ * @param [transform] function that takes the index of an element and the element itself and returns the result of the transform applied to the element.
+ */
+inline fun CharBuffer.mapIndexed(transform: (index: Int, Char) -> R): List = mapIndexedTo(ArrayList(rem), transform)
+/**
+ * Applies the given [transform] function to each element and its index in the original array and appends the results to the given [destination].
+ * @param [transform] function that takes the index of an element and the element itself and returns the result of the transform applied to the element.
+ */
+inline fun > CharBuffer.mapIndexedTo(destination: C, transform: (index: Int, Char) -> R): C {
+ var index = 0
+ for (item in this)
+ destination.add(transform(index++, item))
+ return destination
+}
+/** Applies the given [transform] function to each element of the original array and appends the results to the given [destination]. */
+inline fun > CharBuffer.mapTo(destination: C, transform: (Char) -> R): C {
+ for (item in this)
+ destination.add(transform(item))
+ return destination
+}
+/** Returns `true` if all elements match the given [predicate]. */
+inline fun CharBuffer.all(predicate: (Char) -> Boolean): Boolean {
+ for (element in this) if (!predicate(element)) return false
+ return true
+}
+/** Returns `true` if array has at least one element. */
+fun CharBuffer.any(): Boolean = !isEmpty()
+/** Returns `true` if at least one element matches the given [predicate]. */
+inline fun CharBuffer.any(predicate: (Char) -> Boolean): Boolean {
+ for (element in this) if (predicate(element)) return true
+ return false
+}
+/** Returns the number of elements in this array. */
+inline fun CharBuffer.count(): Int = rem
+/** Returns the number of elements matching the given [predicate]. */
+inline fun CharBuffer.count(predicate: (Char) -> Boolean): Int {
+ var count = 0
+ for (element in this) if (predicate(element)) ++count
+ return count
+}
+/** Performs the given [action] on each element. */
+inline fun CharBuffer.forEach(action: (Char) -> Unit) { for (element in this) action(element) }
+/**
+ * Performs the given [action] on each element, providing sequential index with the element.
+ * @param [action] function that takes the index of an element and the element itself and performs the action on the element.
+ */
+inline fun CharBuffer.forEachIndexed(action: (index: Int, Char) -> Unit): Unit {
+ var index = 0
+ for (item in this) action(index++, item)
+}
+/**
+ * Returns the largest element.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+fun CharBuffer.max(): Char {
+ if (isEmpty()) throw NoSuchElementException()
+ var max = this[0]
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ if (max < e) max = e
+ }
+ return max
+}
+/**
+ * Returns the first element yielding the largest value of the given function.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+inline fun > CharBuffer.maxBy(selector: (Char) -> R): Char {
+ if (isEmpty()) throw NoSuchElementException()
+ var maxElem = this[0]
+ val lastIndex = this.lastIndex
+ if (lastIndex == 0) return maxElem
+ var maxValue = selector(maxElem)
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ val v = selector(e)
+ if (maxValue < v) {
+ maxElem = e
+ maxValue = v
+ }
+ }
+ return maxElem
+}
+/** Returns the first element yielding the largest value of the given function or `null` if there are no elements. */
+inline fun > CharBuffer.maxByOrNull(selector: (Char) -> R): Char? {
+ if (isEmpty()) return null
+ var maxElem = this[0]
+ val lastIndex = this.lastIndex
+ if (lastIndex == 0) return maxElem
+ var maxValue = selector(maxElem)
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ val v = selector(e)
+ if (maxValue < v) {
+ maxElem = e
+ maxValue = v
+ }
+ }
+ return maxElem
+}
+/** Returns the largest element or `null` if there are no elements. */
+fun CharBuffer.maxOrNull(): Char? {
+ if (isEmpty()) return null
+ var max = this[0]
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ if (max < e) max = e
+ }
+ return max
+}
+/**
+ * Returns the first element having the largest value according to the provided [comparator].
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+fun CharBuffer.maxWith(comparator: Comparator): Char {
+ if (isEmpty()) throw NoSuchElementException()
+ var max = this[0]
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ if (comparator.compare(max, e) < 0) max = e
+ }
+ return max
+}
+/** Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. */
+fun CharBuffer.maxWithOrNull(comparator: Comparator): Char? {
+ if (isEmpty()) return null
+ var max = this[0]
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ if (comparator.compare(max, e) < 0) max = e
+ }
+ return max
+}
+/**
+ * Returns the smallest element.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+fun CharBuffer.min(): Char {
+ if (isEmpty()) throw NoSuchElementException()
+ var min = this[0]
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ if (min > e) min = e
+ }
+ return min
+}
+/**
+ * Returns the first element yielding the smallest value of the given function.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+inline fun > CharBuffer.minBy(selector: (Char) -> R): Char {
+ if (isEmpty()) throw NoSuchElementException()
+ var minElem = this[0]
+ val lastIndex = this.lastIndex
+ if (lastIndex == 0) return minElem
+ var minValue = selector(minElem)
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ val v = selector(e)
+ if (minValue > v) {
+ minElem = e
+ minValue = v
+ }
+ }
+ return minElem
+}
+/**
+ * Returns the first element yielding the smallest value of the given function or `null` if there are no elements.
+ *
+ * @sample samples.collections.Collections.Aggregates.minByOrNull
+ */
+inline fun > CharBuffer.minByOrNull(selector: (Char) -> R): Char? {
+ if (isEmpty()) return null
+ var minElem = this[0]
+ val lastIndex = this.lastIndex
+ if (lastIndex == 0) return minElem
+ var minValue = selector(minElem)
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ val v = selector(e)
+ if (minValue > v) {
+ minElem = e
+ minValue = v
+ }
+ }
+ return minElem
+}
+/**
+ * Returns the smallest value among all values produced by [selector] function applied to each element in the array.
+ *
+ * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+inline fun CharBuffer.minOf(selector: (Char) -> Double): Double {
+ if (isEmpty()) throw NoSuchElementException()
+ var minValue = selector(this[0])
+ for (i in 1..lastIndex) {
+ val v = selector(this[i])
+ minValue = minOf(minValue, v)
+ }
+ return minValue
+}
+/**
+ * Returns the smallest value among all values produced by [selector] function applied to each element in the array.
+ *
+ * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+inline fun CharBuffer.minOf(selector: (Char) -> Float): Float {
+ if (isEmpty()) throw NoSuchElementException()
+ var minValue = selector(this[0])
+ for (i in 1..lastIndex) {
+ val v = selector(this[i])
+ minValue = minOf(minValue, v)
+ }
+ return minValue
+}
+/**
+ * Returns the smallest value among all values produced by [selector] function applied to each element in the array.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+inline fun > CharBuffer.minOf(selector: (Char) -> R): R {
+ if (isEmpty()) throw NoSuchElementException()
+ var minValue = selector(this[0])
+ for (i in 1..lastIndex) {
+ val v = selector(this[i])
+ if (minValue > v)
+ minValue = v
+ }
+ return minValue
+}
+/**
+ * Returns the smallest value among all values produced by [selector] function applied to each element in the array or `null` if there are no elements.
+ *
+ * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
+ */
+inline fun CharBuffer.minOfOrNull(selector: (Char) -> Double): Double? {
+ if (isEmpty()) return null
+ var minValue = selector(this[0])
+ for (i in 1..lastIndex) {
+ val v = selector(this[i])
+ minValue = minOf(minValue, v)
+ }
+ return minValue
+}
+/**
+ * Returns the smallest value among all values produced by [selector] function applied to each element in the array or `null` if there are no elements.
+ *
+ * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
+ */
+inline fun CharBuffer.minOfOrNull(selector: (Char) -> Float): Float? {
+ if (isEmpty()) return null
+ var minValue = selector(this[0])
+ for (i in 1..lastIndex) {
+ val v = selector(this[i])
+ minValue = minOf(minValue, v)
+ }
+ return minValue
+}
+/** Returns the smallest value among all values produced by [selector] function applied to each element in the array or `null` if there are no elements. */
+inline fun > CharBuffer.minOfOrNull(selector: (Char) -> R): R? {
+ if (isEmpty()) return null
+ var minValue = selector(this[0])
+ for (i in 1..lastIndex) {
+ val v = selector(this[i])
+ if (minValue > v)
+ minValue = v
+ }
+ return minValue
+}
+/**
+ * Returns the smallest value according to the provided [comparator] among all values produced by [selector] function applied to each element in the array.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+inline fun CharBuffer.minOfWith(comparator: Comparator, selector: (Char) -> R): R {
+ if (isEmpty()) throw NoSuchElementException()
+ var minValue = selector(this[0])
+ for (i in 1..lastIndex) {
+ val v = selector(this[i])
+ if (comparator.compare(minValue, v) > 0)
+ minValue = v
+ }
+ return minValue
+}
+/**
+ * Returns the smallest value according to the provided [comparator] among all values produced by [selector]
+ * function applied to each element in the array or `null` if there are no elements.
+ */
+inline fun CharBuffer.minOfWithOrNull(comparator: Comparator, selector: (Char) -> R): R? {
+ if (isEmpty()) return null
+ var minValue = selector(this[0])
+ for (i in 1..lastIndex) {
+ val v = selector(this[i])
+ if (comparator.compare(minValue, v) > 0)
+ minValue = v
+ }
+ return minValue
+}
+/** Returns the smallest element or `null` if there are no elements. */
+fun CharBuffer.minOrNull(): Char? {
+ if (isEmpty()) return null
+ var min = this[0]
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ if (min > e) min = e
+ }
+ return min
+ }
+/**
+ * Returns the first element having the smallest value according to the provided [comparator].
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+fun CharBuffer.minWith(comparator: Comparator): Char {
+ if (isEmpty()) throw NoSuchElementException()
+ var min = this[0]
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ if (comparator.compare(min, e) > 0) min = e
+ }
+ return min
+}
+/** Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. */
+fun CharBuffer.minWithOrNull(comparator: Comparator): Char? {
+ if (isEmpty()) return null
+ var min = this[0]
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ if (comparator.compare(min, e) > 0) min = e
+ }
+ return min
+}
+/** Returns `true` if the array has no elements. */
+fun CharBuffer.none(): Boolean = isEmpty()
+/** Returns `true` if no elements match the given [predicate]. */
+inline fun CharBuffer.none(predicate: (Char) -> Boolean): Boolean {
+ for (element in this) if (predicate(element)) return false
+ return true
+}
+/** Performs the given [action] on each element and returns the array itself afterwards. */
+inline fun CharBuffer.onEach(action: (Char) -> Unit): CharBuffer = apply { for (element in this) action(element) }
+/**
+ * Performs the given [action] on each element, providing sequential index with the element, and returns the array itself afterwards.
+ * @param [action] function that takes the index of an element and the element itself and performs the action on the element.
+ */
+inline fun CharBuffer.onEachIndexed(action: (index: Int, Char) -> Unit): CharBuffer = apply { forEachIndexed(action) }
+/**
+ * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
+ *
+ * Throws an exception if this array is empty. If the array can be empty in an expected way,
+ * please use [reduceOrNull] instead. It returns `null` when its receiver is empty.
+ *
+ * @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
+ */
+inline fun CharBuffer.reduce(operation: (acc: Char, Char) -> Char): Char {
+ if (isEmpty())
+ throw UnsupportedOperationException("Empty array can't be reduced.")
+ var accumulator = this[0]
+ for (index in 1..lastIndex)
+ accumulator = operation(accumulator, this[index])
+ return accumulator
+}
+/**
+ * Accumulates value starting with the first element and applying [operation] from left to right
+ * to current accumulator value and each element with its index in the original array.
+ *
+ * Throws an exception if this array is empty. If the array can be empty in an expected way,
+ * please use [reduceIndexedOrNull] instead. It returns `null` when its receiver is empty.
+ *
+ * @param [operation] function that takes the index of an element, current accumulator value and the element itself,
+ * and calculates the next accumulator value.
+ */
+inline fun CharBuffer.reduceIndexed(operation: (index: Int, acc: Char, Char) -> Char): Char {
+ if (isEmpty())
+ throw UnsupportedOperationException("Empty array can't be reduced.")
+ var accumulator = this[0]
+ for (index in 1..lastIndex)
+ accumulator = operation(index, accumulator, this[index])
+ return accumulator
+}
+/**
+ * Accumulates value starting with the first element and applying [operation] from left to right
+ * to current accumulator value and each element with its index in the original array.
+ *
+ * Returns `null` if the array is empty.
+ *
+ * @param [operation] function that takes the index of an element, current accumulator value and the element itself,
+ * and calculates the next accumulator value.
+ */
+inline fun CharBuffer.reduceIndexedOrNull(operation: (index: Int, acc: Char, Char) -> Char): Char? {
+ if (isEmpty())
+ return null
+ var accumulator = this[0]
+ for (index in 1..lastIndex)
+ accumulator = operation(index, accumulator, this[index])
+ return accumulator
+}
+/**
+ * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
+ *
+ * Returns `null` if the array is empty.
+ *
+ * @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
+ */
+inline fun CharBuffer.reduceOrNull(operation: (acc: Char, Char) -> Char): Char? {
+ if (isEmpty())
+ return null
+ var accumulator = this[0]
+ for (index in 1..lastIndex)
+ accumulator = operation(accumulator, this[index])
+ return accumulator
+}
+/**
+ * Accumulates value starting with the last element and applying [operation] from right to left to each element and current accumulator value.
+ *
+ * Throws an exception if this array is empty. If the array can be empty in an expected way,
+ * please use [reduceRightOrNull] instead. It returns `null` when its receiver is empty.
+ *
+ * @param [operation] function that takes an element and current accumulator value, and calculates the next accumulator value.
+ */
+inline fun CharBuffer.reduceRight(operation: (Char, acc: Char) -> Char): Char {
+ var index = lastIndex
+ if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.")
+ var accumulator = get(index--)
+ while (index >= 0)
+ accumulator = operation(get(index--), accumulator)
+ return accumulator
+}
+/**
+ * Accumulates value starting with the last element and applying [operation] from right to left
+ * to each element with its index in the original array and current accumulator value.
+ *
+ * Throws an exception if this array is empty. If the array can be empty in an expected way,
+ * please use [reduceRightIndexedOrNull] instead. It returns `null` when its receiver is empty.
+ *
+ * @param [operation] function that takes the index of an element, the element itself and current accumulator value,
+ * and calculates the next accumulator value.
+ */
+inline fun CharBuffer.reduceRightIndexed(operation: (index: Int, Char, acc: Char) -> Char): Char {
+ var index = lastIndex
+ if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.")
+ var accumulator = get(index--)
+ while (index >= 0) {
+ accumulator = operation(index, get(index), accumulator)
+ --index
+ }
+ return accumulator
+}
+/**
+ * Accumulates value starting with the last element and applying [operation] from right to left
+ * to each element with its index in the original array and current accumulator value.
+ *
+ * Returns `null` if the array is empty.
+ *
+ * @param [operation] function that takes the index of an element, the element itself and current accumulator value,
+ * and calculates the next accumulator value.
+ */
+inline fun CharBuffer.reduceRightIndexedOrNull(operation: (index: Int, Char, acc: Char) -> Char): Char? {
+ var index = lastIndex
+ if (index < 0) return null
+ var accumulator = get(index--)
+ while (index >= 0) {
+ accumulator = operation(index, get(index), accumulator)
+ --index
+ }
+ return accumulator
+}
+/**
+ * Accumulates value starting with the last element and applying [operation] from right to left to each element and current accumulator value.
+ *
+ * Returns `null` if the array is empty.
+ *
+ * @param [operation] function that takes an element and current accumulator value, and calculates the next accumulator value.
+ */
+inline fun CharBuffer.reduceRightOrNull(operation: (Char, acc: Char) -> Char): Char? {
+ var index = lastIndex
+ if (index < 0) return null
+ var accumulator = get(index--)
+ while (index >= 0)
+ accumulator = operation(get(index--), accumulator)
+ return accumulator
+}
+/**
+ * Splits the original array into pair of lists, where *first* list contains elements for which [predicate]
+ * yielded `true`, while *second* list contains elements for which [predicate] yielded `false`.
+ */
+inline fun CharBuffer.partition(predicate: (Char) -> Boolean): Pair, List> {
+ val first = ArrayList()
+ val second = ArrayList()
+ for (element in this)
+ if (predicate(element))
+ first.add(element)
+ else
+ second.add(element)
+ return Pair(first, second)
+}
+/**
+ * Returns a list of pairs built from the elements of `this` array and the [other] array with the same index.
+ * The returned list has length of the shortest collection.
+ */
+infix fun CharBuffer.zip(other: Array): List> = zip(other) { t1, t2 -> t1 to t2 }
+/**
+ * Returns a list of values built from the elements of `this` array and the [other] array with the same index
+ * using the provided [transform] function applied to each pair of elements.
+ * The returned list has length of the shortest collection.
+ */
+inline fun CharBuffer.zip(other: Array, transform: (a: Char, b: R) -> V): List {
+ val size = minOf(rem, other.size)
+ val list = ArrayList(size)
+ for (i in 0 until size)
+ list.add(transform(this[i], other[i]))
+ return list
+}
+/**
+ * Returns a list of pairs built from the elements of `this` collection and [other] array with the same index.
+ * The returned list has length of the shortest collection.
+ */
+infix fun CharBuffer.zip(other: Iterable): List> = zip(other) { t1, t2 -> t1 to t2 }
+/**
+ * Returns a list of values built from the elements of `this` array and the [other] collection with the same index
+ * using the provided [transform] function applied to each pair of elements.
+ * The returned list has length of the shortest collection.
+ */
+inline fun CharBuffer.zip(other: Iterable, transform: (a: Char, b: R) -> V): List {
+ val arraySize = rem
+ val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize))
+ var i = 0
+ for (element in other) {
+ if (i >= arraySize) break
+ list.add(transform(this[i++], element))
+ }
+ return list
+}
+/**
+ * Returns a list of pairs built from the elements of `this` array and the [other] array with the same index.
+ * The returned list has length of the shortest collection.
+ */
+infix fun CharBuffer.zip(other: CharArray): List> = zip(other) { t1, t2 -> t1 to t2 }
+/**
+ * Returns a list of values built from the elements of `this` array and the [other] array with the same index
+ * using the provided [transform] function applied to each pair of elements.
+ * The returned list has length of the shortest array.
+ */
+inline fun CharBuffer.zip(other: CharArray, transform: (a: Char, b: Char) -> V): List {
+ val size = minOf(rem, other.size)
+ val list = ArrayList(size)
+ for (i in 0 until size)
+ list.add(transform(this[i], other[i]))
+ return list
+}
+/**
+ * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
+ *
+ * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit]
+ * elements will be appended, followed by the [truncated] string (which defaults to "...").
+ */
+fun CharBuffer.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Char) -> CharSequence)? = null): A {
+ buffer.append(prefix)
+ var count = 0
+ for (element in this) {
+ if (++count > 1) buffer.append(separator)
+ if (limit < 0 || count <= limit)
+ if (transform != null)
+ buffer.append(transform(element))
+ else
+ buffer.append(element.toString())
+ else break
+ }
+ if (limit >= 0 && count > limit) buffer.append(truncated)
+ buffer.append(postfix)
+ return buffer
+}
+/**
+ * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
+ *
+ * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit]
+ * elements will be appended, followed by the [truncated] string (which defaults to "...").
+ */
+fun CharBuffer.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Char) -> CharSequence)? = null): String =
+ joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString()
+/**
+ * Returns 1st *element* from the buffer.
+ *
+ * If the size of this array is less than 1, throws an [IndexOutOfBoundsException]
+ */
+inline operator fun ByteBuffer.component1(): Byte = get(0)
+/**
+ * Returns 2nd *element* from the buffer.
+ *
+ * If the size of this array is less than 2, throws an [IndexOutOfBoundsException]
+ */
+inline operator fun ByteBuffer.component2(): Byte = get(1)
+/**
+ * Returns 3rd *element* from the buffer.
+ *
+ * If the size of this array is less than 3, throws an [IndexOutOfBoundsException]
+ */
+inline operator fun ByteBuffer.component3(): Byte = get(2)
+/**
+ * Returns 4th *element* from the buffer.
+ *
+ * If the size of this array is less than 4, throws an [IndexOutOfBoundsException]
+ */
+inline operator fun ByteBuffer.component4(): Byte = get(3)
+/**
+ * Returns 5th *element* from the buffer.
+ *
+ * If the size of this array is less than 5, throws an [IndexOutOfBoundsException]
+ */
+inline operator fun ByteBuffer.component5(): Byte = get(4)
+/** Returns `true` if [element] is found in the array. */
+operator fun ByteBuffer.contains(element: Byte): Boolean = indexOf(element) >= 0
+/** Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. */
+inline fun ByteBuffer.elementAt(index: Int): Byte = get(index)
+/** Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */
+inline fun ByteBuffer.elementAtOrElse(index: Int, defaultValue: (Int) -> Byte): Byte = if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
+/** Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */
+inline fun ByteBuffer.elementAtOrNull(index: Int): Byte? = this.getOrNull(index)
+/** Returns the first element matching the given [predicate], or `null` if no such element was found. */
+inline fun ByteBuffer.find(predicate: (Byte) -> Boolean): Byte? = firstOrNull(predicate)
+/** Returns the last element matching the given [predicate], or `null` if no such element was found. */
+inline fun ByteBuffer.findLast(predicate: (Byte) -> Boolean): Byte? = lastOrNull(predicate)
+/**
+ * Returns the first element.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+fun ByteBuffer.first(): Byte {
+ if (isEmpty())
+ throw NoSuchElementException("Array is empty.")
+ return this[0]
+}
+/**
+ * Returns the first element matching the given [predicate].
+ * @throws [NoSuchElementException] if no such element is found.
+ */
+inline fun ByteBuffer.first(predicate: (Byte) -> Boolean): Byte {
+ for (element in this) if (predicate(element)) return element
+ throw NoSuchElementException("Array contains no element matching the predicate.")
+}
+/** Returns the first element, or `null` if the array is empty. */
+fun ByteBuffer.firstOrNull(): Byte? = if (isEmpty()) null else this[0]
+/** Returns the first element matching the given [predicate], or `null` if element was not found. */
+inline fun ByteBuffer.firstOrNull(predicate: (Byte) -> Boolean): Byte? {
+ for (element in this) if (predicate(element)) return element
+ return null
+}
+/** Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */
+inline fun ByteBuffer.getOrElse(index: Int, defaultValue: (Int) -> Byte): Byte = if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
+/** Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */
+fun ByteBuffer.getOrNull(index: Int): Byte? = if (index >= 0 && index <= lastIndex) get(index) else null
+/** Returns first index of [element], or -1 if the array does not contain element. */
+fun ByteBuffer.indexOf(element: Byte): Int {
+ for (index in indices)
+ if (element == this[index])
+ return index
+ return -1
+}
+/** Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. */
+inline fun ByteBuffer.indexOfFirst(predicate: (Byte) -> Boolean): Int {
+ for (index in indices)
+ if (predicate(this[index]))
+ return index
+ return -1
+}
+/** Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. */
+inline fun ByteBuffer.indexOfLast(predicate: (Byte) -> Boolean): Int {
+ for (index in indices.reversed())
+ if (predicate(this[index]))
+ return index
+ return -1
+}
+/**
+ * Returns the last element.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+fun ByteBuffer.last(): Byte {
+ if (isEmpty())
+ throw NoSuchElementException("Array is empty.")
+ return this[lastIndex]
+}
+/**
+ * Returns the last element matching the given [predicate].
+ *
+ * @throws NoSuchElementException if no such element is found.
+ */
+inline fun ByteBuffer.last(predicate: (Byte) -> Boolean): Byte {
+ for (index in this.indices.reversed()) {
+ val element = this[index]
+ if (predicate(element)) return element
+ }
+ throw NoSuchElementException("Array contains no element matching the predicate.")
+}
+/** Returns last index of [element], or -1 if the array does not contain element. */
+fun ByteBuffer.lastIndexOf(element: Byte): Int {
+ for (index in indices.reversed())
+ if (element == this[index])
+ return index
+ return -1
+}
+/** Returns the last element, or `null` if the array is empty. */
+fun ByteBuffer.lastOrNull(): Byte? = if (isEmpty()) null else this[rem - 1]
+/** Returns the last element matching the given [predicate], or `null` if no such element was found. */
+inline fun ByteBuffer.lastOrNull(predicate: (Byte) -> Boolean): Byte? {
+ for (index in this.indices.reversed()) {
+ val element = this[index]
+ if (predicate(element)) return element
+ }
+ return null
+}
+/** Returns the single element, or throws an exception if the array is empty or has more than one element. */
+fun ByteBuffer.single(): Byte = when (rem) {
+ 0 -> throw NoSuchElementException("Array is empty.")
+ 1 -> this[0]
+ else -> throw IllegalArgumentException("Array has more than one element.")
+}
+/** Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. */
+inline fun ByteBuffer.single(predicate: (Byte) -> Boolean): Byte {
+ var single: Byte? = null
+ var found = false
+ for (element in this)
+ if (predicate(element)) {
+ if (found) throw IllegalArgumentException("Array contains more than one matching element.")
+ single = element
+ found = true
+ }
+ if (!found) throw NoSuchElementException("Array contains no element matching the predicate.")
+ @Suppress("UNCHECKED_CAST")
+ return single as Byte
+}
+/** Returns single element, or `null` if the array is empty or has more than one element. */
+fun ByteBuffer.singleOrNull(): Byte? = if (rem == 1) this[0] else null
+/** Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. */
+inline fun ByteBuffer.singleOrNull(predicate: (Byte) -> Boolean): Byte? {
+ var single: Byte? = null
+ var found = false
+ for (element in this)
+ if (predicate(element)) {
+ if (found) return null
+ single = element
+ found = true
+ }
+ if (!found) return null
+ return single
+}
+/** Returns a list containing only elements matching the given [predicate]. */
+inline fun ByteBuffer.filter(predicate: (Byte) -> Boolean): List = filterTo(ArrayList(), predicate)
+/**
+ * Returns a list containing only elements matching the given [predicate].
+ * @param [predicate] function that takes the index of an element and the element itself and returns the result of predicate evaluation on the element.
+ */
+inline fun ByteBuffer.filterIndexed(predicate: (index: Int, Byte) -> Boolean): List = filterIndexedTo(ArrayList(), predicate)
+/**
+ * Appends all elements matching the given [predicate] to the given [destination].
+ * @param [predicate] function that takes the index of an element and the element itself and returns the result of predicate evaluation on the element.
+ */
+inline fun > ByteBuffer.filterIndexedTo(destination: C, predicate: (index: Int, Byte) -> Boolean): C {
+ forEachIndexed { index, element ->
+ if (predicate(index, element)) destination.add(element)
+ }
+ return destination
+}
+/** Returns a list containing all elements not matching the given [predicate]. */
+inline fun ByteBuffer.filterNot(predicate: (Byte) -> Boolean): List = filterNotTo(ArrayList(), predicate)
+/** Appends all elements not matching the given [predicate] to the given [destination]. */
+inline fun > ByteBuffer.filterNotTo(destination: C, predicate: (Byte) -> Boolean): C {
+ for (element in this) if (!predicate(element)) destination.add(element)
+ return destination
+}
+/** Appends all elements matching the given [predicate] to the given [destination]. */
+inline fun > ByteBuffer.filterTo(destination: C, predicate: (Byte) -> Boolean): C {
+ for (element in this) if (predicate(element)) destination.add(element)
+ return destination
+}
+/** Returns a list containing elements at specified [indices]. */
+fun ByteBuffer.slice(indices: Iterable): List {
+ val size = indices.collectionSizeOrDefault(10)
+ if (size == 0) return emptyList()
+ val list = ArrayList(size)
+ for (index in indices)
+ list.add(get(index))
+ return list
+}
+/** Returns an array containing elements of this array at specified [indices]. */
+fun ByteBuffer.sliceArray(indices: Collection): ByteArray {
+ val result = ByteArray(indices.size)
+ var targetIndex = 0
+ for (sourceIndex in indices)
+ result[targetIndex++] = this[sourceIndex]
+ return result
+}
+/** Sorts elements in the array in-place descending according to their natural sort order. */
+fun ByteBuffer.sortDescending() {
+ if (rem > 1) {
+ val array = toByteArray().apply{ sortDescending() }
+ for (i in 0 until rem)
+ this[pos + i] = array[i]
+ }
+}
+/** Returns a list of all elements sorted according to their natural sort order. */
+fun ByteBuffer.sorted(): List = toTypedArray().apply { sort() }.asList()
+/** Returns an array with all elements of this array sorted according to their natural sort order. */
+fun ByteBuffer.sortedArray(): ByteArray = toByteArray().apply { sort() }
+/** Returns an array with all elements of this array sorted descending according to their natural sort order. */
+fun ByteBuffer.sortedArrayDescending(): ByteArray = toByteArray().apply { sortDescending() }
+/** Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. */
+inline fun > ByteBuffer.sortedBy(crossinline selector: (Byte) -> R?): List = toByteArray().sortedWith(compareBy(selector))
+/** Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. */
+inline fun > ByteBuffer.sortedByDescending(crossinline selector: (Byte) -> R?): List = toByteArray().sortedWith(compareByDescending(selector))
+/** Returns a list of all elements sorted descending according to their natural sort order. */
+fun ByteBuffer.sortedDescending(): List = toByteArray().apply { sort() }.reversed()
+/** Returns a list of all elements sorted according to the specified [comparator]. */
+fun ByteBuffer.sortedWith(comparator: Comparator): List = toTypedArray().apply { sortWith(comparator) }.asList()
+/** Sorts the array in-place. */
+fun ByteBuffer.sort() {
+ if (rem > 1) {
+ val array = toByteArray().apply{ sort() }
+ for (i in 0 until rem)
+ this[pos + i] = array[i]
+ }
+}
+/** Returns a string representation of the contents of the specified array as if it is [List]. */
+fun ByteBuffer.contentToString(): String {
+ val iMax = lim - 1
+ if (iMax == -1) return "[]"
+ val b = StringBuilder()
+ b.append('[')
+ var i = 0
+ while (true) {
+ b.append(get(i))
+ if (i == iMax) return b.append(']').toString()
+ b.append(", ")
+ i++
+ }
+}
+/**
+ * Fills this array or its subrange with the specified [element] value.
+ *
+ * @param fromIndex the start of the range (inclusive) to fill, 0 by default.
+ * @param toIndex the end of the range (exclusive) to fill, size of this array by default.
+ *
+ * @throws IndexOutOfBoundsException if [fromIndex] is less than zero or [toIndex] is greater than the size of this array.
+ * @throws IllegalArgumentException if [fromIndex] is greater than [toIndex].
+ */
+fun ByteBuffer.fill(element: Byte, fromIndex: Int = pos, toIndex: Int = rem) {
+ for (i in fromIndex until toIndex)
+ this[i] = element
+}
+/** Returns the range of valid indices for the array. */
+val ByteBuffer.indices: IntRange
+ get() = IntRange(0, lastIndex)
+/** Returns `true` if the array is empty. */
+inline fun ByteBuffer.isEmpty(): Boolean = rem == 0
+/** Returns `true` if the array is not empty. */
+inline fun ByteBuffer.isNotEmpty(): Boolean = !isEmpty()
+/** Returns the last valid index for the array. */
+val ByteBuffer.lastIndex: Int
+ get() = rem - 1
+/** Returns an array of Byte containing all of the elements of this generic array. */
+fun ByteBuffer.toByteArray(): ByteArray = ByteArray(rem) { index -> this[index] }
+/** Returns a *typed* object array containing all of the elements of this primitive array. */
+fun ByteBuffer.toTypedArray(): Array = Array(rem) { index -> this[index] }
+/**
+ * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
+ *
+ * If any of two pairs would have the same key the last one gets added to the map.
+ *
+ * The returned map preserves the entry iteration order of the original array.
+ */
+inline fun ByteBuffer.associate(transform: (Byte) -> Pair): Map {
+ val capacity = mapCapacity(rem).coerceAtLeast(16)
+ return associateTo(LinkedHashMap(capacity), transform)
+}
+/**
+ * Returns a [Map] containing the elements from the given array indexed by the key returned from [keySelector] function applied to each element.
+ *
+ * If any two elements would have the same key returned by [keySelector] the last one gets added to the map.
+ *
+ * The returned map preserves the entry iteration order of the original array.
+ */
+inline fun ByteBuffer.associateBy(keySelector: (Byte) -> K): Map {
+ val capacity = mapCapacity(rem).coerceAtLeast(16)
+ return associateByTo(LinkedHashMap(capacity), keySelector)
+}
+/**
+ * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array.
+ *
+ * If any two elements would have the same key returned by [keySelector] the last one gets added to the map.
+ *
+ * The returned map preserves the entry iteration order of the original array.
+ */
+inline fun ByteBuffer.associateBy(keySelector: (Byte) -> K, valueTransform: (Byte) -> V): Map {
+ val capacity = mapCapacity(rem).coerceAtLeast(16)
+ return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform)
+}
+/**
+ * Populates and returns the [destination] mutable map with key-value pairs, where key is provided by
+ * the [keySelector] function applied to each element of the given array and value is the element itself.
+ *
+ * If any two elements would have the same key returned by [keySelector] the last one gets added to the map.
+ */
+inline fun > ByteBuffer.associateByTo(destination: M, keySelector: (Byte) -> K): M {
+ for (element in this)
+ destination.put(keySelector(element), element)
+ return destination
+}
+/**
+ * Populates and returns the [destination] mutable map with key-value pairs, where key is provided by the [keySelector]
+ * function and and value is provided by the [valueTransform] function applied to elements of the given array.
+ *
+ * If any two elements would have the same key returned by [keySelector] the last one gets added to the map.
+ */
+inline fun > ByteBuffer.associateByTo(destination: M, keySelector: (Byte) -> K, valueTransform: (Byte) -> V): M {
+ for (element in this)
+ destination.put(keySelector(element), valueTransform(element))
+ return destination
+}
+/**
+ * Populates and returns the [destination] mutable map with key-value pairs provided by [transform] function
+ * applied to each element of the given array.
+ *
+ * If any of two pairs would have the same key the last one gets added to the map.
+ */
+inline fun > ByteBuffer.associateTo(destination: M, transform: (Byte) -> Pair): M {
+ for (element in this)
+ destination += transform(element)
+ return destination
+}
+/**
+ * Returns a [Map] where keys are elements from the given array and values are produced by the [valueSelector] function applied to each element.
+ *
+ * If any two elements are equal, the last one gets added to the map.
+ *
+ * The returned map preserves the entry iteration order of the original array.
+ */
+inline fun ByteBuffer.associateWith(valueSelector: (Byte) -> V): Map {
+ val result = LinkedHashMap(mapCapacity(rem).coerceAtLeast(16))
+ return associateWithTo(result, valueSelector)
+}
+/**
+ * Populates and returns the [destination] mutable map with key-value pairs for each element of the given array,
+ * where key is the element itself and value is provided by the [valueSelector] function applied to that key.
+ *
+ * If any two elements are equal, the last one overwrites the former value in the map.
+ */
+inline fun > ByteBuffer.associateWithTo(destination: M, valueSelector: (Byte) -> V): M {
+ for (element in this)
+ destination.put(element, valueSelector(element))
+ return destination
+}
+/** Appends all elements to the given [destination] collection. */
+fun > ByteBuffer.toCollection(destination: C): C {
+ for (item in this)
+ destination.add(item)
+ return destination
+}
+/** Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. */
+inline fun ByteBuffer.flatMap(transform: (Byte) -> Iterable): List = flatMapTo(ArrayList(), transform)
+/** Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. */
+inline fun > ByteBuffer.flatMapTo(destination: C, transform: (Byte) -> Iterable): C {
+ for (element in this) {
+ val list = transform(element)
+ destination.addAll(list)
+ }
+ return destination
+}
+/**
+ * Groups elements of the original array by the key returned by the given [keySelector] function applied to
+ * each element and returns a map where each group key is associated with a list of corresponding elements.
+ *
+ * The returned map preserves the entry iteration order of the keys produced from the original array.
+ */
+inline fun ByteBuffer.groupBy(keySelector: (Byte) -> K): Map> = groupByTo(LinkedHashMap>(), keySelector)
+/**
+ * Groups values returned by the [valueTransform] function applied to each element of the original array
+ * by the key returned by the given [keySelector] function applied to the element
+ * and returns a map where each group key is associated with a list of corresponding values.
+ *
+ * The returned map preserves the entry iteration order of the keys produced from the original array.
+ */
+inline fun ByteBuffer.groupBy(keySelector: (Byte) -> K, valueTransform: (Byte) -> V): Map> = groupByTo(LinkedHashMap>(), keySelector, valueTransform)
+/**
+ * Groups elements of the original array by the key returned by the given [keySelector] function applied to each
+ * element and puts to the [destination] map each group key associated with a list of corresponding elements.
+ *
+ * @return The [destination] map.
+ */
+inline fun >> ByteBuffer.groupByTo(destination: M, keySelector: (Byte) -> K): M {
+ for (element in this) {
+ val key = keySelector(element)
+ val list = destination.getOrPut(key) { ArrayList() }
+ list.add(element)
+ }
+ return destination
+}
+/**
+ * Groups values returned by the [valueTransform] function applied to each element of the original array
+ * by the key returned by the given [keySelector] function applied to the element
+ * and puts to the [destination] map each group key associated with a list of corresponding values.
+ *
+ * @return The [destination] map.
+ */
+inline fun >> ByteBuffer.groupByTo(destination: M, keySelector: (Byte) -> K, valueTransform: (Byte) -> V): M {
+ for (element in this) {
+ val key = keySelector(element)
+ val list = destination.getOrPut(key) { ArrayList() }
+ list.add(valueTransform(element))
+ }
+ return destination
+}
+/**
+ * Returns a list containing only distinct elements from the given array.
+ *
+ * The elements in the resulting list are in the same order as they were in the source array.
+ */
+fun ByteBuffer.distinct(): List = this.toMutableSet().toList()
+/**
+ * Returns a list containing only elements from the given array having distinct keys returned by the given [selector] function.
+ *
+ * The elements in the resulting list are in the same order as they were in the source array.
+ */
+inline fun ByteBuffer.distinctBy(selector: (Byte) -> K): List {
+ val set = HashSet()
+ val list = ArrayList()
+ for (e in this) {
+ val key = selector(e)
+ if (set.add(key))
+ list.add(e)
+ }
+ return list
+}
+/**
+ * Returns a new [MutableSet] containing all distinct elements from the given array.
+ *
+ * The returned set preserves the element iteration order of the original array.
+ */
+fun ByteBuffer.toMutableSet(): MutableSet = toCollection(LinkedHashSet(mapCapacity(rem)))
+/** Returns a list containing the results of applying the given [transform] function to each element in the original array. */
+inline fun ByteBuffer.map(transform: (Byte) -> R): List = mapTo(ArrayList(rem), transform)
+/**
+ * Returns a list containing the results of applying the given [transform] function to each element and its index in the original array.
+ * @param [transform] function that takes the index of an element and the element itself and returns the result of the transform applied to the element.
+ */
+inline fun ByteBuffer.mapIndexed(transform: (index: Int, Byte) -> R): List = mapIndexedTo(ArrayList(rem), transform)
+/**
+ * Applies the given [transform] function to each element and its index in the original array and appends the results to the given [destination].
+ * @param [transform] function that takes the index of an element and the element itself and returns the result of the transform applied to the element.
+ */
+inline fun > ByteBuffer.mapIndexedTo(destination: C, transform: (index: Int, Byte) -> R): C {
+ var index = 0
+ for (item in this)
+ destination.add(transform(index++, item))
+ return destination
+}
+/** Applies the given [transform] function to each element of the original array and appends the results to the given [destination]. */
+inline fun > ByteBuffer.mapTo(destination: C, transform: (Byte) -> R): C {
+ for (item in this)
+ destination.add(transform(item))
+ return destination
+}
+/** Returns `true` if all elements match the given [predicate]. */
+inline fun ByteBuffer.all(predicate: (Byte) -> Boolean): Boolean {
+ for (element in this) if (!predicate(element)) return false
+ return true
+}
+/** Returns `true` if array has at least one element. */
+fun ByteBuffer.any(): Boolean = !isEmpty()
+/** Returns `true` if at least one element matches the given [predicate]. */
+inline fun ByteBuffer.any(predicate: (Byte) -> Boolean): Boolean {
+ for (element in this) if (predicate(element)) return true
+ return false
+}
+/** Returns the number of elements in this array. */
+inline fun ByteBuffer.count(): Int = rem
+/** Returns the number of elements matching the given [predicate]. */
+inline fun ByteBuffer.count(predicate: (Byte) -> Boolean): Int {
+ var count = 0
+ for (element in this) if (predicate(element)) ++count
+ return count
+}
+/** Performs the given [action] on each element. */
+inline fun ByteBuffer.forEach(action: (Byte) -> Unit) { for (element in this) action(element) }
+/**
+ * Performs the given [action] on each element, providing sequential index with the element.
+ * @param [action] function that takes the index of an element and the element itself and performs the action on the element.
+ */
+inline fun ByteBuffer.forEachIndexed(action: (index: Int, Byte) -> Unit): Unit {
+ var index = 0
+ for (item in this) action(index++, item)
+}
+/**
+ * Returns the largest element.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+fun ByteBuffer.max(): Byte {
+ if (isEmpty()) throw NoSuchElementException()
+ var max = this[0]
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ if (max < e) max = e
+ }
+ return max
+}
+/**
+ * Returns the first element yielding the largest value of the given function.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+inline fun > ByteBuffer.maxBy(selector: (Byte) -> R): Byte {
+ if (isEmpty()) throw NoSuchElementException()
+ var maxElem = this[0]
+ val lastIndex = this.lastIndex
+ if (lastIndex == 0) return maxElem
+ var maxValue = selector(maxElem)
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ val v = selector(e)
+ if (maxValue < v) {
+ maxElem = e
+ maxValue = v
+ }
+ }
+ return maxElem
+}
+/** Returns the first element yielding the largest value of the given function or `null` if there are no elements. */
+inline fun > ByteBuffer.maxByOrNull(selector: (Byte) -> R): Byte? {
+ if (isEmpty()) return null
+ var maxElem = this[0]
+ val lastIndex = this.lastIndex
+ if (lastIndex == 0) return maxElem
+ var maxValue = selector(maxElem)
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ val v = selector(e)
+ if (maxValue < v) {
+ maxElem = e
+ maxValue = v
+ }
+ }
+ return maxElem
+}
+/** Returns the largest element or `null` if there are no elements. */
+fun ByteBuffer.maxOrNull(): Byte? {
+ if (isEmpty()) return null
+ var max = this[0]
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ if (max < e) max = e
+ }
+ return max
+}
+/**
+ * Returns the first element having the largest value according to the provided [comparator].
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+fun ByteBuffer.maxWith(comparator: Comparator): Byte {
+ if (isEmpty()) throw NoSuchElementException()
+ var max = this[0]
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ if (comparator.compare(max, e) < 0) max = e
+ }
+ return max
+}
+/** Returns the first element having the largest value according to the provided [comparator] or `null` if there are no elements. */
+fun ByteBuffer.maxWithOrNull(comparator: Comparator): Byte? {
+ if (isEmpty()) return null
+ var max = this[0]
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ if (comparator.compare(max, e) < 0) max = e
+ }
+ return max
+}
+/**
+ * Returns the smallest element.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+fun ByteBuffer.min(): Byte {
+ if (isEmpty()) throw NoSuchElementException()
+ var min = this[0]
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ if (min > e) min = e
+ }
+ return min
+}
+/**
+ * Returns the first element yielding the smallest value of the given function.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+inline fun > ByteBuffer.minBy(selector: (Byte) -> R): Byte {
+ if (isEmpty()) throw NoSuchElementException()
+ var minElem = this[0]
+ val lastIndex = this.lastIndex
+ if (lastIndex == 0) return minElem
+ var minValue = selector(minElem)
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ val v = selector(e)
+ if (minValue > v) {
+ minElem = e
+ minValue = v
+ }
+ }
+ return minElem
+}
+/**
+ * Returns the first element yielding the smallest value of the given function or `null` if there are no elements.
+ *
+ * @sample samples.collections.Collections.Aggregates.minByOrNull
+ */
+inline fun > ByteBuffer.minByOrNull(selector: (Byte) -> R): Byte? {
+ if (isEmpty()) return null
+ var minElem = this[0]
+ val lastIndex = this.lastIndex
+ if (lastIndex == 0) return minElem
+ var minValue = selector(minElem)
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ val v = selector(e)
+ if (minValue > v) {
+ minElem = e
+ minValue = v
+ }
+ }
+ return minElem
+}
+/**
+ * Returns the smallest value among all values produced by [selector] function applied to each element in the array.
+ *
+ * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+inline fun ByteBuffer.minOf(selector: (Byte) -> Double): Double {
+ if (isEmpty()) throw NoSuchElementException()
+ var minValue = selector(this[0])
+ for (i in 1..lastIndex) {
+ val v = selector(this[i])
+ minValue = minOf(minValue, v)
+ }
+ return minValue
+}
+/**
+ * Returns the smallest value among all values produced by [selector] function applied to each element in the array.
+ *
+ * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+inline fun ByteBuffer.minOf(selector: (Byte) -> Float): Float {
+ if (isEmpty()) throw NoSuchElementException()
+ var minValue = selector(this[0])
+ for (i in 1..lastIndex) {
+ val v = selector(this[i])
+ minValue = minOf(minValue, v)
+ }
+ return minValue
+}
+/**
+ * Returns the smallest value among all values produced by [selector] function applied to each element in the array.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+inline fun > ByteBuffer.minOf(selector: (Byte) -> R): R {
+ if (isEmpty()) throw NoSuchElementException()
+ var minValue = selector(this[0])
+ for (i in 1..lastIndex) {
+ val v = selector(this[i])
+ if (minValue > v)
+ minValue = v
+ }
+ return minValue
+}
+/**
+ * Returns the smallest value among all values produced by [selector] function applied to each element in the array or `null` if there are no elements.
+ *
+ * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
+ */
+inline fun ByteBuffer.minOfOrNull(selector: (Byte) -> Double): Double? {
+ if (isEmpty()) return null
+ var minValue = selector(this[0])
+ for (i in 1..lastIndex) {
+ val v = selector(this[i])
+ minValue = minOf(minValue, v)
+ }
+ return minValue
+}
+/**
+ * Returns the smallest value among all values produced by [selector] function applied to each element in the array or `null` if there are no elements.
+ *
+ * If any of values produced by [selector] function is `NaN`, the returned result is `NaN`.
+ */
+inline fun ByteBuffer.minOfOrNull(selector: (Byte) -> Float): Float? {
+ if (isEmpty()) return null
+ var minValue = selector(this[0])
+ for (i in 1..lastIndex) {
+ val v = selector(this[i])
+ minValue = minOf(minValue, v)
+ }
+ return minValue
+}
+/** Returns the smallest value among all values produced by [selector] function applied to each element in the array or `null` if there are no elements. */
+inline fun > ByteBuffer.minOfOrNull(selector: (Byte) -> R): R? {
+ if (isEmpty()) return null
+ var minValue = selector(this[0])
+ for (i in 1..lastIndex) {
+ val v = selector(this[i])
+ if (minValue > v)
+ minValue = v
+ }
+ return minValue
+}
+/**
+ * Returns the smallest value according to the provided [comparator] among all values produced by [selector] function applied to each element in the array.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+inline fun ByteBuffer.minOfWith(comparator: Comparator, selector: (Byte) -> R): R {
+ if (isEmpty()) throw NoSuchElementException()
+ var minValue = selector(this[0])
+ for (i in 1..lastIndex) {
+ val v = selector(this[i])
+ if (comparator.compare(minValue, v) > 0)
+ minValue = v
+ }
+ return minValue
+}
+/**
+ * Returns the smallest value according to the provided [comparator] among all values produced by [selector]
+ * function applied to each element in the array or `null` if there are no elements.
+ */
+inline fun ByteBuffer.minOfWithOrNull(comparator: Comparator, selector: (Byte) -> R): R? {
+ if (isEmpty()) return null
+ var minValue = selector(this[0])
+ for (i in 1..lastIndex) {
+ val v = selector(this[i])
+ if (comparator.compare(minValue, v) > 0)
+ minValue = v
+ }
+ return minValue
+}
+/** Returns the smallest element or `null` if there are no elements. */
+fun ByteBuffer.minOrNull(): Byte? {
+ if (isEmpty()) return null
+ var min = this[0]
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ if (min > e) min = e
+ }
+ return min
+ }
+/**
+ * Returns the first element having the smallest value according to the provided [comparator].
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+fun ByteBuffer.minWith(comparator: Comparator): Byte {
+ if (isEmpty()) throw NoSuchElementException()
+ var min = this[0]
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ if (comparator.compare(min, e) > 0) min = e
+ }
+ return min
+}
+/** Returns the first element having the smallest value according to the provided [comparator] or `null` if there are no elements. */
+fun ByteBuffer.minWithOrNull(comparator: Comparator): Byte? {
+ if (isEmpty()) return null
+ var min = this[0]
+ for (i in 1..lastIndex) {
+ val e = this[i]
+ if (comparator.compare(min, e) > 0) min = e
+ }
+ return min
+}
+/** Returns `true` if the array has no elements. */
+fun ByteBuffer.none(): Boolean = isEmpty()
+/** Returns `true` if no elements match the given [predicate]. */
+inline fun ByteBuffer.none(predicate: (Byte) -> Boolean): Boolean {
+ for (element in this) if (predicate(element)) return false
+ return true
+}
+/** Performs the given [action] on each element and returns the array itself afterwards. */
+inline fun ByteBuffer.onEach(action: (Byte) -> Unit): ByteBuffer = apply { for (element in this) action(element) }
+/**
+ * Performs the given [action] on each element, providing sequential index with the element, and returns the array itself afterwards.
+ * @param [action] function that takes the index of an element and the element itself and performs the action on the element.
+ */
+inline fun ByteBuffer.onEachIndexed(action: (index: Int, Byte) -> Unit): ByteBuffer = apply { forEachIndexed(action) }
+/**
+ * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
+ *
+ * Throws an exception if this array is empty. If the array can be empty in an expected way,
+ * please use [reduceOrNull] instead. It returns `null` when its receiver is empty.
+ *
+ * @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
+ */
+inline fun ByteBuffer.reduce(operation: (acc: Byte, Byte) -> Byte): Byte {
+ if (isEmpty())
+ throw UnsupportedOperationException("Empty array can't be reduced.")
+ var accumulator = this[0]
+ for (index in 1..lastIndex)
+ accumulator = operation(accumulator, this[index])
+ return accumulator
+}
+/**
+ * Accumulates value starting with the first element and applying [operation] from left to right
+ * to current accumulator value and each element with its index in the original array.
+ *
+ * Throws an exception if this array is empty. If the array can be empty in an expected way,
+ * please use [reduceIndexedOrNull] instead. It returns `null` when its receiver is empty.
+ *
+ * @param [operation] function that takes the index of an element, current accumulator value and the element itself,
+ * and calculates the next accumulator value.
+ */
+inline fun ByteBuffer.reduceIndexed(operation: (index: Int, acc: Byte, Byte) -> Byte): Byte {
+ if (isEmpty())
+ throw UnsupportedOperationException("Empty array can't be reduced.")
+ var accumulator = this[0]
+ for (index in 1..lastIndex)
+ accumulator = operation(index, accumulator, this[index])
+ return accumulator
+}
+/**
+ * Accumulates value starting with the first element and applying [operation] from left to right
+ * to current accumulator value and each element with its index in the original array.
+ *
+ * Returns `null` if the array is empty.
+ *
+ * @param [operation] function that takes the index of an element, current accumulator value and the element itself,
+ * and calculates the next accumulator value.
+ */
+inline fun ByteBuffer.reduceIndexedOrNull(operation: (index: Int, acc: Byte, Byte) -> Byte): Byte? {
+ if (isEmpty())
+ return null
+ var accumulator = this[0]
+ for (index in 1..lastIndex)
+ accumulator = operation(index, accumulator, this[index])
+ return accumulator
+}
+/**
+ * Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
+ *
+ * Returns `null` if the array is empty.
+ *
+ * @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
+ */
+inline fun ByteBuffer.reduceOrNull(operation: (acc: Byte, Byte) -> Byte): Byte? {
+ if (isEmpty())
+ return null
+ var accumulator = this[0]
+ for (index in 1..lastIndex)
+ accumulator = operation(accumulator, this[index])
+ return accumulator
+}
+/**
+ * Accumulates value starting with the last element and applying [operation] from right to left to each element and current accumulator value.
+ *
+ * Throws an exception if this array is empty. If the array can be empty in an expected way,
+ * please use [reduceRightOrNull] instead. It returns `null` when its receiver is empty.
+ *
+ * @param [operation] function that takes an element and current accumulator value, and calculates the next accumulator value.
+ */
+inline fun ByteBuffer.reduceRight(operation: (Byte, acc: Byte) -> Byte): Byte {
+ var index = lastIndex
+ if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.")
+ var accumulator = get(index--)
+ while (index >= 0)
+ accumulator = operation(get(index--), accumulator)
+ return accumulator
+}
+/**
+ * Accumulates value starting with the last element and applying [operation] from right to left
+ * to each element with its index in the original array and current accumulator value.
+ *
+ * Throws an exception if this array is empty. If the array can be empty in an expected way,
+ * please use [reduceRightIndexedOrNull] instead. It returns `null` when its receiver is empty.
+ *
+ * @param [operation] function that takes the index of an element, the element itself and current accumulator value,
+ * and calculates the next accumulator value.
+ */
+inline fun ByteBuffer.reduceRightIndexed(operation: (index: Int, Byte, acc: Byte) -> Byte): Byte {
+ var index = lastIndex
+ if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.")
+ var accumulator = get(index--)
+ while (index >= 0) {
+ accumulator = operation(index, get(index), accumulator)
+ --index
+ }
+ return accumulator
+}
+/**
+ * Accumulates value starting with the last element and applying [operation] from right to left
+ * to each element with its index in the original array and current accumulator value.
+ *
+ * Returns `null` if the array is empty.
+ *
+ * @param [operation] function that takes the index of an element, the element itself and current accumulator value,
+ * and calculates the next accumulator value.
+ */
+inline fun ByteBuffer.reduceRightIndexedOrNull(operation: (index: Int, Byte, acc: Byte) -> Byte): Byte? {
+ var index = lastIndex
+ if (index < 0) return null
+ var accumulator = get(index--)
+ while (index >= 0) {
+ accumulator = operation(index, get(index), accumulator)
+ --index
+ }
+ return accumulator
+}
+/**
+ * Accumulates value starting with the last element and applying [operation] from right to left to each element and current accumulator value.
+ *
+ * Returns `null` if the array is empty.
+ *
+ * @param [operation] function that takes an element and current accumulator value, and calculates the next accumulator value.
+ */
+inline fun ByteBuffer.reduceRightOrNull(operation: (Byte, acc: Byte) -> Byte): Byte? {
+ var index = lastIndex
+ if (index < 0) return null
+ var accumulator = get(index--)
+ while (index >= 0)
+ accumulator = operation(get(index--), accumulator)
+ return accumulator
+}
+/**
+ * Splits the original array into pair of lists, where *first* list contains elements for which [predicate]
+ * yielded `true`, while *second* list contains elements for which [predicate] yielded `false`.
+ */
+inline fun ByteBuffer.partition(predicate: (Byte) -> Boolean): Pair, List> {
+ val first = ArrayList()
+ val second = ArrayList()
+ for (element in this)
+ if (predicate(element))
+ first.add(element)
+ else
+ second.add(element)
+ return Pair(first, second)
+}
+/**
+ * Returns a list of pairs built from the elements of `this` array and the [other] array with the same index.
+ * The returned list has length of the shortest collection.
+ */
+infix fun ByteBuffer.zip(other: Array): List> = zip(other) { t1, t2 -> t1 to t2 }
+/**
+ * Returns a list of values built from the elements of `this` array and the [other] array with the same index
+ * using the provided [transform] function applied to each pair of elements.
+ * The returned list has length of the shortest collection.
+ */
+inline fun ByteBuffer.zip(other: Array, transform: (a: Byte, b: R) -> V): List {
+ val size = minOf(rem, other.size)
+ val list = ArrayList(size)
+ for (i in 0 until size)
+ list.add(transform(this[i], other[i]))
+ return list
+}
+/**
+ * Returns a list of pairs built from the elements of `this` collection and [other] array with the same index.
+ * The returned list has length of the shortest collection.
+ */
+infix fun ByteBuffer.zip(other: Iterable): List> = zip(other) { t1, t2 -> t1 to t2 }
+/**
+ * Returns a list of values built from the elements of `this` array and the [other] collection with the same index
+ * using the provided [transform] function applied to each pair of elements.
+ * The returned list has length of the shortest collection.
+ */
+inline fun ByteBuffer.zip(other: Iterable, transform: (a: Byte, b: R) -> V): List {
+ val arraySize = rem
+ val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize))
+ var i = 0
+ for (element in other) {
+ if (i >= arraySize) break
+ list.add(transform(this[i++], element))
+ }
+ return list
+}
+/**
+ * Returns a list of pairs built from the elements of `this` array and the [other] array with the same index.
+ * The returned list has length of the shortest collection.
+ */
+infix fun ByteBuffer.zip(other: ByteArray): List> = zip(other) { t1, t2 -> t1 to t2 }
+/**
+ * Returns a list of values built from the elements of `this` array and the [other] array with the same index
+ * using the provided [transform] function applied to each pair of elements.
+ * The returned list has length of the shortest array.
+ */
+inline fun ByteBuffer.zip(other: ByteArray, transform: (a: Byte, b: Byte) -> V): List {
+ val size = minOf(rem, other.size)
+ val list = ArrayList(size)
+ for (i in 0 until size)
+ list.add(transform(this[i], other[i]))
+ return list
+}
+/**
+ * Appends the string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
+ *
+ * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit]
+ * elements will be appended, followed by the [truncated] string (which defaults to "...").
+ */
+fun ByteBuffer.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Byte) -> CharSequence)? = null): A {
+ buffer.append(prefix)
+ var count = 0
+ for (element in this) {
+ if (++count > 1) buffer.append(separator)
+ if (limit < 0 || count <= limit)
+ if (transform != null)
+ buffer.append(transform(element))
+ else
+ buffer.append(element.toString())
+ else break
+ }
+ if (limit >= 0 && count > limit) buffer.append(truncated)
+ buffer.append(postfix)
+ return buffer
+}
+/**
+ * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
+ *
+ * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit]
+ * elements will be appended, followed by the [truncated] string (which defaults to "...").
+ */
+fun ByteBuffer.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((Byte) -> CharSequence)? = null): String =
+ joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString()
+/**
+ * Returns 1st *element* from the buffer.
+ *
+ * If the size of this array is less than 1, throws an [IndexOutOfBoundsException]
+ */
+inline operator fun ShortBuffer.component1(): Short = get(0)
+/**
+ * Returns 2nd *element* from the buffer.
+ *
+ * If the size of this array is less than 2, throws an [IndexOutOfBoundsException]
+ */
+inline operator fun ShortBuffer.component2(): Short = get(1)
+/**
+ * Returns 3rd *element* from the buffer.
+ *
+ * If the size of this array is less than 3, throws an [IndexOutOfBoundsException]
+ */
+inline operator fun ShortBuffer.component3(): Short = get(2)
+/**
+ * Returns 4th *element* from the buffer.
+ *
+ * If the size of this array is less than 4, throws an [IndexOutOfBoundsException]
+ */
+inline operator fun ShortBuffer.component4(): Short = get(3)
+/**
+ * Returns 5th *element* from the buffer.
+ *
+ * If the size of this array is less than 5, throws an [IndexOutOfBoundsException]
+ */
+inline operator fun ShortBuffer.component5(): Short = get(4)
+/** Returns `true` if [element] is found in the array. */
+operator fun ShortBuffer.contains(element: Short): Boolean = indexOf(element) >= 0
+/** Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. */
+inline fun ShortBuffer.elementAt(index: Int): Short = get(index)
+/** Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */
+inline fun ShortBuffer.elementAtOrElse(index: Int, defaultValue: (Int) -> Short): Short = if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
+/** Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */
+inline fun ShortBuffer.elementAtOrNull(index: Int): Short? = this.getOrNull(index)
+/** Returns the first element matching the given [predicate], or `null` if no such element was found. */
+inline fun ShortBuffer.find(predicate: (Short) -> Boolean): Short? = firstOrNull(predicate)
+/** Returns the last element matching the given [predicate], or `null` if no such element was found. */
+inline fun ShortBuffer.findLast(predicate: (Short) -> Boolean): Short? = lastOrNull(predicate)
+/**
+ * Returns the first element.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+fun ShortBuffer.first(): Short {
+ if (isEmpty())
+ throw NoSuchElementException("Array is empty.")
+ return this[0]
+}
+/**
+ * Returns the first element matching the given [predicate].
+ * @throws [NoSuchElementException] if no such element is found.
+ */
+inline fun ShortBuffer.first(predicate: (Short) -> Boolean): Short {
+ for (element in this) if (predicate(element)) return element
+ throw NoSuchElementException("Array contains no element matching the predicate.")
+}
+/** Returns the first element, or `null` if the array is empty. */
+fun ShortBuffer.firstOrNull(): Short? = if (isEmpty()) null else this[0]
+/** Returns the first element matching the given [predicate], or `null` if element was not found. */
+inline fun ShortBuffer.firstOrNull(predicate: (Short) -> Boolean): Short? {
+ for (element in this) if (predicate(element)) return element
+ return null
+}
+/** Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array. */
+inline fun ShortBuffer.getOrElse(index: Int, defaultValue: (Int) -> Short): Short = if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
+/** Returns an element at the given [index] or `null` if the [index] is out of bounds of this array. */
+fun ShortBuffer.getOrNull(index: Int): Short? = if (index >= 0 && index <= lastIndex) get(index) else null
+/** Returns first index of [element], or -1 if the array does not contain element. */
+fun ShortBuffer.indexOf(element: Short): Int {
+ for (index in indices)
+ if (element == this[index])
+ return index
+ return -1
+}
+/** Returns index of the first element matching the given [predicate], or -1 if the array does not contain such element. */
+inline fun ShortBuffer.indexOfFirst(predicate: (Short) -> Boolean): Int {
+ for (index in indices)
+ if (predicate(this[index]))
+ return index
+ return -1
+}
+/** Returns index of the last element matching the given [predicate], or -1 if the array does not contain such element. */
+inline fun ShortBuffer.indexOfLast(predicate: (Short) -> Boolean): Int {
+ for (index in indices.reversed())
+ if (predicate(this[index]))
+ return index
+ return -1
+}
+/**
+ * Returns the last element.
+ *
+ * @throws NoSuchElementException if the array is empty.
+ */
+fun ShortBuffer.last(): Short {
+ if (isEmpty())
+ throw NoSuchElementException("Array is empty.")
+ return this[lastIndex]
+}
+/**
+ * Returns the last element matching the given [predicate].
+ *
+ * @throws NoSuchElementException if no such element is found.
+ */
+inline fun ShortBuffer.last(predicate: (Short) -> Boolean): Short {
+ for (index in this.indices.reversed()) {
+ val element = this[index]
+ if (predicate(element)) return element
+ }
+ throw NoSuchElementException("Array contains no element matching the predicate.")
+}
+/** Returns last index of [element], or -1 if the array does not contain element. */
+fun ShortBuffer.lastIndexOf(element: Short): Int {
+ for (index in indices.reversed())
+ if (element == this[index])
+ return index
+ return -1
+}
+/** Returns the last element, or `null` if the array is empty. */
+fun ShortBuffer.lastOrNull(): Short? = if (isEmpty()) null else this[rem - 1]
+/** Returns the last element matching the given [predicate], or `null` if no such element was found. */
+inline fun ShortBuffer.lastOrNull(predicate: (Short) -> Boolean): Short? {
+ for (index in this.indices.reversed()) {
+ val element = this[index]
+ if (predicate(element)) return element
+ }
+ return null
+}
+/** Returns the single element, or throws an exception if the array is empty or has more than one element. */
+fun ShortBuffer.single(): Short = when (rem) {
+ 0 -> throw NoSuchElementException("Array is empty.")
+ 1 -> this[0]
+ else -> throw IllegalArgumentException("Array has more than one element.")
+}
+/** Returns the single element matching the given [predicate], or throws exception if there is no or more than one matching element. */
+inline fun ShortBuffer.single(predicate: (Short) -> Boolean): Short {
+ var single: Short? = null
+ var found = false
+ for (element in this)
+ if (predicate(element)) {
+ if (found) throw IllegalArgumentException("Array contains more than one matching element.")
+ single = element
+ found = true
+ }
+ if (!found) throw NoSuchElementException("Array contains no element matching the predicate.")
+ @Suppress("UNCHECKED_CAST")
+ return single as Short
+}
+/** Returns single element, or `null` if the array is empty or has more than one element. */
+fun ShortBuffer.singleOrNull(): Short? = if (rem == 1) this[0] else null
+/** Returns the single element matching the given [predicate], or `null` if element was not found or more than one element was found. */
+inline fun ShortBuffer.singleOrNull(predicate: (Short) -> Boolean): Short? {
+ var single: Short? = null
+ var found = false
+ for (element in this)
+ if (predicate(element)) {
+ if (found) return null
+ single = element
+ found = true
+ }
+ if (!found) return null
+ return single
+}
+/** Returns a list containing only elements matching the given [predicate]. */
+inline fun ShortBuffer.filter(predicate: (Short) -> Boolean): List = filterTo(ArrayList(), predicate)
+/**
+ * Returns a list containing only elements matching the given [predicate].
+ * @param [predicate] function that takes the index of an element and the element itself and returns the result of predicate evaluation on the element.
+ */
+inline fun ShortBuffer.filterIndexed(predicate: (index: Int, Short) -> Boolean): List = filterIndexedTo(ArrayList(), predicate)
+/**
+ * Appends all elements matching the given [predicate] to the given [destination].
+ * @param [predicate] function that takes the index of an element and the element itself and returns the result of predicate evaluation on the element.
+ */
+inline fun > ShortBuffer.filterIndexedTo(destination: C, predicate: (index: Int, Short) -> Boolean): C {
+ forEachIndexed { index, element ->
+ if (predicate(index, element)) destination.add(element)
+ }
+ return destination
+}
+/** Returns a list containing all elements not matching the given [predicate]. */
+inline fun ShortBuffer.filterNot(predicate: (Short) -> Boolean): List = filterNotTo(ArrayList(), predicate)
+/** Appends all elements not matching the given [predicate] to the given [destination]. */
+inline fun > ShortBuffer.filterNotTo(destination: C, predicate: (Short) -> Boolean): C {
+ for (element in this) if (!predicate(element)) destination.add(element)
+ return destination
+}
+/** Appends all elements matching the given [predicate] to the given [destination]. */
+inline fun > ShortBuffer.filterTo(destination: C, predicate: (Short) -> Boolean): C {
+ for (element in this) if (predicate(element)) destination.add(element)
+ return destination
+}
+/** Returns a list containing elements at specified [indices]. */
+fun ShortBuffer.slice(indices: Iterable): List {
+ val size = indices.collectionSizeOrDefault(10)
+ if (size == 0) return emptyList()
+ val list = ArrayList(size)
+ for (index in indices)
+ list.add(get(index))
+ return list
+}
+/** Returns an array containing elements of this array at specified [indices]. */
+fun ShortBuffer.sliceArray(indices: Collection): ShortArray {
+ val result = ShortArray(indices.size)
+ var targetIndex = 0
+ for (sourceIndex in indices)
+ result[targetIndex++] = this[sourceIndex]
+ return result
+}
+/** Sorts elements in the array in-place descending according to their natural sort order. */
+fun ShortBuffer.sortDescending() {
+ if (rem > 1) {
+ val array = toShortArray().apply{ sortDescending() }
+ for (i in 0 until rem)
+ this[pos + i] = array[i]
+ }
+}
+/** Returns a list of all elements sorted according to their natural sort order. */
+fun ShortBuffer.sorted(): List = toTypedArray().apply { sort() }.asList()
+/** Returns an array with all elements of this array sorted according to their natural sort order. */
+fun ShortBuffer.sortedArray(): ShortArray = toShortArray().apply { sort() }
+/** Returns an array with all elements of this array sorted descending according to their natural sort order. */
+fun ShortBuffer.sortedArrayDescending(): ShortArray = toShortArray().apply { sortDescending() }
+/** Returns a list of all elements sorted according to natural sort order of the value returned by specified [selector] function. */
+inline fun > ShortBuffer.sortedBy(crossinline selector: (Short) -> R?): List = toShortArray().sortedWith(compareBy(selector))
+/** Returns a list of all elements sorted descending according to natural sort order of the value returned by specified [selector] function. */
+inline fun > ShortBuffer.sortedByDescending(crossinline selector: (Short) -> R?): List = toShortArray().sortedWith(compareByDescending(selector))
+/** Returns a list of all elements sorted descending according to their natural sort order. */
+fun ShortBuffer.sortedDescending(): List = toShortArray().apply { sort() }.reversed()
+/** Returns a list of all elements sorted according to the specified [comparator]. */
+fun ShortBuffer.sortedWith(comparator: Comparator): List = toTypedArray().apply { sortWith(comparator) }.asList()
+/** Sorts the array in-place. */
+fun ShortBuffer.sort() {
+ if (rem > 1) {
+ val array = toShortArray().apply{ sort() }
+ for (i in 0 until rem)
+ this[pos + i] = array[i]
+ }
+}
+/** Returns a string representation of the contents of the specified array as if it is [List]. */
+fun ShortBuffer.contentToString(): String {
+ val iMax = lim - 1
+ if (iMax == -1) return "[]"
+ val b = StringBuilder()
+ b.append('[')
+ var i = 0
+ while (true) {
+ b.append(get(i))
+ if (i == iMax) return b.append(']').toString()
+ b.append(", ")
+ i++
+ }
+}
+/**
+ * Fills this array or its subrange with the specified [element] value.
+ *
+ * @param fromIndex the start of the range (inclusive) to fill, 0 by default.
+ * @param toIndex the end of the range (exclusive) to fill, size of this array by default.
+ *
+ * @throws IndexOutOfBoundsException if [fromIndex] is less than zero or [toIndex] is greater than the size of this array.
+ * @throws IllegalArgumentException if [fromIndex] is greater than [toIndex].
+ */
+fun ShortBuffer.fill(element: Short, fromIndex: Int = pos, toIndex: Int = rem) {
+ for (i in fromIndex until toIndex)
+ this[i] = element
+}
+/** Returns the range of valid indices for the array. */
+val ShortBuffer.indices: IntRange
+ get() = IntRange(0, lastIndex)
+/** Returns `true` if the array is empty. */
+inline fun ShortBuffer.isEmpty(): Boolean = rem == 0
+/** Returns `true` if the array is not empty. */
+inline fun ShortBuffer.isNotEmpty(): Boolean = !isEmpty()
+/** Returns the last valid index for the array. */
+val ShortBuffer.lastIndex: Int
+ get() = rem - 1
+/** Returns an array of Byte containing all of the elements of this generic array. */
+fun ShortBuffer.toShortArray(): ShortArray = ShortArray(rem) { index -> this[index] }
+/** Returns a *typed* object array containing all of the elements of this primitive array. */
+fun ShortBuffer.toTypedArray(): Array = Array(rem) { index -> this[index] }
+/**
+ * Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
+ *
+ * If any of two pairs would have the same key the last one gets added to the map.
+ *
+ * The returned map preserves the entry iteration order of the original array.
+ */
+inline fun ShortBuffer.associate(transform: (Short) -> Pair): Map {
+ val capacity = mapCapacity(rem).coerceAtLeast(16)
+ return associateTo(LinkedHashMap(capacity), transform)
+}
+/**
+ * Returns a [Map] containing the elements from the given array indexed by the key returned from [keySelector] function applied to each element.
+ *
+ * If any two elements would have the same key returned by [keySelector] the last one gets added to the map.
+ *
+ * The returned map preserves the entry iteration order of the original array.
+ */
+inline fun ShortBuffer.associateBy(keySelector: (Short) -> K): Map {
+ val capacity = mapCapacity(rem).coerceAtLeast(16)
+ return associateByTo(LinkedHashMap(capacity), keySelector)
+}
+/**
+ * Returns a [Map] containing the values provided by [valueTransform] and indexed by [keySelector] functions applied to elements of the given array.
+ *
+ * If any two elements would have the same key returned by [keySelector] the last one gets added to the map.
+ *
+ * The returned map preserves the entry iteration order of the original array.
+ */
+inline fun ShortBuffer.associateBy(keySelector: (Short) -> K, valueTransform: (Short) -> V): Map {
+ val capacity = mapCapacity(rem).coerceAtLeast(16)
+ return associateByTo(LinkedHashMap(capacity), keySelector, valueTransform)
+}
+/**
+ * Populates and returns the [destination] mutable map with key-value pairs, where key is provided by
+ * the [keySelector] function applied to each element of the given array and value is the element itself.
+ *
+ * If any two elements would have the same key returned by [keySelector] the last one gets added to the map.
+ */
+inline fun