2018-10-04 15:11:00 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2018 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
package com.android.launcher3.util;
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
2021-05-14 12:21:30 +02:00
|
|
|
import java.util.Iterator;
|
2018-10-04 15:11:00 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A wrapper over IntArray implementing a growing set of int primitives.
|
2021-05-14 12:21:30 +02:00
|
|
|
* The elements in the array are sorted in ascending order.
|
2018-10-04 15:11:00 -07:00
|
|
|
*/
|
2021-05-14 12:21:30 +02:00
|
|
|
public class IntSet implements Iterable<Integer> {
|
2018-10-04 15:11:00 -07:00
|
|
|
|
|
|
|
|
final IntArray mArray = new IntArray();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Appends the specified value to the set if it does not exist.
|
|
|
|
|
*/
|
|
|
|
|
public void add(int value) {
|
|
|
|
|
int index = Arrays.binarySearch(mArray.mValues, 0, mArray.mSize, value);
|
|
|
|
|
if (index < 0) {
|
|
|
|
|
mArray.add(-index - 1, value);
|
|
|
|
|
}
|
2021-05-18 12:13:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes the specified value from the set if it exist.
|
|
|
|
|
*/
|
|
|
|
|
public void remove(int value) {
|
|
|
|
|
int index = Arrays.binarySearch(mArray.mValues, 0, mArray.mSize, value);
|
|
|
|
|
if (index >= 0) {
|
|
|
|
|
mArray.removeIndex(index);
|
|
|
|
|
}
|
2018-10-04 15:11:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean contains(int value) {
|
|
|
|
|
return Arrays.binarySearch(mArray.mValues, 0, mArray.mSize, value) >= 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isEmpty() {
|
|
|
|
|
return mArray.isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the number of values in this set.
|
|
|
|
|
*/
|
|
|
|
|
public int size() {
|
|
|
|
|
return mArray.size();
|
|
|
|
|
}
|
2018-12-07 11:43:47 -08:00
|
|
|
|
2019-01-03 14:33:29 -08:00
|
|
|
public void clear() {
|
|
|
|
|
mArray.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
|
if (obj == this) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return (obj instanceof IntSet) && ((IntSet) obj).mArray.equals(mArray);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-14 12:21:30 +02:00
|
|
|
/**
|
|
|
|
|
* Returns the wrapped IntArray. The elements in the array are sorted in ascending order.
|
|
|
|
|
*/
|
2018-12-07 11:43:47 -08:00
|
|
|
public IntArray getArray() {
|
|
|
|
|
return mArray;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-03 14:33:29 -08:00
|
|
|
/**
|
|
|
|
|
* Sets this set to be same as {@param other}
|
|
|
|
|
*/
|
|
|
|
|
public void copyFrom(IntSet other) {
|
|
|
|
|
mArray.copyFrom(other.mArray);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-07 11:43:47 -08:00
|
|
|
public static IntSet wrap(IntArray array) {
|
|
|
|
|
IntSet set = new IntSet();
|
|
|
|
|
set.mArray.addAll(array);
|
|
|
|
|
Arrays.sort(set.mArray.mValues, 0, set.mArray.mSize);
|
|
|
|
|
return set;
|
|
|
|
|
}
|
2021-05-14 12:21:30 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an IntSet with the given values.
|
|
|
|
|
*/
|
|
|
|
|
public static IntSet wrap(int... array) {
|
|
|
|
|
return wrap(IntArray.wrap(array));
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 12:18:08 +02:00
|
|
|
/**
|
|
|
|
|
* Returns an IntSet with the given values.
|
|
|
|
|
*/
|
|
|
|
|
public static IntSet wrap(Iterable<Integer> iterable) {
|
|
|
|
|
IntSet set = new IntSet();
|
|
|
|
|
iterable.forEach(set::add);
|
|
|
|
|
return set;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-14 12:21:30 +02:00
|
|
|
@Override
|
|
|
|
|
public Iterator<Integer> iterator() {
|
|
|
|
|
return mArray.iterator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return "IntSet{" + mArray.toConcatString() + '}';
|
|
|
|
|
}
|
2018-10-04 15:11:00 -07:00
|
|
|
}
|