Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 8ba3b37

Browse files
author
vsonnier
committed
Fixes HPPCRT-45
1 parent 0172997 commit 8ba3b37

File tree

14 files changed

+326
-281
lines changed

14 files changed

+326
-281
lines changed

CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
[0.7.1]
2+
** Bug fixes
3+
HPPCRT-45: API with intervals arguments is inconsistent with JDK conventions.
4+
15
[0.7.0]
26
This release fix important bugs, and align on some refactorings or changes of HPPC v0.7.1, hence the version number.
37

hppcrt-benchmarks/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
<parent>
66
<groupId>com.github.vsonnier</groupId>
77
<artifactId>hppcrt-parent</artifactId>
8-
<version>0.7.0</version>
8+
<version>0.7.1</version>
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111

1212
<!-- Project info. -->
1313
<artifactId>hppcrt-benchmarks</artifactId>
14-
<version>0.7.0</version>
14+
<version>0.7.1</version>
1515
<packaging>jar</packaging>
1616

1717
<name>HPPC-RT Benchmarks</name>
@@ -24,7 +24,7 @@
2424
<commons-lang.version>2.6</commons-lang.version>
2525

2626
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
27-
<jmh.version>1.10.2</jmh.version>
27+
<jmh.version>1.10.3</jmh.version>
2828
<javac.target>1.7</javac.target>
2929
<uberjar.name>benchmarks</uberjar.name>
3030

hppcrt-template-processor/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
<parent>
66
<groupId>com.github.vsonnier</groupId>
77
<artifactId>hppcrt-parent</artifactId>
8-
<version>0.7.0</version>
8+
<version>0.7.1</version>
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111

1212
<!-- Project info. -->
1313
<groupId>com.github.vsonnier</groupId>
1414
<artifactId>hppcrt-template-processor</artifactId>
15-
<version>0.7.0</version>
15+
<version>0.7.1</version>
1616
<packaging>jar</packaging>
1717

1818
<name>HPPC-RT Template Processor</name>

hppcrt/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
<parent>
77
<groupId>com.github.vsonnier</groupId>
88
<artifactId>hppcrt-parent</artifactId>
9-
<version>0.7.0</version>
9+
<version>0.7.1</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

1313
<!-- Project info. -->
1414
<groupId>com.github.vsonnier</groupId>
1515
<artifactId>hppcrt</artifactId>
16-
<version>0.7.0</version>
16+
<version>0.7.1</version>
1717
<packaging>jar</packaging>
1818

1919
<name>HPPC-RT Collections</name>

hppcrt/src/main/templates/com/carrotsearch/hppcrt/lists/KTypeArrayDeque.java

Lines changed: 84 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
*/
5353
/*! ${TemplateOptions.generatedAnnotation} !*/
5454
public class KTypeArrayDeque<KType>
55-
extends AbstractKTypeCollection<KType> implements KTypeDeque<KType>, KTypeIndexedContainer<KType>, Cloneable
55+
extends AbstractKTypeCollection<KType> implements KTypeDeque<KType>, KTypeIndexedContainer<KType>, Cloneable
5656
{
5757
/**
5858
* Internal array for storing elements.
@@ -65,8 +65,8 @@ public class KTypeArrayDeque<KType>
6565
KType []
6666
#else !*/
6767
Object[]
68-
/*! #end !*/
69-
buffer;
68+
/*! #end !*/
69+
buffer;
7070

7171
/**
7272
* The index of the element at the head of the deque or an
@@ -751,14 +751,18 @@ public <T extends KTypeProcedure<? super KType>> T forEach(final T procedure) {
751751
@Override
752752
public <T extends KTypeProcedure<? super KType>> T forEach(final T procedure, final int fromIndex, final int toIndex) {
753753

754-
if (fromIndex >= toIndex) {
754+
checkRangeBounds(fromIndex, toIndex);
755755

756-
throw new IllegalArgumentException("Index fromIndex " + fromIndex + " is >= toIndex " + toIndex);
756+
if (fromIndex == toIndex) {
757+
758+
return procedure; //nothing to do
757759
}
758760

761+
final int bufferPositionStart = indexToBufferPosition(fromIndex);
762+
759763
final int endBufferPosInclusive = indexToBufferPosition(toIndex - 1); //must be a valid index
760764

761-
internalForEach(procedure, indexToBufferPosition(fromIndex), oneRight(endBufferPosInclusive, this.buffer.length));
765+
internalForEach(procedure, bufferPositionStart, oneRight(endBufferPosInclusive, this.buffer.length));
762766

763767
return procedure;
764768
}
@@ -791,14 +795,18 @@ public <T extends KTypePredicate<? super KType>> T forEach(final T predicate) {
791795
@Override
792796
public <T extends KTypePredicate<? super KType>> T forEach(final T predicate, final int fromIndex, final int toIndex) {
793797

794-
if (fromIndex >= toIndex) {
798+
checkRangeBounds(fromIndex, toIndex);
799+
800+
if (fromIndex == toIndex) {
795801

796-
throw new IllegalArgumentException("Index fromIndex " + fromIndex + " is >= toIndex " + toIndex);
802+
return predicate; //nothing to do
797803
}
798804

805+
final int bufferPositionStart = indexToBufferPosition(fromIndex);
806+
799807
final int endBufferPosInclusive = indexToBufferPosition(toIndex - 1); //must be a valid index
800808

801-
internalForEach(predicate, indexToBufferPosition(fromIndex), oneRight(endBufferPosInclusive, this.buffer.length));
809+
internalForEach(predicate, bufferPositionStart, oneRight(endBufferPosInclusive, this.buffer.length));
802810

803811
return predicate;
804812
}
@@ -1035,7 +1043,7 @@ private boolean allIndexesEqual(final KTypeIndexedContainer<KType> b1, final KTy
10351043
* instead of using a constructor).
10361044
*/
10371045
public static/* #if ($TemplateOptions.KTypeGeneric) */<KType> /* #end */
1038-
KTypeArrayDeque<KType> newInstance() {
1046+
KTypeArrayDeque<KType> newInstance() {
10391047
return new KTypeArrayDeque<KType>();
10401048
}
10411049

@@ -1044,15 +1052,15 @@ KTypeArrayDeque<KType> newInstance() {
10441052
* instead of using a constructor).
10451053
*/
10461054
public static/* #if ($TemplateOptions.KTypeGeneric) */<KType> /* #end */
1047-
KTypeArrayDeque<KType> newInstance(final int initialCapacity) {
1055+
KTypeArrayDeque<KType> newInstance(final int initialCapacity) {
10481056
return new KTypeArrayDeque<KType>(initialCapacity);
10491057
}
10501058

10511059
/**
10521060
* Create a new deque by pushing a variable number of arguments to the end of it.
10531061
*/
10541062
public static/* #if ($TemplateOptions.KTypeGeneric) */<KType> /* #end */
1055-
KTypeArrayDeque<KType> from(final KType... elements) {
1063+
KTypeArrayDeque<KType> from(final KType... elements) {
10561064
final KTypeArrayDeque<KType> coll = new KTypeArrayDeque<KType>(elements.length);
10571065
coll.addLast(elements);
10581066
return coll;
@@ -1062,7 +1070,7 @@ KTypeArrayDeque<KType> from(final KType... elements) {
10621070
* Create a new deque by pushing a variable number of arguments to the end of it.
10631071
*/
10641072
public static/* #if ($TemplateOptions.KTypeGeneric) */<KType> /* #end */
1065-
KTypeArrayDeque<KType> from(final KTypeContainer<KType> container) {
1073+
KTypeArrayDeque<KType> from(final KTypeContainer<KType> container) {
10661074
return new KTypeArrayDeque<KType>(container);
10671075
}
10681076

@@ -1081,21 +1089,27 @@ KTypeArrayDeque<KType> from(final KTypeContainer<KType> container) {
10811089
*/
10821090
public void sort(final int beginIndex, final int endIndex) {
10831091

1084-
if (endIndex - beginIndex > 1) {
1085-
//Fast path : if the actual indices matching [beginIndex; endIndex[
1086-
//in the underlying buffer are in increasing order (means there is no folding of buffer in the interval),
1087-
// use quicksort array version directly.
1088-
final int bufferPosStart = indexToBufferPosition(beginIndex);
1089-
final int bufferPosEndInclusive = indexToBufferPosition(endIndex - 1); //must be a valid index
1092+
checkRangeBounds(beginIndex, endIndex);
10901093

1091-
if (bufferPosEndInclusive > bufferPosStart) {
1094+
if (beginIndex == endIndex) {
10921095

1093-
KTypeSort.quicksort(this.buffer, bufferPosStart, bufferPosEndInclusive + 1);
1094-
} else {
1095-
//Use the slower KTypeIndexedContainer sort
1096-
KTypeSort.quicksort(this, beginIndex, endIndex);
1097-
}
1096+
return; //nothing to do
1097+
}
1098+
1099+
//Fast path : if the actual indices matching [beginIndex; endIndex[
1100+
//in the underlying buffer are in increasing order (means there is no folding of buffer in the interval),
1101+
// use quicksort array version directly.
1102+
final int bufferPosStart = indexToBufferPosition(beginIndex);
1103+
final int bufferPosEndInclusive = indexToBufferPosition(endIndex - 1); //must be a valid index
1104+
1105+
if (bufferPosEndInclusive > bufferPosStart) {
1106+
1107+
KTypeSort.quicksort(this.buffer, bufferPosStart, bufferPosEndInclusive + 1);
1108+
} else {
1109+
//Use the slower KTypeIndexedContainer sort
1110+
KTypeSort.quicksort(this, beginIndex, endIndex);
10981111
}
1112+
10991113
}
11001114

11011115
/**
@@ -1112,26 +1126,32 @@ public void sort(final int beginIndex, final int endIndex) {
11121126
public void sort(final int beginIndex, final int endIndex,
11131127
/*! #if ($TemplateOptions.KTypeGeneric) !*/
11141128
final Comparator<? super KType>
1115-
/*! #else
1116-
KTypeComparator<? super KType>
1117-
#end !*/
1118-
comp) {
1129+
/*! #else
1130+
KTypeComparator<? super KType>
1131+
#end !*/
1132+
comp) {
11191133

1120-
if (endIndex - beginIndex > 1) {
1121-
//Fast path : if the actual indices matching [beginIndex; endIndex[
1122-
//in the underlying buffer are in increasing order (means there is no folding of buffer in the interval),
1123-
// use quicksort array version directly.
1124-
final int bufferPosStart = indexToBufferPosition(beginIndex);
1125-
final int bufferPosEndInclusive = indexToBufferPosition(endIndex - 1); //must be valid indices
1134+
checkRangeBounds(beginIndex, endIndex);
11261135

1127-
if (bufferPosEndInclusive > bufferPosStart) {
1136+
if (beginIndex == endIndex) {
11281137

1129-
KTypeSort.quicksort(Intrinsics.<KType[]> cast(this.buffer), bufferPosStart, bufferPosEndInclusive + 1, comp);
1130-
} else {
1131-
//Use the slower KTypeIndexedContainer sort
1132-
KTypeSort.quicksort(this, beginIndex, endIndex, comp);
1133-
}
1138+
return; //nothing to do
11341139
}
1140+
1141+
//Fast path : if the actual indices matching [beginIndex; endIndex[
1142+
//in the underlying buffer are in increasing order (means there is no folding of buffer in the interval),
1143+
// use quicksort array version directly.
1144+
final int bufferPosStart = indexToBufferPosition(beginIndex);
1145+
final int bufferPosEndInclusive = indexToBufferPosition(endIndex - 1); //must be valid indices
1146+
1147+
if (bufferPosEndInclusive > bufferPosStart) {
1148+
1149+
KTypeSort.quicksort(Intrinsics.<KType[]> cast(this.buffer), bufferPosStart, bufferPosEndInclusive + 1, comp);
1150+
} else {
1151+
//Use the slower KTypeIndexedContainer sort
1152+
KTypeSort.quicksort(this, beginIndex, endIndex, comp);
1153+
}
1154+
11351155
}
11361156

11371157
/**
@@ -1298,12 +1318,15 @@ private void removeBufferIndicesRange(final int fromBufferIndex, final int toBuf
12981318
@Override
12991319
public void removeRange(final int fromIndex, final int toIndex) {
13001320

1301-
if (fromIndex >= toIndex) {
1321+
checkRangeBounds(fromIndex, toIndex);
1322+
1323+
if (fromIndex == toIndex) {
13021324

1303-
throw new IllegalArgumentException("Index fromIndex " + fromIndex + " is >= toIndex " + toIndex);
1325+
return; //nothing to do
13041326
}
13051327

13061328
final int bufferPositionStart = indexToBufferPosition(fromIndex);
1329+
13071330
final int bufferPositionEndInclusive = indexToBufferPosition(toIndex - 1); //must be a valid index
13081331

13091332
removeBufferIndicesRange(bufferPositionStart, oneRight(bufferPositionEndInclusive, this.buffer.length));
@@ -1360,6 +1383,24 @@ private int indexToBufferPosition(final int index) {
13601383
return (int) bufferPos;
13611384
}
13621385

1386+
private void checkRangeBounds(final int beginIndex, final int endIndex) {
1387+
1388+
if (beginIndex > endIndex) {
1389+
1390+
throw new IllegalArgumentException("Index beginIndex " + beginIndex + " is > endIndex " + endIndex);
1391+
}
1392+
1393+
if (beginIndex < 0) {
1394+
1395+
throw new IndexOutOfBoundsException("Index beginIndex < 0");
1396+
}
1397+
1398+
if (endIndex > size()) {
1399+
1400+
throw new IndexOutOfBoundsException("Index endIndex " + endIndex + " out of bounds [" + 0 + ", " + size() + "].");
1401+
}
1402+
}
1403+
13631404
/*! #if ($TemplateOptions.declareInline("oneLeft(index, modulus)", "<*>==>(index >= 1) ? index - 1 : modulus - 1")) !*/
13641405
/**
13651406
* Move one index to the left, wrapping around buffer of size modulus.

0 commit comments

Comments
 (0)