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.immutable
18
19 inline fun IntSet.allIndexed(predicate: (Int, Int) -> Boolean): Boolean {
20 forEachIndexed { index, element ->
21 if (!predicate(index, element)) {
22 return false
23 }
24 }
25 return true
26 }
27
anyIndexednull28 inline fun IntSet.anyIndexed(predicate: (Int, Int) -> Boolean): Boolean {
29 forEachIndexed { index, element ->
30 if (predicate(index, element)) {
31 return true
32 }
33 }
34 return false
35 }
36
forEachIndexednull37 inline fun IntSet.forEachIndexed(action: (Int, Int) -> Unit) {
38 for (index in 0 until size) {
39 action(index, elementAt(index))
40 }
41 }
42
forEachReversedIndexednull43 inline fun IntSet.forEachReversedIndexed(action: (Int, Int) -> Unit) {
44 for (index in lastIndex downTo 0) {
45 action(index, elementAt(index))
46 }
47 }
48
49 inline val IntSet.lastIndex: Int
50 get() = size - 1
51
<lambda>null52 operator fun IntSet.minus(element: Int): MutableIntSet = toMutable().apply { this -= element }
53
minusAssignnull54 operator fun IntSet.minusAssign(element: Int) {
55 array.delete(element)
56 }
57
noneIndexednull58 inline fun IntSet.noneIndexed(predicate: (Int, Int) -> Boolean): Boolean {
59 forEachIndexed { index, element ->
60 if (predicate(index, element)) {
61 return false
62 }
63 }
64 return true
65 }
66
<lambda>null67 operator fun IntSet.plus(element: Int): MutableIntSet = toMutable().apply { this += element }
68
<lambda>null69 fun MutableIntSet(values: IntArray): MutableIntSet = MutableIntSet().apply { this += values }
70
plusAssignnull71 operator fun MutableIntSet.plusAssign(element: Int) {
72 array.put(element, true)
73 }
74
plusAssignnull75 operator fun MutableIntSet.plusAssign(set: IntSet) {
76 set.forEachIndexed { _, it -> this += it }
77 }
78
plusAssignnull79 operator fun MutableIntSet.plusAssign(array: IntArray) {
80 array.forEach { this += it }
81 }
82