io.jenetics.internal.collection.ArrayISeq Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jenetics Show documentation
Show all versions of jenetics Show documentation
Jenetics - Java Genetic Algorithm Library
/*
* Java Genetic Algorithm Library (jenetics-7.1.2).
* Copyright (c) 2007-2023 Franz Wilhelmstötter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author:
* Franz Wilhelmstötter ([email protected])
*/
package io.jenetics.internal.collection;
import static java.lang.String.format;
import java.io.Serial;
import java.util.function.Function;
import io.jenetics.util.ISeq;
import io.jenetics.util.MSeq;
/**
* @author Franz Wilhelmstötter
* @since 1.4
* @version 3.4
*/
public class ArrayISeq extends ArraySeq implements ISeq {
@Serial
private static final long serialVersionUID = 1L;
public ArrayISeq(final Array array) {
super(array);
assert array.isSealed();
}
@Override
public ISeq map(final Function super T, ? extends B> mapper) {
final Array mapped = Array.ofLength(length());
for (int i = 0; i < length(); ++i) {
mapped.set(i, mapper.apply(array.get(i)));
}
return new ArrayISeq<>(mapped.seal());
}
@Override
public ISeq append(final Iterable extends T> values) {
return new ArrayISeq<>(__append(values).seal());
}
@Override
public ISeq prepend(final Iterable extends T> values) {
return new ArrayISeq<>(__prepend(values).seal());
}
@Override
public ISeq subSeq(final int start) {
if (start < 0 || start > length()) {
throw new ArrayIndexOutOfBoundsException(format(
"Index %d range: [%d..%d)", start, 0, length()
));
}
return start == length()
? Empty.iseq()
: new ArrayISeq<>(array.slice(start, length()));
}
@Override
public ISeq subSeq(int start, int end) {
if (start > end) {
throw new ArrayIndexOutOfBoundsException(format(
"start[%d] > end[%d]", start, end
));
}
if (start < 0 || end > length()) {
throw new ArrayIndexOutOfBoundsException(format(
"Indexes (%d, %d) range: [%d..%d)", start, end, 0, length()
));
}
return start == end
? Empty.iseq()
: new ArrayISeq<>(array.slice(start, end));
}
@Override
public MSeq copy() {
return isEmpty()
? Empty.mseq()
: new ArrayMSeq<>(array.copy());
}
}