Skip to content
This repository was archived by the owner on Jul 7, 2020. It is now read-only.

Commit

Permalink
- ensure tmpIndex is reset when the tmpSet is merged with the sparseSet
Browse files Browse the repository at this point in the history
- only sort values in the encodedSet up to a specified index to prevent uninitialized values from polluting result
- fix failing tests and add a new test of a single element
  • Loading branch information
abramsm committed Jul 15, 2013
1 parent c5dd394 commit e4466d8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ public boolean offer(Object o)
if (tmpIndex > sortThreshold)
{
mergeTempList();
tmpIndex = 0;
if (sparseSet != null && sparseSet.length > sparseSetThreshold)
{
convertToNormal();
Expand Down Expand Up @@ -850,19 +849,21 @@ protected void mergeTempList()
int[] retSet = sparseSet;
if (tmpIndex > 0)
{
tmpSet = sortEncodedSet(tmpSet);
tmpSet = sortEncodedSet(tmpSet, tmpIndex);
retSet = merge(sparseSet, tmpSet);
tmpSet = new int[sortThreshold+1];
tmpIndex = 0;
}
sparseSet = retSet == null ? new int[0] : retSet;
}

// exposed for testing
public int[] sortEncodedSet(int[] encodedSet)
public int[] sortEncodedSet(int[] encodedSet, int validIndex)
{
List<Integer> sortedList = new ArrayList<Integer>();
for (int k : encodedSet)
for (int i = 0; i < validIndex; i++)
{
int k = encodedSet[i];
sortedList.add(k);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void testSortEncodedSet()
testSet[1] = 655416;
testSet[2] = 655425;
HyperLogLogPlus hyperLogLogPlus = new HyperLogLogPlus(14, 25);
testSet = hyperLogLogPlus.sortEncodedSet(testSet);
testSet = hyperLogLogPlus.sortEncodedSet(testSet, 3);
assertEquals(655403, testSet[0]);
assertEquals(655425, testSet[1]);
assertEquals(655416, testSet[2]);
Expand Down Expand Up @@ -222,6 +222,14 @@ public void testMergeSelf() throws CardinalityMergeException, IOException

}

@Test
public void testOne() throws IOException
{
HyperLogLogPlus one = new HyperLogLogPlus(8,25);
one.offer("a");
assertEquals(1, one.cardinality());
}

@Test
public void testSparseSpace() throws IOException
{
Expand Down Expand Up @@ -374,7 +382,7 @@ public void testLegacyCodec_sparse() throws IOException

byte[] legacyBytes = baos.toByteArray();

// decode legacy
// decode legacy
HyperLogLogPlus decoded = HyperLogLogPlus.Builder.build(legacyBytes);
assertEquals(baseline.cardinality(), decoded.cardinality());
byte[] newBytes = baseline.getBytes();
Expand Down

0 comments on commit e4466d8

Please sign in to comment.