001/*
002 * Java Genetic Algorithm Library (jenetics-8.0.0).
003 * Copyright (c) 2007-2024 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020package io.jenetics.internal.util;
021
022import java.util.random.RandomGenerator;
023
024/**
025 * Helper class which contains array helper methods.
026 *
027 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
028 * @since 3.0
029 * @version 3.0
030 */
031public final class Arrays {
032        private Arrays() {}
033
034        public static <T> void swap(final T[] array, final int i, final int j) {
035                final T temp = array[i];
036                array[i] = array[j];
037                array[j] = temp;
038        }
039
040        public static void swap(final int[] array, final int i, final int j) {
041                final int temp = array[i];
042                array[i] = array[j];
043                array[j] = temp;
044        }
045
046        public static void swap(final long[] array, final int i, final int j) {
047                final long temp = array[i];
048                array[i] = array[j];
049                array[j] = temp;
050        }
051
052        public static void swap(final double[] array, final int i, final int j) {
053                final double temp = array[i];
054                array[i] = array[j];
055                array[j] = temp;
056        }
057
058        public static <T> T[] revert(final T[] array) {
059                for (int i = 0, j = array.length - 1; i < j; ++i, --j) {
060                        swap(array, i, j);
061                }
062
063                return array;
064        }
065
066        public static int[] revert(final int[] array) {
067                for (int i = 0, j = array.length - 1; i < j; ++i, --j) {
068                        swap(array, i, j);
069                }
070
071                return array;
072        }
073
074        public static long[] revert(final long[] array) {
075                for (int i = 0, j = array.length - 1; i < j; ++i, --j) {
076                        swap(array, i, j);
077                }
078
079                return array;
080        }
081
082        public static double[] revert(final double[] array) {
083                for (int i = 0, j = array.length - 1; i < j; ++i, --j) {
084                        swap(array, i, j);
085                }
086
087                return array;
088        }
089
090        /**
091         * Randomize the {@code array} using the given {@link RandomGenerator}
092         * object. The used shuffling algorithm is from D. Knuth TAOCP, Seminumerical
093         * Algorithms, Third edition, page 142, Algorithm S (Selection sampling
094         * technique).
095         *
096         * @param array the array to shuffle
097         * @param random the PRNG
098         * @return the shuffled array
099         */
100        public static double[] shuffle(
101                final double[] array,
102                final RandomGenerator random
103        ) {
104                for (int j = array.length - 1; j > 0; --j) {
105                        swap(array, j, random.nextInt(j + 1));
106                }
107                return array;
108        }
109
110        public static int[] shuffle(
111                final int[] array,
112                final RandomGenerator random
113        ) {
114                for (int j = array.length - 1; j > 0; --j) {
115                        swap(array, j, random.nextInt(j + 1));
116                }
117                return array;
118        }
119
120        public static int[] add(final int[] array, final int b) {
121                for (int i = 0; i < array.length; ++i) {
122                        array[i] += b;
123                }
124                return array;
125        }
126
127        public static void rangeCheck(final int from, final int to) {
128                if (from > to) {
129                        throw new IllegalArgumentException(
130                                "fromIndex(" + from + ") > toIndex(" + to + ")");
131                }
132                if (from < 0) {
133                        throw new ArrayIndexOutOfBoundsException(from);
134                }
135        }
136
137        public static long sum(final long[] values) {
138                long sum = 0;
139                for (var value : values) {
140                        sum += value;
141                }
142                return sum;
143        }
144
145}