• 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 import android.annotation.NonNull;
19 
20 /**
21  * A sparse array of ArraySets, which is suitable to hold userid->packages association.
22  *
23  * @hide
24  */
25 @android.ravenwood.annotation.RavenwoodKeepWholeClass
26 public class SparseSetArray<T> {
27     private final SparseArray<ArraySet<T>> mData;
28 
SparseSetArray()29     public SparseSetArray() {
30         mData = new SparseArray<>();
31     }
32 
33     /**
34      * Copy constructor
35      */
SparseSetArray(@onNull SparseSetArray<T> src)36     public SparseSetArray(@NonNull SparseSetArray<T> src) {
37         final int arraySize = src.size();
38         mData = new SparseArray<>(arraySize);
39         for (int i = 0; i < arraySize; i++) {
40             final int key = src.keyAt(i);
41             final ArraySet<T> set = src.get(key);
42             addAll(key, set);
43         }
44     }
45 
46     /**
47      * Add a value for key n.
48      * @return FALSE when the value already existed for the given key, TRUE otherwise.
49      */
add(int n, T value)50     public boolean add(int n, T value) {
51         ArraySet<T> set = mData.get(n);
52         if (set == null) {
53             set = new ArraySet<>();
54             mData.put(n, set);
55         }
56         if (set.contains(value)) {
57             return false;
58         }
59         set.add(value);
60         return true;
61     }
62 
63     /**
64      * Add a set of values for key n.
65      */
addAll(int n, ArraySet<T> values)66     public void addAll(int n, ArraySet<T> values) {
67         ArraySet<T> set = mData.get(n);
68         if (set == null) {
69             set = new ArraySet<>(values);
70             mData.put(n, set);
71             return;
72         }
73         set.addAll(values);
74     }
75 
76     /**
77      * Removes all mappings from this SparseSetArray.
78      */
clear()79     public void clear() {
80         mData.clear();
81     }
82 
83     /**
84      * @return whether the value exists for the key n.
85      */
contains(int n, T value)86     public boolean contains(int n, T value) {
87         final ArraySet<T> set = mData.get(n);
88         if (set == null) {
89             return false;
90         }
91         return set.contains(value);
92     }
93 
94     /**
95      * @return the set of items of key n
96      */
get(int n)97     public ArraySet<T> get(int n) {
98         return mData.get(n);
99     }
100 
101     /**
102      * Remove a value for key n.
103      * @return TRUE when the value existed for the given key and removed, FALSE otherwise.
104      */
remove(int n, T value)105     public boolean remove(int n, T value) {
106         final ArraySet<T> set = mData.get(n);
107         if (set == null) {
108             return false;
109         }
110         final boolean ret = set.remove(value);
111         if (set.size() == 0) {
112             mData.remove(n);
113         }
114         return ret;
115     }
116 
117     /**
118      * Remove all values for key n.
119      */
remove(int n)120     public void remove(int n) {
121         mData.remove(n);
122     }
123 
size()124     public int size() {
125         return mData.size();
126     }
127 
keyAt(int index)128     public int keyAt(int index) {
129         return mData.keyAt(index);
130     }
131 
sizeAt(int index)132     public int sizeAt(int index) {
133         final ArraySet<T> set = mData.valueAt(index);
134         if (set == null) {
135             return 0;
136         }
137         return set.size();
138     }
139 
valueAt(int intIndex, int valueIndex)140     public T valueAt(int intIndex, int valueIndex) {
141         return mData.valueAt(intIndex).valueAt(valueIndex);
142     }
143 
144     /** @return The set of values for key at position {@code intIndex}. */
valuesAt(int intIndex)145     public ArraySet<T> valuesAt(int intIndex) {
146         return mData.valueAt(intIndex);
147     }
148 }
149