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.ArrayMap 20 21 inline fun <K, V> ArrayMap<K, V>.allIndexed(predicate: (Int, K, V) -> 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 <K, V> ArrayMap<K, V>.anyIndexed(predicate: (Int, K, V) -> 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 <K, V> ArrayMap<K, V>.forEachIndexed(action: (Int, K, V) -> Unit) { 40 for (index in 0 until size) { 41 action(index, keyAt(index), valueAt(index)) 42 } 43 } 44 forEachReversedIndexednull45 inline fun <K, V> ArrayMap<K, V>.forEachReversedIndexed(action: (Int, K, V) -> Unit) { 46 for (index in lastIndex downTo 0) { 47 action(index, keyAt(index), valueAt(index)) 48 } 49 } 50 getOrPutnull51 inline fun <K, V> ArrayMap<K, V>.getOrPut(key: K, defaultValue: () -> V): V { 52 get(key)?.let { 53 return it 54 } 55 return defaultValue().also { put(key, it) } 56 } 57 58 inline val <K, V> ArrayMap<K, V>.lastIndex: Int 59 get() = size - 1 60 61 @Suppress("NOTHING_TO_INLINE") minusAssignnull62 inline operator fun <K, V> ArrayMap<K, V>.minusAssign(key: K) { 63 remove(key) 64 } 65 noneIndexednull66 inline fun <K, V> ArrayMap<K, V>.noneIndexed(predicate: (Int, K, V) -> Boolean): Boolean { 67 forEachIndexed { index, key, value -> 68 if (predicate(index, key, value)) { 69 return false 70 } 71 } 72 return true 73 } 74 removeAllIndexednull75 inline fun <K, V> ArrayMap<K, V>.removeAllIndexed(predicate: (Int, K, V) -> Boolean): Boolean { 76 var isChanged = false 77 forEachReversedIndexed { index, key, value -> 78 if (predicate(index, key, value)) { 79 removeAt(index) 80 isChanged = true 81 } 82 } 83 return isChanged 84 } 85 retainAllIndexednull86 inline fun <K, V> ArrayMap<K, V>.retainAllIndexed(predicate: (Int, K, V) -> Boolean): Boolean { 87 var isChanged = false 88 forEachReversedIndexed { index, key, value -> 89 if (!predicate(index, key, value)) { 90 removeAt(index) 91 isChanged = true 92 } 93 } 94 return isChanged 95 } 96 97 @Suppress("NOTHING_TO_INLINE") setnull98 inline operator fun <K, V> ArrayMap<K, V>.set(key: K, value: V) { 99 put(key, value) 100 } 101