2222import static org .apache .datasketches .common .Util .LS ;
2323import static org .apache .datasketches .quantilescommon .QuantileSearchCriteria .INCLUSIVE ;
2424
25+ import java .lang .foreign .MemorySegment ;
2526import java .util .Arrays ;
2627
28+ import org .apache .datasketches .common .positional .PositionalSegment ;
2729import org .apache .datasketches .common .SketchesArgumentException ;
28- import org .apache .datasketches .memory .WritableBuffer ;
29- import org .apache .datasketches .memory .WritableMemory ;
3030import org .apache .datasketches .quantilescommon .InequalitySearch ;
3131import org .apache .datasketches .quantilescommon .QuantileSearchCriteria ;
3232
@@ -176,7 +176,7 @@ FloatBuffer ensureCapacity(final int newCapacity) {
176176 * @return this
177177 */
178178 private FloatBuffer ensureSpace (final int space ) {
179- if (count_ + space > capacity_ ) {
179+ if (( count_ + space ) > capacity_ ) {
180180 final int newCap = count_ + space + delta_ ;
181181 ensureCapacity (newCap );
182182 }
@@ -219,7 +219,7 @@ int getCountWithCriterion(final float item, final QuantileSearchCriteria searchC
219219 }
220220 final InequalitySearch crit = (searchCrit == INCLUSIVE ) ? InequalitySearch .LE : InequalitySearch .LT ;
221221 final int index = InequalitySearch .find (arr_ , low , high , item , crit );
222- return index == -1 ? 0 : index - low + 1 ;
222+ return index == -1 ? 0 : ( index - low ) + 1 ;
223223 }
224224
225225 /**
@@ -234,8 +234,8 @@ int getCountWithCriterion(final float item, final QuantileSearchCriteria searchC
234234 * @return the selected odds from the range
235235 */
236236 FloatBuffer getEvensOrOdds (final int startOffset , final int endOffset , final boolean odds ) {
237- final int start = spaceAtBottom_ ? capacity_ - count_ + startOffset : startOffset ;
238- final int end = spaceAtBottom_ ? capacity_ - count_ + endOffset : endOffset ;
237+ final int start = spaceAtBottom_ ? ( capacity_ - count_ ) + startOffset : startOffset ;
238+ final int end = spaceAtBottom_ ? ( capacity_ - count_ ) + endOffset : endOffset ;
239239 sort ();
240240 final int range = endOffset - startOffset ;
241241 if ((range & 1 ) == 1 ) {
@@ -265,7 +265,7 @@ float getItemFromIndex(final int index) {
265265 * @return an item given its offset
266266 */
267267 float getItem (final int offset ) {
268- final int index = spaceAtBottom_ ? capacity_ - count_ + offset : offset ;
268+ final int index = spaceAtBottom_ ? ( capacity_ - count_ ) + offset : offset ;
269269 return arr_ [index ];
270270 }
271271
@@ -317,11 +317,11 @@ boolean isEmpty() {
317317 * @return true iff this is exactly equal to that FloatBuffer.
318318 */
319319 boolean isEqualTo (final FloatBuffer that ) {
320- if (capacity_ != that .capacity_
321- || count_ != that .count_
322- || delta_ != that .delta_
323- || sorted_ != that .sorted_
324- || spaceAtBottom_ != that .spaceAtBottom_ ) { return false ; }
320+ if (( capacity_ != that .capacity_ )
321+ || ( count_ != that .count_ )
322+ || ( delta_ != that .delta_ )
323+ || ( sorted_ != that .sorted_ )
324+ || ( spaceAtBottom_ != that .spaceAtBottom_ ) ) { return false ; }
325325 for (int i = 0 ; i < capacity_ ; i ++) {
326326 if (arr_ [i ] != that .arr_ [i ]) { return false ; }
327327 }
@@ -354,7 +354,7 @@ FloatBuffer mergeSortIn(final FloatBuffer bufIn) {
354354 int i = capacity_ - count_ ;
355355 int j = bufIn .capacity_ - bufIn .count_ ;
356356 for (int k = tgtStart ; k < capacity_ ; k ++) {
357- if (i < capacity_ && j < bufIn .capacity_ ) { //both valid
357+ if (( i < capacity_ ) && ( j < bufIn .capacity_ ) ) { //both valid
358358 arr_ [k ] = arr_ [i ] <= arrIn [j ] ? arr_ [i ++] : arrIn [j ++];
359359 } else if (i < capacity_ ) { //i is valid
360360 arr_ [k ] = arr_ [i ++];
@@ -368,7 +368,7 @@ FloatBuffer mergeSortIn(final FloatBuffer bufIn) {
368368 int i = count_ - 1 ;
369369 int j = bufInLen - 1 ;
370370 for (int k = totLen ; k -- > 0 ; ) {
371- if (i >= 0 && j >= 0 ) { //both valid
371+ if (( i >= 0 ) && ( j >= 0 ) ) { //both valid
372372 arr_ [k ] = arr_ [i ] >= arrIn [j ] ? arr_ [i --] : arrIn [j --];
373373 } else if (i >= 0 ) { //i is valid
374374 arr_ [k ] = arr_ [i --];
@@ -401,18 +401,18 @@ FloatBuffer sort() {
401401 byte [] floatsToBytes () {
402402 final int bytes = Float .BYTES * count_ ;
403403 final byte [] arr = new byte [bytes ];
404- final WritableBuffer wbuf = WritableMemory . writableWrap ( arr ). asWritableBuffer ( );
404+ final PositionalSegment posSeg = PositionalSegment . wrap ( MemorySegment . ofArray ( arr ) );
405405 if (spaceAtBottom_ ) {
406- wbuf . putFloatArray (arr_ , capacity_ - count_ , count_ );
406+ posSeg . setFloatArray (arr_ , capacity_ - count_ , count_ );
407407 } else {
408- wbuf . putFloatArray (arr_ , 0 , count_ );
408+ posSeg . setFloatArray (arr_ , 0 , count_ );
409409 }
410- assert wbuf .getPosition () == bytes ;
410+ assert posSeg .getPosition () == bytes ;
411411 return arr ;
412412 }
413413
414414 /**
415- * Returns a printable formatted string of the items of this buffer separated by a single space.
415+ * Returns a printable formatted string of the items of this FloatBuffer separated by a single space.
416416 * @param fmt The format for each printed item.
417417 * @param width the number of items to print per line
418418 * @return a printable, formatted string of the items of this buffer.
@@ -427,7 +427,7 @@ String toHorizList(final String fmt, final int width) {
427427 for (int i = start ; i < end ; i ++) {
428428 final float v = arr_ [i ];
429429 final String str = String .format (fmt , v );
430- if (i > start && ++cnt % width == 0 ) { sb .append (LS ).append (spaces ); }
430+ if (( i > start ) && (( ++cnt % width ) == 0 ) ) { sb .append (LS ).append (spaces ); }
431431 sb .append (str );
432432 }
433433 return sb .toString ();
0 commit comments