summaryrefslogtreecommitdiff
path: root/packages/SystemUI/src/com/android/systemui/util/SparseArrayUtils.kt
blob: 1a25c84a796528793b0a9fe5aac8d7e1f80b8273 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.util

import android.util.SparseArray

/**
 * Transforms a sequence of Key/Value pairs into a SparseArray.
 *
 * See [kotlin.collections.toMap].
 */
fun <T> Sequence<Pair<Int, T>>.toSparseArray(size: Int = -1): SparseArray<T> {
    val sparseArray = when {
        size < 0 -> SparseArray<T>()
        else -> SparseArray<T>(size)
    }
    for ((i, v) in this) {
        sparseArray.put(i, v)
    }
    return sparseArray
}

/**
 * Transforms an [Array] into a [SparseArray], by applying each element to [keySelector] in order to
 * generate the index at which it will be placed. If two elements produce the same index, the latter
 * replaces the former in the final result.
 *
 * See [Array.associateBy].
 */
inline fun <T> Array<T>.associateByToSparseArray(
    crossinline keySelector: (T) -> Int
): SparseArray<T> {
    val sparseArray = SparseArray<T>(size)
    for (value in this) {
        sparseArray.put(keySelector(value), value)
    }
    return sparseArray
}

/**
 * Folds a [Grouping] into a [SparseArray]. See [Grouping.fold].
 */
inline fun <T, R> Grouping<T, Int>.foldToSparseArray(
    initial: R,
    size: Int = -1,
    crossinline operation: (R, T) -> R
): SparseArray<R> {
    val sparseArray = when {
        size < 0 -> SparseArray<R>()
        else -> SparseArray<R>(size)
    }
    sourceIterator().forEach { elem ->
        val key = keyOf(elem)
        val acc = sparseArray.get(key) ?: initial
        sparseArray.put(key, operation(acc, elem))
    }
    return sparseArray
}

/**
 * Wraps this [SparseArray] into an immutable [Map], the methods of which forward to this
 * [SparseArray].
 */
fun <T> SparseArray<T>.asMap(): Map<Int, T> = SparseArrayMapWrapper(this)

private class SparseArrayMapWrapper<T>(
    private val sparseArray: SparseArray<T>
) : Map<Int, T> {

    private data class Entry<T>(override val key: Int, override val value: T) : Map.Entry<Int, T>

    private val entrySequence = sequence {
        val size = sparseArray.size()
        for (i in 0 until size) {
            val key = sparseArray.keyAt(i)
            val value = sparseArray.get(key)
            yield(Entry(key, value))
        }
    }

    override val entries: Set<Map.Entry<Int, T>>
        get() = object : Set<Map.Entry<Int, T>> {
            override val size: Int
                get() = this@SparseArrayMapWrapper.size

            override fun contains(element: Map.Entry<Int, T>): Boolean =
                    sparseArray[element.key]?.let { it == element.value } == true

            override fun containsAll(elements: Collection<Map.Entry<Int, T>>): Boolean =
                    elements.all { contains(it) }

            override fun isEmpty(): Boolean = size == 0

            override fun iterator(): Iterator<Map.Entry<Int, T>> = entrySequence.iterator()
        }

    override val keys: Set<Int> = object : Set<Int> {
        private val keySequence = entrySequence.map { it.key }

        override val size: Int
            get() = this@SparseArrayMapWrapper.size

        override fun contains(element: Int): Boolean = containsKey(element)

        override fun containsAll(elements: Collection<Int>): Boolean =
                elements.all { contains(it) }

        override fun isEmpty(): Boolean = size == 0

        override fun iterator(): Iterator<Int> = keySequence.iterator()
    }
    override val size: Int
        get() = sparseArray.size()
    override val values: Collection<T>
        get() = object : Collection<T> {
            private val valueSequence = entrySequence.map { it.value }

            override val size: Int
                get() = this@SparseArrayMapWrapper.size

            override fun contains(element: T): Boolean = containsValue(element)

            override fun containsAll(elements: Collection<T>): Boolean =
                    elements.all { contains(it) }

            override fun isEmpty(): Boolean = this@SparseArrayMapWrapper.isEmpty()

            override fun iterator(): Iterator<T> = valueSequence.iterator()
        }

    override fun containsKey(key: Int): Boolean = sparseArray.contains(key)

    override fun containsValue(value: T): Boolean = sparseArray.indexOfValue(value) >= 0

    override fun get(key: Int): T? = sparseArray.get(key)

    override fun isEmpty(): Boolean = sparseArray.size() == 0
}