|
| 1 | +/* |
| 2 | + * Copyright 2018, Yahoo! Inc. Licensed under the terms of the |
| 3 | + * Apache License 2.0. See LICENSE file at the project root for terms. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.yahoo.sketches.kll; |
| 7 | + |
| 8 | +import java.util.Arrays; |
| 9 | + |
| 10 | +import com.yahoo.sketches.QuantilesHelper; |
| 11 | + |
| 12 | +/** |
| 13 | + * Data structure for answering quantile queries based on the samples from KllSketch |
| 14 | + * @author Kevin Lang |
| 15 | + * @author Alexander Saydakov |
| 16 | + */ |
| 17 | +final class KllFloatsQuantileCalculator { |
| 18 | + |
| 19 | + private long n_; |
| 20 | + private float[] items_; |
| 21 | + private long[] weights_; |
| 22 | + private int[] levels_; |
| 23 | + private int numLevels_; |
| 24 | + |
| 25 | + // assumes that all levels are sorted including level 0 |
| 26 | + KllFloatsQuantileCalculator(final float[] items, final int[] levels, final int numLevels, |
| 27 | + final long n) { |
| 28 | + n_ = n; |
| 29 | + final int numItems = levels[numLevels] - levels[0]; |
| 30 | + items_ = new float[numItems]; |
| 31 | + weights_ = new long[numItems + 1]; // one more is intentional |
| 32 | + levels_ = new int[numLevels + 1]; |
| 33 | + populateFromSketch(items, levels, numLevels, numItems); |
| 34 | + blockyTandemMergeSort(items_, weights_, levels_, numLevels_); |
| 35 | + QuantilesHelper.convertToPrecedingCummulative(weights_); |
| 36 | + } |
| 37 | + |
| 38 | + float getQuantile(final double phi) { |
| 39 | + final long pos = QuantilesHelper.posOfPhi(phi, n_); |
| 40 | + return approximatelyAnswerPositonalQuery(pos); |
| 41 | + } |
| 42 | + |
| 43 | + private float approximatelyAnswerPositonalQuery(final long pos) { |
| 44 | + assert pos >= 0; |
| 45 | + assert pos < n_; |
| 46 | + final int index = QuantilesHelper.chunkContainingPos(weights_, pos); |
| 47 | + return items_[index]; |
| 48 | + } |
| 49 | + |
| 50 | + private void populateFromSketch(final float[] srcItems, final int[] srcLevels, |
| 51 | + final int numLevels, final int numItems) { |
| 52 | + final int offset = srcLevels[0]; |
| 53 | + System.arraycopy(srcItems, offset, items_, 0, numItems); |
| 54 | + int srcLevel = 0; |
| 55 | + int dstLevel = 0; |
| 56 | + int weight = 1; |
| 57 | + while (srcLevel < numLevels) { |
| 58 | + final int fromIndex = srcLevels[srcLevel] - offset; |
| 59 | + final int toIndex = srcLevels[srcLevel + 1] - offset; // exclusive |
| 60 | + if (fromIndex < toIndex) { // skip empty levels |
| 61 | + Arrays.fill(weights_, fromIndex, toIndex, weight); |
| 62 | + levels_[dstLevel] = fromIndex; |
| 63 | + levels_[dstLevel + 1] = toIndex; |
| 64 | + dstLevel++; |
| 65 | + } |
| 66 | + srcLevel++; |
| 67 | + weight *= 2; |
| 68 | + } |
| 69 | + weights_[numItems] = 0; |
| 70 | + numLevels_ = dstLevel; |
| 71 | + } |
| 72 | + |
| 73 | + private static void blockyTandemMergeSort(final float[] items, final long[] weights, |
| 74 | + final int[] levels, final int numLevels) { |
| 75 | + if (numLevels == 1) { return; } |
| 76 | + |
| 77 | + // duplicate the input in preparation for the "ping-pong" copy reduction strategy. |
| 78 | + final float[] itemsTmp = Arrays.copyOf(items, items.length); |
| 79 | + final long[] weightsTmp = Arrays.copyOf(weights, items.length); // don't need the extra one here |
| 80 | + |
| 81 | + blockyTandemMergeSortRecursion(itemsTmp, weightsTmp, items, weights, levels, 0, numLevels); |
| 82 | + } |
| 83 | + |
| 84 | + private static void blockyTandemMergeSortRecursion(final float[] itemsSrc, final long[] weightsSrc, |
| 85 | + final float[] itemsDst, final long[] weightsDst, final int[] levels, final int startingLevel, |
| 86 | + final int numLevels) { |
| 87 | + if (numLevels == 1) { return; } |
| 88 | + final int numLevels1 = numLevels / 2; |
| 89 | + final int numLevels2 = numLevels - numLevels1; |
| 90 | + assert numLevels1 >= 1; |
| 91 | + assert numLevels2 >= numLevels1; |
| 92 | + final int startingLevel1 = startingLevel; |
| 93 | + final int startingLevel2 = startingLevel + numLevels1; |
| 94 | + // swap roles of src and dst |
| 95 | + blockyTandemMergeSortRecursion(itemsDst, weightsDst, itemsSrc, weightsSrc, levels, |
| 96 | + startingLevel1, numLevels1); |
| 97 | + blockyTandemMergeSortRecursion(itemsDst, weightsDst, itemsSrc, weightsSrc, levels, |
| 98 | + startingLevel2, numLevels2); |
| 99 | + tandemMerge(itemsSrc, weightsSrc, itemsDst, weightsDst, levels, startingLevel1, numLevels1, |
| 100 | + startingLevel2, numLevels2); |
| 101 | + } |
| 102 | + |
| 103 | + private static void tandemMerge(final float[] itemsSrc, final long[] weightsSrc, |
| 104 | + final float[] itemsDst, final long[] weightsDst, |
| 105 | + final int[] levelStarts, final int startingLevel1, final int numLevels1, |
| 106 | + final int startingLevel2, final int numLevels2) { |
| 107 | + final int fromIndex1 = levelStarts[startingLevel1]; |
| 108 | + final int toIndex1 = levelStarts[startingLevel1 + numLevels1]; // exclusive |
| 109 | + final int fromIndex2 = levelStarts[startingLevel2]; |
| 110 | + final int toIndex2 = levelStarts[startingLevel2 + numLevels2]; // exclusive |
| 111 | + int iSrc1 = fromIndex1; |
| 112 | + int iSrc2 = fromIndex2; |
| 113 | + int iDst = fromIndex1; |
| 114 | + |
| 115 | + while ((iSrc1 < toIndex1) && (iSrc2 < toIndex2)) { |
| 116 | + if (itemsSrc[iSrc1] < itemsSrc[iSrc2]) { |
| 117 | + itemsDst[iDst] = itemsSrc[iSrc1]; |
| 118 | + weightsDst[iDst] = weightsSrc[iSrc1]; |
| 119 | + iSrc1++; |
| 120 | + } else { |
| 121 | + itemsDst[iDst] = itemsSrc[iSrc2]; |
| 122 | + weightsDst[iDst] = weightsSrc[iSrc2]; |
| 123 | + iSrc2++; |
| 124 | + } |
| 125 | + iDst++; |
| 126 | + } |
| 127 | + if (iSrc1 < toIndex1) { |
| 128 | + System.arraycopy(itemsSrc, iSrc1, itemsDst, iDst, toIndex1 - iSrc1); |
| 129 | + System.arraycopy(weightsSrc, iSrc1, weightsDst, iDst, toIndex1 - iSrc1); |
| 130 | + } else if (iSrc2 < toIndex2) { |
| 131 | + System.arraycopy(itemsSrc, iSrc2, itemsDst, iDst, toIndex2 - iSrc2); |
| 132 | + System.arraycopy(weightsSrc, iSrc2, weightsDst, iDst, toIndex2 - iSrc2); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments