what-the-bank/sources/com/google/common/collect/ImmutableSortedMultiset.java

418 lines
18 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package com.google.common.collect;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.Multiset;
import com.google.common.math.IntMath;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
/* loaded from: classes2.dex */
public abstract class ImmutableSortedMultiset<E> extends ImmutableSortedMultisetFauxverideShim<E> implements SortedMultiset<E> {
transient ImmutableSortedMultiset<E> descendingMultiset;
@Override // com.google.common.collect.ImmutableMultiset, com.google.common.collect.Multiset
public abstract ImmutableSortedSet<E> elementSet();
public abstract ImmutableSortedMultiset<E> headMultiset(E e, BoundType boundType);
public abstract ImmutableSortedMultiset<E> tailMultiset(E e, BoundType boundType);
/* JADX WARN: Multi-variable type inference failed */
public /* bridge */ /* synthetic */ SortedMultiset headMultiset(Object obj, BoundType boundType) {
return headMultiset((ImmutableSortedMultiset<E>) obj, boundType);
}
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.collect.SortedMultiset
public /* bridge */ /* synthetic */ SortedMultiset subMultiset(Object obj, BoundType boundType, Object obj2, BoundType boundType2) {
return subMultiset((BoundType) obj, boundType, (BoundType) obj2, boundType2);
}
/* JADX WARN: Multi-variable type inference failed */
public /* bridge */ /* synthetic */ SortedMultiset tailMultiset(Object obj, BoundType boundType) {
return tailMultiset((ImmutableSortedMultiset<E>) obj, boundType);
}
public static <E> ImmutableSortedMultiset<E> of() {
return (ImmutableSortedMultiset<E>) RegularImmutableSortedMultiset.NATURAL_EMPTY_MULTISET;
}
/* JADX WARN: Incorrect types in method signature: <E::Ljava/lang/Comparable<-TE;>;>(TE;)Lcom/google/common/collect/ImmutableSortedMultiset<TE;>; */
public static ImmutableSortedMultiset of(Comparable comparable) {
return new RegularImmutableSortedMultiset((RegularImmutableSortedSet) ImmutableSortedSet.of(comparable), new long[]{0, 1}, 0, 1);
}
/* JADX WARN: Incorrect types in method signature: <E::Ljava/lang/Comparable<-TE;>;>(TE;TE;)Lcom/google/common/collect/ImmutableSortedMultiset<TE;>; */
public static ImmutableSortedMultiset of(Comparable comparable, Comparable comparable2) {
return copyOf(Ordering.natural(), Arrays.asList(comparable, comparable2));
}
/* JADX WARN: Incorrect types in method signature: <E::Ljava/lang/Comparable<-TE;>;>(TE;TE;TE;)Lcom/google/common/collect/ImmutableSortedMultiset<TE;>; */
public static ImmutableSortedMultiset of(Comparable comparable, Comparable comparable2, Comparable comparable3) {
return copyOf(Ordering.natural(), Arrays.asList(comparable, comparable2, comparable3));
}
/* JADX WARN: Incorrect types in method signature: <E::Ljava/lang/Comparable<-TE;>;>(TE;TE;TE;TE;)Lcom/google/common/collect/ImmutableSortedMultiset<TE;>; */
public static ImmutableSortedMultiset of(Comparable comparable, Comparable comparable2, Comparable comparable3, Comparable comparable4) {
return copyOf(Ordering.natural(), Arrays.asList(comparable, comparable2, comparable3, comparable4));
}
/* JADX WARN: Incorrect types in method signature: <E::Ljava/lang/Comparable<-TE;>;>(TE;TE;TE;TE;TE;)Lcom/google/common/collect/ImmutableSortedMultiset<TE;>; */
public static ImmutableSortedMultiset of(Comparable comparable, Comparable comparable2, Comparable comparable3, Comparable comparable4, Comparable comparable5) {
return copyOf(Ordering.natural(), Arrays.asList(comparable, comparable2, comparable3, comparable4, comparable5));
}
/* JADX WARN: Incorrect types in method signature: <E::Ljava/lang/Comparable<-TE;>;>(TE;TE;TE;TE;TE;TE;[TE;)Lcom/google/common/collect/ImmutableSortedMultiset<TE;>; */
public static ImmutableSortedMultiset of(Comparable comparable, Comparable comparable2, Comparable comparable3, Comparable comparable4, Comparable comparable5, Comparable comparable6, Comparable... comparableArr) {
ArrayList newArrayListWithCapacity = Lists.newArrayListWithCapacity(comparableArr.length + 6);
Collections.addAll(newArrayListWithCapacity, comparable, comparable2, comparable3, comparable4, comparable5, comparable6);
Collections.addAll(newArrayListWithCapacity, comparableArr);
return copyOf(Ordering.natural(), newArrayListWithCapacity);
}
/* JADX WARN: Incorrect types in method signature: <E::Ljava/lang/Comparable<-TE;>;>([TE;)Lcom/google/common/collect/ImmutableSortedMultiset<TE;>; */
public static ImmutableSortedMultiset copyOf(Comparable[] comparableArr) {
return copyOf(Ordering.natural(), Arrays.asList(comparableArr));
}
public static <E> ImmutableSortedMultiset<E> copyOf(Iterable<? extends E> iterable) {
return copyOf(Ordering.natural(), iterable);
}
public static <E> ImmutableSortedMultiset<E> copyOf(Iterator<? extends E> it) {
return copyOf(Ordering.natural(), it);
}
public static <E> ImmutableSortedMultiset<E> copyOf(Comparator<? super E> comparator, Iterator<? extends E> it) {
Preconditions.checkNotNull(comparator);
return new Builder(comparator).addAll((Iterator) it).build();
}
public static <E> ImmutableSortedMultiset<E> copyOf(Comparator<? super E> comparator, Iterable<? extends E> iterable) {
if (iterable instanceof ImmutableSortedMultiset) {
ImmutableSortedMultiset<E> immutableSortedMultiset = (ImmutableSortedMultiset) iterable;
if (comparator.equals(immutableSortedMultiset.comparator())) {
return immutableSortedMultiset.isPartialView() ? copyOfSortedEntries(comparator, immutableSortedMultiset.entrySet().asList()) : immutableSortedMultiset;
}
}
return new Builder(comparator).addAll((Iterable) iterable).build();
}
public static <E> ImmutableSortedMultiset<E> copyOfSorted(SortedMultiset<E> sortedMultiset) {
return copyOfSortedEntries(sortedMultiset.comparator(), Lists.newArrayList(sortedMultiset.entrySet()));
}
private static <E> ImmutableSortedMultiset<E> copyOfSortedEntries(Comparator<? super E> comparator, Collection<Multiset.Entry<E>> collection) {
if (collection.isEmpty()) {
return emptyMultiset(comparator);
}
ImmutableList.Builder builder = new ImmutableList.Builder(collection.size());
long[] jArr = new long[collection.size() + 1];
Iterator<Multiset.Entry<E>> it = collection.iterator();
int i = 0;
while (it.hasNext()) {
builder.add((ImmutableList.Builder) it.next().getElement());
int i2 = i + 1;
jArr[i2] = jArr[i] + r5.getCount();
i = i2;
}
return new RegularImmutableSortedMultiset(new RegularImmutableSortedSet(builder.build(), comparator), jArr, 0, collection.size());
}
/* JADX INFO: Access modifiers changed from: package-private */
public static <E> ImmutableSortedMultiset<E> emptyMultiset(Comparator<? super E> comparator) {
if (Ordering.natural().equals(comparator)) {
return (ImmutableSortedMultiset<E>) RegularImmutableSortedMultiset.NATURAL_EMPTY_MULTISET;
}
return new RegularImmutableSortedMultiset(comparator);
}
@Override // com.google.common.collect.SortedMultiset, com.google.common.collect.SortedIterable
public final Comparator<? super E> comparator() {
return elementSet().comparator();
}
@Override // com.google.common.collect.SortedMultiset
public ImmutableSortedMultiset<E> descendingMultiset() {
ImmutableSortedMultiset<E> immutableSortedMultiset = this.descendingMultiset;
if (immutableSortedMultiset == null) {
if (isEmpty()) {
immutableSortedMultiset = emptyMultiset(Ordering.from(comparator()).reverse());
} else {
immutableSortedMultiset = new DescendingImmutableSortedMultiset<>(this);
}
this.descendingMultiset = immutableSortedMultiset;
}
return immutableSortedMultiset;
}
@Override // com.google.common.collect.SortedMultiset
@Deprecated
public final Multiset.Entry<E> pollFirstEntry() {
throw new UnsupportedOperationException();
}
@Override // com.google.common.collect.SortedMultiset
@Deprecated
public final Multiset.Entry<E> pollLastEntry() {
throw new UnsupportedOperationException();
}
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.collect.SortedMultiset
public ImmutableSortedMultiset<E> subMultiset(E e, BoundType boundType, E e2, BoundType boundType2) {
Preconditions.checkArgument(comparator().compare(e, e2) <= 0, "Expected lowerBound <= upperBound but %s > %s", e, e2);
return tailMultiset((ImmutableSortedMultiset<E>) e, boundType).headMultiset((ImmutableSortedMultiset<E>) e2, boundType2);
}
public static <E> Builder<E> orderedBy(Comparator<E> comparator) {
return new Builder<>(comparator);
}
public static <E extends Comparable<?>> Builder<E> reverseOrder() {
return new Builder<>(Ordering.natural().reverse());
}
public static <E extends Comparable<?>> Builder<E> naturalOrder() {
return new Builder<>(Ordering.natural());
}
/* loaded from: classes2.dex */
public static class Builder<E> extends ImmutableMultiset.Builder<E> {
private final Comparator<? super E> comparator;
private int[] counts;
E[] elements;
private boolean forceCopyElements;
private int length;
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.collect.ImmutableMultiset.Builder, com.google.common.collect.ImmutableCollection.Builder
public /* bridge */ /* synthetic */ ImmutableCollection.Builder add(Object obj) {
return add((Builder<E>) obj);
}
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.collect.ImmutableMultiset.Builder, com.google.common.collect.ImmutableCollection.Builder
public /* bridge */ /* synthetic */ ImmutableMultiset.Builder add(Object obj) {
return add((Builder<E>) obj);
}
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.collect.ImmutableMultiset.Builder
public /* bridge */ /* synthetic */ ImmutableMultiset.Builder addCopies(Object obj, int i) {
return addCopies((Builder<E>) obj, i);
}
/* JADX WARN: Multi-variable type inference failed */
@Override // com.google.common.collect.ImmutableMultiset.Builder
public /* bridge */ /* synthetic */ ImmutableMultiset.Builder setCount(Object obj, int i) {
return setCount((Builder<E>) obj, i);
}
public Builder(Comparator<? super E> comparator) {
super(true);
this.comparator = (Comparator) Preconditions.checkNotNull(comparator);
this.elements = (E[]) new Object[4];
this.counts = new int[4];
}
private void maintenance() {
int i = this.length;
E[] eArr = this.elements;
if (i == eArr.length) {
dedupAndCoalesce(true);
} else if (this.forceCopyElements) {
this.elements = (E[]) Arrays.copyOf(eArr, eArr.length);
}
this.forceCopyElements = false;
}
private void dedupAndCoalesce(boolean z) {
int i = this.length;
if (i == 0) {
return;
}
Object[] objArr = (E[]) Arrays.copyOf(this.elements, i);
Arrays.sort(objArr, this.comparator);
int i2 = 1;
for (int i3 = 1; i3 < objArr.length; i3++) {
if (this.comparator.compare((Object) objArr[i2 - 1], (Object) objArr[i3]) < 0) {
objArr[i2] = objArr[i3];
i2++;
}
}
Arrays.fill(objArr, i2, this.length, (Object) null);
if (z) {
int i4 = this.length;
if ((i2 << 2) > i4 * 3) {
objArr = (E[]) Arrays.copyOf(objArr, IntMath.saturatedAdd(i4, (i4 / 2) + 1));
}
}
int[] iArr = new int[objArr.length];
for (int i5 = 0; i5 < this.length; i5++) {
int binarySearch = Arrays.binarySearch(objArr, 0, i2, this.elements[i5], this.comparator);
int i6 = this.counts[i5];
if (i6 >= 0) {
iArr[binarySearch] = iArr[binarySearch] + i6;
} else {
iArr[binarySearch] = ~i6;
}
}
this.elements = (E[]) objArr;
this.counts = iArr;
this.length = i2;
}
@Override // com.google.common.collect.ImmutableMultiset.Builder, com.google.common.collect.ImmutableCollection.Builder
public Builder<E> add(E e) {
return addCopies((Builder<E>) e, 1);
}
@Override // com.google.common.collect.ImmutableMultiset.Builder, com.google.common.collect.ImmutableCollection.Builder
public Builder<E> add(E... eArr) {
for (E e : eArr) {
add((Builder<E>) e);
}
return this;
}
@Override // com.google.common.collect.ImmutableMultiset.Builder
public Builder<E> addCopies(E e, int i) {
Preconditions.checkNotNull(e);
CollectPreconditions.checkNonnegative(i, "occurrences");
if (i == 0) {
return this;
}
maintenance();
E[] eArr = this.elements;
int i2 = this.length;
eArr[i2] = e;
this.counts[i2] = i;
this.length = i2 + 1;
return this;
}
@Override // com.google.common.collect.ImmutableMultiset.Builder
public Builder<E> setCount(E e, int i) {
Preconditions.checkNotNull(e);
CollectPreconditions.checkNonnegative(i, "count");
maintenance();
E[] eArr = this.elements;
int i2 = this.length;
eArr[i2] = e;
this.counts[i2] = ~i;
this.length = i2 + 1;
return this;
}
@Override // com.google.common.collect.ImmutableMultiset.Builder, com.google.common.collect.ImmutableCollection.Builder
public Builder<E> addAll(Iterable<? extends E> iterable) {
if (iterable instanceof Multiset) {
for (Multiset.Entry<E> entry : ((Multiset) iterable).entrySet()) {
addCopies((Builder<E>) entry.getElement(), entry.getCount());
}
} else {
Iterator<? extends E> it = iterable.iterator();
while (it.hasNext()) {
add((Builder<E>) it.next());
}
}
return this;
}
@Override // com.google.common.collect.ImmutableMultiset.Builder, com.google.common.collect.ImmutableCollection.Builder
public Builder<E> addAll(Iterator<? extends E> it) {
while (it.hasNext()) {
add((Builder<E>) it.next());
}
return this;
}
private void dedupAndCoalesceAndDeleteEmpty() {
dedupAndCoalesce(false);
int i = 0;
int i2 = 0;
while (true) {
int i3 = this.length;
if (i < i3) {
int[] iArr = this.counts;
int i4 = iArr[i];
if (i4 > 0) {
E[] eArr = this.elements;
eArr[i2] = eArr[i];
iArr[i2] = i4;
i2++;
}
i++;
} else {
Arrays.fill(this.elements, i2, i3, (Object) null);
Arrays.fill(this.counts, i2, this.length, 0);
this.length = i2;
return;
}
}
}
@Override // com.google.common.collect.ImmutableMultiset.Builder, com.google.common.collect.ImmutableCollection.Builder
public ImmutableSortedMultiset<E> build() {
dedupAndCoalesceAndDeleteEmpty();
int i = this.length;
if (i == 0) {
return ImmutableSortedMultiset.emptyMultiset(this.comparator);
}
RegularImmutableSortedSet regularImmutableSortedSet = (RegularImmutableSortedSet) ImmutableSortedSet.construct(this.comparator, i, this.elements);
long[] jArr = new long[this.length + 1];
int i2 = 0;
while (i2 < this.length) {
int i3 = i2 + 1;
jArr[i3] = jArr[i2] + this.counts[i2];
i2 = i3;
}
this.forceCopyElements = true;
return new RegularImmutableSortedMultiset(regularImmutableSortedSet, jArr, 0, this.length);
}
}
/* loaded from: classes2.dex */
static final class SerializedForm<E> implements Serializable {
final Comparator<? super E> comparator;
final int[] counts;
final E[] elements;
SerializedForm(SortedMultiset<E> sortedMultiset) {
this.comparator = sortedMultiset.comparator();
int size = sortedMultiset.entrySet().size();
this.elements = (E[]) new Object[size];
this.counts = new int[size];
int i = 0;
for (Multiset.Entry<E> entry : sortedMultiset.entrySet()) {
this.elements[i] = entry.getElement();
this.counts[i] = entry.getCount();
i++;
}
}
final Object readResolve() {
int length = this.elements.length;
Builder builder = new Builder(this.comparator);
for (int i = 0; i < length; i++) {
builder.addCopies((Builder) this.elements[i], this.counts[i]);
}
return builder.build();
}
}
@Override // com.google.common.collect.ImmutableMultiset, com.google.common.collect.ImmutableCollection
Object writeReplace() {
return new SerializedForm(this);
}
}