• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SparseSetArray()26     public SparseSetArray() {
27     }
28 
29     /**
30      * Add a value for key n.
31      * @return FALSE when the value already existed for the given key, TRUE otherwise.
32      */
add(int n, T value)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 false;
41         }
42         set.add(value);
43         return true;
44     }
45 
46     /**
47      * Removes all mappings from this SparseSetArray.
48      */
clear()49     public void clear() {
50         mData.clear();
51     }
52 
53     /**
54      * @return whether the value exists for the key n.
55      */
contains(int n, T value)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     /**
65      * @return the set of items of key n
66      */
get(int n)67     public ArraySet<T> get(int n) {
68         return mData.get(n);
69     }
70 
71     /**
72      * Remove a value for key n.
73      * @return TRUE when the value existed for the given key and removed, FALSE otherwise.
74      */
remove(int n, T value)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     /**
88      * Remove all values for key n.
89      */
remove(int n)90     public void remove(int n) {
91         mData.remove(n);
92     }
size()93     public int size() {
94         return mData.size();
95     }
96 
keyAt(int index)97     public int keyAt(int index) {
98         return mData.keyAt(index);
99     }
100 
sizeAt(int index)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 
valueAt(int intIndex, int valueIndex)109     public T valueAt(int intIndex, int valueIndex) {
110         return mData.valueAt(intIndex).valueAt(valueIndex);
111     }
112 }
113