Search in sources :

Example 16 with Counter

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++;
        }
    }
}
Also used : Counter(org.eclipse.collections.impl.Counter) MutableSortedMap(org.eclipse.collections.api.map.sorted.MutableSortedMap) Map(java.util.Map) TreeSortedMap(org.eclipse.collections.impl.map.sorted.mutable.TreeSortedMap)

Example 17 with Counter

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++;
        }
    }
}
Also used : Counter(org.eclipse.collections.impl.Counter) MutableSortedMap(org.eclipse.collections.api.map.sorted.MutableSortedMap) Map(java.util.Map) TreeSortedMap(org.eclipse.collections.impl.map.sorted.mutable.TreeSortedMap)

Example 18 with Counter

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;
}
Also used : Counter(org.eclipse.collections.impl.Counter)

Example 19 with Counter

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;
}
Also used : Counter(org.eclipse.collections.impl.Counter)

Example 20 with Counter

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();
}
Also used : Counter(org.eclipse.collections.impl.Counter)

Aggregations

Counter (org.eclipse.collections.impl.Counter)25 Test (org.junit.Test)10 MutableList (org.eclipse.collections.api.list.MutableList)6 NoSuchElementException (java.util.NoSuchElementException)5 Function (org.eclipse.collections.api.block.function.Function)5 Predicates (org.eclipse.collections.impl.block.factory.Predicates)5 Procedures (org.eclipse.collections.impl.block.factory.Procedures)5 FastList (org.eclipse.collections.impl.list.mutable.FastList)5 Collections (java.util.Collections)4 Iterator (java.util.Iterator)4 RichIterable (org.eclipse.collections.api.RichIterable)4 TreeBag (org.eclipse.collections.impl.bag.sorted.mutable.TreeBag)4 Lists (org.eclipse.collections.impl.factory.Lists)4 Interval (org.eclipse.collections.impl.list.Interval)4 Assert (org.junit.Assert)4 Map (java.util.Map)3 LazyIterable (org.eclipse.collections.api.LazyIterable)3 MutableSortedBag (org.eclipse.collections.api.bag.sorted.MutableSortedBag)3 MutableSortedMap (org.eclipse.collections.api.map.sorted.MutableSortedMap)3 TreeSortedMap (org.eclipse.collections.impl.map.sorted.mutable.TreeSortedMap)3