use of org.eclipse.collections.api.collection.primitive.MutableIntCollection in project eclipse-collections by eclipse.
the class AbstractLazyIterableTestCase method collectIntWithTarget.
@Test
public void collectIntWithTarget() {
MutableIntCollection target = new IntArrayList();
MutableIntCollection result = this.lazyIterable.collectInt(PrimitiveFunctions.unboxIntegerToInt(), target);
Assert.assertEquals(IntArrayList.newListWith(1, 2, 3, 4, 5, 6, 7), result.toList());
Assert.assertSame("Target list sent as parameter not returned", target, result);
}
use of org.eclipse.collections.api.collection.primitive.MutableIntCollection in project eclipse-collections by eclipse.
the class IterateTest method collectIntWithTarget.
@Test
public void collectIntWithTarget() {
this.iterables.each(each -> {
MutableIntCollection expected = new IntArrayList();
MutableIntCollection actual = Iterate.collectInt(each, PrimitiveFunctions.unboxIntegerToInt(), expected);
Assert.assertTrue(actual.containsAll(1, 2, 3, 4, 5));
Assert.assertSame("Target list sent as parameter not returned", expected, actual);
});
Verify.assertThrows(IllegalArgumentException.class, () -> Iterate.collectInt(null, PrimitiveFunctions.unboxIntegerToInt(), new IntArrayList()));
}
use of org.eclipse.collections.api.collection.primitive.MutableIntCollection in project eclipse-collections by eclipse.
the class SortedNaturalOrderTestCase method RichIterable_collectPrimitive.
@Override
@Test
default void RichIterable_collectPrimitive() {
assertEquals(this.getExpectedBoolean(false, false, true, true, false, false), this.newWith(1, 1, 2, 2, 3, 3).collectBoolean(each -> each % 2 == 0));
{
MutableBooleanCollection target = this.newBooleanForTransform();
MutableBooleanCollection result = this.newWith(1, 1, 2, 2, 3, 3).collectBoolean(each -> each % 2 == 0, target);
assertEquals(this.newBooleanForTransform(false, false, true, true, false, false), result);
assertSame(target, result);
}
RichIterable<Integer> iterable = this.newWith(1, 1, 2, 2, 3, 3, 11, 11, 12, 12, 13, 13);
assertEquals(this.getExpectedByte((byte) 1, (byte) 1, (byte) 2, (byte) 2, (byte) 3, (byte) 3, (byte) 1, (byte) 1, (byte) 2, (byte) 2, (byte) 3, (byte) 3), iterable.collectByte(each -> (byte) (each % 10)));
{
MutableByteCollection target = this.newByteForTransform();
MutableByteCollection result = iterable.collectByte(each -> (byte) (each % 10), target);
assertEquals(this.newByteForTransform((byte) 1, (byte) 1, (byte) 2, (byte) 2, (byte) 3, (byte) 3, (byte) 1, (byte) 1, (byte) 2, (byte) 2, (byte) 3, (byte) 3), result);
assertSame(target, result);
}
assertEquals(this.getExpectedChar((char) 1, (char) 1, (char) 2, (char) 2, (char) 3, (char) 3, (char) 1, (char) 1, (char) 2, (char) 2, (char) 3, (char) 3), iterable.collectChar(each -> (char) (each % 10)));
{
MutableCharCollection target = this.newCharForTransform();
MutableCharCollection result = iterable.collectChar(each -> (char) (each % 10), target);
assertEquals(this.newCharForTransform((char) 1, (char) 1, (char) 2, (char) 2, (char) 3, (char) 3, (char) 1, (char) 1, (char) 2, (char) 2, (char) 3, (char) 3), result);
assertSame(target, result);
}
assertEquals(this.getExpectedDouble(1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 1.0, 1.0, 2.0, 2.0, 3.0, 3.0), iterable.collectDouble(each -> (double) (each % 10)));
{
MutableDoubleCollection target = this.newDoubleForTransform();
MutableDoubleCollection result = iterable.collectDouble(each -> (double) (each % 10), target);
assertEquals(this.newDoubleForTransform(1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 1.0, 1.0, 2.0, 2.0, 3.0, 3.0), result);
assertSame(target, result);
}
assertEquals(this.getExpectedFloat(1.0f, 1.0f, 2.0f, 2.0f, 3.0f, 3.0f, 1.0f, 1.0f, 2.0f, 2.0f, 3.0f, 3.0f), iterable.collectFloat(each -> (float) (each % 10)));
{
MutableFloatCollection target = this.newFloatForTransform();
MutableFloatCollection result = iterable.collectFloat(each -> (float) (each % 10), target);
assertEquals(this.newFloatForTransform(1.0f, 1.0f, 2.0f, 2.0f, 3.0f, 3.0f, 1.0f, 1.0f, 2.0f, 2.0f, 3.0f, 3.0f), result);
assertSame(target, result);
}
assertEquals(this.getExpectedInt(1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3), iterable.collectInt(each -> each % 10));
{
MutableIntCollection target = this.newIntForTransform();
MutableIntCollection result = iterable.collectInt(each -> each % 10, target);
assertEquals(this.newIntForTransform(1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3), result);
assertSame(target, result);
}
assertEquals(this.getExpectedLong(1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3), iterable.collectLong(each -> each % 10));
{
MutableLongCollection target = this.newLongForTransform();
MutableLongCollection result = iterable.collectLong(each -> each % 10, target);
assertEquals(this.newLongForTransform(1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3), result);
assertSame(target, result);
}
assertEquals(this.getExpectedShort((short) 1, (short) 1, (short) 2, (short) 2, (short) 3, (short) 3, (short) 1, (short) 1, (short) 2, (short) 2, (short) 3, (short) 3), iterable.collectShort(each -> (short) (each % 10)));
MutableShortCollection target = this.newShortForTransform();
MutableShortCollection result = iterable.collectShort(each -> (short) (each % 10), target);
assertEquals(this.newShortForTransform((short) 1, (short) 1, (short) 2, (short) 2, (short) 3, (short) 3, (short) 1, (short) 1, (short) 2, (short) 2, (short) 3, (short) 3), result);
assertSame(target, result);
}
use of org.eclipse.collections.api.collection.primitive.MutableIntCollection in project eclipse-collections by eclipse.
the class UnmodifiableMutableCollectionTest method delegatingCollectPrimitiveMethods.
@Test
public void delegatingCollectPrimitiveMethods() {
MutableCollection<Integer> mutable = Interval.oneTo(4).toList();
MutableCollection<Integer> unmodifiable = new UnmodifiableMutableCollection<>(mutable);
MutableBooleanCollection expectedBooleans = mutable.collectBoolean(PrimitiveFunctions.integerIsPositive());
Assert.assertEquals(expectedBooleans, unmodifiable.collectBoolean(PrimitiveFunctions.integerIsPositive()));
Assert.assertEquals(BooleanArrayList.newListWith(true, true, true, true), expectedBooleans);
MutableByteCollection expectedBytes = mutable.collectByte(PrimitiveFunctions.unboxIntegerToByte());
Assert.assertEquals(expectedBytes, unmodifiable.collectByte(PrimitiveFunctions.unboxIntegerToByte()));
Assert.assertEquals(ByteArrayList.newListWith((byte) 1, (byte) 2, (byte) 3, (byte) 4), expectedBytes);
MutableCharCollection expectedChars = mutable.collectChar(PrimitiveFunctions.unboxIntegerToChar());
Assert.assertEquals(expectedChars, unmodifiable.collectChar(PrimitiveFunctions.unboxIntegerToChar()));
Assert.assertEquals(CharArrayList.newListWith((char) 1, (char) 2, (char) 3, (char) 4), expectedChars);
MutableDoubleCollection expectedDoubles = mutable.collectDouble(PrimitiveFunctions.unboxIntegerToDouble());
Assert.assertEquals(expectedDoubles, unmodifiable.collectDouble(PrimitiveFunctions.unboxIntegerToDouble()));
Assert.assertEquals(DoubleArrayList.newListWith(1.0d, 2.0d, 3.0d, 4.0d), expectedDoubles);
MutableFloatCollection expectedFloats = mutable.collectFloat(PrimitiveFunctions.unboxIntegerToFloat());
Assert.assertEquals(expectedFloats, unmodifiable.collectFloat(PrimitiveFunctions.unboxIntegerToFloat()));
Assert.assertEquals(FloatArrayList.newListWith(1.0f, 2.0f, 3.0f, 4.0f), expectedFloats);
MutableIntCollection expectedInts = mutable.collectInt(PrimitiveFunctions.unboxIntegerToInt());
Assert.assertEquals(expectedInts, unmodifiable.collectInt(PrimitiveFunctions.unboxIntegerToInt()));
Assert.assertEquals(IntArrayList.newListWith(1, 2, 3, 4), expectedInts);
MutableLongCollection expectedLongs = mutable.collectLong(PrimitiveFunctions.unboxIntegerToLong());
Assert.assertEquals(expectedLongs, unmodifiable.collectLong(PrimitiveFunctions.unboxIntegerToLong()));
Assert.assertEquals(LongArrayList.newListWith(1L, 2L, 3L, 4L), expectedLongs);
MutableShortCollection expectedShorts = mutable.collectShort(PrimitiveFunctions.unboxIntegerToShort());
Assert.assertEquals(expectedShorts, unmodifiable.collectShort(PrimitiveFunctions.unboxIntegerToShort()));
Assert.assertEquals(ShortArrayList.newListWith((short) 1, (short) 2, (short) 3, (short) 4), expectedShorts);
}
use of org.eclipse.collections.api.collection.primitive.MutableIntCollection in project narchy by automenta.
the class GraphMeter method multicast.
// --------------------------------------------------------------------
/**
* Performs anti-entropy epidemic multicasting from node 0.
* As a result the number of nodes that have been reached in cycle i
* is put into <code>b[i]</code>.
* The number of cycles performed is determined by <code>b.length</code>.
* In each cycle each node contacts a random neighbour and exchanges
* information. The simulation is generational: when a node contacts a neighbor
* in cycle i, it sees their state as in cycle i-1, besides, all nodes update
* their state at the same time point, synchronously.
*/
public static void multicast(Graph g, int[] b, Random r) {
int[] c1 = new int[g.size()];
int[] c2 = new int[g.size()];
for (int i = 0; i < c1.length; ++i) c2[i] = c1[i] = WHITE;
c2[0] = c1[0] = BLACK;
int k = 0;
for (int black = 1; k < b.length || black < g.size(); ++k) {
for (int i = 0; i < c2.length; ++i) {
MutableIntCollection neighbours = g.neighbors(i);
IntIterator it = neighbours.intIterator();
for (int j = r.nextInt(neighbours.size()); j > 0; --j) it.next();
int randn = it.next();
// push pull exchane with random neighbour
if (// c2[i] is black too
c1[i] == BLACK) {
if (c2[randn] == WHITE)
++black;
c2[randn] = BLACK;
} else if (c1[randn] == BLACK) {
if (c2[i] == WHITE)
++black;
c2[i] = BLACK;
}
}
System.arraycopy(c2, 0, c1, 0, c1.length);
b[k] = black;
}
for (; k < b.length; ++k) b[k] = g.size();
}
Aggregations