/** * Creates a new sequence, which is filled with objects created be the given * {@code supplier}. * * @since 3.3 * * @param <T> the element type of the sequence * @param supplier the {@code Supplier} which creates the elements, the * returned sequence is filled with * @param length the length of the returned sequence * @return a new sequence filled with elements given by the {@code supplier} * @throws NegativeArraySizeException if the given {@code length} is * negative * @throws NullPointerException if the given {@code supplier} is * {@code null} */ public static <T> MSeq<T> of( final Supplier<? extends T> supplier, final int length ) { requireNonNull(supplier); return length == 0 ? empty() : MSeq.<T>ofLength(length).fill(supplier); }
@Test(expectedExceptions = IndexOutOfBoundsException.class) public void mseqSubSeqOutOfBounds1() { MSeq.empty().subSeq(-1); }
@Test(expectedExceptions = IllegalArgumentException.class) public void mseqSubSeqOutOfBounds2() { MSeq.empty().subSeq(1); }
/** * Create a new {@code MSeq} from the given values. * * @param <T> the element type * @param values the array values. * @return a new {@code Meq} with the given values. * @throws NullPointerException if the {@code values} array is {@code null}. */ @SafeVarargs public static <T> MSeq<T> of(final T... values) { return values.length == 0 ? empty() : new ArrayMSeq<>(Array.of(ObjectStore.of(values.clone()))); }
/** * Create a new {@code MSeq} with the given {@code length}. * * @param length the length of the created {@code MSeq}. * @param <T> the element type of the new {@code MSeq}. * @return the new mutable sequence. * @throws NegativeArraySizeException if the given {@code length} is * negative */ public static <T> MSeq<T> ofLength(final int length) { return length == 0 ? empty() : new ArrayMSeq<>(Array.of(ObjectStore.ofLength(length))); }
if (values instanceof ISeq) { final ISeq<T> seq = (ISeq<T>)values; mseq = seq.isEmpty() ? empty() : seq.copy(); } else if (values instanceof MSeq) { final MSeq<T> seq = (MSeq<T>)values; mseq = seq.isEmpty() ? empty() : MSeq.of(seq); } else if (values instanceof Collection) { final Collection<T> collection = (Collection<T>)values; mseq = collection.isEmpty() ? empty() : MSeq.<T>ofLength(collection.size()).setAll(values); } else { ? empty() : new ArrayMSeq<>(Array.of(ObjectStore.of(objects)));
@Test public void mseqSubSeq() { final MSeq<String> empty = MSeq.empty(); Assert.assertEquals(empty.subSeq(0), empty); Assert.assertEquals(empty.subSeq(0, 0), empty); }
@Test public void emptySeqPrepend() { final MSeq<Integer> empty = MSeq.empty(); final MSeq<Integer> seq = MSeq.of(1, 2, 3, 4); final MSeq<Integer> aseq = empty.prepend(seq); Assert.assertEquals(aseq, seq); }
@Test public void emptySeqAppend() { final MSeq<Integer> empty = MSeq.empty(); final MSeq<Integer> seq = MSeq.of(1, 2, 3, 4); final MSeq<Integer> aseq = empty.append(seq); Assert.assertEquals(aseq, seq); }
@Override public ISeq<Phenotype<G, C>> select( final Seq<Phenotype<G, C>> population, final int count, final Optimize opt ) { requireNonNull(population, "Population"); requireNonNull(opt, "Optimization"); if (count < 0) { throw new IllegalArgumentException(format( "Selection count must be greater or equal then zero, but was %d.", count )); } final MSeq<Phenotype<G, C>> selection; if (count > 0 && !population.isEmpty()) { selection = MSeq.ofLength(count); final Random random = RandomRegistry.getRandom(); final int size = population.size(); for (int i = 0; i < count; ++i) { final int pos = random.nextInt(size); selection.set(i, population.get(pos)); } } else { selection = MSeq.empty(); } return selection.toISeq(); }
@Test public void zeroLengthSameAsEmpty() { Assert.assertSame(MSeq.of(), MSeq.empty()); }
@Test public void toISeqEmptyMSeq() { Assert.assertSame(MSeq.empty().toISeq(), ISeq.empty()); }
@Test public void copyEmptyISeq() { Assert.assertSame(ISeq.empty().copy(), MSeq.empty()); }
@Test public void subSeqEmptyMSeq() { Assert.assertSame(MSeq.of(1, 2, 3).subSeq(3), MSeq.empty()); Assert.assertSame(MSeq.of(1, 2, 3).subSeq(3, 3), MSeq.empty()); Assert.assertSame(MSeq.of(1, 2, 3).subSeq(2, 2), MSeq.empty()); Assert.assertSame(MSeq.of(1, 2, 3).subSeq(1, 1), MSeq.empty()); Assert.assertSame(MSeq.of(1, 2, 3).subSeq(0, 0), MSeq.empty()); }