Search in sources :

Example 51 with Sum

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

the class ParallelArrayIterateTest method parallelForEach.

@Test
public void parallelForEach() {
    Sum sum1 = new IntegerSum(0);
    Integer[] array1 = this.createIntegerArray(16);
    ParallelArrayIterate.forEach(array1, new SumProcedure<>(sum1), new SumCombiner<>(sum1), 1, array1.length / 2);
    Assert.assertEquals(16, sum1.getValue());
    Sum sum2 = new IntegerSum(0);
    Integer[] array2 = this.createIntegerArray(7);
    ParallelArrayIterate.forEach(array2, new SumProcedure<>(sum2), new SumCombiner<>(sum2));
    Assert.assertEquals(7, sum2.getValue());
    Sum sum3 = new IntegerSum(0);
    Integer[] array3 = this.createIntegerArray(15);
    ParallelArrayIterate.forEach(array3, new SumProcedure<>(sum3), new SumCombiner<>(sum3), 1, array3.length / 2);
    Assert.assertEquals(15, sum3.getValue());
    Sum sum4 = new IntegerSum(0);
    Integer[] array4 = this.createIntegerArray(35);
    ParallelArrayIterate.forEach(array4, new SumProcedure<>(sum4), new SumCombiner<>(sum4));
    Assert.assertEquals(35, sum4.getValue());
    Sum sum5 = new IntegerSum(0);
    Integer[] array5 = this.createIntegerArray(40);
    ParallelArrayIterate.forEach(array5, new SumProcedure<>(sum5), new SumCombiner<>(sum5), 1, array5.length / 2);
    Assert.assertEquals(40, sum5.getValue());
}
Also used : IntegerSum(org.eclipse.collections.impl.math.IntegerSum) LongSum(org.eclipse.collections.impl.math.LongSum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) Test(org.junit.Test)

Example 52 with Sum

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

the class ImmutableUnifiedSetTest 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) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) BatchIterable(org.eclipse.collections.impl.parallel.BatchIterable) Test(org.junit.Test)

Example 53 with Sum

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

the class ImmutableUnifiedMapWithHashingStrategyTest method batchForEach.

@Test
public void batchForEach() {
    Sum sum = new IntegerSum(0);
    BatchIterable<String> integerBatchIterable = (BatchIterable<String>) this.classUnderTest();
    integerBatchIterable.batchForEach(each -> sum.add(Integer.valueOf(each)), 0, 1);
    Assert.assertEquals(10, sum.getValue());
}
Also used : IntegerSum(org.eclipse.collections.impl.math.IntegerSum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) BatchIterable(org.eclipse.collections.impl.parallel.BatchIterable) Test(org.junit.Test)

Example 54 with Sum

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

the class UnifiedMapWithHashingStrategyTest method batchForEachChains.

private void batchForEachChains(BatchIterable<Integer> batchIterable, int expectedValue) {
    // Testing 1 batch with chains
    Sum sum = new IntegerSum(0);
    // testing getBatchCount returns 1
    int numBatches = batchIterable.getBatchCount(100000);
    for (int i = 0; i < numBatches; ++i) {
        batchIterable.batchForEach(new SumProcedure<>(sum), i, numBatches);
    }
    Assert.assertEquals(1, numBatches);
    Assert.assertEquals(expectedValue, sum.getValue());
    // Testing 3 batches with chains and uneven last batch
    Sum sum2 = new IntegerSum(0);
    for (int i = 0; i < 5; ++i) {
        batchIterable.batchForEach(new SumProcedure<>(sum2), i, 5);
    }
    Assert.assertEquals(expectedValue, sum2.getValue());
}
Also used : IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum)

Example 55 with Sum

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

the class UnifiedMapWithHashingStrategyTest method batchIterable_forEachEntry.

@Test
public void batchIterable_forEachEntry() {
    BatchIterable<Map.Entry<Integer, Integer>> entries = (BatchIterable<Map.Entry<Integer, Integer>>) UnifiedMapWithHashingStrategy.newWithKeysValues(INTEGER_HASHING_STRATEGY, 1, 1, 2, 2, 3, 3, 4, 4).entrySet();
    Sum sum = new IntegerSum(0);
    entries.forEach(new EntrySumProcedure(sum));
    Assert.assertEquals(20, sum.getValue());
}
Also used : ImmutableEntry(org.eclipse.collections.impl.tuple.ImmutableEntry) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) HashMap(java.util.HashMap) MutableMap(org.eclipse.collections.api.map.MutableMap) Map(java.util.Map) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) BatchIterable(org.eclipse.collections.impl.parallel.BatchIterable) 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