52
52
*/
53
53
/*! ${TemplateOptions.generatedAnnotation} !*/
54
54
public class KTypeArrayDeque <KType >
55
- extends AbstractKTypeCollection <KType > implements KTypeDeque <KType >, KTypeIndexedContainer <KType >, Cloneable
55
+ extends AbstractKTypeCollection <KType > implements KTypeDeque <KType >, KTypeIndexedContainer <KType >, Cloneable
56
56
{
57
57
/**
58
58
* Internal array for storing elements.
@@ -65,8 +65,8 @@ public class KTypeArrayDeque<KType>
65
65
KType []
66
66
#else !*/
67
67
Object []
68
- /*! #end !*/
69
- buffer ;
68
+ /*! #end !*/
69
+ buffer ;
70
70
71
71
/**
72
72
* 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) {
751
751
@ Override
752
752
public <T extends KTypeProcedure <? super KType >> T forEach (final T procedure , final int fromIndex , final int toIndex ) {
753
753
754
- if (fromIndex >= toIndex ) {
754
+ checkRangeBounds (fromIndex , toIndex );
755
755
756
- throw new IllegalArgumentException ("Index fromIndex " + fromIndex + " is >= toIndex " + toIndex );
756
+ if (fromIndex == toIndex ) {
757
+
758
+ return procedure ; //nothing to do
757
759
}
758
760
761
+ final int bufferPositionStart = indexToBufferPosition (fromIndex );
762
+
759
763
final int endBufferPosInclusive = indexToBufferPosition (toIndex - 1 ); //must be a valid index
760
764
761
- internalForEach (procedure , indexToBufferPosition ( fromIndex ) , oneRight (endBufferPosInclusive , this .buffer .length ));
765
+ internalForEach (procedure , bufferPositionStart , oneRight (endBufferPosInclusive , this .buffer .length ));
762
766
763
767
return procedure ;
764
768
}
@@ -791,14 +795,18 @@ public <T extends KTypePredicate<? super KType>> T forEach(final T predicate) {
791
795
@ Override
792
796
public <T extends KTypePredicate <? super KType >> T forEach (final T predicate , final int fromIndex , final int toIndex ) {
793
797
794
- if (fromIndex >= toIndex ) {
798
+ checkRangeBounds (fromIndex , toIndex );
799
+
800
+ if (fromIndex == toIndex ) {
795
801
796
- throw new IllegalArgumentException ( "Index fromIndex " + fromIndex + " is >= toIndex " + toIndex );
802
+ return predicate ; //nothing to do
797
803
}
798
804
805
+ final int bufferPositionStart = indexToBufferPosition (fromIndex );
806
+
799
807
final int endBufferPosInclusive = indexToBufferPosition (toIndex - 1 ); //must be a valid index
800
808
801
- internalForEach (predicate , indexToBufferPosition ( fromIndex ) , oneRight (endBufferPosInclusive , this .buffer .length ));
809
+ internalForEach (predicate , bufferPositionStart , oneRight (endBufferPosInclusive , this .buffer .length ));
802
810
803
811
return predicate ;
804
812
}
@@ -1035,7 +1043,7 @@ private boolean allIndexesEqual(final KTypeIndexedContainer<KType> b1, final KTy
1035
1043
* instead of using a constructor).
1036
1044
*/
1037
1045
public static /* #if ($TemplateOptions.KTypeGeneric) */ <KType > /* #end */
1038
- KTypeArrayDeque <KType > newInstance () {
1046
+ KTypeArrayDeque <KType > newInstance () {
1039
1047
return new KTypeArrayDeque <KType >();
1040
1048
}
1041
1049
@@ -1044,15 +1052,15 @@ KTypeArrayDeque<KType> newInstance() {
1044
1052
* instead of using a constructor).
1045
1053
*/
1046
1054
public static /* #if ($TemplateOptions.KTypeGeneric) */ <KType > /* #end */
1047
- KTypeArrayDeque <KType > newInstance (final int initialCapacity ) {
1055
+ KTypeArrayDeque <KType > newInstance (final int initialCapacity ) {
1048
1056
return new KTypeArrayDeque <KType >(initialCapacity );
1049
1057
}
1050
1058
1051
1059
/**
1052
1060
* Create a new deque by pushing a variable number of arguments to the end of it.
1053
1061
*/
1054
1062
public static /* #if ($TemplateOptions.KTypeGeneric) */ <KType > /* #end */
1055
- KTypeArrayDeque <KType > from (final KType ... elements ) {
1063
+ KTypeArrayDeque <KType > from (final KType ... elements ) {
1056
1064
final KTypeArrayDeque <KType > coll = new KTypeArrayDeque <KType >(elements .length );
1057
1065
coll .addLast (elements );
1058
1066
return coll ;
@@ -1062,7 +1070,7 @@ KTypeArrayDeque<KType> from(final KType... elements) {
1062
1070
* Create a new deque by pushing a variable number of arguments to the end of it.
1063
1071
*/
1064
1072
public static /* #if ($TemplateOptions.KTypeGeneric) */ <KType > /* #end */
1065
- KTypeArrayDeque <KType > from (final KTypeContainer <KType > container ) {
1073
+ KTypeArrayDeque <KType > from (final KTypeContainer <KType > container ) {
1066
1074
return new KTypeArrayDeque <KType >(container );
1067
1075
}
1068
1076
@@ -1081,21 +1089,27 @@ KTypeArrayDeque<KType> from(final KTypeContainer<KType> container) {
1081
1089
*/
1082
1090
public void sort (final int beginIndex , final int endIndex ) {
1083
1091
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 );
1090
1093
1091
- if (bufferPosEndInclusive > bufferPosStart ) {
1094
+ if (beginIndex == endIndex ) {
1092
1095
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 );
1098
1111
}
1112
+
1099
1113
}
1100
1114
1101
1115
/**
@@ -1112,26 +1126,32 @@ public void sort(final int beginIndex, final int endIndex) {
1112
1126
public void sort (final int beginIndex , final int endIndex ,
1113
1127
/*! #if ($TemplateOptions.KTypeGeneric) !*/
1114
1128
final Comparator <? super KType >
1115
- /*! #else
1116
- KTypeComparator<? super KType>
1117
- #end !*/
1118
- comp ) {
1129
+ /*! #else
1130
+ KTypeComparator<? super KType>
1131
+ #end !*/
1132
+ comp ) {
1119
1133
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 );
1126
1135
1127
- if (bufferPosEndInclusive > bufferPosStart ) {
1136
+ if (beginIndex == endIndex ) {
1128
1137
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
1134
1139
}
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
+
1135
1155
}
1136
1156
1137
1157
/**
@@ -1298,12 +1318,15 @@ private void removeBufferIndicesRange(final int fromBufferIndex, final int toBuf
1298
1318
@ Override
1299
1319
public void removeRange (final int fromIndex , final int toIndex ) {
1300
1320
1301
- if (fromIndex >= toIndex ) {
1321
+ checkRangeBounds (fromIndex , toIndex );
1322
+
1323
+ if (fromIndex == toIndex ) {
1302
1324
1303
- throw new IllegalArgumentException ( "Index fromIndex " + fromIndex + " is >= toIndex " + toIndex );
1325
+ return ; //nothing to do
1304
1326
}
1305
1327
1306
1328
final int bufferPositionStart = indexToBufferPosition (fromIndex );
1329
+
1307
1330
final int bufferPositionEndInclusive = indexToBufferPosition (toIndex - 1 ); //must be a valid index
1308
1331
1309
1332
removeBufferIndicesRange (bufferPositionStart , oneRight (bufferPositionEndInclusive , this .buffer .length ));
@@ -1360,6 +1383,24 @@ private int indexToBufferPosition(final int index) {
1360
1383
return (int ) bufferPos ;
1361
1384
}
1362
1385
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
+
1363
1404
/*! #if ($TemplateOptions.declareInline("oneLeft(index, modulus)", "<*>==>(index >= 1) ? index - 1 : modulus - 1")) !*/
1364
1405
/**
1365
1406
* Move one index to the left, wrapping around buffer of size modulus.
0 commit comments