Search in sources :

Example 1 with MutableList

use of org.eclipse.collections.api.list.MutableList in project eclipse-collections by eclipse.

the class ParallelIterableTestCase method sumOfFloatConsistentRounding.

@Test
public void sumOfFloatConsistentRounding() {
    FloatFunction<Integer> roundingSensitiveElementFunction = i -> (i <= 99995) ? 1.0e-18f : 1.0f;
    MutableList<Integer> list = Interval.oneTo(100_000).toList().shuffleThis();
    double baseline = this.getExpectedWith(list.toArray(new Integer[] {})).sumOfFloat(roundingSensitiveElementFunction);
    for (Integer batchSize : BATCH_SIZES) {
        this.batchSize = batchSize;
        ParallelIterable<Integer> testCollection = this.newWith(list.toArray(new Integer[] {}));
        Assert.assertEquals("Batch size: " + this.batchSize, baseline, testCollection.sumOfFloat(roundingSensitiveElementFunction), 1.0e-15d);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ParallelSortedSetIterable(org.eclipse.collections.api.set.sorted.ParallelSortedSetIterable) NegativeIntervalFunction(org.eclipse.collections.impl.block.function.NegativeIntervalFunction) CharHashBag(org.eclipse.collections.impl.bag.mutable.primitive.CharHashBag) Procedures2(org.eclipse.collections.impl.block.factory.Procedures2) Function(org.eclipse.collections.api.block.function.Function) Predicate(org.eclipse.collections.api.block.predicate.Predicate) Verify(org.eclipse.collections.impl.test.Verify) CheckedPredicate(org.eclipse.collections.impl.block.predicate.checked.CheckedPredicate) MutableList(org.eclipse.collections.api.list.MutableList) ParallelIterable(org.eclipse.collections.api.ParallelIterable) RichIterable(org.eclipse.collections.api.RichIterable) Function0(org.eclipse.collections.api.block.function.Function0) HashBag(org.eclipse.collections.impl.bag.mutable.HashBag) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Function2(org.eclipse.collections.api.block.function.Function2) CheckedFunction(org.eclipse.collections.impl.block.function.checked.CheckedFunction) After(org.junit.After) Tuples(org.eclipse.collections.impl.tuple.Tuples) ParallelListIterable(org.eclipse.collections.api.list.ParallelListIterable) Interval(org.eclipse.collections.impl.list.Interval) NoSuchElementException(java.util.NoSuchElementException) Pair(org.eclipse.collections.api.tuple.Pair) DoubleFunction(org.eclipse.collections.api.block.function.primitive.DoubleFunction) MutableCollection(org.eclipse.collections.api.collection.MutableCollection) ExecutorService(java.util.concurrent.ExecutorService) Comparators(org.eclipse.collections.impl.block.factory.Comparators) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Before(org.junit.Before) Predicates2(org.eclipse.collections.impl.block.factory.Predicates2) CollectionAddProcedure(org.eclipse.collections.impl.block.procedure.CollectionAddProcedure) IntegerPredicates(org.eclipse.collections.impl.block.factory.IntegerPredicates) CheckedProcedure(org.eclipse.collections.impl.block.procedure.checked.CheckedProcedure) IOException(java.io.IOException) Test(org.junit.Test) PassThruFunction0(org.eclipse.collections.impl.block.function.PassThruFunction0) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) ParallelSetIterable(org.eclipse.collections.api.set.ParallelSetIterable) ImmutableList(org.eclipse.collections.api.list.ImmutableList) Lists(org.eclipse.collections.impl.factory.Lists) Pattern(java.util.regex.Pattern) Assert(org.junit.Assert) FloatFunction(org.eclipse.collections.api.block.function.primitive.FloatFunction) Test(org.junit.Test)

Example 2 with MutableList

use of org.eclipse.collections.api.list.MutableList in project eclipse-collections by eclipse.

the class ParallelIterableTestCase method sumOfDoubleConsistentRounding.

@Test
public void sumOfDoubleConsistentRounding() {
    DoubleFunction<Integer> roundingSensitiveElementFunction = i -> (i <= 99995) ? 1.0e-18d : 1.0d;
    MutableList<Integer> list = Interval.oneTo(100_000).toList().shuffleThis();
    double baseline = this.getExpectedWith(list.toArray(new Integer[] {})).sumOfDouble(roundingSensitiveElementFunction);
    for (Integer batchSize : BATCH_SIZES) {
        this.batchSize = batchSize;
        ParallelIterable<Integer> testCollection = this.newWith(list.toArray(new Integer[] {}));
        Assert.assertEquals("Batch size: " + this.batchSize, baseline, testCollection.sumOfDouble(roundingSensitiveElementFunction), 1.0e-15d);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ParallelSortedSetIterable(org.eclipse.collections.api.set.sorted.ParallelSortedSetIterable) NegativeIntervalFunction(org.eclipse.collections.impl.block.function.NegativeIntervalFunction) CharHashBag(org.eclipse.collections.impl.bag.mutable.primitive.CharHashBag) Procedures2(org.eclipse.collections.impl.block.factory.Procedures2) Function(org.eclipse.collections.api.block.function.Function) Predicate(org.eclipse.collections.api.block.predicate.Predicate) Verify(org.eclipse.collections.impl.test.Verify) CheckedPredicate(org.eclipse.collections.impl.block.predicate.checked.CheckedPredicate) MutableList(org.eclipse.collections.api.list.MutableList) ParallelIterable(org.eclipse.collections.api.ParallelIterable) RichIterable(org.eclipse.collections.api.RichIterable) Function0(org.eclipse.collections.api.block.function.Function0) HashBag(org.eclipse.collections.impl.bag.mutable.HashBag) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Function2(org.eclipse.collections.api.block.function.Function2) CheckedFunction(org.eclipse.collections.impl.block.function.checked.CheckedFunction) After(org.junit.After) Tuples(org.eclipse.collections.impl.tuple.Tuples) ParallelListIterable(org.eclipse.collections.api.list.ParallelListIterable) Interval(org.eclipse.collections.impl.list.Interval) NoSuchElementException(java.util.NoSuchElementException) Pair(org.eclipse.collections.api.tuple.Pair) DoubleFunction(org.eclipse.collections.api.block.function.primitive.DoubleFunction) MutableCollection(org.eclipse.collections.api.collection.MutableCollection) ExecutorService(java.util.concurrent.ExecutorService) Comparators(org.eclipse.collections.impl.block.factory.Comparators) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Before(org.junit.Before) Predicates2(org.eclipse.collections.impl.block.factory.Predicates2) CollectionAddProcedure(org.eclipse.collections.impl.block.procedure.CollectionAddProcedure) IntegerPredicates(org.eclipse.collections.impl.block.factory.IntegerPredicates) CheckedProcedure(org.eclipse.collections.impl.block.procedure.checked.CheckedProcedure) IOException(java.io.IOException) Test(org.junit.Test) PassThruFunction0(org.eclipse.collections.impl.block.function.PassThruFunction0) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) ParallelSetIterable(org.eclipse.collections.api.set.ParallelSetIterable) ImmutableList(org.eclipse.collections.api.list.ImmutableList) Lists(org.eclipse.collections.impl.factory.Lists) Pattern(java.util.regex.Pattern) Assert(org.junit.Assert) FloatFunction(org.eclipse.collections.api.block.function.primitive.FloatFunction) Test(org.junit.Test)

Example 3 with MutableList

use of org.eclipse.collections.api.list.MutableList in project eclipse-collections by eclipse.

the class SerialParallelLazyPerformanceTest method forEach.

private void forEach(FastList<Integer> collection) {
    MutableList<Runnable> runnables = FastList.newList();
    runnables.add(() -> this.basicSerialForEachPerformance(collection, SERIAL_RUN_COUNT));
    int cores = Runtime.getRuntime().availableProcessors();
    ExecutorService service = Executors.newFixedThreadPool(cores);
    runnables.add(() -> {
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicParallelLazyForEachPerformance(collection, "Lambda", item -> map.put(item, Boolean.TRUE), PARALLEL_RUN_COUNT, cores, service);
    });
    runnables.add(() -> {
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicParallelLazyForEachPerformance(collection, "Procedure", (Procedure<Integer>) each -> map.put(each, Boolean.TRUE), PARALLEL_RUN_COUNT, cores, service);
    });
    runnables.add(() -> {
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicJava8ParallelLazyForEachPerformance(collection, "Lambda", item -> map.put(item, Boolean.TRUE), PARALLEL_RUN_COUNT);
    });
    List<Integer> arrayList = new ArrayList<>(collection);
    runnables.add(() -> {
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicJava8ParallelLazyForEachPerformance(arrayList, "Lambda", item -> map.put(item, Boolean.TRUE), PARALLEL_RUN_COUNT);
    });
    runnables.add(() -> {
        MutableMap<Integer, Boolean> map = new ConcurrentHashMap<>();
        this.basicJava8ParallelLazyForEachPerformance(arrayList, "Consumer", each -> map.put(each, Boolean.TRUE), PARALLEL_RUN_COUNT);
    });
    this.shuffleAndRun(runnables);
    service.shutdown();
    try {
        service.awaitTermination(1, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}
Also used : Arrays(java.util.Arrays) ConcurrentHashMap(org.eclipse.collections.impl.map.mutable.ConcurrentHashMap) Function(org.eclipse.collections.api.block.function.Function) Predicate(org.eclipse.collections.api.block.predicate.Predicate) LoggerFactory(org.slf4j.LoggerFactory) Procedure(org.eclipse.collections.api.block.procedure.Procedure) Verify(org.eclipse.collections.impl.test.Verify) MutableList(org.eclipse.collections.api.list.MutableList) NumberFormat(java.text.NumberFormat) FastList(org.eclipse.collections.impl.list.mutable.FastList) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Function0(org.eclipse.collections.api.block.function.Function0) MutableMap(org.eclipse.collections.api.map.MutableMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Interval(org.eclipse.collections.impl.list.Interval) ExecutorService(java.util.concurrent.ExecutorService) RandomStringUtils(org.apache.commons.lang.RandomStringUtils) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Before(org.junit.Before) ParallelTests(org.eclipse.collections.impl.ParallelTests) Logger(org.slf4j.Logger) IntegerPredicates(org.eclipse.collections.impl.block.factory.IntegerPredicates) Collection(java.util.Collection) Set(java.util.Set) Procedures(org.eclipse.collections.impl.block.factory.Procedures) Test(org.junit.Test) Iterate(org.eclipse.collections.impl.utility.Iterate) Category(org.junit.experimental.categories.Category) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) List(java.util.List) IntInterval(org.eclipse.collections.impl.list.primitive.IntInterval) Assert(org.junit.Assert) IntProcedure(org.eclipse.collections.api.block.procedure.primitive.IntProcedure) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) ConcurrentHashMap(org.eclipse.collections.impl.map.mutable.ConcurrentHashMap)

Example 4 with MutableList

use of org.eclipse.collections.api.list.MutableList in project eclipse-collections by eclipse.

the class ParallelIterateTest method sumByBigInteger.

@Test
public void sumByBigInteger() {
    MutableList<BigInteger> list = Interval.oneTo(100000).collect(Object::toString).collect(BigInteger::new).toList().shuffleThis();
    MutableMap<String, BigInteger> sumByCount = ParallelIterate.sumByBigInteger(list, EVEN_OR_ODD_BI, bi -> BigInteger.valueOf(1L));
    Assert.assertEquals(BigInteger.valueOf(50000L), sumByCount.get("Even"));
    Assert.assertEquals(BigInteger.valueOf(50000L), sumByCount.get("Odd"));
    MutableMap<String, BigInteger> sumByValue = ParallelIterate.sumByBigInteger(list, EVEN_OR_ODD_BI, bi -> bi);
    Assert.assertEquals(Iterate.sumByBigInteger(list, EVEN_OR_ODD_BI, bi -> bi), sumByValue);
    MutableMap<Integer, BigInteger> sumByValue2 = ParallelIterate.sumByBigInteger(list, bi -> bi.intValue() % 1000, bi -> bi);
    Assert.assertEquals(Iterate.sumByBigInteger(list, bi -> bi.intValue() % 1000, bi -> bi), sumByValue2);
    MutableList<BigInteger> list2 = Interval.oneTo(UNEVEN_COUNT_FOR_SUMBY).collect(Object::toString).collect(BigInteger::new).toList();
    MutableMap<String, BigInteger> sumByValue3 = ParallelIterate.sumByBigInteger(list2, EVEN_OR_ODD_BI, bi -> bi);
    Assert.assertEquals(Iterate.sumByBigInteger(list2, EVEN_OR_ODD_BI, bi -> bi), sumByValue3);
    MutableMap<Integer, BigInteger> sumByValue4 = ParallelIterate.sumByBigInteger(list2, bi -> bi.intValue() % 1000, bi -> bi);
    Assert.assertEquals(Iterate.sumByBigInteger(list2, bi -> bi.intValue() % 1000, bi -> bi), sumByValue4);
    Interval small = Interval.oneTo(11);
    MutableMap<String, BigInteger> smallSumByCount = ParallelIterate.sumByBigInteger(small, EVEN_OR_ODD, i -> BigInteger.valueOf(1L));
    Assert.assertEquals(new BigInteger("5"), smallSumByCount.get("Even"));
    Assert.assertEquals(new BigInteger("6"), smallSumByCount.get("Odd"));
}
Also used : Arrays(java.util.Arrays) Predicate(org.eclipse.collections.api.block.predicate.Predicate) Multimap(org.eclipse.collections.api.multimap.Multimap) CompositeFastList(org.eclipse.collections.impl.list.mutable.CompositeFastList) HashingStrategies(org.eclipse.collections.impl.block.factory.HashingStrategies) Verify(org.eclipse.collections.impl.test.Verify) StringFunctions(org.eclipse.collections.impl.block.factory.StringFunctions) MutableList(org.eclipse.collections.api.list.MutableList) UnifiedSetWithHashingStrategy(org.eclipse.collections.impl.set.strategy.mutable.UnifiedSetWithHashingStrategy) BigDecimal(java.math.BigDecimal) MutableSet(org.eclipse.collections.api.set.MutableSet) RichIterable(org.eclipse.collections.api.RichIterable) HashBag(org.eclipse.collections.impl.bag.mutable.HashBag) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Functions(org.eclipse.collections.impl.block.factory.Functions) After(org.junit.After) MutableMultimap(org.eclipse.collections.api.multimap.MutableMultimap) Interval(org.eclipse.collections.impl.list.Interval) BigInteger(java.math.BigInteger) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) Collection(java.util.Collection) ObjectDoubleMap(org.eclipse.collections.api.map.primitive.ObjectDoubleMap) Procedures(org.eclipse.collections.impl.block.factory.Procedures) Iterate(org.eclipse.collections.impl.utility.Iterate) Executors(java.util.concurrent.Executors) ListAdapter(org.eclipse.collections.impl.list.mutable.ListAdapter) List(java.util.List) ObjectIntProcedure(org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure) Procedure2(org.eclipse.collections.api.block.procedure.Procedure2) ObjectLongMap(org.eclipse.collections.api.map.primitive.ObjectLongMap) Lists(org.eclipse.collections.impl.factory.Lists) Function(org.eclipse.collections.api.block.function.Function) Bag(org.eclipse.collections.api.bag.Bag) Procedure(org.eclipse.collections.api.block.procedure.Procedure) ArrayListAdapter(org.eclipse.collections.impl.list.mutable.ArrayListAdapter) FastList(org.eclipse.collections.impl.list.mutable.FastList) ArrayList(java.util.ArrayList) LazyIterable(org.eclipse.collections.api.LazyIterable) MutableMap(org.eclipse.collections.api.map.MutableMap) HashBagMultimap(org.eclipse.collections.impl.multimap.bag.HashBagMultimap) SynchronizedPutUnifiedSetMultimap(org.eclipse.collections.impl.multimap.set.SynchronizedPutUnifiedSetMultimap) Function2(org.eclipse.collections.api.block.function.Function2) SynchronizedPutHashBagMultimap(org.eclipse.collections.impl.multimap.bag.SynchronizedPutHashBagMultimap) LinkedList(java.util.LinkedList) ExecutorService(java.util.concurrent.ExecutorService) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Before(org.junit.Before) ArrayIterate(org.eclipse.collections.impl.utility.ArrayIterate) Test(org.junit.Test) LazyIterate(org.eclipse.collections.impl.utility.LazyIterate) ImmutableList(org.eclipse.collections.api.list.ImmutableList) MapIterable(org.eclipse.collections.api.map.MapIterable) Assert(org.junit.Assert) Collections(java.util.Collections) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) Interval(org.eclipse.collections.impl.list.Interval) Test(org.junit.Test)

Example 5 with MutableList

use of org.eclipse.collections.api.list.MutableList in project eclipse-collections by eclipse.

the class FastListTest method testBAOSSize.

@Test
public void testBAOSSize() {
    MutableList<MutableList<Object>> mutableArrayList = FastList.<MutableList<Object>>newList().with(FastList.newList(), FastList.newList(), FastList.newList(), FastList.newList()).with(FastList.newList(), FastList.newList(), FastList.newList(), FastList.newList());
    List<List<Object>> arrayList = new ArrayList<>();
    Interval.oneTo(8).forEach(Procedures.cast(object -> arrayList.add(new ArrayList<>())));
    ByteArrayOutputStream stream2 = SerializeTestHelper.getByteArrayOutputStream(arrayList);
    Assert.assertEquals(194L, stream2.size());
    ByteArrayOutputStream stream1 = SerializeTestHelper.getByteArrayOutputStream(mutableArrayList);
    Assert.assertEquals(182L, stream1.size());
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ArrayAdapter(org.eclipse.collections.impl.list.fixed.ArrayAdapter) ListIterator(java.util.ListIterator) Predicate2(org.eclipse.collections.api.block.predicate.Predicate2) Iterables.mList(org.eclipse.collections.impl.factory.Iterables.mList) Iterables.mSet(org.eclipse.collections.impl.factory.Iterables.mSet) Procedure(org.eclipse.collections.api.block.procedure.Procedure) Verify(org.eclipse.collections.impl.test.Verify) Sum(org.eclipse.collections.impl.math.Sum) MutableList(org.eclipse.collections.api.list.MutableList) ArrayList(java.util.ArrayList) LazyIterable(org.eclipse.collections.api.LazyIterable) MutableSet(org.eclipse.collections.api.set.MutableSet) Function0(org.eclipse.collections.api.block.function.Function0) Tuples(org.eclipse.collections.impl.tuple.Tuples) Interval(org.eclipse.collections.impl.list.Interval) SerializeTestHelper(org.eclipse.collections.impl.test.SerializeTestHelper) WeakReference(java.lang.ref.WeakReference) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Predicates2(org.eclipse.collections.impl.block.factory.Predicates2) CountProcedure(org.eclipse.collections.impl.block.procedure.CountProcedure) Iterator(java.util.Iterator) CollectionAddProcedure(org.eclipse.collections.impl.block.procedure.CollectionAddProcedure) SumProcedure(org.eclipse.collections.impl.math.SumProcedure) Twin(org.eclipse.collections.api.tuple.Twin) Bags(org.eclipse.collections.impl.factory.Bags) Procedures(org.eclipse.collections.impl.block.factory.Procedures) Test(org.junit.Test) PassThruFunction0(org.eclipse.collections.impl.block.function.PassThruFunction0) ListIterate(org.eclipse.collections.impl.utility.ListIterate) List(java.util.List) MaxSizeFunction(org.eclipse.collections.impl.block.function.MaxSizeFunction) LazyIterate(org.eclipse.collections.impl.utility.LazyIterate) MinSizeFunction(org.eclipse.collections.impl.block.function.MinSizeFunction) Iterables.iList(org.eclipse.collections.impl.factory.Iterables.iList) Lists(org.eclipse.collections.impl.factory.Lists) Sets(org.eclipse.collections.impl.factory.Sets) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) AddFunction(org.eclipse.collections.impl.block.function.AddFunction) Assert(org.junit.Assert) Collections(java.util.Collections) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) MutableList(org.eclipse.collections.api.list.MutableList) ArrayList(java.util.ArrayList) Iterables.mList(org.eclipse.collections.impl.factory.Iterables.mList) MutableList(org.eclipse.collections.api.list.MutableList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Iterables.iList(org.eclipse.collections.impl.factory.Iterables.iList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

MutableList (org.eclipse.collections.api.list.MutableList)11 Predicates (org.eclipse.collections.impl.block.factory.Predicates)8 Lists (org.eclipse.collections.impl.factory.Lists)8 Interval (org.eclipse.collections.impl.list.Interval)8 Verify (org.eclipse.collections.impl.test.Verify)8 Assert (org.junit.Assert)8 Test (org.junit.Test)8 Function (org.eclipse.collections.api.block.function.Function)7 Collections (java.util.Collections)6 RichIterable (org.eclipse.collections.api.RichIterable)6 Procedures (org.eclipse.collections.impl.block.factory.Procedures)6 Arrays (java.util.Arrays)5 List (java.util.List)5 NoSuchElementException (java.util.NoSuchElementException)5 ExecutorService (java.util.concurrent.ExecutorService)5 LazyIterable (org.eclipse.collections.api.LazyIterable)5 Predicates2 (org.eclipse.collections.impl.block.factory.Predicates2)5 CollectionAddProcedure (org.eclipse.collections.impl.block.procedure.CollectionAddProcedure)5 ArrayList (java.util.ArrayList)4 Executors (java.util.concurrent.Executors)4