Skip to content

Commit

Permalink
Added update(char[]) to UnionImpl and unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Rhodes committed May 21, 2016
1 parent a173bb2 commit 3504f9a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 19 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/yahoo/sketches/theta/Union.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public interface Union {
* The string is converted to a byte array using UTF8 encoding.
* If the string is null or empty no update attempt is made and the method returns.
*
* <p>Note: this will not produce the same output hash values as the {@link #update(char[])}
* method and will generally be a little slower depending on the complexity of the UTF8 encoding.
* </p>
*
* @param datum The given String.
*/
void update(String datum);
Expand All @@ -77,6 +81,17 @@ public interface Union {
*/
void update(int[] data);

/**
* Present this union with the given char array.
* If the char array is null or empty no update attempt is made and the method returns.
*
* <p>Note: this will not produce the same output hash values as the {@link #update(String)}
* method but will be a little faster as it avoids the complexity of the UTF8 encoding.</p>
*
* @param data The given char array.
*/
void update(char[] data);

/**
* Present this union with the given long array.
* If the long array is null or empty no update attempt is made and the method returns.
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/yahoo/sketches/theta/UnionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ public void update(byte[] data) {
gadget_.update(data);
}

@Override
public void update(char[] data) {
gadget_.update(data);
}

@Override
public void update(int[] data) {
gadget_.update(data);
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/yahoo/sketches/theta/UpdateSketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ public UpdateReturnState update(double datum) {
* The string is converted to a byte array using UTF8 encoding.
* If the string is null or empty no update attempt is made and the method returns.
*
* <p>Note: this will not produce the same output hash values as the {@link #update(char[])}
* method and will generally be a little slower depending on the complexity of the UTF8 encoding.
* </p>
*
* @param datum The given String.
* @return
* <a href="{@docRoot}/resources/dictionary.html#updateReturnState">See Update Return State</a>
Expand Down Expand Up @@ -181,6 +185,9 @@ public UpdateReturnState update(byte[] data) {
* Present this sketch with the given char array.
* If the char array is null or empty no update attempt is made and the method returns.
*
* <p>Note: this will not produce the same output hash values as the {@link #update(String)}
* method but will be a little faster as it avoids the complexity of the UTF8 encoding.</p>
*
* @param data The given char array.
* @return
* <a href="{@docRoot}/resources/dictionary.html#updateReturnState">See Update Return State</a>
Expand Down
31 changes: 21 additions & 10 deletions src/test/java/com/yahoo/sketches/theta/HeapUnionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -497,38 +497,49 @@ public void checkPrimitiveUpdates() {
int k = 32;
Union union = Sketches.setOperationBuilder().buildUnion(k);

union.update(1L);
union.update(1.5); //#1 double
union.update(1L); //#1 long
union.update(1.5); //#2 double
union.update(0.0);
union.update(-0.0);
union.update(-0.0); //#3 double
String s = null;
union.update(s); //null string
union.update(s); //null string
s = "";
union.update(s); //empty string
union.update(s); //empty string
s = "String";
union.update(s); //#2 actual string
union.update(s); //#4 actual string

byte[] byteArr = null;
union.update(byteArr); //null byte[]
byteArr = new byte[0];
union.update(byteArr); //empty byte[]
byteArr = "Byte Array".getBytes();
union.update(byteArr); //#3 actual byte[]
union.update(byteArr); //#5 actual byte[]

char[] charArr = null;
union.update(charArr); //null char[]
charArr = new char[0];
union.update(charArr); //empty char[]
charArr = "String".toCharArray();
union.update(charArr); //#6 actual char[]

int[] intArr = null;
union.update(intArr); //null int[]
intArr = new int[0];
union.update(intArr); //empty int[]
int[] intArr2 = { 1, 2, 3, 4, 5 };
union.update(intArr2); //#4 actual int[]
union.update(intArr2); //#7 actual int[]

long[] longArr = null;
union.update(longArr); //null long[]
longArr = new long[0];
union.update(longArr); //empty long[]
long[] longArr2 = { 6, 7, 8, 9 };
union.update(longArr2); //#5 actual long[]
union.update(longArr2); //#8 actual long[]

CompactSketch comp = union.getResult();
double est = comp.getEstimate();
boolean empty = comp.isEmpty();
assertEquals(est, 7.0, 0.0);
assertEquals(est, 8.0, 0.0);
assertFalse(empty);
}

Expand Down
29 changes: 20 additions & 9 deletions src/test/java/com/yahoo/sketches/theta/UpdateSketchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,47 @@ public class UpdateSketchTest {
public void checkOtherUpdates() {
int k = 512;
UpdateSketch sk1 = UpdateSketch.builder().build(k);
sk1.update(1.5); //#1 double
sk1.update(1L); //#1 long
sk1.update(1.5); //#2 double
sk1.update(0.0);
sk1.update(-0.0);
sk1.update(-0.0); //#3 double
String s = null;
sk1.update(s); //null string
sk1.update(s); //null string
s = "";
sk1.update(s); //empty string
sk1.update(s); //empty string
s = "String";
sk1.update(s); //#2 actual string
sk1.update(s); //#4 actual string

byte[] byteArr = null;
sk1.update(byteArr); //null byte[]
byteArr = new byte[0];
sk1.update(byteArr); //empty byte[]
byteArr = "Byte Array".getBytes();
sk1.update(byteArr); //#3 actual byte[]
sk1.update(byteArr); //#5 actual byte[]

char[] charArr = null;
sk1.update(charArr); //null char[]
charArr = new char[0];
sk1.update(charArr); //empty char[]
charArr = "String".toCharArray();
sk1.update(charArr); //#6 actual char[]

int[] intArr = null;
sk1.update(intArr); //null int[]
intArr = new int[0];
sk1.update(intArr); //empty int[]
int[] intArr2 = { 1, 2, 3, 4, 5 };
sk1.update(intArr2); //#4 actual int[]
sk1.update(intArr2); //#7 actual int[]

long[] longArr = null;
sk1.update(longArr); //null long[]
longArr = new long[0];
sk1.update(longArr); //empty long[]
long[] longArr2 = { 6, 7, 8, 9 };
sk1.update(longArr2); //#5 actual long[]
sk1.update(longArr2); //#8 actual long[]

double est = sk1.getEstimate();
assertEquals(est, 6.0, 0.0);
assertEquals(est, 8.0, 0.0);
}

@Test
Expand Down

0 comments on commit 3504f9a

Please sign in to comment.