1  /*
<lambda>null2   * Copyright (C) 2023 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.SparseIntArray
20  
21  inline fun SparseIntArray.allIndexed(predicate: (Int, Int, Int) -> 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 SparseIntArray.anyIndexed(predicate: (Int, Int, Int) -> 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 SparseIntArray.forEachIndexed(action: (Int, Int, Int) -> Unit) {
40      for (index in 0 until size) {
41          action(index, keyAt(index), valueAt(index))
42      }
43  }
44  
forEachReversedIndexednull45  inline fun SparseIntArray.forEachReversedIndexed(action: (Int, Int, Int) -> Unit) {
46      for (index in lastIndex downTo 0) {
47          action(index, keyAt(index), valueAt(index))
48      }
49  }
50  
getOrPutnull51  inline fun SparseIntArray.getOrPut(key: Int, defaultValue: () -> Int): Int {
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 SparseIntArray.lastIndex: Int
61      get() = size - 1
62  
63  @Suppress("NOTHING_TO_INLINE")
minusAssignnull64  inline operator fun SparseIntArray.minusAssign(key: Int) {
65      delete(key)
66  }
67  
noneIndexednull68  inline fun SparseIntArray.noneIndexed(predicate: (Int, Int, Int) -> Boolean): Boolean {
69      forEachIndexed { index, key, value ->
70          if (predicate(index, key, value)) {
71              return false
72          }
73      }
74      return true
75  }
76  
SparseIntArraynull77  fun SparseIntArray.remove(key: Int) {
78      delete(key)
79  }
80  
SparseIntArraynull81  fun SparseIntArray.remove(key: Int, defaultValue: Int): Int {
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 SparseIntArray.removeAllIndexed(predicate: (Int, Int, Int) -> 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 SparseIntArray.retainAllIndexed(predicate: (Int, Int, Int) -> 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 SparseIntArray.set(key: Int, value: Int) {
116      put(key, value)
117  }
118  
119  inline val SparseIntArray.size: Int
120      get() = size()
121