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.ArraySet
20 
21 fun <T> arraySetOf(vararg elements: T): ArraySet<T> = ArraySet(elements.asList())
22 
23 inline fun <T> ArraySet<T>.allIndexed(predicate: (Int, T) -> Boolean): Boolean {
24     forEachIndexed { index, value ->
25         if (!predicate(index, value)) {
26             return false
27         }
28     }
29     return true
30 }
31 
anyIndexednull32 inline fun <T> ArraySet<T>.anyIndexed(predicate: (Int, T) -> Boolean): Boolean {
33     forEachIndexed { index, value ->
34         if (predicate(index, value)) {
35             return true
36         }
37     }
38     return false
39 }
40 
forEachIndexednull41 inline fun <T> ArraySet<T>.forEachIndexed(action: (Int, T) -> Unit) {
42     for (index in 0 until size) {
43         action(index, valueAt(index))
44     }
45 }
46 
forEachReversedIndexednull47 inline fun <T> ArraySet<T>.forEachReversedIndexed(action: (Int, T) -> Unit) {
48     for (index in lastIndex downTo 0) {
49         action(index, valueAt(index))
50     }
51 }
52 
53 inline val <T> ArraySet<T>.lastIndex: Int
54     get() = size - 1
55 
56 @Suppress("NOTHING_TO_INLINE")
minusAssignnull57 inline operator fun <T> ArraySet<T>.minusAssign(value: T) {
58     remove(value)
59 }
60 
noneIndexednull61 inline fun <T> ArraySet<T>.noneIndexed(predicate: (Int, T) -> Boolean): Boolean {
62     forEachIndexed { index, value ->
63         if (predicate(index, value)) {
64             return false
65         }
66     }
67     return true
68 }
69 
70 @Suppress("NOTHING_TO_INLINE")
plusAssignnull71 inline operator fun <T> ArraySet<T>.plusAssign(value: T) {
72     add(value)
73 }
74 
removeAllIndexednull75 inline fun <T> ArraySet<T>.removeAllIndexed(predicate: (Int, T) -> Boolean): Boolean {
76     var isChanged = false
77     forEachReversedIndexed { index, value ->
78         if (predicate(index, value)) {
79             removeAt(index)
80             isChanged = true
81         }
82     }
83     return isChanged
84 }
85 
retainAllIndexednull86 inline fun <T> ArraySet<T>.retainAllIndexed(predicate: (Int, T) -> Boolean): Boolean {
87     var isChanged = false
88     forEachReversedIndexed { index, value ->
89         if (!predicate(index, value)) {
90             removeAt(index)
91             isChanged = true
92         }
93     }
94     return isChanged
95 }
96