Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicate code #485

Merged
merged 4 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ final class KllDirectCompactItemsSketch<T> extends KllItemsSketch<T> {
levelsArr = memVal.levelsArr; //always converted to writable form.
}

//End of constructors

@Override
String getItemAsString(final int index) {
if (isEmpty()) { return "Null"; }
return serDe.toString(getTotalItemsArray()[index]);
}

@Override
public int getK() {
return getMemoryK(mem);
Expand All @@ -83,6 +91,12 @@ public T getMaxItem() {
return serDe.deserializeFromMemory(mem, offset, 2)[1];
}

@Override
String getMaxItemAsString() {
if (isEmpty()) { return "Null"; }
return serDe.toString(getMaxItem());
}

@Override
public T getMinItem() {
if (sketchStructure == COMPACT_EMPTY || isEmpty()) {
Expand All @@ -96,6 +110,12 @@ public T getMinItem() {
return serDe.deserializeFromMemory(mem, offset, 1)[0];
}

@Override
String getMinItemAsString() {
if (isEmpty()) { return "Null"; }
return serDe.toString(getMinItem());
}

@Override
public long getN() {
if (sketchStructure == COMPACT_EMPTY) { return 0; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ static KllDirectDoublesSketch newDirectUpdatableInstance(
return new KllDirectDoublesSketch(UPDATABLE, wMem, memReqSvr, memVal);
}

//END of Constructors
//End of constructors

@Override
String getItemAsString(final int index) {
if (isEmpty()) { return "NaN"; }
return Double.toString(getDoubleItemsArray()[index]);
}

@Override
public int getK() {
Expand All @@ -137,6 +143,12 @@ else if (sketchStructure == COMPACT_FULL) {
return wmem.getDouble(offset);
}

@Override
String getMaxItemAsString() {
if (isEmpty()) { return "NaN"; }
return Double.toString(getMaxItem());
}

@Override
public double getMinItem() {
int levelsArrBytes = 0;
Expand All @@ -151,6 +163,12 @@ else if (sketchStructure == COMPACT_FULL) {
return wmem.getDouble(offset);
}

@Override
String getMinItemAsString() {
if (isEmpty()) { return "NaN"; }
return Double.toString(getMinItem());
}

@Override
public long getN() {
if (sketchStructure == COMPACT_EMPTY) { return 0; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ static KllDirectFloatsSketch newDirectUpdatableInstance(

//END of Constructors

@Override
String getItemAsString(final int index) {
if (isEmpty()) { return "NaN"; }
return Float.toString(getFloatItemsArray()[index]);
}

@Override
public int getK() {
return getMemoryK(wmem);
Expand All @@ -137,6 +143,12 @@ else if (sketchStructure == COMPACT_FULL) {
return wmem.getFloat(offset);
}

@Override
String getMaxItemAsString() {
if (isEmpty()) { return "NaN"; }
return Float.toString(getMaxItem());
}

@Override
public float getMinItem() {
int levelsArrBytes = 0;
Expand All @@ -151,6 +163,12 @@ else if (sketchStructure == COMPACT_FULL) {
return wmem.getFloat(offset);
}

@Override
String getMinItemAsString() {
if (isEmpty()) { return "NaN"; }
return Float.toString(getMinItem());
}

@Override
public long getN() {
if (sketchStructure == COMPACT_EMPTY) { return 0; }
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/org/apache/datasketches/kll/KllDoublesHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,16 @@ static void updateDouble(final KllDoublesSketch dblSk,
dblSk.setMinItem(min(dblSk.getMinItem(), item));
dblSk.setMaxItem(max(dblSk.getMaxItem(), item));
}
if (dblSk.levelsArr[0] == 0) { compressWhileUpdatingSketch(dblSk); }
final int myLevelsArrAtZero = dblSk.levelsArr[0]; //LevelsArr could be expanded
int level0space = dblSk.levelsArr[0];
assert (level0space >= 0);
if (level0space == 0) {
compressWhileUpdatingSketch(dblSk);
level0space = dblSk.levelsArr[0];
assert (level0space > 0);
}
dblSk.incN();
dblSk.setLevelZeroSorted(false);
final int nextPos = myLevelsArrAtZero - 1;
assert myLevelsArrAtZero >= 0;
final int nextPos = level0space - 1;
dblSk.setLevelsArrayAt(0, nextPos);
dblSk.setDoubleItemsArrayAt(nextPos, item);
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/apache/datasketches/kll/KllDoublesSketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,17 @@ public byte[] toByteArray() {
return KllHelper.toByteArray(this, false);
}

@Override
public String toString(final boolean withSummary, final boolean withData) {
KllSketch sketch = this;
if (withData && sketchStructure != UPDATABLE) {
final Memory mem = getWritableMemory();
assert mem != null;
sketch = KllDoublesSketch.heapify(getWritableMemory());
}
return KllHelper.toStringImpl(sketch, withSummary, withData, getSerDe());
}

@Override
public void update(final double item) {
if (readOnly) { throw new SketchesArgumentException(TGT_IS_READ_ONLY_MSG); }
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/org/apache/datasketches/kll/KllFloatsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,16 @@ static void updateFloat(final KllFloatsSketch fltSk,
fltSk.setMinItem(min(fltSk.getMinItem(), item));
fltSk.setMaxItem(max(fltSk.getMaxItem(), item));
}
if (fltSk.levelsArr[0] == 0) { compressWhileUpdatingSketch(fltSk); }
final int myLevelsArrAtZero = fltSk.levelsArr[0]; //LevelsArr could be expanded
int level0space = fltSk.levelsArr[0];
assert level0space >= 0;
if (level0space == 0) {
compressWhileUpdatingSketch(fltSk);
level0space = fltSk.levelsArr[0];
assert (level0space > 0);
}
fltSk.incN();
fltSk.setLevelZeroSorted(false);
final int nextPos = myLevelsArrAtZero - 1;
assert myLevelsArrAtZero >= 0;
final int nextPos = level0space - 1;
fltSk.setLevelsArrayAt(0, nextPos);
fltSk.setFloatItemsArrayAt(nextPos, item);
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/apache/datasketches/kll/KllFloatsSketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,17 @@ public byte[] toByteArray() {
return KllHelper.toByteArray(this, false);
}

@Override
public String toString(final boolean withSummary, final boolean withData) {
KllSketch sketch = this;
if (withData && sketchStructure != UPDATABLE) {
final Memory mem = getWritableMemory();
assert mem != null;
sketch = KllFloatsSketch.heapify(getWritableMemory());
}
return KllHelper.toStringImpl(sketch, withSummary, withData, getSerDe());
}

@Override
public void update(final float item) {
if (readOnly) { throw new SketchesArgumentException(TGT_IS_READ_ONLY_MSG); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ static KllHeapDoublesSketch heapifyImpl(final Memory srcMem) {
return new KllHeapDoublesSketch(srcMem, memVal);
}

//End of constructors

@Override
String getItemAsString(final int index) {
if (isEmpty()) { return "NaN"; }
return Double.toString(doubleItems[index]);
}

@Override
public int getK() { return k; }

Expand All @@ -150,12 +158,24 @@ public double getMaxItem() {
return maxDoubleItem;
}

@Override
String getMaxItemAsString() {
if (isEmpty()) { return "NaN"; }
return Double.toString(maxDoubleItem);
}

@Override
public double getMinItem() {
if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); }
return minDoubleItem;
}

@Override
String getMinItemAsString() {
if (isEmpty()) { return "NaN"; }
return Double.toString(minDoubleItem);
}

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

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/apache/datasketches/kll/KllHeapFloatsSketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ static KllHeapFloatsSketch heapifyImpl(final Memory srcMem) {
return new KllHeapFloatsSketch(srcMem, memVal);
}

//End of constructors

@Override
String getItemAsString(final int index) {
if (isEmpty()) { return "NaN"; }
return Double.toString(floatItems[index]);
}

@Override
public int getK() { return k; }

Expand All @@ -150,12 +158,24 @@ public float getMaxItem() {
return maxFloatItem;
}

@Override
String getMaxItemAsString() {
if (isEmpty()) { return "NaN"; }
return Float.toString(maxFloatItem);
}

@Override
public float getMinItem() {
if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); }
return minFloatItem;
}

@Override
String getMinItemAsString() {
if (isEmpty()) { return "NaN"; }
return Float.toString(minFloatItem);
}

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

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/apache/datasketches/kll/KllHeapItemsSketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ final class KllHeapItemsSketch<T> extends KllItemsSketch<T> {
}
}

//End of constructors

@Override
String getItemAsString(final int index) {
if (isEmpty()) { return "Null"; }
return serDe.toString((T)(itemsArr[index]));
}

@Override
public int getK() {
return k;
Expand All @@ -128,12 +136,24 @@ public T getMaxItem() {
return maxItem;
}

@Override
String getMaxItemAsString() {
if (isEmpty()) { return "Null"; }
return serDe.toString(maxItem);
}

@Override
public T getMinItem() {
if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); }
return minItem;
}

@Override
String getMinItemAsString() {
if (isEmpty()) { return "Null"; }
return serDe.toString(minItem);
}

@Override
public long getN() {
return n;
Expand Down
Loading