1+ package ru.nobird.android.core.model
2+
3+ fun <T : Comparable <T >> Array<T>.isOrdered (): Boolean =
4+ (0 until this .size - 1 ).none { this [it] > this [it + 1 ] }
5+
6+ fun <T : Comparable <T >> Array<T>.isNotOrdered (): Boolean =
7+ ! this .isOrdered()
8+
9+ fun <T : Comparable <T >> Array<T>.isOrderedDesc (): Boolean =
10+ (0 until this .size - 1 ).none { this [it] < this [it + 1 ] }
11+
12+ /* *
13+ * Maps collection straight to long array
14+ */
15+ inline fun <T > Collection<T>.mapToLongArray (transform : (T ) -> Long ): LongArray {
16+ val array = LongArray (this .size)
17+ forEachIndexed { index, t ->
18+ array[index] = transform(t)
19+ }
20+ return array
21+ }
22+
23+ /* *
24+ * Maps array straight to long array
25+ */
26+ inline fun <T > Array<T>.mapToLongArray (transform : (T ) -> Long ): LongArray {
27+ val array = LongArray (this .size)
28+ forEachIndexed { index, t ->
29+ array[index] = transform(t)
30+ }
31+ return array
32+ }
33+
34+ fun LongArray?.isNullOrEmpty (): Boolean =
35+ this == null || this .isEmpty()
36+
37+ /* *
38+ * Flattens collection of long arrays
39+ */
40+ @JvmName(" Iterable_LongArray__flatten" )
41+ fun Iterable<LongArray>.flatten (): LongArray {
42+ val size = sumBy { it.size }
43+ val array = LongArray (size)
44+ var offset = 0
45+ forEach { subArray ->
46+ subArray.copyInto(array, destinationOffset = offset)
47+ offset + = subArray.size
48+ }
49+
50+ return array
51+ }
52+
53+ /* *
54+ * Removes duplicates from long array
55+ */
56+ @JvmName(" LongArray_distinct" )
57+ fun LongArray.distinct (): LongArray =
58+ toMutableSet().toLongArray()
59+
60+ /* *
61+ * Immutable swap
62+ */
63+ fun <T > List<T>.swap (i : Int , j : Int ): List <T > {
64+ if (i !in 0 until size ||
65+ j !in 0 until size) {
66+ return this
67+ }
68+
69+ val a = this [i]
70+ val b = this [j]
71+ return mapIndexed { index, t ->
72+ when (index) {
73+ i -> b
74+ j -> a
75+ else -> t
76+ }
77+ }
78+ }
79+
80+ /* *
81+ * Applies mutation to list
82+ */
83+ inline fun <T > List<T>.mutate (mutation : MutableList <T >.() -> Unit ): List <T > =
84+ this .toMutableList().apply (mutation)
85+
86+ /* *
87+ * Applies mutation to list
88+ */
89+ inline fun <T > PagedList<T>.mutate (mutation : MutableList <T >.() -> Unit ): PagedList <T > =
90+ PagedList (this .toMutableList().apply (mutation), hasPrev = hasPrev, hasNext = hasNext, page = page)
91+
92+ /* *
93+ * Creates new list from current with inserted [item] at [pos]
94+ */
95+ fun <T > List<T>.insert (item : T , pos : Int = size): List <T > {
96+ val list = mutableListOf<T >()
97+ list.addAll(this .slice(to = pos))
98+ list.add(item)
99+ list.addAll(this .slice(from = pos))
100+ return list
101+ }
102+
103+ /* *
104+ * Creates sublist as view from current
105+ */
106+ fun <T > List<T>.slice (from : Int = 0, to : Int = size): List <T > =
107+ subList(from.coerceAtLeast(0 ), to.coerceAtMost(size))
108+
109+ /* *
110+ * Puts [value] in map if it is not null
111+ */
112+ fun <K , V > MutableMap <K , V >.putNullable (key : K , value : V ? ) {
113+ if (value != null ) {
114+ put(key, value)
115+ }
116+ }
0 commit comments