blob: 9f0f24617f5d7715794a542beb8a3f7acf3a8b3e [file] [log] [blame]
Makoto Onukieb898f12018-01-23 15:26:27 -08001/*
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 */
16package android.util;
17
18/**
19 * A sparse array of ArraySets, which is suitable to hold userid->packages association.
20 *
21 * @hide
22 */
23public class SparseSetArray<T> {
24 private final SparseArray<ArraySet<T>> mData = new SparseArray<>();
25
26 public SparseSetArray() {
27 }
28
29 /**
Kweku Adams79cbc342019-12-13 18:03:49 -080030 * Add a value for key n.
31 * @return FALSE when the value already existed for the given key, TRUE otherwise.
Makoto Onukieb898f12018-01-23 15:26:27 -080032 */
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 Adams7d6a31c2019-04-16 17:05:30 -070047 * Removes all mappings from this SparseSetArray.
48 */
49 public void clear() {
50 mData.clear();
51 }
52
53 /**
Kweku Adams79cbc342019-12-13 18:03:49 -080054 * @return whether the value exists for the key n.
Makoto Onukieb898f12018-01-23 15:26:27 -080055 */
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 Adams79cbc342019-12-13 18:03:49 -080065 * @return the set of items of key n
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010066 */
67 public ArraySet<T> get(int n) {
68 return mData.get(n);
69 }
70
71 /**
Kweku Adams79cbc342019-12-13 18:03:49 -080072 * Remove a value for key n.
73 * @return TRUE when the value existed for the given key and removed, FALSE otherwise.
Makoto Onukieb898f12018-01-23 15:26:27 -080074 */
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 Adams79cbc342019-12-13 18:03:49 -080088 * Remove all values for key n.
Makoto Onukieb898f12018-01-23 15:26:27 -080089 */
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}