use of org.eclipse.collections.impl.Counter in project eclipse-collections by eclipse.
the class TreeBag method forEach.
@Override
public void forEach(int fromIndex, int toIndex, Procedure<? super T> procedure) {
ListIterate.rangeCheck(fromIndex, toIndex, this.size);
if (fromIndex > toIndex) {
throw new IllegalArgumentException("fromIndex must not be greater than toIndex");
}
Iterator<Map.Entry<T, Counter>> iterator = this.items.entrySet().iterator();
int i = 0;
while (iterator.hasNext() && i < fromIndex) {
Map.Entry<T, Counter> entry = iterator.next();
Counter value = entry.getValue();
int count = value.getCount();
if (i + count < fromIndex) {
i += count;
} else {
for (int j = 0; j < count; j++) {
if (i >= fromIndex && i <= toIndex) {
procedure.value(entry.getKey());
}
i++;
}
}
}
while (iterator.hasNext() && i <= toIndex) {
Map.Entry<T, Counter> entry = iterator.next();
Counter value = entry.getValue();
int count = value.getCount();
for (int j = 0; j < count; j++) {
if (i <= toIndex) {
procedure.value(entry.getKey());
}
i++;
}
}
}
use of org.eclipse.collections.impl.Counter in project eclipse-collections by eclipse.
the class TreeBag method forEachWithIndex.
@Override
public void forEachWithIndex(int fromIndex, int toIndex, ObjectIntProcedure<? super T> objectIntProcedure) {
ListIterate.rangeCheck(fromIndex, toIndex, this.size);
if (fromIndex > toIndex) {
throw new IllegalArgumentException("fromIndex must not be greater than toIndex");
}
Iterator<Map.Entry<T, Counter>> iterator = this.items.entrySet().iterator();
int i = 0;
while (iterator.hasNext() && i < fromIndex) {
Map.Entry<T, Counter> entry = iterator.next();
Counter value = entry.getValue();
int count = value.getCount();
if (i + count < fromIndex) {
i += count;
} else {
for (int j = 0; j < count; j++) {
if (i >= fromIndex && i <= toIndex) {
objectIntProcedure.value(entry.getKey(), i);
}
i++;
}
}
}
while (iterator.hasNext() && i <= toIndex) {
Map.Entry<T, Counter> entry = iterator.next();
Counter value = entry.getValue();
int count = value.getCount();
for (int j = 0; j < count; j++) {
if (i <= toIndex) {
objectIntProcedure.value(entry.getKey(), i);
}
i++;
}
}
}
use of org.eclipse.collections.impl.Counter in project eclipse-collections by eclipse.
the class TreeBag method removeOccurrences.
@Override
public boolean removeOccurrences(Object item, int occurrences) {
if (occurrences < 0) {
throw new IllegalArgumentException("Cannot remove a negative number of occurrences");
}
if (occurrences == 0) {
return false;
}
Counter counter = this.items.get(item);
if (counter == null) {
return false;
}
int startCount = counter.getCount();
if (occurrences >= startCount) {
this.items.remove(item);
this.size -= startCount;
return true;
}
counter.add(occurrences * -1);
this.size -= occurrences;
return true;
}
use of org.eclipse.collections.impl.Counter in project eclipse-collections by eclipse.
the class TreeBag method setOccurrences.
@Override
public boolean setOccurrences(T item, int occurrences) {
if (occurrences < 0) {
throw new IllegalArgumentException("Cannot set a negative number of occurrences");
}
int originalOccurrences = this.occurrencesOf(item);
if (originalOccurrences == occurrences) {
return false;
}
if (occurrences == 0) {
this.items.remove(item);
} else {
this.items.put(item, new Counter(occurrences));
}
this.size -= originalOccurrences - occurrences;
return true;
}
use of org.eclipse.collections.impl.Counter in project eclipse-collections by eclipse.
the class TreeBag method hashCode.
@Override
public int hashCode() {
Counter counter = new Counter();
this.forEachWithOccurrences((each, count) -> counter.add((each == null ? 0 : each.hashCode()) ^ count));
return counter.getCount();
}
Aggregations