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 inline fun <T> List<T>.allIndexed(predicate: (Int, T) -> Boolean): Boolean {
20 forEachIndexed { index, element ->
21 if (!predicate(index, element)) {
22 return false
23 }
24 }
25 return true
26 }
27
anyIndexednull28 inline fun <T> List<T>.anyIndexed(predicate: (Int, T) -> Boolean): Boolean {
29 forEachIndexed { index, element ->
30 if (predicate(index, element)) {
31 return true
32 }
33 }
34 return false
35 }
36
forEachIndexednull37 inline fun <T> List<T>.forEachIndexed(action: (Int, T) -> Unit) {
38 for (index in indices) {
39 action(index, this[index])
40 }
41 }
42
forEachReversedIndexednull43 inline fun <T> List<T>.forEachReversedIndexed(action: (Int, T) -> Unit) {
44 for (index in lastIndex downTo 0) {
45 action(index, this[index])
46 }
47 }
48
noneIndexednull49 inline fun <T> List<T>.noneIndexed(predicate: (Int, T) -> Boolean): Boolean {
50 forEachIndexed { index, element ->
51 if (predicate(index, element)) {
52 return false
53 }
54 }
55 return true
56 }
57
removeAllIndexednull58 inline fun <T> MutableList<T>.removeAllIndexed(predicate: (Int, T) -> Boolean): Boolean {
59 var isChanged = false
60 forEachReversedIndexed { index, element ->
61 if (predicate(index, element)) {
62 removeAt(index)
63 isChanged = true
64 }
65 }
66 return isChanged
67 }
68
retainAllIndexednull69 inline fun <T> MutableList<T>.retainAllIndexed(predicate: (Int, T) -> Boolean): Boolean {
70 var isChanged = false
71 forEachReversedIndexed { index, element ->
72 if (!predicate(index, element)) {
73 removeAt(index)
74 isChanged = true
75 }
76 }
77 return isChanged
78 }
79