Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | package android.util; |
| 17 | |
| 18 | /** |
| 19 | * A sparse array of ArraySets, which is suitable to hold userid->packages association. |
| 20 | * |
| 21 | * @hide |
| 22 | */ |
| 23 | public class SparseSetArray<T> { |
| 24 | private final SparseArray<ArraySet<T>> mData = new SparseArray<>(); |
| 25 | |
| 26 | public SparseSetArray() { |
| 27 | } |
| 28 | |
| 29 | /** |
Kweku Adams | 79cbc34 | 2019-12-13 18:03:49 -0800 | [diff] [blame] | 30 | * Add a value for key n. |
| 31 | * @return FALSE when the value already existed for the given key, TRUE otherwise. |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 32 | */ |
| 33 | public boolean add(int n, T value) { |
| 34 | ArraySet<T> set = mData.get(n); |
| 35 | if (set == null) { |
| 36 | set = new ArraySet<>(); |
| 37 | mData.put(n, set); |
| 38 | } |
| 39 | if (set.contains(value)) { |
| 40 | return true; |
| 41 | } |
| 42 | set.add(value); |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | /** |
Kweku Adams | 7d6a31c | 2019-04-16 17:05:30 -0700 | [diff] [blame] | 47 | * Removes all mappings from this SparseSetArray. |
| 48 | */ |
| 49 | public void clear() { |
| 50 | mData.clear(); |
| 51 | } |
| 52 | |
| 53 | /** |
Kweku Adams | 79cbc34 | 2019-12-13 18:03:49 -0800 | [diff] [blame] | 54 | * @return whether the value exists for the key n. |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 55 | */ |
| 56 | public boolean contains(int n, T value) { |
| 57 | final ArraySet<T> set = mData.get(n); |
| 58 | if (set == null) { |
| 59 | return false; |
| 60 | } |
| 61 | return set.contains(value); |
| 62 | } |
| 63 | |
| 64 | /** |
Kweku Adams | 79cbc34 | 2019-12-13 18:03:49 -0800 | [diff] [blame] | 65 | * @return the set of items of key n |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 66 | */ |
| 67 | public ArraySet<T> get(int n) { |
| 68 | return mData.get(n); |
| 69 | } |
| 70 | |
| 71 | /** |
Kweku Adams | 79cbc34 | 2019-12-13 18:03:49 -0800 | [diff] [blame] | 72 | * Remove a value for key n. |
| 73 | * @return TRUE when the value existed for the given key and removed, FALSE otherwise. |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 74 | */ |
| 75 | public boolean remove(int n, T value) { |
| 76 | final ArraySet<T> set = mData.get(n); |
| 77 | if (set == null) { |
| 78 | return false; |
| 79 | } |
| 80 | final boolean ret = set.remove(value); |
| 81 | if (set.size() == 0) { |
| 82 | mData.remove(n); |
| 83 | } |
| 84 | return ret; |
| 85 | } |
| 86 | |
| 87 | /** |
Kweku Adams | 79cbc34 | 2019-12-13 18:03:49 -0800 | [diff] [blame] | 88 | * Remove all values for key n. |
Makoto Onuki | eb898f1 | 2018-01-23 15:26:27 -0800 | [diff] [blame] | 89 | */ |
| 90 | public void remove(int n) { |
| 91 | mData.remove(n); |
| 92 | } |
| 93 | public int size() { |
| 94 | return mData.size(); |
| 95 | } |
| 96 | |
| 97 | public int keyAt(int index) { |
| 98 | return mData.keyAt(index); |
| 99 | } |
| 100 | |
| 101 | public int sizeAt(int index) { |
| 102 | final ArraySet<T> set = mData.valueAt(index); |
| 103 | if (set == null) { |
| 104 | return 0; |
| 105 | } |
| 106 | return set.size(); |
| 107 | } |
| 108 | |
| 109 | public T valueAt(int intIndex, int valueIndex) { |
| 110 | return mData.valueAt(intIndex).valueAt(valueIndex); |
| 111 | } |
| 112 | } |