Skip to content

Commit

Permalink
Optimize sumByLong and sumByInt Primitive methods for Bags. Partially…
Browse files Browse the repository at this point in the history
… addresses #448.

Signed-off-by: Sirisha Pratha <sirisha.pratha@bnymellon.com>
  • Loading branch information
prathasirisha committed Sep 28, 2020
1 parent bfc3821 commit d235358
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ public <K, V> ImmutableMap<K, V> aggregateInPlaceBy(
public <V> ImmutableObjectLongMap<V> sumByInt(Function<? super T, ? extends V> groupBy, IntFunction<? super T> function)
{
MutableObjectLongMap<V> result = ObjectLongHashMap.newMap();
return this.injectInto(result, PrimitiveFunctions.sumByIntFunction(groupBy, function)).toImmutable();
this.forEachWithOccurrences((each, occurrences) -> result.addToValue(
groupBy.valueOf(each),
function.intValueOf(each) * (long) occurrences));
return result.toImmutable();
}

@Override
Expand All @@ -78,7 +81,10 @@ public <V> ImmutableObjectDoubleMap<V> sumByFloat(Function<? super T, ? extends
public <V> ImmutableObjectLongMap<V> sumByLong(Function<? super T, ? extends V> groupBy, LongFunction<? super T> function)
{
MutableObjectLongMap<V> result = ObjectLongHashMap.newMap();
return this.injectInto(result, PrimitiveFunctions.sumByLongFunction(groupBy, function)).toImmutable();
this.forEachWithOccurrences((each, occurrences) -> result.addToValue(
groupBy.valueOf(each),
function.longValueOf(each) * (long) occurrences));
return result.toImmutable();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,10 @@ public <K, V> MutableMap<K, V> aggregateInPlaceBy(
public <V> MutableObjectLongMap<V> sumByInt(Function<? super T, ? extends V> groupBy, IntFunction<? super T> function)
{
MutableObjectLongMap<V> result = ObjectLongHashMap.newMap();
return this.injectInto(result, PrimitiveFunctions.sumByIntFunction(groupBy, function));
this.forEachWithOccurrences((each, occurrences) -> result.addToValue(
groupBy.valueOf(each),
function.intValueOf(each) * (long) occurrences));
return result;
}

@Override
Expand All @@ -313,7 +316,10 @@ public <V> MutableObjectDoubleMap<V> sumByFloat(Function<? super T, ? extends V>
public <V> MutableObjectLongMap<V> sumByLong(Function<? super T, ? extends V> groupBy, LongFunction<? super T> function)
{
MutableObjectLongMap<V> result = ObjectLongHashMap.newMap();
return this.injectInto(result, PrimitiveFunctions.sumByLongFunction(groupBy, function));
this.forEachWithOccurrences((each, occurrences) -> result.addToValue(
groupBy.valueOf(each),
function.longValueOf(each) * (long) occurrences));
return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Goldman Sachs and others.
* Copyright (c) 2020 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -68,6 +68,7 @@
import org.eclipse.collections.api.factory.Bags;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.primitive.MutableObjectLongMap;
import org.eclipse.collections.api.multimap.bag.MutableBagMultimap;
import org.eclipse.collections.api.ordered.OrderedIterable;
import org.eclipse.collections.api.partition.bag.PartitionMutableBag;
Expand All @@ -76,6 +77,7 @@
import org.eclipse.collections.api.tuple.primitive.ObjectIntPair;
import org.eclipse.collections.impl.collection.mutable.AbstractMultiReaderMutableCollection;
import org.eclipse.collections.impl.factory.Iterables;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectLongHashMap;
import org.eclipse.collections.impl.utility.LazyIterate;

/**
Expand Down Expand Up @@ -606,6 +608,26 @@ public T detectWithOccurrences(ObjectIntPredicate<? super T> predicate)
}
}

@Override
public <V> MutableObjectLongMap<V> sumByInt(Function<? super T, ? extends V> groupBy, IntFunction<? super T> function)
{
MutableObjectLongMap<V> result = ObjectLongHashMap.newMap();
this.forEachWithOccurrences((each, occurrences) -> result.addToValue(
groupBy.valueOf(each),
function.intValueOf(each) * (long) occurrences));
return result;
}

@Override
public <V> MutableObjectLongMap<V> sumByLong(Function<? super T, ? extends V> groupBy, LongFunction<? super T> function)
{
MutableObjectLongMap<V> result = ObjectLongHashMap.newMap();
this.forEachWithOccurrences((each, occurrences) -> result.addToValue(
groupBy.valueOf(each),
function.longValueOf(each) * (long) occurrences));
return result;
}

@Override
public void forEachWithOccurrences(ObjectIntProcedure<? super T> procedure)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018 Goldman Sachs and others.
* Copyright (c) 2020 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -47,13 +47,15 @@
import org.eclipse.collections.api.factory.Bags;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.primitive.MutableObjectLongMap;
import org.eclipse.collections.api.multimap.bag.MutableBagMultimap;
import org.eclipse.collections.api.ordered.OrderedIterable;
import org.eclipse.collections.api.partition.bag.PartitionMutableBag;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.api.tuple.primitive.ObjectIntPair;
import org.eclipse.collections.impl.collection.mutable.AbstractUnmodifiableMutableCollection;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectLongHashMap;

/**
* An unmodifiable view of a bag.
Expand Down Expand Up @@ -463,4 +465,24 @@ public MutableSet<T> selectUnique()
{
return this.getMutableBag().selectUnique();
}

@Override
public <V> MutableObjectLongMap<V> sumByInt(Function<? super T, ? extends V> groupBy, IntFunction<? super T> function)
{
MutableObjectLongMap<V> result = ObjectLongHashMap.newMap();
this.forEachWithOccurrences((each, occurrences) -> result.addToValue(
groupBy.valueOf(each),
function.intValueOf(each) * (long) occurrences));
return result;
}

@Override
public <V> MutableObjectLongMap<V> sumByLong(Function<? super T, ? extends V> groupBy, LongFunction<? super T> function)
{
MutableObjectLongMap<V> result = ObjectLongHashMap.newMap();
this.forEachWithOccurrences((each, occurrences) -> result.addToValue(
groupBy.valueOf(each),
function.longValueOf(each) * (long) occurrences));
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018 Goldman Sachs and others.
* Copyright (c) 2020 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -45,6 +45,7 @@
import org.eclipse.collections.api.list.primitive.MutableIntList;
import org.eclipse.collections.api.list.primitive.MutableLongList;
import org.eclipse.collections.api.list.primitive.MutableShortList;
import org.eclipse.collections.api.map.primitive.MutableObjectLongMap;
import org.eclipse.collections.api.map.sorted.MutableSortedMap;
import org.eclipse.collections.api.multimap.sortedbag.MutableSortedBagMultimap;
import org.eclipse.collections.api.ordered.OrderedIterable;
Expand All @@ -55,6 +56,7 @@
import org.eclipse.collections.api.tuple.primitive.ObjectIntPair;
import org.eclipse.collections.impl.collection.mutable.AbstractUnmodifiableMutableCollection;
import org.eclipse.collections.impl.collection.mutable.UnmodifiableCollectionSerializationProxy;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectLongHashMap;

/**
* An unmodifiable view of a SortedBag.
Expand Down Expand Up @@ -560,4 +562,22 @@ public MutableSortedSet<T> selectUnique()
{
return this.getSortedBag().selectUnique();
}

@Override
public <V> MutableObjectLongMap<V> sumByInt(Function<? super T, ? extends V> groupBy, IntFunction<? super T> function)
{
MutableObjectLongMap<V> result = ObjectLongHashMap.newMap();
this.forEachWithOccurrences((each, occurrences) -> result.addToValue(groupBy.valueOf(each), function.intValueOf(each) * (long) occurrences));
return result;
}

@Override
public <V> MutableObjectLongMap<V> sumByLong(Function<? super T, ? extends V> groupBy, LongFunction<? super T> function)
{
MutableObjectLongMap<V> result = ObjectLongHashMap.newMap();
this.forEachWithOccurrences((each, occurrences) -> result.addToValue(
groupBy.valueOf(each),
function.longValueOf(each) * (long) occurrences));
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Goldman Sachs and others.
* Copyright (c) 2020 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -998,7 +998,7 @@ default void RichIterable_iterationOrder()
assertEquals(expectedIterationOrder, sumOfLongIterationOrder);

/*
* TODO: Fix sumByPrimitive methods for bags, to only iterate once per item, not per occurrence.
* TODO: Fix sumByDouble and sumByFloat methods for bags, to only iterate once per item, not per occurrence.
MutableCollection<Integer> sumByDoubleIterationOrder1 = this.newMutableForFilter();
MutableCollection<Integer> sumByDoubleIterationOrder2 = this.newMutableForFilter();
this.getInstanceUnderTest().sumByDouble(
Expand Down Expand Up @@ -1026,6 +1026,7 @@ default void RichIterable_iterationOrder()
});
assertEquals(expectedIterationOrder, sumByFloatIterationOrder1);
assertEquals(expectedIterationOrder, sumByFloatIterationOrder2);
*/

MutableCollection<Integer> sumByIntIterationOrder1 = this.newMutableForFilter();
MutableCollection<Integer> sumByIntIterationOrder2 = this.newMutableForFilter();
Expand Down Expand Up @@ -1054,7 +1055,6 @@ default void RichIterable_iterationOrder()
});
assertEquals(expectedIterationOrder, sumByLongIterationOrder1);
assertEquals(expectedIterationOrder, sumByLongIterationOrder2);
*/

MutableCollection<Integer> expectedInjectIntoIterationOrder = this.allowsDuplicates()
? this.newMutableForFilter(4, 4, 4, 4, 3, 3, 3, 2, 2, 1)
Expand Down

0 comments on commit d235358

Please sign in to comment.