@Override protected int crossover(final MSeq<G> that, final MSeq<G> other) { assert that.length() == other.length(); final int n = min(that.length(), other.length()); final int k = min(n, _n); final Random random = RandomRegistry.getRandom(); final int[] points = k > 0 ? comb.subset(n, k, random) : new int[0]; crossover(that, other, points); return 2; }
/** * Reverses the order of the elements this sequence (in place). * * @return this sequence with reverse order or the elements */ public default MSeq<T> reverse() { for (int i = 0, j = length() - 1; i < j; ++i, --j) { swap(i, j); } return this; }
/** * Sorts this sequence according to the natural order of the elements. * * @throws ClassCastException if the sequence contains elements that are not * <i>mutually comparable</i> using the specified comparator * @return {@code this} sequence */ public default MSeq<T> sort() { return sort(0, length(), null); }
/** * Fills the sequence with values of the given iterator. * * @param it the iterator of the values to fill this sequence. * @return {@code this} sequence. */ public default MSeq<T> setAll(final Iterator<? extends T> it) { for (int i = 0, n = length(); i < n && it.hasNext(); ++i) { set(i, it.next()); } return this; }
/** * Sorts this sequence according to the natural order of the elements. * * @param start the start index where to start sorting (inclusively) * @throws ClassCastException if the sequence contains elements that are not * <i>mutually comparable</i> using the specified comparator * @return {@code this} sequence */ public default MSeq<T> sort(final int start) { return sort(start, length(), null); }
public static <T> MSeq<T> shuffle(final MSeq<T> array, final Random random) { for (int j = array.length() - 1; j > 0; --j) { array.swap(j, random.nextInt(j + 1)); } return array; }
@Test public void size() { final MSeq<Integer> proxy = MSeq.ofLength(1000); final List<Integer> list = new SeqList<>(proxy); Assert.assertEquals(list.size(), proxy.length()); }
@Test public void equals() { final MSeq<T> same = newEqualObjects(5); final Object that = same.get(0); for (int i = 1; i < same.length(); ++i) { final Object other = same.get(i); Assert.assertEquals(other, other); Assert.assertEquals(other, that); Assert.assertEquals(that, other); Assert.assertEquals(that.hashCode(), other.hashCode()); } }
@Test(dataProvider = "sequences") public void setAll(final MSeq<Integer> seq) { final long seed = random.seed(); final Random random = new Random(seed); final Integer v = random.nextInt(); seq.fill(() -> v); random.setSeed(seed); final Integer value = random.nextInt(); for (int i = 0; i < seq.length(); ++i) { Assert.assertEquals(seq.get(i), value); } }
private static <G extends Gene<?, G> & Mean<G>> MSeq<G> mean(final MSeq<G> a, final Seq<G> b) { for (int i = a.length(); --i >= 0;) { a.set(i, a.get(i).mean(b.get(i))); } return a; }
@Test public void isEmpty() { Assert.assertTrue(MSeq.empty().isEmpty()); Assert.assertEquals(MSeq.empty().length(), 0); }
@Override protected int crossover(final MSeq<G> that, final MSeq<G> other) { return (int)random.indexes(RandomRegistry.getRandom(), that.length(), 0.5) .peek(i -> crossover(that, other, i)) .count(); }
@Override @SuppressWarnings("unchecked") public void swap(int start, int end, MSeq<T> other, int otherStart) { checkIndex(start, end, otherStart, other.length()); if (start < end) { for (int i = end - start; --i >= 0;) { final T temp = array.get(i + start); array.set(i + start, other.get(otherStart + i)); other.set(otherStart + i, temp); } } }
@Test public void empty() { Assert.assertNotNull(MSeq.EMPTY); Assert.assertNotNull(MSeq.empty()); Assert.assertSame(MSeq.EMPTY, MSeq.empty()); Assert.assertEquals(MSeq.EMPTY.length(), 0); Assert.assertEquals(MSeq.empty().asList().size(), 0); }
@Test public void validPredicate() { final MSeq<Verifiable> array = MSeq.ofLength(100); for (int i = 0; i < array.length(); ++i) { array.set(i, () -> true); } Assert.assertEquals(array.indexWhere(o -> !o.isValid()), -1); array.set(77, () -> false); Assert.assertEquals(array.indexWhere(o -> !o.isValid()), 77); }
@Test public void equalsAllele() { final MSeq<G> same = newEqualObjects(5); final G that = same.get(0); for (int i = 1; i < same.length(); ++i) { final G other = same.get(i); Assert.assertEquals(other.getAllele(), other.getAllele()); Assert.assertEquals(other.getAllele(), that.getAllele()); Assert.assertEquals(that.getAllele(), other.getAllele()); } }