Search in sources :

Example 1 with FastList

use of org.eclipse.collections.impl.list.mutable.FastList 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 2 with FastList

use of org.eclipse.collections.impl.list.mutable.FastList in project eclipse-collections by eclipse.

the class ListsTest method assertPresizedMultiReaderListEquals.

private static void assertPresizedMultiReaderListEquals(int initialCapacity, MultiReaderFastList<String> list) {
    try {
        Field delegateField = MultiReaderFastList.class.getDeclaredField("delegate");
        delegateField.setAccessible(true);
        FastList<String> delegate = (FastList<String>) delegateField.get(list);
        Field itemsField = FastList.class.getDeclaredField("items");
        itemsField.setAccessible(true);
        Object[] items = (Object[]) itemsField.get(delegate);
        Assert.assertEquals(initialCapacity, items.length);
    } catch (SecurityException | NoSuchFieldException | IllegalAccessException e) {
        throw new AssertionError(e);
    }
}
Also used : Field(java.lang.reflect.Field) FastList(org.eclipse.collections.impl.list.mutable.FastList) MultiReaderFastList(org.eclipse.collections.impl.list.mutable.MultiReaderFastList)

Example 3 with FastList

use of org.eclipse.collections.impl.list.mutable.FastList in project eclipse-collections by eclipse.

the class DropIterableTest method forEachWithIndex.

@Test
public void forEachWithIndex() {
    Sum sum = new IntegerSum(0);
    FastList<Integer> indices = FastList.newList(5);
    ObjectIntProcedure<Integer> indexRecordingAndSumProcedure = (each, index) -> {
        indices.add(index);
        sum.add(each);
    };
    this.dropIterable.forEachWithIndex(indexRecordingAndSumProcedure);
    Assert.assertEquals(FastList.newListWith(0, 1, 2), indices);
    Assert.assertEquals(12, sum.getValue().intValue());
    indices.clear();
    sum.add(sum.getValue().intValue() * -1);
    this.emptyListDropIterable.forEachWithIndex(indexRecordingAndSumProcedure);
    Assert.assertEquals(0, indices.size());
    indices.clear();
    sum.add(sum.getValue().intValue() * -1);
    this.zeroCountDropIterable.forEachWithIndex(indexRecordingAndSumProcedure);
    Assert.assertEquals(FastList.newListWith(0, 1, 2, 3, 4), indices);
    Assert.assertEquals(15, sum.getValue().intValue());
    indices.clear();
    sum.add(sum.getValue().intValue() * -1);
    this.nearCountDropIterable.forEachWithIndex(indexRecordingAndSumProcedure);
    Assert.assertEquals(FastList.newListWith(0), indices);
    Assert.assertEquals(5, sum.getValue().intValue());
    indices.clear();
    sum.add(sum.getValue().intValue() * -1);
    this.sameCountDropIterable.forEachWithIndex(indexRecordingAndSumProcedure);
    Assert.assertEquals(0, indices.size());
    indices.clear();
    sum.add(sum.getValue().intValue() * -1);
    this.higherCountDropIterable.forEachWithIndex(indexRecordingAndSumProcedure);
    Assert.assertEquals(0, indices.size());
}
Also used : LazyIterable(org.eclipse.collections.api.LazyIterable) ObjectIntProcedure(org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure) SumProcedure(org.eclipse.collections.impl.math.SumProcedure) LazyIterate(org.eclipse.collections.impl.utility.LazyIterate) Procedure2(org.eclipse.collections.api.block.procedure.Procedure2) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Interval(org.eclipse.collections.impl.list.Interval) Test(org.junit.Test) Sum(org.eclipse.collections.impl.math.Sum) Assert(org.junit.Assert) FastList(org.eclipse.collections.impl.list.mutable.FastList) Before(org.junit.Before) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Sum(org.eclipse.collections.impl.math.Sum) Test(org.junit.Test)

Example 4 with FastList

use of org.eclipse.collections.impl.list.mutable.FastList in project eclipse-collections by eclipse.

the class DropWhileIterableTest method forEachWithIndex.

@Test
public void forEachWithIndex() {
    Sum sum = new IntegerSum(0);
    FastList<Integer> indices = FastList.newList(5);
    ObjectIntProcedure<Integer> indexRecordingAndSumProcedure = (each, index) -> {
        indices.add(index);
        sum.add(each);
    };
    this.dropWhileIterable.forEachWithIndex(indexRecordingAndSumProcedure);
    Assert.assertEquals(FastList.newListWith(0, 1, 2), indices);
    Assert.assertEquals(12, sum.getValue().intValue());
    indices.clear();
    sum.add(sum.getValue().intValue() * -1);
    this.emptyListDropWhileIterable.forEachWithIndex(indexRecordingAndSumProcedure);
    Assert.assertEquals(0, indices.size());
    indices.clear();
    sum.add(sum.getValue().intValue() * -1);
    this.alwaysFalseDropWhileIterable.forEachWithIndex(indexRecordingAndSumProcedure);
    Assert.assertEquals(FastList.newListWith(0, 1, 2, 3, 4), indices);
    Assert.assertEquals(15, sum.getValue().intValue());
    indices.clear();
    sum.add(sum.getValue().intValue() * -1);
    this.mostlyFalseDropWhileIterable.forEachWithIndex(indexRecordingAndSumProcedure);
    Assert.assertEquals(FastList.newListWith(0), indices);
    Assert.assertEquals(5, sum.getValue().intValue());
    indices.clear();
    sum.add(sum.getValue().intValue() * -1);
    this.alwaysTrueDropWhileIterable.forEachWithIndex(indexRecordingAndSumProcedure);
    Assert.assertEquals(0, indices.size());
}
Also used : SumProcedure(org.eclipse.collections.impl.math.SumProcedure) Test(org.junit.Test) Sum(org.eclipse.collections.impl.math.Sum) FastList(org.eclipse.collections.impl.list.mutable.FastList) LazyIterable(org.eclipse.collections.api.LazyIterable) ObjectIntProcedure(org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure) LazyIterate(org.eclipse.collections.impl.utility.LazyIterate) Procedure2(org.eclipse.collections.api.block.procedure.Procedure2) IntegerSum(org.eclipse.collections.impl.math.IntegerSum) Interval(org.eclipse.collections.impl.list.Interval) Assert(org.junit.Assert) Predicates(org.eclipse.collections.impl.block.factory.Predicates) Before(org.junit.Before) 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

FastList (org.eclipse.collections.impl.list.mutable.FastList)4 Interval (org.eclipse.collections.impl.list.Interval)3 Assert (org.junit.Assert)3 Before (org.junit.Before)3 Test (org.junit.Test)3 LazyIterable (org.eclipse.collections.api.LazyIterable)2 Procedure2 (org.eclipse.collections.api.block.procedure.Procedure2)2 ObjectIntProcedure (org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure)2 Predicates (org.eclipse.collections.impl.block.factory.Predicates)2 IntegerSum (org.eclipse.collections.impl.math.IntegerSum)2 Sum (org.eclipse.collections.impl.math.Sum)2 SumProcedure (org.eclipse.collections.impl.math.SumProcedure)2 LazyIterate (org.eclipse.collections.impl.utility.LazyIterate)2 Field (java.lang.reflect.Field)1 NumberFormat (java.text.NumberFormat)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 List (java.util.List)1