192 lines
6.4 KiB
Java
192 lines
6.4 KiB
Java
package com.google.common.collect;
|
|
|
|
import com.google.common.base.Preconditions;
|
|
import com.google.common.collect.AbstractMultiset;
|
|
import com.google.common.collect.AbstractObjectCountMap;
|
|
import com.google.common.collect.Multiset;
|
|
import com.google.common.primitives.Ints;
|
|
import java.io.InvalidObjectException;
|
|
import java.io.ObjectStreamException;
|
|
import java.io.Serializable;
|
|
import java.util.ConcurrentModificationException;
|
|
import java.util.Iterator;
|
|
import java.util.Set;
|
|
|
|
/* loaded from: classes.dex */
|
|
abstract class AbstractMapBasedMultiset<E> extends AbstractMultiset<E> implements Serializable {
|
|
private static final long serialVersionUID = -2250766705698539974L;
|
|
transient AbstractObjectCountMap<E> backingMap;
|
|
private transient long size;
|
|
|
|
private class MapBasedMultisetIterator implements Iterator<E> {
|
|
final Iterator<Multiset.Entry<E>> a;
|
|
Multiset.Entry<E> b;
|
|
int c = 0;
|
|
boolean d = false;
|
|
|
|
MapBasedMultisetIterator() {
|
|
this.a = AbstractMapBasedMultiset.this.backingMap.d().iterator();
|
|
}
|
|
|
|
@Override // java.util.Iterator
|
|
public boolean hasNext() {
|
|
return this.c > 0 || this.a.hasNext();
|
|
}
|
|
|
|
@Override // java.util.Iterator
|
|
public E next() {
|
|
if (this.c == 0) {
|
|
this.b = this.a.next();
|
|
this.c = this.b.getCount();
|
|
}
|
|
this.c--;
|
|
this.d = true;
|
|
return this.b.a();
|
|
}
|
|
|
|
@Override // java.util.Iterator
|
|
public void remove() {
|
|
CollectPreconditions.a(this.d);
|
|
int count = this.b.getCount();
|
|
if (count <= 0) {
|
|
throw new ConcurrentModificationException();
|
|
}
|
|
if (count == 1) {
|
|
this.a.remove();
|
|
} else {
|
|
((AbstractObjectCountMap.MapEntry) this.b).a(count - 1);
|
|
}
|
|
AbstractMapBasedMultiset.access$010(AbstractMapBasedMultiset.this);
|
|
this.d = false;
|
|
}
|
|
}
|
|
|
|
protected AbstractMapBasedMultiset(AbstractObjectCountMap<E> abstractObjectCountMap) {
|
|
Preconditions.a(abstractObjectCountMap);
|
|
this.backingMap = abstractObjectCountMap;
|
|
this.size = super.size();
|
|
}
|
|
|
|
static /* synthetic */ long access$010(AbstractMapBasedMultiset abstractMapBasedMultiset) {
|
|
long j = abstractMapBasedMultiset.size;
|
|
abstractMapBasedMultiset.size = j - 1;
|
|
return j;
|
|
}
|
|
|
|
private void readObjectNoData() throws ObjectStreamException {
|
|
throw new InvalidObjectException("Stream data required");
|
|
}
|
|
|
|
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
|
|
public int add(E e, int i) {
|
|
if (i == 0) {
|
|
return count(e);
|
|
}
|
|
Preconditions.a(i > 0, "occurrences cannot be negative: %s", i);
|
|
int a = this.backingMap.a(e);
|
|
long j = i;
|
|
long j2 = a + j;
|
|
Preconditions.a(j2 <= 2147483647L, "too many occurrences: %s", j2);
|
|
this.backingMap.a(e, (int) j2);
|
|
this.size += j;
|
|
return a;
|
|
}
|
|
|
|
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection
|
|
public void clear() {
|
|
this.backingMap.a();
|
|
this.size = 0L;
|
|
}
|
|
|
|
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
|
|
public int count(Object obj) {
|
|
return this.backingMap.a(obj);
|
|
}
|
|
|
|
@Override // com.google.common.collect.AbstractMultiset
|
|
Set<E> createElementSet() {
|
|
return this.backingMap.g();
|
|
}
|
|
|
|
@Override // com.google.common.collect.AbstractMultiset
|
|
public Set<Multiset.Entry<E>> createEntrySet() {
|
|
return new AbstractMultiset.EntrySet();
|
|
}
|
|
|
|
@Override // com.google.common.collect.AbstractMultiset
|
|
int distinctElements() {
|
|
return this.backingMap.h();
|
|
}
|
|
|
|
@Override // com.google.common.collect.AbstractMultiset
|
|
Iterator<Multiset.Entry<E>> entryIterator() {
|
|
final Iterator<Multiset.Entry<E>> it = this.backingMap.d().iterator();
|
|
return new Iterator<Multiset.Entry<E>>() { // from class: com.google.common.collect.AbstractMapBasedMultiset.1
|
|
Multiset.Entry<E> a;
|
|
boolean b;
|
|
|
|
@Override // java.util.Iterator
|
|
public boolean hasNext() {
|
|
return it.hasNext();
|
|
}
|
|
|
|
@Override // java.util.Iterator
|
|
public void remove() {
|
|
CollectPreconditions.a(this.b);
|
|
AbstractMapBasedMultiset.this.size -= this.a.getCount();
|
|
it.remove();
|
|
this.b = false;
|
|
this.a = null;
|
|
}
|
|
|
|
@Override // java.util.Iterator
|
|
public Multiset.Entry<E> next() {
|
|
Multiset.Entry<E> entry = (Multiset.Entry) it.next();
|
|
this.a = entry;
|
|
this.b = true;
|
|
return entry;
|
|
}
|
|
};
|
|
}
|
|
|
|
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection, java.lang.Iterable
|
|
public Iterator<E> iterator() {
|
|
return new MapBasedMultisetIterator();
|
|
}
|
|
|
|
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
|
|
public int remove(Object obj, int i) {
|
|
if (i == 0) {
|
|
return count(obj);
|
|
}
|
|
Preconditions.a(i > 0, "occurrences cannot be negative: %s", i);
|
|
int a = this.backingMap.a(obj);
|
|
if (a > i) {
|
|
this.backingMap.a(obj, a - i);
|
|
} else {
|
|
this.backingMap.c(obj);
|
|
i = a;
|
|
}
|
|
this.size -= i;
|
|
return a;
|
|
}
|
|
|
|
void setBackingMap(AbstractObjectCountMap<E> abstractObjectCountMap) {
|
|
this.backingMap = abstractObjectCountMap;
|
|
}
|
|
|
|
@Override // com.google.common.collect.AbstractMultiset, com.google.common.collect.Multiset
|
|
public int setCount(E e, int i) {
|
|
CollectPreconditions.a(i, "count");
|
|
AbstractObjectCountMap<E> abstractObjectCountMap = this.backingMap;
|
|
int c = i == 0 ? abstractObjectCountMap.c(e) : abstractObjectCountMap.a(e, i);
|
|
this.size += i - c;
|
|
return c;
|
|
}
|
|
|
|
@Override // com.google.common.collect.AbstractMultiset, java.util.AbstractCollection, java.util.Collection, com.google.common.collect.Multiset
|
|
public int size() {
|
|
return Ints.b(this.size);
|
|
}
|
|
}
|