1 /*
<lambda>null2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.permission.access.collection
18 
19 import android.util.SparseLongArray
20 
21 inline fun SparseLongArray.allIndexed(predicate: (Int, Int, Long) -> Boolean): Boolean {
22     forEachIndexed { index, key, value ->
23         if (!predicate(index, key, value)) {
24             return false
25         }
26     }
27     return true
28 }
29 
anyIndexednull30 inline fun SparseLongArray.anyIndexed(predicate: (Int, Int, Long) -> Boolean): Boolean {
31     forEachIndexed { index, key, value ->
32         if (predicate(index, key, value)) {
33             return true
34         }
35     }
36     return false
37 }
38 
forEachIndexednull39 inline fun SparseLongArray.forEachIndexed(action: (Int, Int, Long) -> Unit) {
40     for (index in 0 until size) {
41         action(index, keyAt(index), valueAt(index))
42     }
43 }
44 
forEachReversedIndexednull45 inline fun SparseLongArray.forEachReversedIndexed(action: (Int, Int, Long) -> Unit) {
46     for (index in lastIndex downTo 0) {
47         action(index, keyAt(index), valueAt(index))
48     }
49 }
50 
getOrPutnull51 inline fun SparseLongArray.getOrPut(key: Int, defaultValue: () -> Long): Long {
52     val index = indexOfKey(key)
53     return if (index >= 0) {
54         valueAt(index)
55     } else {
56         defaultValue().also { put(key, it) }
57     }
58 }
59 
60 inline val SparseLongArray.lastIndex: Int
61     get() = size - 1
62 
63 @Suppress("NOTHING_TO_INLINE")
minusAssignnull64 inline operator fun SparseLongArray.minusAssign(key: Int) {
65     delete(key)
66 }
67 
noneIndexednull68 inline fun SparseLongArray.noneIndexed(predicate: (Int, Int, Long) -> Boolean): Boolean {
69     forEachIndexed { index, key, value ->
70         if (predicate(index, key, value)) {
71             return false
72         }
73     }
74     return true
75 }
76 
SparseLongArraynull77 fun SparseLongArray.remove(key: Int) {
78     delete(key)
79 }
80 
SparseLongArraynull81 fun SparseLongArray.remove(key: Int, defaultValue: Long): Long {
82     val index = indexOfKey(key)
83     return if (index >= 0) {
84         val oldValue = valueAt(index)
85         removeAt(index)
86         oldValue
87     } else {
88         defaultValue
89     }
90 }
91 
removeAllIndexednull92 inline fun SparseLongArray.removeAllIndexed(predicate: (Int, Int, Long) -> Boolean): Boolean {
93     var isChanged = false
94     forEachReversedIndexed { index, key, value ->
95         if (predicate(index, key, value)) {
96             removeAt(index)
97             isChanged = true
98         }
99     }
100     return isChanged
101 }
102 
retainAllIndexednull103 inline fun SparseLongArray.retainAllIndexed(predicate: (Int, Int, Long) -> Boolean): Boolean {
104     var isChanged = false
105     forEachReversedIndexed { index, key, value ->
106         if (!predicate(index, key, value)) {
107             removeAt(index)
108             isChanged = true
109         }
110     }
111     return isChanged
112 }
113 
114 @Suppress("NOTHING_TO_INLINE")
setnull115 inline operator fun SparseLongArray.set(key: Int, value: Long) {
116     put(key, value)
117 }
118 
119 inline val SparseLongArray.size: Int
120     get() = size()
121