You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by le...@apache.org on 2024/02/29 19:44:26 UTC
(datasketches-java) 02/02: Update the built in shuffle class.
This is an automated email from the ASF dual-hosted git repository.
leerho pushed a commit to branch debug_issue_514
in repository https://gitbox.apache.org/repos/asf/datasketches-java.git
commit 4ac7757366e9ea1ab5a27530324d0dca2fd3ca41
Author: Lee Rhodes <le...@users.noreply.github.com>
AuthorDate: Thu Feb 29 11:42:49 2024 -0800
Update the built in shuffle class.
Includes generic type arrays and also gives the option of providing a
user source of randomness derived from java.util.Random.
---
.../org/apache/datasketches/common/Shuffle.java | 67 ++++++++++++++++++----
1 file changed, 57 insertions(+), 10 deletions(-)
diff --git a/src/test/java/org/apache/datasketches/common/Shuffle.java b/src/test/java/org/apache/datasketches/common/Shuffle.java
index 223e3486..af6c4f8b 100644
--- a/src/test/java/org/apache/datasketches/common/Shuffle.java
+++ b/src/test/java/org/apache/datasketches/common/Shuffle.java
@@ -28,10 +28,19 @@ public final class Shuffle {
private static final Random rand = new Random();
/**
- * Shuffle the given input float array
- * @param array input array
+ * Shuffle the given input array using a default source of randomness.
+ * @param array the array to be shuffled.
*/
public static void shuffle(final float[] array) {
+ shuffle(array, rand);
+ }
+
+ /**
+ * Shuffle the given input array using the given source of randomness.
+ * @param array the array to be shuffled.
+ * @param rand the source of randomness used to shuffle the list.
+ */
+ public static void shuffle(final float[] array, final Random rand) {
final int arrLen = array.length;
for (int i = 0; i < arrLen; i++) {
final int r = rand.nextInt(i + 1);
@@ -46,10 +55,19 @@ public final class Shuffle {
}
/**
- * Shuffle the given input double array
- * @param array input array
+ * Shuffle the given input array using a default source of randomness.
+ * @param array the array to be shuffled.
*/
public static void shuffle(final double[] array) {
+ shuffle(array, rand);
+ }
+
+ /**
+ * Shuffle the given input array using the given source of randomness.
+ * @param array the array to be shuffled.
+ * @param rand the source of randomness used to shuffle the list.
+ */
+ public static void shuffle(final double[] array, final Random rand) {
final int arrLen = array.length;
for (int i = 0; i < arrLen; i++) {
final int r = rand.nextInt(i + 1);
@@ -64,10 +82,19 @@ public final class Shuffle {
}
/**
- * Shuffle the given input long array
- * @param array input array
+ * Shuffle the given input array using a default source of randomness.
+ * @param array the array to be shuffled.
*/
public static void shuffle(final long[] array) {
+ shuffle(array, rand);
+ }
+
+ /**
+ * Shuffle the given input array using the given source of randomness.
+ * @param array the array to be shuffled.
+ * @param rand the source of randomness used to shuffle the list.
+ */
+ public static void shuffle(final long[] array, final Random rand) {
final int arrLen = array.length;
for (int i = 0; i < arrLen; i++) {
final int r = rand.nextInt(i + 1);
@@ -82,10 +109,19 @@ public final class Shuffle {
}
/**
- * Shuffle the given input int array
- * @param array input array
+ * Shuffle the given input array using a default source of randomness.
+ * @param array the array to be shuffled.
*/
public static void shuffle(final int[] array) {
+ shuffle(array, rand);
+ }
+
+ /**
+ * Shuffle the given input array using the given source of randomness.
+ * @param array the array to be shuffled.
+ * @param rand the source of randomness used to shuffle the list.
+ */
+ public static void shuffle(final int[] array, final Random rand) {
final int arrLen = array.length;
for (int i = 0; i < arrLen; i++) {
final int r = rand.nextInt(i + 1);
@@ -100,10 +136,21 @@ public final class Shuffle {
}
/**
- * Shuffle the given input array of type T
- * @param array input array
+ * Shuffle the given input array using a default source of randomness.
+ * @param array the array to be shuffled.
+ * @param <T> the component type of the given array.
*/
public static <T> void shuffle(final T[] array) {
+ shuffle(array, rand);
+ }
+
+ /**
+ * Shuffle the given input array using the given source of randomness.
+ * @param array the array to be shuffled.
+ * @param rand the source of randomness used to shuffle the list.
+ * @param <T> the component type of the given array.
+ */
+ public static <T> void shuffle(final T[] array, final Random rand) {
final int arrLen = array.length;
for (int i = 0; i < arrLen; i++) {
final int r = rand.nextInt(i + 1);
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org