0001 /*
0002 * Java Genetic Algorithm Library (jenetics-1.5.0).
0003 * Copyright (c) 2007-2013 Franz Wilhelmstötter
0004 *
0005 * Licensed under the Apache License, Version 2.0 (the "License");
0006 * you may not use this file except in compliance with the License.
0007 * You may obtain a copy of the License at
0008 *
0009 * http://www.apache.org/licenses/LICENSE-2.0
0010 *
0011 * Unless required by applicable law or agreed to in writing, software
0012 * distributed under the License is distributed on an "AS IS" BASIS,
0013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014 * See the License for the specific language governing permissions and
0015 * limitations under the License.
0016 *
0017 * Author:
0018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
0019 */
0020 package org.jenetics.util;
0021
0022 import static java.lang.Math.min;
0023 import static java.lang.String.format;
0024 import static java.lang.System.arraycopy;
0025 import static java.util.Objects.requireNonNull;
0026
0027 import java.util.Arrays;
0028 import java.util.Collection;
0029 import java.util.Comparator;
0030 import java.util.Iterator;
0031 import java.util.List;
0032 import java.util.ListIterator;
0033 import java.util.Random;
0034 import java.util.RandomAccess;
0035
0036 import javolution.context.StackContext;
0037 import javolution.util.FastList;
0038
0039 /**
0040 * Array class which wraps the the java build in array type T[]. Once the array
0041 * is created the array length can't be changed (like the build in array).
0042 * <p/>
0043 * <strong>Note that this implementation is not synchronized.</strong> If
0044 * multiple threads access this object concurrently, and at least one of the
0045 * threads modifies it, it must be synchronized externally.
0046 *
0047 * @param <T> the element type of the array.
0048 *
0049 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
0050 * @since 1.0
0051 * @version 1.3 — <em>$Date: 2013-12-05 $</em>
0052 */
0053 public final class Array<T>
0054 extends ArraySeq<T>
0055 implements
0056 MSeq<T>,
0057 RandomAccess
0058 {
0059 private static final long serialVersionUID = 2L;
0060
0061 @SuppressWarnings("rawtypes")
0062 private static final Array EMPTY = new Array(0);
0063
0064 /**
0065 * Return the empty array.
0066 *
0067 * @param <T> the element type.
0068 * @return empty array.
0069 */
0070 @SuppressWarnings("unchecked")
0071 public static <T> Array<T> empty() {
0072 return EMPTY;
0073 }
0074
0075 Array(final ArrayRef array, final int start, final int end) {
0076 super(array, start, end);
0077 }
0078
0079 /**
0080 * Create a new array with the given length.
0081 *
0082 * @param length the array length.
0083 * @throws NegativeArraySizeException if the specified {@code length}
0084 * is negative
0085 */
0086 public Array(final int length) {
0087 super(length);
0088 }
0089
0090 /**
0091 * Create a new array with length one. The array will be initialized with
0092 * the given value.
0093 *
0094 * @param first the only element of the array.
0095 *
0096 * @deprecated Use {@link #valueOf(Object...)} instead.
0097 */
0098 @Deprecated
0099 public Array(final T first) {
0100 this(1);
0101 _array.data[0] = first;
0102 }
0103
0104 /**
0105 * Create a new array with length two. The array will be initialized with
0106 * the given values.
0107 *
0108 * @param first first array element.
0109 * @param second second array element.
0110 *
0111 * @deprecated Use {@link #valueOf(Object...)} instead.
0112 */
0113 @Deprecated
0114 public Array(
0115 final T first,
0116 final T second
0117 ) {
0118 this(2);
0119 _array.data[0] = first;
0120 _array.data[1] = second;
0121 }
0122
0123 /**
0124 * Create a new array with length three. The array will be initialized with
0125 * the given values.
0126 *
0127 * @param first first array element.
0128 * @param second second array element.
0129 * @param third third array element.
0130 *
0131 * @deprecated Use {@link #valueOf(Object...)} instead.
0132 */
0133 @Deprecated
0134 public Array(
0135 final T first,
0136 final T second,
0137 final T third
0138 ) {
0139 this(3);
0140 _array.data[0] = first;
0141 _array.data[1] = second;
0142 _array.data[2] = third;
0143 }
0144
0145 /**
0146 * Create a new array with length four. The array will be initialized with
0147 * the given values.
0148 *
0149 * @param first first array element.
0150 * @param second second array element.
0151 * @param third third array element.
0152 * @param fourth fourth array element.
0153 *
0154 * @deprecated Use {@link #valueOf(Object...)} instead.
0155 */
0156 @Deprecated
0157 public Array(
0158 final T first,
0159 final T second,
0160 final T third,
0161 final T fourth
0162 ) {
0163 this(4);
0164 _array.data[0] = first;
0165 _array.data[1] = second;
0166 _array.data[2] = third;
0167 _array.data[3] = fourth;
0168 }
0169
0170 /**
0171 * Create a new array with length five. The array will be initialized with
0172 * the given values.
0173 *
0174 * @param first first array element.
0175 * @param second second array element.
0176 * @param third third array element.
0177 * @param fourth fourth array element.
0178 * @param fifth fifth array element.
0179 *
0180 * @deprecated Use {@link #valueOf(Object...)} instead.
0181 */
0182 @Deprecated
0183 public Array(
0184 final T first,
0185 final T second,
0186 final T third,
0187 final T fourth,
0188 final T fifth
0189 ) {
0190 this(5);
0191 _array.data[0] = first;
0192 _array.data[1] = second;
0193 _array.data[2] = third;
0194 _array.data[3] = fourth;
0195 _array.data[4] = fifth;
0196 }
0197
0198 /**
0199 * Create a new array from the given values.
0200 *
0201 * @param first first array element.
0202 * @param second second array element.
0203 * @param third third array element.
0204 * @param fourth fourth array element.
0205 * @param fifth fifth array element.
0206 * @param rest the rest of the array element.
0207 * @throws NullPointerException if the {@code rest} array is {@code null}.
0208 *
0209 * @deprecated Use {@link #valueOf(Object...)} instead.
0210 */
0211 @Deprecated
0212 @SafeVarargs
0213 public Array(
0214 final T first,
0215 final T second,
0216 final T third,
0217 final T fourth,
0218 final T fifth,
0219 final T... rest
0220 ) {
0221 this(5 + rest.length);
0222 _array.data[0] = first;
0223 _array.data[1] = second;
0224 _array.data[2] = third;
0225 _array.data[3] = fourth;
0226 _array.data[4] = fifth;
0227 arraycopy(rest, 0, _array.data, 5, rest.length);
0228 }
0229
0230 /**
0231 * Create a new array from the given values.
0232 *
0233 * @param values the array values.
0234 * @throws NullPointerException if the {@code values} array is {@code null}.
0235 *
0236 * @deprecated Use {@link #valueOf(Object...)} instead.
0237 */
0238 @Deprecated
0239 public Array(final T[] values) {
0240 this(values.length);
0241 arraycopy(values, 0, _array.data, 0, values.length);
0242 }
0243
0244 /**
0245 * Create a new Array from the values of the given Collection. The order of
0246 * the elements are determined by the iterator of the Collection.
0247 *
0248 * @param values the array values.
0249 * @throws NullPointerException if the {@code values} array is {@code null}.
0250 *
0251 * @deprecated Use {@link #valueOf(Collection)} instead.
0252 */
0253 @Deprecated
0254 public Array(final Collection<? extends T> values) {
0255 this(values.size());
0256
0257 int index = 0;
0258 for (Iterator<? extends T>
0259 it = values.iterator(); it.hasNext(); ++index)
0260 {
0261 _array.data[index] = it.next();
0262 }
0263 }
0264
0265 /**
0266 * Selects all elements of this list which satisfy a predicate.
0267 *
0268 * @param predicate the predicate used to test elements.
0269 * @return a new array consisting of all elements of this list that satisfy
0270 * the given {@code predicate}. The order of the elements is
0271 * preserved.
0272 * @throws NullPointerException if the given {@code predicate} is
0273 * {@code null}.
0274 */
0275 public Array<T> filter(final Function<? super T, Boolean> predicate) {
0276 StackContext.enter();
0277 try {
0278 final FastList<T> filtered = FastList.newInstance();
0279 for (int i = 0, n = length(); i < n; ++i) {
0280 @SuppressWarnings("unchecked")
0281 final T value = (T)_array.data[i + _start];
0282
0283 if (predicate.apply(value) == Boolean.TRUE) {
0284 filtered.add(value);
0285 }
0286 }
0287
0288 final Array<T> copy = new Array<>(filtered.size());
0289 int index = 0;
0290 for (FastList.Node<T> n = filtered.head(), end = filtered.tail();
0291 (n = n.getNext()) != end;)
0292 {
0293 copy.set(index++, n.getValue());
0294 }
0295
0296 return copy;
0297 } finally {
0298 StackContext.exit();
0299 }
0300 }
0301
0302 @Override
0303 public void set(final int index, final T value) {
0304 checkIndex(index);
0305
0306 _array.cloneIfSealed();
0307 _array.data[index + _start] = value;
0308 }
0309
0310 /**
0311 * <p>
0312 * Sorts the array of objects into ascending order, according to the natural
0313 * ordering of its elements. All elements in the array <b>must</b> implement
0314 * the Comparable interface. Furthermore, all elements in the array must be
0315 * mutually comparable.
0316 * </p>
0317 * The sorting algorithm is the Quicksort.
0318 *
0319 * @see <a href="https://secure.wikimedia.org/wikipedia/en/wiki/Quicksort">
0320 * Wikipedia: Quicksort
0321 * </a>
0322 *
0323 * @throws ClassCastException if the array contains elements that are not
0324 * <i>mutually comparable</i> (for example, strings and integers).
0325 */
0326 public void sort() {
0327 sort(0, length());
0328 }
0329
0330 /**
0331 * <p>
0332 * Sorts the array of objects into ascending order, according to the natural
0333 * ordering of its elements. All elements in the array <b>must</b> implement
0334 * the Comparable interface. Furthermore, all elements in the array must be
0335 * mutually comparable.
0336 * </p>
0337 * The sorting algorithm is the Quicksort.
0338 *
0339 * @see <a href="https://secure.wikimedia.org/wikipedia/en/wiki/Quicksort">
0340 * Wikipedia: Quicksort
0341 * </a>
0342 *
0343 * @param from the index of the first element (inclusive) to be sorted.
0344 * @param to the index of the last element (exclusive) to be sorted.
0345 * @throws IndexOutOfBoundsException if {@code from < 0 or to > length()}
0346 * @throws IllegalArgumentException if {@code from > to}
0347 * @throws ClassCastException if the array contains elements that are not
0348 * <i>mutually comparable</i> (for example, strings and integers).
0349 */
0350 public void sort(final int from, final int to) {
0351 sort(from, to, new Comparator<T>() {
0352 @SuppressWarnings({ "unchecked", "rawtypes" })
0353 @Override
0354 public int compare(final T o1, final T o2) {
0355 return ((Comparable)o1).compareTo(o2);
0356 }
0357 });
0358 }
0359
0360 /**
0361 * <p>
0362 * Sorts the array of objects according to the order induced by the specified
0363 * comparator. All elements in the array must be mutually comparable by the
0364 * specified comparator.
0365 * </p>
0366 * The sorting algorithm is the Quicksort.
0367 *
0368 * @see <a href="https://secure.wikimedia.org/wikipedia/en/wiki/Quicksort">
0369 * Wikipedia: Quicksort
0370 * </a>
0371 *
0372 * @throws NullPointerException if the given {@code comparator} is
0373 * {@code null}.
0374 * @throws ClassCastException if the array contains elements that are not
0375 * <i>mutually comparable</i> (for example, strings and integers).
0376 */
0377 public void sort(final Comparator<? super T> comparator) {
0378 sort(0, length(), comparator);
0379 }
0380
0381 /**
0382 * <p>
0383 * Sorts the array of objects according to the order induced by the specified
0384 * comparator. All elements in the array must be mutually comparable by the
0385 * specified comparator.
0386 * </p>
0387 * The sorting algorithm is the <i>Timsort</i>.
0388 *
0389 * @see <a href="https://secure.wikimedia.org/wikipedia/en/wiki/Timsort">
0390 * Wikipedia: Timsort
0391 * </a>
0392 * @see Arrays#sort(Object[], int, int, Comparator)
0393 *
0394 * @param from the index of the first element (inclusive) to be sorted.
0395 * @param to the index of the last element (exclusive) to be sorted.
0396 * @throws NullPointerException if the given {@code comparator} is
0397 * {@code null}.
0398 * @throws IndexOutOfBoundsException if {@code from < 0 or to > length()}
0399 * @throws IllegalArgumentException if {@code from > to}
0400 * @throws ClassCastException if the array contains elements that are not
0401 * <i>mutually comparable</i> (for example, strings and integers).
0402 */
0403 public void sort(
0404 final int from, final int to,
0405 final Comparator<? super T> comparator
0406 ) {
0407 checkIndex(from, to);
0408 if (from > to) {
0409 throw new IllegalArgumentException(format(
0410 "From index > to index: %d > %d.", from, to
0411 ));
0412 }
0413 requireNonNull(comparator, "Comparator");
0414
0415 _array.cloneIfSealed();
0416
0417 @SuppressWarnings("unchecked")
0418 final T[] data = (T[])_array.data;
0419 Arrays.sort(data, from + _start, to + _start, comparator);
0420 }
0421
0422 private void uncheckedSwap(final int i, final int j) {
0423 final Object temp = _array.data[i + _start];
0424 _array.data[i + _start] = _array.data[j + _start];
0425 _array.data[j + _start] = temp;
0426 }
0427
0428 /**
0429 * Reverses the given array in place.
0430 *
0431 * @return this (reversed) array.
0432 */
0433 public Array<T> reverse() {
0434 return reverse(0, length());
0435 }
0436
0437 /**
0438 * Reverses the part of the array determined by the to indexes. The reverse
0439 * method is performed in place.
0440 *
0441 * @param from the first index (inclusive)
0442 * @param to the second index (exclusive)
0443 * @return this (reversed) array.
0444 * @throws IllegalArgumentException if <tt>from > to</tt>
0445 * @throws IndexOutOfBoundsException if <tt>from < 0</tt> or
0446 * <tt>to > a.length</tt>
0447 */
0448 public Array<T> reverse(final int from, final int to) {
0449 checkIndex(from, to);
0450 _array.cloneIfSealed();
0451
0452 int i = from;
0453 int j = to;
0454 while (i < j) {
0455 uncheckedSwap(i++, --j);
0456 }
0457
0458 return this;
0459 }
0460
0461 @Override
0462 public void swap(final int i, final int j) {
0463 checkIndex(i);
0464 checkIndex(j);
0465
0466 _array.cloneIfSealed();
0467 uncheckedSwap(i, j);
0468 }
0469
0470 @Override
0471 public void swap(
0472 final int start, final int end,
0473 final MSeq<T> other, final int otherStart
0474 ) {
0475 if (other instanceof Array<?>) {
0476 swap(start, end, (Array<T>)other, otherStart);
0477 } else {
0478 checkIndex(start, end);
0479 if (otherStart < 0 || (otherStart + (end - start)) > _length) {
0480 throw new ArrayIndexOutOfBoundsException(format(
0481 "Invalid index range: [%d, %d)",
0482 otherStart, (otherStart + (end - start))
0483 ));
0484 }
0485
0486 if (start < end) {
0487 _array.cloneIfSealed();
0488
0489 for (int i = (end - start); --i >= 0;) {
0490 @SuppressWarnings("unchecked")
0491 final T temp = (T)_array.data[_start + start + i];
0492 _array.data[_start + start + i] = other.get(otherStart + i);
0493 other.set(otherStart + i, temp);
0494 }
0495 }
0496 }
0497 }
0498
0499 /**
0500 * @see MSeq#swap(int, int, MSeq, int)
0501 */
0502 public void swap(
0503 final int start, final int end,
0504 final Array<T> other, final int otherStart
0505 ) {
0506 checkIndex(start, end);
0507 other.checkIndex(otherStart, otherStart + (end - start));
0508
0509 if (start < end) {
0510 _array.cloneIfSealed();
0511 other._array.cloneIfSealed();
0512
0513 for (int i = (end - start); --i >= 0;) {
0514 final Object temp = _array.data[_start + start + i];
0515 _array.data[_start + start + i] = (
0516 other._array.data[other._start + otherStart + i]
0517 );
0518 other._array.data[other._start + otherStart + i] = temp;
0519 }
0520 }
0521 }
0522
0523 /**
0524 * Randomize this array using the given {@link Random} object. The used
0525 * shuffling algorithm is from D. Knuth TAOCP, Seminumerical Algorithms,
0526 * Third edition, page 142, Algorithm S (Selection sampling technique).
0527 *
0528 * @param random the {@link Random} object to use for randomize.
0529 * @return this array
0530 * @throws NullPointerException if the give random object is {@code null}.
0531 */
0532 public Array<T> shuffle(final Random random) {
0533 _array.cloneIfSealed();
0534
0535 for (int j = length() - 1; j > 0; --j) {
0536 uncheckedSwap(j, random.nextInt(j + 1));
0537 }
0538
0539 return this;
0540 }
0541
0542 /**
0543 * Randomize this array using the <i>registered</i> {@link Random} object.
0544 * The used shuffling algorithm is from D. Knuth TAOCP, Seminumerical
0545 * Algorithms, Third edition, page 142, Algorithm S (Selection sampling
0546 * technique).
0547 *
0548 * @return this array
0549 */
0550 public Array<T> shuffle() {
0551 return shuffle(RandomRegistry.getRandom());
0552 }
0553
0554 @Override
0555 public Array<T> setAll(final T value) {
0556 _array.cloneIfSealed();
0557 for (int i = _start; i < _end; ++i) {
0558 _array.data[i] = value;
0559 }
0560 return this;
0561 }
0562
0563 @Override
0564 public Array<T> setAll(final Iterator<? extends T> it) {
0565 _array.cloneIfSealed();
0566 for (int i = _start; i < _end && it.hasNext(); ++i) {
0567 _array.data[i] = it.next();
0568 }
0569 return this;
0570 }
0571
0572 @Override
0573 public Array<T> setAll(final Iterable<? extends T> values) {
0574 return setAll(values.iterator());
0575 }
0576
0577 @Override
0578 public Array<T> setAll(final T[] values) {
0579 _array.cloneIfSealed();
0580 arraycopy(
0581 values, 0, _array.data, _start, min(length(), values.length)
0582 );
0583 return this;
0584 }
0585
0586 @Override
0587 public Array<T> fill(final Factory<? extends T> factory) {
0588 requireNonNull(factory);
0589
0590 _array.cloneIfSealed();
0591 for (int i = _start; i < _end; ++i) {
0592 _array.data[i] = factory.newInstance();
0593 }
0594 return this;
0595 }
0596
0597 @Override
0598 public ISeq<T> toISeq() {
0599 return new ArrayISeq<>(new ArrayRef(_array.seal().data), _start, _end);
0600 }
0601
0602 /**
0603 * Create a new array which contains the values of {@code this} and the
0604 * given {@code value}. The length of the new array is
0605 * {@code this.length() + 1}. The returned array is not sealed.
0606 *
0607 * @param value the value to append to this array.
0608 * @return a new array which contains the values of {@code this} and the
0609 * given {@code value}
0610 */
0611 public Array<T> add(final T value) {
0612 final Array<T> array = new Array<>(length() + 1);
0613 arraycopy(_array.data, _start, array._array.data, 0, length());
0614 array._array.data[array.length() - 1] = value;
0615 return array;
0616 }
0617
0618 /**
0619 * Create a new array which contains the values of {@code this} and the
0620 * given {@code array}. The length of the new array is
0621 * {@code this.length() + array.length()}. The returned array is not sealed.
0622 *
0623 * @param array the array to append to this array.
0624 * @return a new array which contains the values of {@code this} and the
0625 * given {@code array}
0626 * @throws NullPointerException if the {@code arrays} is {@code null}.
0627 */
0628 public Array<T> add(final Array<? extends T> array) {
0629 final Array<T> appended = new Array<>(length() + array.length());
0630
0631 arraycopy(
0632 _array.data, _start,
0633 appended._array.data, 0, length()
0634 );
0635 arraycopy(
0636 array._array.data, array._start,
0637 appended._array.data, length(), array.length()
0638 );
0639
0640 return appended;
0641 }
0642
0643 /**
0644 * Create a new array which contains the values of {@code this} and the
0645 * given {@code values}. The length of the new array is
0646 * {@code this.length() + values.size()}. The returned array is not sealed.
0647 *
0648 * @param values the array to append to this array.
0649 * @return a new array which contains the values of {@code this} and the
0650 * given {@code array}
0651 * @throws NullPointerException if the {@code values} is {@code null}.
0652 */
0653 public Array<T> add(final Collection<? extends T> values) {
0654 requireNonNull(values, "Values");
0655 final Array<T> array = new Array<>(length() + values.size());
0656
0657 arraycopy(_array.data, _start, array._array.data, 0, length());
0658 int index = length();
0659 for (Iterator<? extends T>
0660 it = values.iterator(); it.hasNext(); ++index)
0661 {
0662 array._array.data[index] = it.next();
0663 }
0664
0665 return array;
0666 }
0667
0668 @Override
0669 public <B> Array<B> map(final Function<? super T, ? extends B> mapper) {
0670 requireNonNull(mapper, "Converter");
0671
0672 final int length = length();
0673 final Array<B> result = new Array<>(length);
0674 assert (result._array.data.length == length);
0675
0676 for (int i = length; --i >= 0;) {
0677 @SuppressWarnings("unchecked")
0678 final T value = (T)_array.data[i + _start];
0679 result._array.data[i] = mapper.apply(value);
0680 }
0681 return result;
0682 }
0683
0684 @Override
0685 public Array<T> copy() {
0686 return new Array<>(new ArrayRef(toArray()), 0, length());
0687 }
0688
0689 @Override
0690 public Array<T> subSeq(final int start, final int end) {
0691 checkIndex(start, end);
0692 return new Array<>(_array, start + _start, _end + end - _length);
0693 //return new Array<>(_array, start + _start, end + _start);
0694 }
0695
0696 @Override
0697 public Array<T> subSeq(final int start) {
0698 return subSeq(start, length());
0699 }
0700
0701 @Override
0702 public List<T> asList() {
0703 return new ArrayMSeqList<>(this);
0704 }
0705
0706 @Override
0707 public ListIterator<T> listIterator() {
0708 return new ArrayMSeqIterator<>(this);
0709 }
0710
0711
0712 /* *************************************************************************
0713 * Static factory methods.
0714 **************************************************************************/
0715
0716 /**
0717 * Create a new array from the given values.
0718 *
0719 * @param values the array values.
0720 * @throws NullPointerException if the {@code values} array is {@code null}.
0721 */
0722 @SafeVarargs
0723 public static <T> Array<T> valueOf(final T... values) {
0724 Array<T> array = empty();
0725 if (values.length > 0) {
0726 array = new Array<>(values.length);
0727 arraycopy(values, 0, array._array.data, 0, values.length);
0728 }
0729
0730 return array;
0731 }
0732
0733 /**
0734 * Create a new Array from the values of the given {@code Collection}. The
0735 * order of the elements are determined by the iterator of the Collection.
0736 *
0737 * @param values the array values.
0738 * @throws NullPointerException if the {@code values} array is {@code null}.
0739 */
0740 public static <T> Array<T> valueOf(final Collection<? extends T> values) {
0741 Array<T> array = empty();
0742 if (!values.isEmpty()) {
0743 array = new Array<>(values.size());
0744 int index = 0;
0745 for (Iterator<? extends T>
0746 it = values.iterator(); it.hasNext(); ++index)
0747 {
0748 array._array.data[index] = it.next();
0749 }
0750 }
0751
0752 return array;
0753 }
0754
0755 /**
0756 * Create a new Array from the values of the given {@code Seq}.
0757 *
0758 * @param values the array values.
0759 * @throws NullPointerException if the {@code values} array is {@code null}.
0760 */
0761 public static <T> Array<T> valueOf(final Seq<T> values) {
0762 Array<T> array = empty();
0763 if (values.length() > 0) {
0764 if (values instanceof Array<?>) {
0765 array = ((Array<T>)values).copy();
0766 } else {
0767 array = new Array<>(values.length());
0768 int index = 0;
0769 for (Iterator<? extends T>
0770 it = values.iterator(); it.hasNext(); ++index)
0771 {
0772 array._array.data[index] = it.next();
0773 }
0774 }
0775 }
0776
0777 return array;
0778 }
0779
0780 /**
0781 * Boxes the given native array into an {@code Array<Boolean>}.
0782 *
0783 * @param values the native array to box.
0784 * @return the boxed array.
0785 */
0786 public static Array<Boolean> box(final boolean... values) {
0787 Array<Boolean> array = empty();
0788 if (values.length > 0) {
0789 array = new Array<>(values.length);
0790 for (int i = values.length; --i >= 0;) {
0791 array._array.data[i] = values[i];
0792 }
0793 }
0794
0795 return array;
0796 }
0797
0798
0799 /**
0800 * Boxes the given native array into an {@code Array<Char>}.
0801 *
0802 * @param values the native array to box.
0803 * @return the boxed array.
0804 */
0805 public static Array<Character> box(final char... values) {
0806 Array<Character> array = empty();
0807 if (values.length > 0) {
0808 array = new Array<>(values.length);
0809 for (int i = values.length; --i >= 0;) {
0810 array._array.data[i] = values[i];
0811 }
0812 }
0813
0814 return array;
0815 }
0816
0817 /**
0818 * Boxes the given native array into an {@code Array<Short>}.
0819 *
0820 * @param values the native array to box.
0821 * @return the boxed array.
0822 */
0823 public static Array<Short> box(final short... values) {
0824 Array<Short> array = empty();
0825 if (values.length > 0) {
0826 array = new Array<>(values.length);
0827 for (int i = values.length; --i >= 0;) {
0828 array._array.data[i] = values[i];
0829 }
0830 }
0831
0832 return array;
0833 }
0834
0835 /**
0836 * Boxes the given native array into an {@code Array<Integer>}.
0837 *
0838 * @param values the native array to box.
0839 * @return the boxed array.
0840 */
0841 public static Array<Integer> box(final int... values) {
0842 Array<Integer> array = empty();
0843 if (values.length > 0) {
0844 array = new Array<>(values.length);
0845 for (int i = values.length; --i >= 0;) {
0846 array._array.data[i] = values[i];
0847 }
0848 }
0849
0850 return array;
0851 }
0852
0853 /**
0854 * Boxes the given native array into an {@code Array<Long>}.
0855 *
0856 * @param values the native array to box.
0857 * @return the boxed array.
0858 */
0859 public static Array<Long> box(final long... values) {
0860 Array<Long> array = empty();
0861 if (values.length > 0) {
0862 array = new Array<>(values.length);
0863 for (int i = values.length; --i >= 0;) {
0864 array._array.data[i] = values[i];
0865 }
0866 }
0867
0868 return array;
0869 }
0870
0871 /**
0872 * Boxes the given native array into an {@code Array<Float>}.
0873 *
0874 * @param values the native array to box.
0875 * @return the boxed array.
0876 */
0877 public static Array<Float> box(final float... values) {
0878 Array<Float> array = empty();
0879 if (values.length > 0) {
0880 array = new Array<>(values.length);
0881 for (int i = values.length; --i >= 0;) {
0882 array._array.data[i] = values[i];
0883 }
0884 }
0885
0886 return array;
0887 }
0888
0889 /**
0890 * Boxes the given native array into an {@code Array<Double>}.
0891 *
0892 * @param values the native array to box.
0893 * @return the boxed array.
0894 */
0895 public static Array<Double> box(final double... values) {
0896 Array<Double> array = empty();
0897 if (values.length > 0) {
0898 array = new Array<>(values.length);
0899 for (int i = values.length; --i >= 0;) {
0900 array._array.data[i] = values[i];
0901 }
0902 }
0903
0904 return array;
0905 }
0906
0907 /**
0908 * Unboxes the given array to the corresponding native version.
0909 *
0910 * @param values the {@code Array} to unbox.
0911 * @return the unboxed native array.
0912 */
0913 public static boolean[] unboxBoolean(final Array<Boolean> values) {
0914 final boolean[] array = new boolean[values.length()];
0915 for (int i = values._start; i < values._end; ++i) {
0916 array[i - values._start] = (Boolean)values._array.data[i];
0917 }
0918
0919 return array;
0920 }
0921
0922 /**
0923 * Unboxes the given array to the corresponding native version.
0924 *
0925 * @param values the {@code Array} to unbox.
0926 * @return the unboxed native array.
0927 */
0928 public static char[] unboxChar(final Array<Character> values) {
0929 final char[] array = new char[values.length()];
0930 for (int i = values._start; i < values._end; ++i) {
0931 array[i - values._start] = (Character)values._array.data[i];
0932 }
0933
0934 return array;
0935 }
0936
0937 /**
0938 * Unboxes the given array to the corresponding native version.
0939 *
0940 * @param values the {@code Array} to unbox.
0941 * @return the unboxed native array.
0942 */
0943 public static short[] unboxShort(final Array<Short> values) {
0944 final short[] array = new short[values.length()];
0945 for (int i = values._start; i < values._end; ++i) {
0946 array[i - values._start] = (Short)values._array.data[i];
0947 }
0948
0949 return array;
0950 }
0951
0952 /**
0953 * Unboxes the given array to the corresponding native version.
0954 *
0955 * @param values the {@code Array} to unbox.
0956 * @return the unboxed native array.
0957 */
0958 public static int[] unboxInt(final Array<Integer> values) {
0959 final int[] array = new int[values.length()];
0960 for (int i = values._start; i < values._end; ++i) {
0961 array[i - values._start] = (Integer)values._array.data[i];
0962 }
0963
0964 return array;
0965 }
0966
0967 /**
0968 * Unboxes the given array to the corresponding native version.
0969 *
0970 * @param values the {@code Array} to unbox.
0971 * @return the unboxed native array.
0972 */
0973 public static long[] unboxLong(final Array<Long> values) {
0974 final long[] array = new long[values.length()];
0975 for (int i = values._start; i < values._end; ++i) {
0976 array[i - values._start] = (Long)values._array.data[i];
0977 }
0978
0979 return array;
0980 }
0981
0982 /**
0983 * Unboxes the given array to the corresponding native version.
0984 *
0985 * @param values the {@code Array} to unbox.
0986 * @return the unboxed native array.
0987 */
0988 public static float[] unboxFloat(final Array<Float> values) {
0989 final float[] array = new float[values.length()];
0990 for (int i = values._start; i < values._end; ++i) {
0991 array[i - values._start] = (Float)values._array.data[i];
0992 }
0993
0994 return array;
0995 }
0996
0997 /**
0998 * Unboxes the given array to the corresponding native version.
0999 *
1000 * @param values the {@code Array} to unbox.
1001 * @return the unboxed native array.
1002 */
1003 public static double[] unboxDouble(final Array<Double> values) {
1004 final double[] array = new double[values.length()];
1005 for (int i = values._start; i < values._end; ++i) {
1006 array[i - values._start] = (Double)values._array.data[i];
1007 }
1008
1009 return array;
1010 }
1011
1012 }
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
|