Search in sources :

Example 26 with Sum

use of org.eclipse.collections.impl.math.Sum in project eclipse-collections by eclipse.

the class LazyIterateTest method selectIterator.

@Test
public void selectIterator() {
    LazyIterable<Integer> select = LazyIterate.select(Interval.oneTo(5), Predicates.lessThan(5));
    Sum sum = new IntegerSum(0);
    for (Integer each : select) {
        sum.add(each);
    }
    Assert.assertEquals(10, sum.getValue().intValue());
}
Also used : IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Test(org.junit.Test)

Example 27 with Sum

use of org.eclipse.collections.impl.math.Sum in project eclipse-collections by eclipse.

the class IterableIterateTest method forEachWith.

@Test
public void forEachWith() {
    Sum result = new IntegerSum(0);
    Iterable<Integer> integers = new IterableAdapter<>(Interval.oneTo(5));
    Iterate.forEachWith(integers, (each, parm) -> result.add(each.intValue() * parm.intValue()), 2);
    Assert.assertEquals(30, result.getValue().intValue());
}
Also used : IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Test(org.junit.Test)

Example 28 with Sum

use of org.eclipse.collections.impl.math.Sum in project eclipse-collections by eclipse.

the class IterableIterateTest method injectIntoWith.

@Test
public void injectIntoWith() {
    Sum result = new IntegerSum(0);
    Iterable<Integer> iterable = new IterableAdapter<>(Interval.oneTo(5));
    Function3<Sum, Integer, Integer, Sum> function = (sum, element, withValue) -> sum.add(element.intValue() * withValue.intValue());
    Sum sumOfDoubledValues = Iterate.injectIntoWith(result, iterable, function, 2);
    Assert.assertEquals(30, sumOfDoubledValues.getValue().intValue());
}
Also used : Iterables.mList(org.eclipse.collections.impl.factory.Iterables.mList) HashingStrategies(org.eclipse.collections.impl.block.factory.HashingStrategies) Verify(org.eclipse.collections.impl.test.Verify) Sum(org.eclipse.collections.impl.math.Sum) MutableList(org.eclipse.collections.api.list.MutableList) FastList(org.eclipse.collections.impl.list.mutable.FastList) ArrayList(java.util.ArrayList) Function3(org.eclipse.collections.api.block.function.Function3) Interval(org.eclipse.collections.impl.list.Interval) LinkedList(java.util.LinkedList) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Predicates2(org.eclipse.collections.impl.block.factory.Predicates2) Iterator(java.util.Iterator) IntegerPredicates(org.eclipse.collections.impl.block.factory.IntegerPredicates) Collection(java.util.Collection) Twin(org.eclipse.collections.api.tuple.Twin) Test(org.junit.Test) Iterate(org.eclipse.collections.impl.utility.Iterate) ListIterate(org.eclipse.collections.impl.utility.ListIterate) Objects(java.util.Objects) List(java.util.List) MaxSizeFunction(org.eclipse.collections.impl.block.function.MaxSizeFunction) ObjectIntProcedure(org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure) MinSizeFunction(org.eclipse.collections.impl.block.function.MinSizeFunction) Iterables.iList(org.eclipse.collections.impl.factory.Iterables.iList) ImmutableList(org.eclipse.collections.api.list.ImmutableList) Lists(org.eclipse.collections.impl.factory.Lists) ObjectIntProcedures(org.eclipse.collections.impl.block.factory.ObjectIntProcedures) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Optional(java.util.Optional) PartitionIterable(org.eclipse.collections.api.partition.PartitionIterable) AddFunction(org.eclipse.collections.impl.block.function.AddFunction) Assert(org.junit.Assert) Collections(java.util.Collections) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Test(org.junit.Test)

Example 29 with Sum

use of org.eclipse.collections.impl.math.Sum in project eclipse-collections by eclipse.

the class ImmutableUnifiedSetWithHashingStrategyTest method batchForEach.

@Test
public void batchForEach() {
    Sum sum = new IntegerSum(0);
    BatchIterable<Integer> integerBatchIterable = (BatchIterable<Integer>) this.newSet(1, 2, 3, 4, 5);
    integerBatchIterable.batchForEach(new SumProcedure<>(sum), 0, 1);
    Assert.assertEquals(15, sum.getValue());
}
Also used : IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) BatchIterable(org.eclipse.collections.impl.parallel.BatchIterable) Test(org.junit.Test)

Example 30 with Sum

use of org.eclipse.collections.impl.math.Sum in project eclipse-collections by eclipse.

the class UnifiedSetTest method batchForEach.

@Test
public void batchForEach() {
    // Testing batch size of 1 to 16 with no chains
    UnifiedSet<Integer> set = UnifiedSet.<Integer>newSet(10).with(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    for (int sectionCount = 1; sectionCount <= 16; ++sectionCount) {
        Sum sum = new IntegerSum(0);
        for (int sectionIndex = 0; sectionIndex < sectionCount; ++sectionIndex) {
            set.batchForEach(new SumProcedure<>(sum), sectionIndex, sectionCount);
        }
        Assert.assertEquals(55, sum.getValue());
    }
    // Testing 1 batch with chains
    Sum sum2 = new IntegerSum(0);
    UnifiedSet<Integer> set2 = UnifiedSet.<Integer>newSet(3).with(COLLISION_1, COLLISION_2, COLLISION_3, 1, 2);
    int numBatches = set2.getBatchCount(100);
    for (int i = 0; i < numBatches; ++i) {
        set2.batchForEach(new SumProcedure<>(sum2), i, numBatches);
    }
    Assert.assertEquals(1, numBatches);
    Assert.assertEquals(54, sum2.getValue());
    // Testing batch size of 3 with chains and uneven last batch
    Sum sum3 = new IntegerSum(0);
    UnifiedSet<Integer> set3 = UnifiedSet.<Integer>newSet(4, 1.0F).with(COLLISION_1, COLLISION_2, 1, 2, 3, 4, 5);
    int numBatches2 = set3.getBatchCount(3);
    for (int i = 0; i < numBatches2; ++i) {
        set3.batchForEach(new SumProcedure<>(sum3), i, numBatches2);
    }
    Assert.assertEquals(32, sum3.getValue());
    // Test batchForEach on empty set, it should simply do nothing and not throw any exceptions
    Sum sum4 = new IntegerSum(0);
    UnifiedSet<Integer> set4 = UnifiedSet.newSet();
    set4.batchForEach(new SumProcedure<>(sum4), 0, set4.getBatchCount(1));
    Assert.assertEquals(0, sum4.getValue());
}
Also used : IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Test(org.junit.Test)

Aggregations

IntegerSum (org.eclipse.collections.impl.math.IntegerSum)88 Sum (org.eclipse.collections.impl.math.Sum)88 Test (org.junit.Test)76 BatchIterable (org.eclipse.collections.impl.parallel.BatchIterable)20 MutableMap (org.eclipse.collections.api.map.MutableMap)17 Map (java.util.Map)16 Interval (org.eclipse.collections.impl.list.Interval)10 FastList (org.eclipse.collections.impl.list.mutable.FastList)10 UnifiedMap (org.eclipse.collections.impl.map.mutable.UnifiedMap)10 Assert (org.junit.Assert)10 ObjectIntProcedure (org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure)9 HashMap (java.util.HashMap)8 ImmutableEntry (org.eclipse.collections.impl.tuple.ImmutableEntry)8 Procedure2 (org.eclipse.collections.api.block.procedure.Procedure2)7 Predicates (org.eclipse.collections.impl.block.factory.Predicates)7 Before (org.junit.Before)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 LazyIterable (org.eclipse.collections.api.LazyIterable)6 Verify (org.eclipse.collections.impl.test.Verify)6 BigInteger (java.math.BigInteger)5