Skip to content

Commit

Permalink
Merge pull request #541 from apache/removed_duplicate_req_sorted_view
Browse files Browse the repository at this point in the history
Removed duplicate req sorted view
  • Loading branch information
leerho authored Apr 8, 2024
2 parents 3604d5a + 9973ec1 commit 4f74acc
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 497 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ String getItemAsString(final int index) {
public int getK() { return k; }

//MinMax Methods

@Override
float getMaxItemInternal() { return maxFloatItem; }

@Override
public float getMaxItem() {
if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); }
Expand All @@ -187,7 +187,7 @@ String getMaxItemAsString() {

@Override
float getMinItemInternal() { return minFloatItem; }

@Override
public float getMinItem() {
if (isEmpty() || Float.isNaN(minFloatItem)) { throw new SketchesArgumentException(EMPTY_MSG); }
Expand All @@ -214,7 +214,7 @@ byte[] getMinMaxByteArr() {
void setMinItem(final float item) { this.minFloatItem = item; }

//END MinMax Methods

@Override
public long getN() { return n; }

Expand Down Expand Up @@ -286,10 +286,10 @@ void incNumLevels() {
void setFloatItemsArrayAt(final int index, final float item) { this.floatItems[index] = item; }

@Override
void setFloatItemsArrayAt(final int dstIndex, final float[] srcItems, final int srcOffset, final int length) { //TODO
void setFloatItemsArrayAt(final int dstIndex, final float[] srcItems, final int srcOffset, final int length) {
System.arraycopy(srcItems, srcOffset, floatItems, dstIndex, length);
}

@Override
void setLevelZeroSorted(final boolean sorted) { this.isLevelZeroSorted = sorted; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.datasketches.common.SketchesArgumentException;

/**
* The SortedView of the Quantiles Classic DoublesSketch and the KllDoublesSketch.
* The SortedView for the KllFloatsSketch and the ReqSketch.
* @author Alexander Saydakov
* @author Lee Rhodes
*/
Expand Down
102 changes: 92 additions & 10 deletions src/main/java/org/apache/datasketches/req/ReqSketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import org.apache.datasketches.common.SketchesArgumentException;
import org.apache.datasketches.memory.Memory;
import org.apache.datasketches.quantilescommon.FloatsSortedView;
import org.apache.datasketches.quantilescommon.FloatsSketchSortedView;
import org.apache.datasketches.quantilescommon.QuantileSearchCriteria;
import org.apache.datasketches.quantilescommon.QuantilesAPI;
import org.apache.datasketches.quantilescommon.QuantilesFloatsSketchIterator;
Expand Down Expand Up @@ -102,7 +102,7 @@ static class CompactorReturn {
private int retItems = 0; //number of retained items in the sketch
private int maxNomSize = 0; //sum of nominal capacities of all compactors
//Objects
private ReqSketchSortedView reqSV = null;
private FloatsSketchSortedView reqSV = null;
private List<ReqCompactor> compactors = new ArrayList<>();
private ReqDebug reqDebug = null; //user config, default: null, can be set after construction.

Expand Down Expand Up @@ -362,12 +362,6 @@ public int getSerializedSizeBytes() {
return ReqSerDe.getSerBytes(this, serDeFormat);
}

@Override
public FloatsSortedView getSortedView() {
refreshSortedView();
return reqSV;
}

@Override
public boolean isEmpty() {
return totalN == 0;
Expand Down Expand Up @@ -562,8 +556,96 @@ private void grow() {
if (reqDebug != null) { reqDebug.emitNewCompactor(lgWeight); }
}

private final void refreshSortedView() {
reqSV = (reqSV == null) ? new ReqSketchSortedView(this) : reqSV;
// SORTED VIEW

@Override
public FloatsSketchSortedView getSortedView() {
refreshSortedView();
return reqSV;
}

private final FloatsSketchSortedView refreshSortedView() {
if (reqSV == null) {
final CreateSortedView csv = new CreateSortedView();
reqSV = csv.getSV();
}
return reqSV;
}

private final class CreateSortedView {
float[] quantiles;
long[] cumWeights;

FloatsSketchSortedView getSV() {
if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); }
//build the SV arrays
final List<ReqCompactor> compactors = getCompactors();
final int numComp = compactors.size();
final int totalQuantiles = getNumRetained();
quantiles = new float[totalQuantiles]; //could have zero entries
cumWeights = new long[totalQuantiles];
int count = 0;
for (int i = 0; i < numComp; i++) {
final ReqCompactor c = compactors.get(i);
final FloatBuffer bufIn = c.getBuffer();
final long bufWeight = 1 << c.getLgWeight();
final int bufInLen = bufIn.getCount();
mergeSortIn(bufIn, bufWeight, count, getHighRankAccuracyMode());
count += bufInLen;
}
createCumulativeNativeRanks();
return new FloatsSketchSortedView(
quantiles, cumWeights, getN(), getMaxItem(), getMinItem());
}

/**
* Specially modified version of FloatBuffer.mergeSortIn(). Here spaceAtBottom is always false and
* the ultimate array size has already been set. However, this must simultaneously deal with
* sorting the base FloatBuffer as well.
*
* @param bufIn given FloatBuffer. If not sorted it will be sorted here.
* @param bufWeight associated weight of input FloatBuffer
* @param count tracks number of items inserted into the class arrays
*/
private void mergeSortIn(final FloatBuffer bufIn, final long bufWeight, final int count, final boolean hra) {
if (!bufIn.isSorted()) { bufIn.sort(); }
final float[] arrIn = bufIn.getArray(); //may be larger than its item count.
final int bufInLen = bufIn.getCount();
final int totLen = count + bufInLen;
int i = count - 1;
int j = bufInLen - 1;
int h = hra ? bufIn.getCapacity() - 1 : bufInLen - 1;
for (int k = totLen; k-- > 0; ) {
if (i >= 0 && j >= 0) { //both valid
if (quantiles[i] >= arrIn[h]) {
quantiles[k] = quantiles[i];
cumWeights[k] = cumWeights[i--]; //not yet natRanks, just individual wts
} else {
quantiles[k] = arrIn[h--]; j--;
cumWeights[k] = bufWeight;
}
} else if (i >= 0) { //i is valid
quantiles[k] = quantiles[i];
cumWeights[k] = cumWeights[i--];
} else if (j >= 0) { //j is valid
quantiles[k] = arrIn[h--]; j--;
cumWeights[k] = bufWeight;
} else {
break;
}
}
}

private void createCumulativeNativeRanks() {
final int len = quantiles.length;
for (int i = 1; i < len; i++) {
cumWeights[i] += cumWeights[i - 1];
}
if (totalN > 0) {
assert cumWeights[len - 1] == totalN;
}
}

} //End CreateSortedView

}
211 changes: 0 additions & 211 deletions src/main/java/org/apache/datasketches/req/ReqSketchSortedView.java

This file was deleted.

Loading

0 comments on commit 4f74acc

Please sign in to comment.