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 com.android.launcher3.util;
17 
18 import java.util.Arrays;
19 import java.util.Iterator;
20 
21 /**
22  * A wrapper over IntArray implementing a growing set of int primitives.
23  * The elements in the array are sorted in ascending order.
24  */
25 public class IntSet implements Iterable<Integer> {
26 
27     final IntArray mArray = new IntArray();
28 
29     /**
30      * Appends the specified value to the set if it does not exist.
31      */
add(int value)32     public void add(int value) {
33         int index = Arrays.binarySearch(mArray.mValues, 0, mArray.mSize, value);
34         if (index < 0) {
35             mArray.add(-index - 1, value);
36         }
37     }
38 
39     /**
40      * Appends the specified IntSet's values to the set if they does not exist, then returns the
41      * original set that now also contains the new values.
42      */
addAll(IntSet other)43     public IntSet addAll(IntSet other) {
44         other.forEach(this::add);
45         return this;
46     }
47 
48     /**
49      * Removes the specified value from the set if it exist.
50      */
remove(int value)51     public void remove(int value) {
52         int index = Arrays.binarySearch(mArray.mValues, 0, mArray.mSize, value);
53         if (index >= 0) {
54             mArray.removeIndex(index);
55         }
56     }
57 
contains(int value)58     public boolean contains(int value) {
59         return Arrays.binarySearch(mArray.mValues, 0, mArray.mSize, value) >= 0;
60     }
61 
isEmpty()62     public boolean isEmpty() {
63         return mArray.isEmpty();
64     }
65 
66     /**
67      * Returns the number of values in this set.
68      */
size()69     public int size() {
70         return mArray.size();
71     }
72 
clear()73     public void clear() {
74         mArray.clear();
75     }
76 
77     @Override
equals(Object obj)78     public boolean equals(Object obj) {
79         if (obj == this) {
80             return true;
81         }
82         return (obj instanceof IntSet) && ((IntSet) obj).mArray.equals(mArray);
83     }
84 
85     /**
86      * Returns the wrapped IntArray. The elements in the array are sorted in ascending order.
87      */
getArray()88     public IntArray getArray() {
89         return mArray;
90     }
91 
92     /**
93      * Sets this set to be same as {@param other}
94      */
copyFrom(IntSet other)95     public void copyFrom(IntSet other) {
96         mArray.copyFrom(other.mArray);
97     }
98 
wrap(IntArray array)99     public static IntSet wrap(IntArray array) {
100         IntSet set = new IntSet();
101         set.mArray.addAll(array);
102         Arrays.sort(set.mArray.mValues, 0, set.mArray.mSize);
103         return set;
104     }
105 
106     /**
107      * Returns an IntSet with the given values.
108      */
wrap(int... array)109     public static IntSet wrap(int... array) {
110         return wrap(IntArray.wrap(array));
111     }
112 
113     /**
114      * Returns an IntSet with the given values.
115      */
wrap(Iterable<Integer> iterable)116     public static IntSet wrap(Iterable<Integer> iterable) {
117         IntSet set = new IntSet();
118         iterable.forEach(set::add);
119         return set;
120     }
121 
122     @Override
iterator()123     public Iterator<Integer> iterator() {
124         return mArray.iterator();
125     }
126 
127     @Override
toString()128     public String toString() {
129         return "IntSet{" + mArray.toConcatString() + '}';
130     }
131 }
132