From 6c3d3b1c9f9133852042d1c48e9b06788849e22e Mon Sep 17 00:00:00 2001 From: des Date: Tue, 14 Jun 2022 09:25:46 +0100 Subject: [PATCH] Refactor to use internal array iterate - closes #1350 --- .../impl/list/mutable/FastList.java | 48 +---- .../impl/utility/ArrayIterate.java | 48 +---- .../internal/InternalArrayIterate.java | 164 ++++++++++++++++++ .../internal/InternalArrayIterateTest.java | 121 +++++++++++++ 4 files changed, 301 insertions(+), 80 deletions(-) create mode 100644 unit-tests/src/test/java/org/eclipse/collections/impl/utility/internal/InternalArrayIterateTest.java diff --git a/eclipse-collections/src/main/java/org/eclipse/collections/impl/list/mutable/FastList.java b/eclipse-collections/src/main/java/org/eclipse/collections/impl/list/mutable/FastList.java index 61e6e9befd..9facb96bbf 100644 --- a/eclipse-collections/src/main/java/org/eclipse/collections/impl/list/mutable/FastList.java +++ b/eclipse-collections/src/main/java/org/eclipse/collections/impl/list/mutable/FastList.java @@ -754,11 +754,7 @@ public FastList collect(Function function) @Override public R collectBoolean(BooleanFunction booleanFunction, R target) { - for (int i = 0; i < this.size; i++) - { - target.add(booleanFunction.booleanValueOf(this.items[i])); - } - return target; + return InternalArrayIterate.collectBoolean(this.items, this.size, booleanFunction, target); } @Override @@ -775,11 +771,7 @@ public R flatCollectBoolean( @Override public R collectByte(ByteFunction byteFunction, R target) { - for (int i = 0; i < this.size; i++) - { - target.add(byteFunction.byteValueOf(this.items[i])); - } - return target; + return InternalArrayIterate.collectByte(this.items, this.size, byteFunction, target); } @Override @@ -796,11 +788,7 @@ public R flatCollectByte( @Override public R collectChar(CharFunction charFunction, R target) { - for (int i = 0; i < this.size; i++) - { - target.add(charFunction.charValueOf(this.items[i])); - } - return target; + return InternalArrayIterate.collectChar(this.items, this.size, charFunction, target); } @Override @@ -817,11 +805,7 @@ public R flatCollectChar( @Override public R collectDouble(DoubleFunction doubleFunction, R target) { - for (int i = 0; i < this.size; i++) - { - target.add(doubleFunction.doubleValueOf(this.items[i])); - } - return target; + return InternalArrayIterate.collectDouble(this.items, this.size, doubleFunction, target); } @Override @@ -838,11 +822,7 @@ public R flatCollectDouble( @Override public R collectFloat(FloatFunction floatFunction, R target) { - for (int i = 0; i < this.size; i++) - { - target.add(floatFunction.floatValueOf(this.items[i])); - } - return target; + return InternalArrayIterate.collectFloat(this.items, this.size, floatFunction, target); } @Override @@ -859,11 +839,7 @@ public R flatCollectFloat( @Override public R collectInt(IntFunction intFunction, R target) { - for (int i = 0; i < this.size; i++) - { - target.add(intFunction.intValueOf(this.items[i])); - } - return target; + return InternalArrayIterate.collectInt(this.items, this.size, intFunction, target); } @Override @@ -880,11 +856,7 @@ public R flatCollectInt( @Override public R collectLong(LongFunction longFunction, R target) { - for (int i = 0; i < this.size; i++) - { - target.add(longFunction.longValueOf(this.items[i])); - } - return target; + return InternalArrayIterate.collectLong(this.items, this.size, longFunction, target); } @Override @@ -901,11 +873,7 @@ public R flatCollectLong( @Override public R collectShort(ShortFunction shortFunction, R target) { - for (int i = 0; i < this.size; i++) - { - target.add(shortFunction.shortValueOf(this.items[i])); - } - return target; + return InternalArrayIterate.collectShort(this.items, this.size, shortFunction, target); } @Override diff --git a/eclipse-collections/src/main/java/org/eclipse/collections/impl/utility/ArrayIterate.java b/eclipse-collections/src/main/java/org/eclipse/collections/impl/utility/ArrayIterate.java index 16b5eb54d7..1b5af32c1b 100644 --- a/eclipse-collections/src/main/java/org/eclipse/collections/impl/utility/ArrayIterate.java +++ b/eclipse-collections/src/main/java/org/eclipse/collections/impl/utility/ArrayIterate.java @@ -464,11 +464,7 @@ public static R collectBoolean( { throw new IllegalArgumentException("Cannot perform a collectBoolean on null"); } - for (T each : objectArray) - { - target.add(booleanFunction.booleanValueOf(each)); - } - return target; + return InternalArrayIterate.collectBoolean(objectArray, objectArray.length, booleanFunction, target); } /** @@ -495,11 +491,7 @@ public static R collectByte( { throw new IllegalArgumentException("Cannot perform a collectByte on null"); } - for (T each : objectArray) - { - target.add(byteFunction.byteValueOf(each)); - } - return target; + return InternalArrayIterate.collectByte(objectArray, objectArray.length, byteFunction, target); } /** @@ -526,11 +518,7 @@ public static R collectChar( { throw new IllegalArgumentException("Cannot perform a collectChar on null"); } - for (T each : objectArray) - { - target.add(charFunction.charValueOf(each)); - } - return target; + return InternalArrayIterate.collectChar(objectArray, objectArray.length, charFunction, target); } /** @@ -557,11 +545,7 @@ public static R collectDouble( { throw new IllegalArgumentException("Cannot perform a collectDouble on null"); } - for (T each : objectArray) - { - target.add(doubleFunction.doubleValueOf(each)); - } - return target; + return InternalArrayIterate.collectDouble(objectArray, objectArray.length, doubleFunction, target); } /** @@ -588,11 +572,7 @@ public static R collectFloat( { throw new IllegalArgumentException("Cannot perform a collectFloat on null"); } - for (T each : objectArray) - { - target.add(floatFunction.floatValueOf(each)); - } - return target; + return InternalArrayIterate.collectFloat(objectArray, objectArray.length, floatFunction, target); } /** @@ -621,11 +601,7 @@ public static R collectInt( { throw new IllegalArgumentException("Cannot perform a collectInt on null"); } - for (T each : objectArray) - { - target.add(intFunction.intValueOf(each)); - } - return target; + return InternalArrayIterate.collectInt(objectArray, objectArray.length, intFunction, target); } /** @@ -654,11 +630,7 @@ public static R collectLong( { throw new IllegalArgumentException("Cannot perform a collectLong on null"); } - for (T each : objectArray) - { - target.add(longFunction.longValueOf(each)); - } - return target; + return InternalArrayIterate.collectLong(objectArray, objectArray.length, longFunction, target); } /** @@ -687,11 +659,7 @@ public static R collectShort( { throw new IllegalArgumentException("Cannot perform a collectShort on null"); } - for (T each : objectArray) - { - target.add(shortFunction.shortValueOf(each)); - } - return target; + return InternalArrayIterate.collectShort(objectArray, objectArray.length, shortFunction, target); } /** diff --git a/eclipse-collections/src/main/java/org/eclipse/collections/impl/utility/internal/InternalArrayIterate.java b/eclipse-collections/src/main/java/org/eclipse/collections/impl/utility/internal/InternalArrayIterate.java index 356f443f11..743fee29d4 100644 --- a/eclipse-collections/src/main/java/org/eclipse/collections/impl/utility/internal/InternalArrayIterate.java +++ b/eclipse-collections/src/main/java/org/eclipse/collections/impl/utility/internal/InternalArrayIterate.java @@ -33,16 +33,28 @@ import org.eclipse.collections.api.block.HashingStrategy; import org.eclipse.collections.api.block.function.Function; import org.eclipse.collections.api.block.function.Function2; +import org.eclipse.collections.api.block.function.primitive.BooleanFunction; +import org.eclipse.collections.api.block.function.primitive.ByteFunction; +import org.eclipse.collections.api.block.function.primitive.CharFunction; import org.eclipse.collections.api.block.function.primitive.DoubleFunction; import org.eclipse.collections.api.block.function.primitive.FloatFunction; import org.eclipse.collections.api.block.function.primitive.IntFunction; import org.eclipse.collections.api.block.function.primitive.LongFunction; import org.eclipse.collections.api.block.function.primitive.ObjectIntToObjectFunction; +import org.eclipse.collections.api.block.function.primitive.ShortFunction; import org.eclipse.collections.api.block.predicate.Predicate; import org.eclipse.collections.api.block.predicate.Predicate2; import org.eclipse.collections.api.block.predicate.primitive.ObjectIntPredicate; import org.eclipse.collections.api.block.procedure.Procedure; import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure; +import org.eclipse.collections.api.collection.primitive.MutableBooleanCollection; +import org.eclipse.collections.api.collection.primitive.MutableByteCollection; +import org.eclipse.collections.api.collection.primitive.MutableCharCollection; +import org.eclipse.collections.api.collection.primitive.MutableDoubleCollection; +import org.eclipse.collections.api.collection.primitive.MutableFloatCollection; +import org.eclipse.collections.api.collection.primitive.MutableIntCollection; +import org.eclipse.collections.api.collection.primitive.MutableLongCollection; +import org.eclipse.collections.api.collection.primitive.MutableShortCollection; import org.eclipse.collections.api.factory.Lists; import org.eclipse.collections.api.factory.Sets; import org.eclipse.collections.api.factory.primitive.ObjectDoubleMaps; @@ -1214,4 +1226,156 @@ public static , T> R rejectWithIndex( } return target; } + + /** + * Adds all array elements to the target MutableBooleanCollection after using the function supplied to convert + * each source element to the appropriate type + * + * @since 12.0 + */ + public static R collectBoolean( + T[] items, + int size, + BooleanFunction booleanFunction, + R target) + { + for (int i = 0; i < size; i++) + { + target.add(booleanFunction.booleanValueOf(items[i])); + } + return target; + } + + /** + * Adds all array elements to the target MutableByteCollection after using the function supplied to convert + * each source element to the appropriate type + * + * @since 12.0 + */ + public static R collectByte( + T[] items, + int size, + ByteFunction byteFunction, + R target) + { + for (int i = 0; i < size; i++) + { + target.add(byteFunction.byteValueOf(items[i])); + } + return target; + } + + /** + * Adds all array elements to the target MutableCharCollection after using the function supplied to convert + * each source element to the appropriate type + * + * @since 12.0 + */ + public static R collectChar( + T[] items, + int size, + CharFunction charFunction, + R target) + { + for (int i = 0; i < size; i++) + { + target.add(charFunction.charValueOf(items[i])); + } + return target; + } + + /** + * Adds all array elements to the target MutableDoubleCollection after using the function supplied to convert + * each source element to the appropriate type + * + * @since 12.0 + */ + public static R collectDouble( + T[] items, + int size, + DoubleFunction doubleFunction, + R target) + { + for (int i = 0; i < size; i++) + { + target.add(doubleFunction.doubleValueOf(items[i])); + } + return target; + } + + /** + * Adds all array elements to the target MutableFloatCollection after using the function supplied to convert + * each source element to the appropriate type + * + * @since 12.0 + */ + public static R collectFloat( + T[] items, + int size, + FloatFunction floatFunction, + R target) + { + for (int i = 0; i < size; i++) + { + target.add(floatFunction.floatValueOf(items[i])); + } + return target; + } + + /** + * Adds all array elements to the target MutableIntCollection after using the function supplied to convert + * each source element to the appropriate type + * + * @since 12.0 + */ + public static R collectInt( + T[] items, + int size, + IntFunction intFunction, + R target) + { + for (int i = 0; i < size; i++) + { + target.add(intFunction.intValueOf(items[i])); + } + return target; + } + + /** + * Adds all array elements to the target MutableLongCollection after using the function supplied to convert + * each source element to the appropriate type + * + * @since 12.0 + */ + public static R collectLong( + T[] items, + int size, + LongFunction longFunction, + R target) + { + for (int i = 0; i < size; i++) + { + target.add(longFunction.longValueOf(items[i])); + } + return target; + } + + /** + * Adds all array elements to the target MutableShortCollection after using the function supplied to convert + * each source element to the appropriate type + * + * @since 12.0 + */ + public static R collectShort( + T[] items, + int size, + ShortFunction shortFunction, + R target) + { + for (int i = 0; i < size; i++) + { + target.add(shortFunction.shortValueOf(items[i])); + } + return target; + } } diff --git a/unit-tests/src/test/java/org/eclipse/collections/impl/utility/internal/InternalArrayIterateTest.java b/unit-tests/src/test/java/org/eclipse/collections/impl/utility/internal/InternalArrayIterateTest.java new file mode 100644 index 0000000000..8aed9db2fa --- /dev/null +++ b/unit-tests/src/test/java/org/eclipse/collections/impl/utility/internal/InternalArrayIterateTest.java @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2022 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. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + */ + +package org.eclipse.collections.impl.utility.internal; + +import org.eclipse.collections.api.list.primitive.MutableBooleanList; +import org.eclipse.collections.api.list.primitive.MutableByteList; +import org.eclipse.collections.api.list.primitive.MutableCharList; +import org.eclipse.collections.api.list.primitive.MutableDoubleList; +import org.eclipse.collections.api.list.primitive.MutableFloatList; +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.impl.factory.primitive.BooleanLists; +import org.eclipse.collections.impl.factory.primitive.ByteLists; +import org.eclipse.collections.impl.factory.primitive.CharLists; +import org.eclipse.collections.impl.factory.primitive.DoubleLists; +import org.eclipse.collections.impl.factory.primitive.FloatLists; +import org.eclipse.collections.impl.factory.primitive.IntLists; +import org.eclipse.collections.impl.factory.primitive.LongLists; +import org.eclipse.collections.impl.factory.primitive.ShortLists; +import org.junit.Assert; +import org.junit.Test; + +public class InternalArrayIterateTest +{ + @Test + public void collectBoolean() + { + Integer[] items = {1, 2, 3}; + + MutableBooleanList result = InternalArrayIterate.collectBoolean( + items, 3, anObject -> anObject % 2 == 0, BooleanLists.mutable.withInitialCapacity(3)); + + Assert.assertEquals(result, BooleanLists.mutable.of(false, true, false)); + } + + @Test + public void collectByte() + { + Integer[] items = {1, 2, 3}; + + MutableByteList result = InternalArrayIterate.collectByte( + items, 3, Integer::byteValue, ByteLists.mutable.withInitialCapacity(3)); + + Assert.assertEquals(result, ByteLists.mutable.of((byte) 1, (byte) 2, (byte) 3)); + } + + @Test + public void collectChar() + { + Character[] items = {'a', 'b', 'c'}; + + MutableCharList result = InternalArrayIterate.collectChar( + items, 3, Character::toUpperCase, CharLists.mutable.withInitialCapacity(3)); + + Assert.assertEquals(result, CharLists.mutable.of('A', 'B', 'C')); + } + + @Test + public void collectDouble() + { + Integer[] items = {1, 2, 3}; + + MutableDoubleList result = InternalArrayIterate.collectDouble( + items, 3, Integer::doubleValue, DoubleLists.mutable.withInitialCapacity(3)); + + Assert.assertEquals(result, DoubleLists.mutable.of(1.0, 2.0, 3.0)); + } + + @Test + public void collectFloat() + { + Integer[] items = {1, 2, 3}; + + MutableFloatList result = InternalArrayIterate.collectFloat( + items, 3, Integer::floatValue, FloatLists.mutable.withInitialCapacity(3)); + + Assert.assertEquals(result, FloatLists.mutable.of(1.0F, 2.0F, 3.0F)); + } + + @Test + public void collectInt() + { + Integer[] items = {1, 2, 3}; + + MutableIntList result = InternalArrayIterate.collectInt( + items, 3, anObject -> anObject + 1, IntLists.mutable.withInitialCapacity(3)); + + Assert.assertEquals(result, IntLists.mutable.of(2, 3, 4)); + } + + @Test + public void collectLong() + { + Integer[] items = {1, 2, 3}; + + MutableLongList result = InternalArrayIterate.collectLong( + items, 3, Integer::longValue, LongLists.mutable.withInitialCapacity(3)); + + Assert.assertEquals(result, LongLists.mutable.of(1L, 2L, 3L)); + } + + @Test + public void collectShort() + { + Integer[] items = {1, 2, 3}; + + MutableShortList result = InternalArrayIterate.collectShort( + items, 3, Integer::shortValue, ShortLists.mutable.withInitialCapacity(3)); + + Assert.assertEquals(result, ShortLists.mutable.of((short) 1, (short) 2, (short) 3)); + } +}