-
Notifications
You must be signed in to change notification settings - Fork 205
/
KllDoublesSketch.java
669 lines (586 loc) · 23.8 KB
/
KllDoublesSketch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.datasketches.kll;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static org.apache.datasketches.common.ByteArrayUtil.putDoubleLE;
import static org.apache.datasketches.kll.KllSketch.SketchStructure.UPDATABLE;
import static org.apache.datasketches.kll.KllSketch.SketchType.DOUBLES_SKETCH;
import java.util.Arrays;
import java.util.Objects;
import org.apache.datasketches.common.ArrayOfItemsSerDe;
import org.apache.datasketches.common.SketchesArgumentException;
import org.apache.datasketches.common.SuppressFBWarnings;
import org.apache.datasketches.kll.KllDirectDoublesSketch.KllDirectCompactDoublesSketch;
import org.apache.datasketches.memory.DefaultMemoryRequestServer;
import org.apache.datasketches.memory.Memory;
import org.apache.datasketches.memory.MemoryRequestServer;
import org.apache.datasketches.memory.WritableMemory;
import org.apache.datasketches.quantilescommon.DoublesSketchSortedView;
import org.apache.datasketches.quantilescommon.QuantileSearchCriteria;
import org.apache.datasketches.quantilescommon.QuantilesDoublesAPI;
import org.apache.datasketches.quantilescommon.QuantilesDoublesSketchIterator;
/**
* This variation of the KllSketch implements primitive doubles.
*
* @see org.apache.datasketches.kll.KllSketch
*/
public abstract class KllDoublesSketch extends KllSketch implements QuantilesDoublesAPI {
private DoublesSketchSortedView doublesSV = null;
final static int ITEM_BYTES = Double.BYTES;
KllDoublesSketch(
final SketchStructure sketchStructure) {
super(SketchType.DOUBLES_SKETCH, sketchStructure);
}
//Factories for new heap instances.
/**
* Create a new heap instance of this sketch with the default <em>k = 200</em>.
* The default <em>k</em> = 200 results in a normalized rank error of about
* 1.65%. Larger K will have smaller error but the sketch will be larger (and slower).
* @return new KllDoublesSketch on the Java heap.
*/
public static KllDoublesSketch newHeapInstance() {
return newHeapInstance(DEFAULT_K);
}
/**
* Create a new heap instance of this sketch with a given parameter <em>k</em>.
* <em>k</em> can be between 8, inclusive, and 65535, inclusive.
* The default <em>k</em> = 200 results in a normalized rank error of about
* 1.65%. Larger K will have smaller error but the sketch will be larger (and slower).
* @param k parameter that controls size of the sketch and accuracy of estimates.
* @return new KllDoublesSketch on the Java heap.
*/
public static KllDoublesSketch newHeapInstance(final int k) {
return new KllHeapDoublesSketch(k, DEFAULT_M);
}
//Factories for new direct instances.
/**
* Create a new direct updatable instance of this sketch with the default <em>k</em>.
* The default <em>k</em> = 200 results in a normalized rank error of about
* 1.65%. Larger <em>k</em> will have smaller error but the sketch will be larger (and slower).
* @param dstMem the given destination WritableMemory object for use by the sketch
* @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
* @return a new direct instance of this sketch
*/
public static KllDoublesSketch newDirectInstance(
final WritableMemory dstMem,
final MemoryRequestServer memReqSvr) {
return newDirectInstance(DEFAULT_K, dstMem, memReqSvr);
}
/**
* Create a new direct updatable instance of this sketch with a given <em>k</em>.
* @param k parameter that controls size of the sketch and accuracy of estimates.
* @param dstMem the given destination WritableMemory object for use by the sketch
* @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
* @return a new direct instance of this sketch
*/
public static KllDoublesSketch newDirectInstance(
final int k,
final WritableMemory dstMem,
final MemoryRequestServer memReqSvr) {
Objects.requireNonNull(dstMem, "Parameter 'dstMem' must not be null");
Objects.requireNonNull(memReqSvr, "Parameter 'memReqSvr' must not be null");
return KllDirectDoublesSketch.newDirectUpdatableInstance(k, DEFAULT_M, dstMem, memReqSvr);
}
//Factory to create an heap instance from a Memory image
/**
* Factory heapify takes a compact sketch image in Memory and instantiates an on-heap sketch.
* The resulting sketch will not retain any link to the source Memory.
* @param srcMem a compact Memory image of a sketch serialized by this sketch.
* <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a>
* @return a heap-based sketch based on the given Memory.
*/
public static KllDoublesSketch heapify(final Memory srcMem) {
Objects.requireNonNull(srcMem, "Parameter 'srcMem' must not be null");
return KllHeapDoublesSketch.heapifyImpl(srcMem);
}
//Factory to wrap a Read-Only Memory
/**
* Wrap a sketch around the given read only compact source Memory containing sketch data
* that originated from this sketch.
* @param srcMem the read only source Memory
* @return instance of this sketch
*/
public static KllDoublesSketch wrap(final Memory srcMem) {
Objects.requireNonNull(srcMem, "Parameter 'srcMem' must not be null");
final KllMemoryValidate memVal = new KllMemoryValidate(srcMem, DOUBLES_SKETCH, null);
if (memVal.sketchStructure == UPDATABLE) {
final MemoryRequestServer memReqSvr = new DefaultMemoryRequestServer(); //dummy
return new KllDirectDoublesSketch(memVal.sketchStructure, (WritableMemory)srcMem, memReqSvr, memVal);
} else {
return new KllDirectCompactDoublesSketch(memVal.sketchStructure, srcMem, memVal);
}
}
//Factory to wrap a WritableMemory image
/**
* Wrap a sketch around the given source Writable Memory containing sketch data
* that originated from this sketch.
* @param srcMem a WritableMemory that contains data.
* @param memReqSvr the given MemoryRequestServer to request a larger WritableMemory
* @return instance of this sketch
*/
public static KllDoublesSketch writableWrap(
final WritableMemory srcMem,
final MemoryRequestServer memReqSvr) {
Objects.requireNonNull(srcMem, "Parameter 'srcMem' must not be null");
Objects.requireNonNull(memReqSvr, "Parameter 'memReqSvr' must not be null");
final KllMemoryValidate memVal = new KllMemoryValidate(srcMem, DOUBLES_SKETCH);
if (memVal.sketchStructure == UPDATABLE) {
return new KllDirectDoublesSketch(UPDATABLE, srcMem, memReqSvr, memVal);
} else {
return new KllDirectCompactDoublesSketch(memVal.sketchStructure, srcMem, memVal);
}
}
//END of Constructors
@Override
public double[] getCDF(final double[] splitPoints, final QuantileSearchCriteria searchCrit) {
if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); }
refreshSortedView();
return doublesSV.getCDF(splitPoints, searchCrit);
}
@Override
public double[] getPMF(final double[] splitPoints, final QuantileSearchCriteria searchCrit) {
if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); }
refreshSortedView();
return doublesSV.getPMF(splitPoints, searchCrit);
}
@Override
public double getQuantile(final double rank, final QuantileSearchCriteria searchCrit) {
if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); }
refreshSortedView();
return doublesSV.getQuantile(rank, searchCrit);
}
@Override
public double[] getQuantiles(final double[] ranks, final QuantileSearchCriteria searchCrit) {
if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); }
refreshSortedView();
final int len = ranks.length;
final double[] quantiles = new double[len];
for (int i = 0; i < len; i++) {
quantiles[i] = doublesSV.getQuantile(ranks[i], searchCrit);
}
return quantiles;
}
/**
* {@inheritDoc}
* The approximate probability that the true quantile is within the confidence interval
* specified by the upper and lower quantile bounds for this sketch is 0.99.
*/
@Override
public double getQuantileLowerBound(final double rank) {
return getQuantile(max(0, rank - KllHelper.getNormalizedRankError(getMinK(), false)));
}
/**
* {@inheritDoc}
* The approximate probability that the true quantile is within the confidence interval
* specified by the upper and lower quantile bounds for this sketch is 0.99.
*/
@Override
public double getQuantileUpperBound(final double rank) {
return getQuantile(min(1.0, rank + KllHelper.getNormalizedRankError(getMinK(), false)));
}
@Override
public double getRank(final double quantile, final QuantileSearchCriteria searchCrit) {
if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); }
refreshSortedView();
return doublesSV.getRank(quantile, searchCrit);
}
/**
* {@inheritDoc}
* The approximate probability that the true rank is within the confidence interval
* specified by the upper and lower rank bounds for this sketch is 0.99.
*/
@Override
public double getRankLowerBound(final double rank) {
return max(0.0, rank - KllHelper.getNormalizedRankError(getMinK(), false));
}
/**
* {@inheritDoc}
* The approximate probability that the true rank is within the confidence interval
* specified by the upper and lower rank bounds for this sketch is 0.99.
*/
@Override
public double getRankUpperBound(final double rank) {
return min(1.0, rank + KllHelper.getNormalizedRankError(getMinK(), false));
}
@Override
public double[] getRanks(final double[] quantiles, final QuantileSearchCriteria searchCrit) {
if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); }
refreshSortedView();
final int len = quantiles.length;
final double[] ranks = new double[len];
for (int i = 0; i < len; i++) {
ranks[i] = doublesSV.getRank(quantiles[i], searchCrit);
}
return ranks;
}
@Override
public QuantilesDoublesSketchIterator iterator() {
return new KllDoublesSketchIterator(
getDoubleItemsArray(), getLevelsArray(SketchStructure.UPDATABLE), getNumLevels());
}
@Override
public final void merge(final KllSketch other) {
if (readOnly || sketchStructure != UPDATABLE) { throw new SketchesArgumentException(TGT_IS_READ_ONLY_MSG); }
if (this == other) { throw new SketchesArgumentException(SELF_MERGE_MSG); }
final KllDoublesSketch othDblSk = (KllDoublesSketch)other;
if (othDblSk.isEmpty()) { return; }
KllDoublesHelper.mergeDoubleImpl(this, othDblSk);
doublesSV = null;
}
/**
* {@inheritDoc}
* <p>The parameter <i>k</i> will not change.</p>
*/
@Override
public final void reset() {
if (readOnly) { throw new SketchesArgumentException(TGT_IS_READ_ONLY_MSG); }
final int k = getK();
setN(0);
setMinK(k);
setNumLevels(1);
setLevelZeroSorted(false);
setLevelsArray(new int[] {k, k});
setMinItem(Double.NaN);
setMaxItem(Double.NaN);
setDoubleItemsArray(new double[k]);
doublesSV = null;
}
@Override
public byte[] toByteArray() {
return KllHelper.toByteArray(this, false);
}
@Override
public String toString(final boolean withLevels, final boolean withLevelsAndItems) {
KllSketch sketch = this;
if (withLevelsAndItems && sketchStructure != UPDATABLE) {
final Memory mem = getWritableMemory();
assert mem != null;
sketch = KllDoublesSketch.heapify(getWritableMemory());
}
return KllHelper.toStringImpl(sketch, withLevels, withLevelsAndItems, getSerDe());
}
//SINGLE UPDATE
@Override
public void update(final double item) {
if (Double.isNaN(item)) { return; } //ignore
if (readOnly) { throw new SketchesArgumentException(TGT_IS_READ_ONLY_MSG); }
updateDouble(this, item);
doublesSV = null;
}
//Also Called from KllDoublesHelper::merge
static void updateDouble(final KllDoublesSketch dblSk, final double item) {
dblSk.updateMinMax(item);
int freeSpace = dblSk.levelsArr[0];
assert (freeSpace >= 0);
if (freeSpace == 0) {
KllDoublesHelper.compressWhileUpdatingSketch(dblSk);
freeSpace = dblSk.levelsArr[0];
assert (freeSpace > 0);
}
dblSk.incN(1);
dblSk.setLevelZeroSorted(false);
final int nextPos = freeSpace - 1;
dblSk.setLevelsArrayAt(0, nextPos);
dblSk.setDoubleItemsArrayAt(nextPos, item);
}
/**
* Single update of min and max
* @param item the source item, it must not be a NaN.
*/
final void updateMinMax(final double item) {
if (isEmpty() || Double.isNaN(getMinItemInternal())) {
setMinItem(item);
setMaxItem(item);
} else {
setMinItem(min(getMinItemInternal(), item));
setMaxItem(max(getMaxItemInternal(), item));
}
}
//WEIGHTED UPDATE
/**
* Weighted update. Updates this sketch with the given item the number of times specified by the given integer weight.
* @param item the item to be repeated. NaNs are ignored.
* @param weight the number of times the update of item is to be repeated. It must be ≥ one.
*/
public void update(final double item, final long weight) {
if (Double.isNaN(item)) { return; } //ignore
if (readOnly) { throw new SketchesArgumentException(TGT_IS_READ_ONLY_MSG); }
if (weight < 1L) { throw new SketchesArgumentException("Weight is less than one."); }
if (weight == 1L) { updateDouble(this, item); }
else {
if (weight < levelsArr[0]) {
for (int i = 0; i < (int)weight; i++) { updateDouble(this, item); }
} else {
final KllHeapDoublesSketch tmpSk = new KllHeapDoublesSketch(getK(), DEFAULT_M, item, weight);
merge(tmpSk);
}
}
doublesSV = null;
}
// VECTOR UPDATE
/**
* Vector update. Updates this sketch with the given array (vector) of items, starting at the items
* offset for a length number of items. This is not supported for direct sketches.
* @param items the vector of items
* @param offset the starting index of the items[] array
* @param length the number of items
*/
public void update(final double[] items, final int offset, final int length) {
if (readOnly) { throw new SketchesArgumentException(TGT_IS_READ_ONLY_MSG); }
if (length == 0) { return; }
if (!hasNaN(items, offset, length)) {
updateDouble(items, offset, length); //fast path
doublesSV = null;
return;
}
//has at least one NaN
final int end = offset + length;
for (int i = offset; i < end; i++) {
final double v = items[i];
if (!Double.isNaN(v)) {
updateDouble(this, v); //normal path
doublesSV = null;
}
}
}
// No NaNs are allowed at this point
private void updateDouble(final double[] srcItems, final int srcOffset, final int length) {
if (isEmpty() || Double.isNaN(getMinItemInternal())) {
setMinItem(srcItems[srcOffset]); //initialize with a real value
setMaxItem(srcItems[srcOffset]);
}
int count = 0;
while (count < length) {
if (levelsArr[0] == 0) {
KllDoublesHelper.compressWhileUpdatingSketch(this);
}
final int spaceNeeded = length - count;
final int freeSpace = levelsArr[0];
assert (freeSpace > 0);
final int numItemsToCopy = min(spaceNeeded, freeSpace);
final int dstOffset = freeSpace - numItemsToCopy;
final int localSrcOffset = srcOffset + count;
setDoubleItemsArrayAt(dstOffset, srcItems, localSrcOffset, numItemsToCopy);
updateMinMax(srcItems, localSrcOffset, numItemsToCopy);
count += numItemsToCopy;
incN(numItemsToCopy);
setLevelsArrayAt(0, dstOffset);
}
setLevelZeroSorted(false);
}
/**
* Vector update of min and max.
* @param srcItems the input source array of values, no NaNs allowed.
* @param srcOffset the starting offset in srcItems
* @param length the number of items to update min and max
*/
private void updateMinMax(final double[] srcItems, final int srcOffset, final int length) {
final int end = srcOffset + length;
for (int i = srcOffset; i < end; i++) {
setMinItem(min(getMinItemInternal(), srcItems[i]));
setMaxItem(max(getMaxItemInternal(), srcItems[i]));
}
}
// this returns on the first detected NaN.
private static boolean hasNaN(final double[] items, final int offset, final int length) {
final int end = offset + length;
for (int i = offset; i < end; i++) {
if (Double.isNaN(items[i])) { return true; }
}
return false;
}
// END ALL UPDATE METHODS
/**
* @return full size of internal items array including empty space at bottom.
*/
abstract double[] getDoubleItemsArray();
/**
* @return items array of retained items.
*/
abstract double[] getDoubleRetainedItemsArray();
abstract double getDoubleSingleItem();
// Min & Max Methods
abstract double getMaxItemInternal();
abstract void setMaxItem(double item);
abstract double getMinItemInternal();
abstract void setMinItem(double item);
@Override
abstract byte[] getMinMaxByteArr();
@Override
int getMinMaxSizeBytes() {
return Double.BYTES * 2;
}
//END Min & Max Methods
@Override
abstract byte[] getRetainedItemsByteArr();
@Override
int getRetainedItemsSizeBytes() {
return getNumRetained() * Double.BYTES;
}
@Override
ArrayOfItemsSerDe<?> getSerDe() { return null; }
@Override
final byte[] getSingleItemByteArr() {
final byte[] bytes = new byte[ITEM_BYTES];
putDoubleLE(bytes, 0, getDoubleSingleItem());
return bytes;
}
@Override
int getSingleItemSizeBytes() {
return Double.BYTES;
}
@Override
abstract byte[] getTotalItemsByteArr();
@Override
int getTotalItemsNumBytes() {
return levelsArr[getNumLevels()] * Double.BYTES;
}
abstract void setDoubleItemsArray(double[] doubleItems);
abstract void setDoubleItemsArrayAt(int index, double item);
abstract void setDoubleItemsArrayAt(int dstIndex, double[] srcItems, int srcOffset, int length);
// SORTED VIEW
@Override
@SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "OK in this case.")
public DoublesSketchSortedView getSortedView() {
refreshSortedView();
return doublesSV;
}
private final DoublesSketchSortedView refreshSortedView() {
if (doublesSV == null) {
final CreateSortedView csv = new CreateSortedView();
doublesSV = csv.getSV();
}
return doublesSV;
}
private final class CreateSortedView {
double[] quantiles;
long[] cumWeights;
DoublesSketchSortedView getSV() {
if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); }
final double[] srcQuantiles = getDoubleItemsArray();
final int[] srcLevels = levelsArr;
final int srcNumLevels = getNumLevels();
if (!isLevelZeroSorted()) {
Arrays.sort(srcQuantiles, srcLevels[0], srcLevels[1]);
if (!hasMemory()) { setLevelZeroSorted(true); }
//we don't sort level0 in Memory, only our copy.
}
final int numQuantiles = getNumRetained();
quantiles = new double[numQuantiles];
cumWeights = new long[numQuantiles];
populateFromSketch(srcQuantiles, srcLevels, srcNumLevels, numQuantiles);
return new DoublesSketchSortedView(
quantiles, cumWeights, KllDoublesSketch.this);
}
private void populateFromSketch(final double[] srcQuantiles, final int[] srcLevels,
final int srcNumLevels, final int numItems) {
final int[] myLevels = new int[srcNumLevels + 1];
final int offset = srcLevels[0];
System.arraycopy(srcQuantiles, offset, quantiles, 0, numItems);
int srcLevel = 0;
int dstLevel = 0;
long weight = 1;
while (srcLevel < srcNumLevels) {
final int fromIndex = srcLevels[srcLevel] - offset;
final int toIndex = srcLevels[srcLevel + 1] - offset; // exclusive
if (fromIndex < toIndex) { // if equal, skip empty level
Arrays.fill(cumWeights, fromIndex, toIndex, weight);
myLevels[dstLevel] = fromIndex;
myLevels[dstLevel + 1] = toIndex;
dstLevel++;
}
srcLevel++;
weight *= 2;
}
final int numLevels = dstLevel;
blockyTandemMergeSort(quantiles, cumWeights, myLevels, numLevels); //create unit weights
KllHelper.convertToCumulative(cumWeights);
}
} //End of class CreateSortedView
private static void blockyTandemMergeSort(final double[] quantiles, final long[] weights,
final int[] levels, final int numLevels) {
if (numLevels == 1) { return; }
// duplicate the input in preparation for the "ping-pong" copy reduction strategy.
final double[] quantilesTmp = Arrays.copyOf(quantiles, quantiles.length);
final long[] weightsTmp = Arrays.copyOf(weights, quantiles.length); // don't need the extra one
blockyTandemMergeSortRecursion(quantilesTmp, weightsTmp, quantiles, weights, levels, 0, numLevels);
}
private static void blockyTandemMergeSortRecursion(
final double[] quantilesSrc, final long[] weightsSrc,
final double[] quantilesDst, final long[] weightsDst,
final int[] levels, final int startingLevel, final int numLevels) {
if (numLevels == 1) { return; }
final int numLevels1 = numLevels / 2;
final int numLevels2 = numLevels - numLevels1;
assert numLevels1 >= 1;
assert numLevels2 >= numLevels1;
final int startingLevel1 = startingLevel;
final int startingLevel2 = startingLevel + numLevels1;
// swap roles of src and dst
blockyTandemMergeSortRecursion(
quantilesDst, weightsDst,
quantilesSrc, weightsSrc,
levels, startingLevel1, numLevels1);
blockyTandemMergeSortRecursion(
quantilesDst, weightsDst,
quantilesSrc, weightsSrc,
levels, startingLevel2, numLevels2);
tandemMerge(
quantilesSrc, weightsSrc,
quantilesDst, weightsDst,
levels,
startingLevel1, numLevels1,
startingLevel2, numLevels2);
}
private static void tandemMerge(
final double[] quantilesSrc, final long[] weightsSrc,
final double[] quantilesDst, final long[] weightsDst,
final int[] levelStarts,
final int startingLevel1, final int numLevels1,
final int startingLevel2, final int numLevels2) {
final int fromIndex1 = levelStarts[startingLevel1];
final int toIndex1 = levelStarts[startingLevel1 + numLevels1]; // exclusive
final int fromIndex2 = levelStarts[startingLevel2];
final int toIndex2 = levelStarts[startingLevel2 + numLevels2]; // exclusive
int iSrc1 = fromIndex1;
int iSrc2 = fromIndex2;
int iDst = fromIndex1;
while (iSrc1 < toIndex1 && iSrc2 < toIndex2) {
if (quantilesSrc[iSrc1] < quantilesSrc[iSrc2]) {
quantilesDst[iDst] = quantilesSrc[iSrc1];
weightsDst[iDst] = weightsSrc[iSrc1];
iSrc1++;
} else {
quantilesDst[iDst] = quantilesSrc[iSrc2];
weightsDst[iDst] = weightsSrc[iSrc2];
iSrc2++;
}
iDst++;
}
if (iSrc1 < toIndex1) {
System.arraycopy(quantilesSrc, iSrc1, quantilesDst, iDst, toIndex1 - iSrc1);
System.arraycopy(weightsSrc, iSrc1, weightsDst, iDst, toIndex1 - iSrc1);
} else if (iSrc2 < toIndex2) {
System.arraycopy(quantilesSrc, iSrc2, quantilesDst, iDst, toIndex2 - iSrc2);
System.arraycopy(weightsSrc, iSrc2, weightsDst, iDst, toIndex2 - iSrc2);
}
}
// END SORTED VIEW
}