Skip to content

Commit 9cf6b9e

Browse files
authored
Merge pull request #406 from cicirello/update-based-on-rhomu
Optimized Permutation.scramble methods
2 parents 5b3b471 + a48c0fa commit 9cf6b9e

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## [Unreleased] - 2024-05-14
7+
## [Unreleased] - 2024-05-15
88

99
**Breaking Changes**: Due to breaking changes, the next release will be a major release (see the Removed section below for details). Timing of that major release will likely be in the Fall of 2023 to coincide with the planned transition to Java 21 upon its release.
1010

@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
* org.cicirello.sequences.distance.EditDistance
2222
* org.cicirello.sequences.distance.EditDistanceDouble
2323
* Refactored to improve code quality and to optimize SequenceReservoirSampler, SequencePoolSampler, SequenceInsertionSampler, SequenceCompositeSampler.
24+
* Minor optimizations to Permutation.scramble() methods.
2425

2526
### Deprecated
2627

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![JavaPermutationTools - A Java library for computation on permutations and sequences](https://jpt.cicirello.org/images/jpt640.png)](#javapermutationtools-jpt-a-java-library-for-computation-on-permutations-and-sequences)
44

5-
Copyright (C) 2018-2023 [Vincent A. Cicirello](https://www.cicirello.org/).
5+
Copyright (C) 2018-2024 [Vincent A. Cicirello](https://www.cicirello.org/).
66

77
Website: https://jpt.cicirello.org/
88

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<distribution>repo</distribution>
3131
<comments>
3232
JavaPermutationTools (JPT): A library for computation on permutations and sequences.
33-
Copyright (C) 2005-2023 Vincent A. Cicirello.
33+
Copyright (C) 2005-2024 Vincent A. Cicirello.
3434

3535
JavaPermutationTools is free software: you can redistribute it and/or modify
3636
it under the terms of the GNU General Public License as published by
@@ -283,7 +283,7 @@
283283
<link>https://rho-mu.cicirello.org/api</link>
284284
<link>https://core.cicirello.org/api</link>
285285
</links>
286-
<bottom><![CDATA[Copyright &copy; 2005-2023 <a href=\"https://www.cicirello.org/\" target=_top>Vincent A. Cicirello</a>. All rights reserved.]]></bottom>
286+
<bottom><![CDATA[Copyright &copy; 2005-2024 <a href=\"https://www.cicirello.org/\" target=_top>Vincent A. Cicirello</a>. All rights reserved.]]></bottom>
287287
</configuration>
288288
</plugin>
289289
<plugin>

src/main/java/org/cicirello/permutations/Permutation.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* JavaPermutationTools: A Java library for computation on permutations and sequences
3-
* Copyright 2005-2023 Vincent A. Cicirello, <https://www.cicirello.org/>.
3+
* Copyright 2005-2024 Vincent A. Cicirello, <https://www.cicirello.org/>.
44
*
55
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
66
*
@@ -497,8 +497,9 @@ public void scramble(boolean guaranteeDifferent) {
497497
public void scramble(RandomGenerator r, boolean guaranteeDifferent) {
498498
if (guaranteeDifferent) {
499499
boolean changed = false;
500-
for (int i = permutation.length - 1; i > 1; i--) {
501-
int j = RandomIndexer.nextInt(i + 1, r);
500+
for (int j = permutation.length; j > 2; ) {
501+
int i = RandomIndexer.nextInt(j, r);
502+
j--;
502503
if (i != j) {
503504
internalSwap(i, j);
504505
changed = true;
@@ -515,7 +516,8 @@ public void scramble(RandomGenerator r, boolean guaranteeDifferent) {
515516

516517
/**
517518
* Randomly shuffles a segment. Uses {@link ThreadLocalRandom} as the source of efficient random
518-
* number generation.
519+
* number generation. As long as two different indexes are passed to this method, it is guaranteed
520+
* to change the permutation.
519521
*
520522
* @param i endpoint of the segment (precondition: 0 &le; i &lt; length())
521523
* @param j endpoint of the segment (precondition: 0 &le; j &lt; length())
@@ -527,7 +529,8 @@ public void scramble(int i, int j) {
527529
}
528530

529531
/**
530-
* Randomly shuffles a segment.
532+
* Randomly shuffles a segment. As long as two different indexes are passed to this method, it is
533+
* guaranteed to change the permutation.
531534
*
532535
* @param i endpoint of the segment (precondition: 0 &le; i &lt; length())
533536
* @param j endpoint of the segment (precondition: 0 &le; j &lt; length())
@@ -574,8 +577,9 @@ public void scramble(int i, int j, RandomGenerator r) {
574577
public void scramble(int[] indexes, RandomGenerator r) {
575578
if (indexes.length > 1) {
576579
boolean changed = false;
577-
for (int j = indexes.length - 1; j > 1; j--) {
578-
int i = RandomIndexer.nextInt(j + 1, r);
580+
for (int j = indexes.length; j > 2; ) {
581+
int i = RandomIndexer.nextInt(j, r);
582+
j--;
579583
if (i != j) {
580584
internalSwap(indexes[i], indexes[j]);
581585
changed = true;
@@ -603,10 +607,10 @@ public void scramble(int[] indexes) {
603607
}
604608

605609
/**
606-
* Retrieves the i'th integer of the permutation.
610+
* Retrieves the i-th integer of the permutation.
607611
*
608612
* @param i the index of the integer to retrieve. (precondition: 0 &le; i &lt; length())
609-
* @return the integer in the i'th position.
613+
* @return the integer in the i-th position.
610614
* @throws ArrayIndexOutOfBoundsException if i is negative, or if i is greater than or equal to
611615
* length()
612616
*/

0 commit comments

Comments
 (0)