1 /*
2  * Copyright 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 androidx.compose.ui.node
18 
19 import androidx.compose.runtime.collection.MutableVector
20 
21 /**
22  * This class tracks the mutation to the provided [vector] through the provided methods. On
23  * mutation, the [onVectorMutated] lambda will be invoked.
24  */
25 internal class MutableVectorWithMutationTracking<T>(
26     val vector: MutableVector<T>,
27     val onVectorMutated: () -> Unit,
28 ) {
29     val size: Int
30         inline get() = vector.size
31 
clearnull32     fun clear() {
33         vector.clear()
34         onVectorMutated()
35     }
36 
addnull37     fun add(index: Int, element: T) {
38         vector.add(index, element)
39         onVectorMutated()
40     }
41 
removeAtnull42     fun removeAt(index: Int): T {
43         return vector.removeAt(index).also { onVectorMutated() }
44     }
45 
forEachnull46     @Suppress("NOTHING_TO_INLINE") inline fun forEach(block: (T) -> Unit) = vector.forEach(block)
47 
48     @Suppress("NOTHING_TO_INLINE") inline fun asList(): List<T> = vector.asMutableList()
49 
50     @Suppress("NOTHING_TO_INLINE") inline operator fun get(index: Int): T = vector[index]
51 }
52