index
int64
0
0
repo_id
stringlengths
9
205
file_path
stringlengths
31
246
content
stringlengths
1
12.2M
__index_level_0__
int64
0
10k
0
Create_ds/datasketches-java/src/test/java/org/apache/datasketches
Create_ds/datasketches-java/src/test/java/org/apache/datasketches/quantiles/DebugUnionTest.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.quantiles; import static org.apache.datasketches.quantiles.ClassicUtil.LS; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; import java.util.HashSet; import org.testng.annotations.Test; import org.apache.datasketches.memory.WritableHandle; import org.apache.datasketches.memory.WritableMemory; import org.apache.datasketches.quantilescommon.QuantilesDoublesSketchIterator; /** * @author Lee Rhodes */ public class DebugUnionTest { @Test public void test() { final int n = 70_000; final int valueLimit = 1000; final int numSketches = 3; final int sketchK = 8; final int unionK = 8; UpdateDoublesSketch[] sketchArr = new UpdateDoublesSketch[numSketches]; //builds the input sketches, all on heap DoublesSketch.setRandom(1); //make deterministic for test final HashSet<Double> set = new HashSet<>(); //holds input values for (int s = 0; s < numSketches; s++) { sketchArr[s] = buildHeapSketch(sketchK, n, valueLimit, set); } //loads the on heap union DoublesSketch.setRandom(1); //make deterministic for test DoublesUnion hUnion = DoublesUnion.builder().setMaxK(unionK).build(); for (int s = 0; s < numSketches; s++) { hUnion.union(sketchArr[s]); } DoublesSketch hSketch = hUnion.getResult(); //loads the direct union DoublesSketch.setRandom(1); //make deterministic for test DoublesUnion dUnion; DoublesSketch dSketch; try ( WritableHandle wdh = WritableMemory.allocateDirect(10_000_000) ) { WritableMemory wmem = wdh.getWritable(); dUnion = DoublesUnion.builder().setMaxK(8).build(wmem); for (int s = 0; s < numSketches; s++) { dUnion.union(sketchArr[s]); } dSketch = dUnion.getResult(); //result is on heap } catch (final Exception e) { throw new RuntimeException(e); } //iterates and counts errors int hCount = hSketch.getNumRetained(); int dCount = dSketch.getNumRetained(); assertEquals(hCount, dCount); //Retained items must be the same int hErrors = 0; int dErrors = 0; QuantilesDoublesSketchIterator hit = hSketch.iterator(); QuantilesDoublesSketchIterator dit = dSketch.iterator(); while (hit.next() && dit.next()) { double v = hit.getQuantile(); if (!set.contains(v)) { hErrors++; } double w = dit.getQuantile(); if (!set.contains(w)) { dErrors++; } assertEquals(v, w, 0); //Items must be returned in same order and be equal } assertTrue(hErrors == 0); assertTrue(dErrors == 0); println("HeapUnion : Values: " + hCount + ", errors: " + hErrors); //println(hSketch.toString(true, true)); println("DirectUnion: Values: " + dCount + ", errors: " + dErrors); //println(dSketch.toString(true, true)); } private static UpdateDoublesSketch buildHeapSketch(final int k, final int n, final int valueLimit, final HashSet<Double> set) { final UpdateDoublesSketch uSk = DoublesSketch.builder().setK(k).build(); for (int i = 0; i < n; i++) { final double value = DoublesSketch.rand.nextInt(valueLimit) + 1; uSk.update(value); set.add(value); } return uSk; } @Test public void printlnTest() { println("PRINTING: " + this.getClass().getName()); } /** * @param s value to print */ static void println(String s) { print(s+LS); } /** * @param s value to print */ static void print(String s) { //System.out.print(s); //disable here } }
2,500
0
Create_ds/datasketches-java/src/main/java/org/apache
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/package-info.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /** * This package is the parent package for all sketch families and common code areas. * * @author Lee Rhodes */ package org.apache.datasketches;
2,501
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/HarmonicNumbers.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; /** * @author Lee Rhodes * @author Kevin Lang */ final class HarmonicNumbers { /** * This is the estimator you would use for flat bit map random accessed, similar to a Bloom filter. * @param bitVectorLength the length of the bit vector in bits. Must be &gt; 0. * @param numBitsSet the number of bits set in this bit vector. Must be &ge; 0 and &le; * bitVectorLength. * @return the estimate. */ static double getBitMapEstimate(final int bitVectorLength, final int numBitsSet) { return (bitVectorLength * (harmonicNumber(bitVectorLength) - harmonicNumber(bitVectorLength - numBitsSet))); //converts to numZeros } //In C: two-registers.c Line 1170 private static final int NUM_EXACT_HARMONIC_NUMBERS = 25; private static double[] tableOfExactHarmonicNumbers = { 0.0, // 0 1.0, // 1 1.5, // 2 11.0 / 6.0, // 3 25.0 / 12.0, // 4 137.0 / 60.0, // 5 49.0 / 20.0, // 6 363.0 / 140.0, // 7 761.0 / 280.0, // 8 7129.0 / 2520.0, // 9 7381.0 / 2520.0, // 10 83711.0 / 27720.0, // 11 86021.0 / 27720.0, // 12 1145993.0 / 360360.0, // 13 1171733.0 / 360360.0, // 14 1195757.0 / 360360.0, // 15 2436559.0 / 720720.0, // 16 42142223.0 / 12252240.0, // 17 14274301.0 / 4084080.0, // 18 275295799.0 / 77597520.0, // 19 55835135.0 / 15519504.0, // 20 18858053.0 / 5173168.0, // 21 19093197.0 / 5173168.0, // 22 444316699.0 / 118982864.0, // 23 1347822955.0 / 356948592.0 // 24 }; //In C: two-register.c Line 1202 private static final double EULER_MASCHERONI_CONSTANT = 0.577215664901532860606512090082; private static double harmonicNumber(final long x_i) { if (x_i < NUM_EXACT_HARMONIC_NUMBERS) { return tableOfExactHarmonicNumbers[(int) x_i]; } else { final double x = x_i; final double invSq = 1.0 / (x * x); double sum = Math.log(x) + EULER_MASCHERONI_CONSTANT + (1.0 / (2.0 * x)); /* note: the number of terms included from this series expansion is appropriate for the size of the exact table (25) and the precision of doubles */ double pow = invSq; /* now n^-2 */ sum -= pow * (1.0 / 12.0); pow *= invSq; /* now n^-4 */ sum += pow * (1.0 / 120.0); pow *= invSq; /* now n^-6 */ sum -= pow * (1.0 / 252.0); pow *= invSq; /* now n^-8 */ sum += pow * (1.0 / 240.0); return sum; } } }
2,502
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/Hll4Array.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.HllUtil.AUX_TOKEN; import static org.apache.datasketches.hll.HllUtil.KEY_BITS_26; import static org.apache.datasketches.hll.HllUtil.LG_AUX_ARR_INTS; import static org.apache.datasketches.hll.HllUtil.hiNibbleMask; import static org.apache.datasketches.hll.HllUtil.loNibbleMask; import static org.apache.datasketches.hll.PreambleUtil.HLL_BYTE_ARR_START; import static org.apache.datasketches.hll.PreambleUtil.extractAuxCount; import static org.apache.datasketches.hll.PreambleUtil.extractCompactFlag; import static org.apache.datasketches.hll.PreambleUtil.extractLgK; import org.apache.datasketches.common.SketchesStateException; import org.apache.datasketches.memory.Memory; /** * Uses 4 bits per slot in a packed byte array. * @author Lee Rhodes */ final class Hll4Array extends HllArray { /** * Standard constructor for new instance * @param lgConfigK the configured Lg K */ Hll4Array(final int lgConfigK) { super(lgConfigK, TgtHllType.HLL_4); hllByteArr = new byte[hll4ArrBytes(lgConfigK)]; } /** * Copy constructor * @param that another Hll4Array */ Hll4Array(final Hll4Array that) { super(that); } static final Hll4Array heapify(final Memory mem) { final int lgConfigK = extractLgK(mem); final Hll4Array hll4Array = new Hll4Array(lgConfigK); HllArray.extractCommonHll(mem, hll4Array); //load AuxHashMap final int auxStart = hll4Array.auxStart; final int auxCount = extractAuxCount(mem); final boolean compact = extractCompactFlag(mem); HeapAuxHashMap auxHashMap = null; if (auxCount > 0) { auxHashMap = HeapAuxHashMap.heapify(mem, auxStart, lgConfigK, auxCount, compact); } hll4Array.putAuxHashMap(auxHashMap, false); return hll4Array; } @Override Hll4Array copy() { return new Hll4Array(this); } @Override HllSketchImpl couponUpdate(final int coupon) { final int newValue = coupon >>> KEY_BITS_26; final int configKmask = (1 << getLgConfigK()) - 1; final int slotNo = coupon & configKmask; updateSlotWithKxQ(slotNo, newValue); return this; } @Override int getNibble(final int slotNo) { int theByte = hllByteArr[slotNo >>> 1]; if ((slotNo & 1) > 0) { //odd? theByte >>>= 4; } return theByte & loNibbleMask; } @Override int getSlotValue(final int slotNo) { final int nib = getNibble(slotNo); if (nib == AUX_TOKEN) { final AuxHashMap auxHashMap = getAuxHashMap(); return auxHashMap.mustFindValueFor(slotNo); //auxHashMap cannot be null here } else { return nib + getCurMin(); } } @Override int getUpdatableSerializationBytes() { final AuxHashMap auxHashMap = getAuxHashMap(); final int auxBytes; if (auxHashMap == null) { auxBytes = 4 << LG_AUX_ARR_INTS[lgConfigK]; } else { auxBytes = 4 << auxHashMap.getLgAuxArrInts(); } return HLL_BYTE_ARR_START + getHllByteArrBytes() + auxBytes; } @Override PairIterator iterator() { return new HeapHll4Iterator(1 << lgConfigK); } @Override void putNibble(final int slotNo, final int nibValue) { final int byteno = slotNo >>> 1; final int oldValue = hllByteArr[byteno]; if ((slotNo & 1) == 0) { // set low nibble hllByteArr[byteno] = (byte) ((oldValue & hiNibbleMask) | (nibValue & loNibbleMask)); } else { //set high nibble hllByteArr[byteno] = (byte) ((oldValue & loNibbleMask) | ((nibValue << 4) & hiNibbleMask)); } } @Override //Would be used by Union, but not used because the gadget is always HLL8 type void updateSlotNoKxQ(final int slotNo, final int newValue) { throw new SketchesStateException("Improper access."); } @Override //Used by this couponUpdate() //updates HipAccum, CurMin, NumAtCurMin, KxQs and checks newValue > oldValue void updateSlotWithKxQ(final int slotNo, final int newValue) { Hll4Update.internalHll4Update(this, slotNo, newValue); } @Override byte[] toCompactByteArray() { return ToByteArrayImpl.toHllByteArray(this, true); } //ITERATOR final class HeapHll4Iterator extends HllPairIterator { HeapHll4Iterator(final int lengthPairs) { super(lengthPairs); } @Override int value() { return getSlotValue(index); } } }
2,503
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/Union.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.common.Util.invPow2; import static org.apache.datasketches.hll.HllUtil.AUX_TOKEN; import static org.apache.datasketches.hll.HllUtil.EMPTY; import static org.apache.datasketches.hll.HllUtil.loNibbleMask; import static org.apache.datasketches.hll.PreambleUtil.HLL_BYTE_ARR_START; import static org.apache.datasketches.hll.PreambleUtil.extractTgtHllType; import static org.apache.datasketches.hll.TgtHllType.HLL_4; import static org.apache.datasketches.hll.TgtHllType.HLL_8; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.memory.WritableMemory; /** * This performs union operations for all HllSketches. This union operator can be configured to be * on or off heap. The source sketch given to this union using the {@link #update(HllSketch)} can * be configured with any precision value <i>lgConfigK</i> (from 4 to 21), any <i>TgtHllType</i> * (HLL_4, HLL_6, HLL_8), and either on or off-heap; and it can be in either of the sparse modes * (<i>LIST</i> or <i>SET</i>), or the dense mode (<i>HLL</i>). * * <p>Although the API for this union operator parallels many of the methods of the * <i>HllSketch</i>, the behavior of the union operator has some fundamental differences.</p> * * <p>First, this union operator is configured with a <i>lgMaxK</i> instead of the normal * <i>lgConfigK</i>. Generally, this union operator will inherit the lowest <i>lgConfigK</i> * less than <i>lgMaxK</i> that it has seen. However, the <i>lgConfigK</i> of incoming sketches that * are still in sparse are ignored. The <i>lgMaxK</i> provides the user the ability to specify the * largest maximum size for the union operation. * * <p>Second, the user cannot specify the {@link TgtHllType} as an input parameter to the union. * Instead, it is specified for the sketch returned with {@link #getResult(TgtHllType)}. * * <p>The following graph illustrates the HLL Merge speed.</p> * * <p><img src="doc-files/HLL_UnionTime4_6_8_Java_CPP.png" width="500" alt="HLL LgK12 Union Speed"></p> * This graph illustrates the relative merging speed of the HLL 4,6,8 Java HLL sketches compared to * the DataSketches C++ implementations of the same sketches. With this particular test (merging 32 relative large * sketches together), the Java HLL 8 is the fastest and the Java HLL 4 the slowest, with a mixed cluster in the middle. * Union / Merging speed is somewhat difficult to measure as the performance is very dependent on the mix of sketch * sizes (and types) you are merging. So your mileage will vary! * * <p>For a complete example of using the Union operator * see <a href="https://datasketches.apache.org/docs/HLL/HllJavaExample.html">Union Example</a>.</p> * * @author Lee Rhodes * @author Kevin Lang */ public class Union extends BaseHllSketch { final int lgMaxK; private final HllSketch gadget; /** * Construct this Union operator with the default maximum log-base-2 of <i>K</i>. */ public Union() { lgMaxK = HllSketch.DEFAULT_LG_K; gadget = new HllSketch(lgMaxK, HLL_8); } /** * Construct this Union operator with a given maximum log-base-2 of <i>K</i>. * @param lgMaxK the desired maximum log-base-2 of <i>K</i>. This value must be * between 4 and 21 inclusively. */ public Union(final int lgMaxK) { this.lgMaxK = HllUtil.checkLgK(lgMaxK); gadget = new HllSketch(lgMaxK, HLL_8); } /** * Construct this Union operator with a given maximum log-base-2 of <i>K</i> and the given * WritableMemory as the destination for this Union. This WritableMemory is usually configured * for off-heap memory. What remains on the java heap is a thin wrapper object that reads and * writes to the given WritableMemory. * * <p>The given <i>dstMem</i> is checked for the required capacity as determined by * {@link HllSketch#getMaxUpdatableSerializationBytes(int, TgtHllType)}. * @param lgMaxK the desired maximum log-base-2 of <i>K</i>. This value must be * between 4 and 21 inclusively. * @param dstWmem the destination writable memory for the sketch. */ public Union(final int lgMaxK, final WritableMemory dstWmem) { this.lgMaxK = HllUtil.checkLgK(lgMaxK); gadget = new HllSketch(lgMaxK, HLL_8, dstWmem); } //used only by writableWrap private Union(final HllSketch sketch) { lgMaxK = sketch.getLgConfigK(); gadget = sketch; } /** * Construct a union operator populated with the given byte array image of an HllSketch. * @param byteArray the given byte array * @return a union operator populated with the given byte array image of an HllSketch. */ public static final Union heapify(final byte[] byteArray) { return heapify(Memory.wrap(byteArray)); } /** * Construct a union operator populated with the given Memory image of an HllSketch. * @param mem the given Memory * @return a union operator populated with the given Memory image of an HllSketch. */ public static final Union heapify(final Memory mem) { final int lgK = HllUtil.checkLgK(mem.getByte(PreambleUtil.LG_K_BYTE)); final HllSketch sk = HllSketch.heapify(mem, false); //allows non-finalized image final Union union = new Union(lgK); union.update(sk); return union; } /** * Wraps the given WritableMemory, which must be a image of a valid updatable HLL_8 sketch, * and may have data. What remains on the java heap is a * thin wrapper object that reads and writes to the given WritableMemory, which, depending on * how the user configures the WritableMemory, may actually reside on the Java heap or off-heap. * * <p>The given <i>dstMem</i> is checked for the required capacity as determined by * {@link HllSketch#getMaxUpdatableSerializationBytes(int, TgtHllType)}, and for the correct type. * @param srcWmem an writable image of a valid sketch with data. * @return a Union operator where the sketch data is in the given dstMem. */ public static final Union writableWrap(final WritableMemory srcWmem) { final TgtHllType tgtHllType = extractTgtHllType(srcWmem); if (tgtHllType != TgtHllType.HLL_8) { throw new SketchesArgumentException( "Union can only wrap writable HLL_8 sketches that were the Gadget of a Union."); } //allows writableWrap of non-finalized image return new Union(HllSketch.writableWrap(srcWmem, false)); } @Override public double getCompositeEstimate() { checkRebuildCurMinNumKxQ(gadget); return gadget.hllSketchImpl.getCompositeEstimate(); } @Override CurMode getCurMode() { return gadget.getCurMode(); } @Override public int getCompactSerializationBytes() { return gadget.getCompactSerializationBytes(); } @Override public double getEstimate() { checkRebuildCurMinNumKxQ(gadget); return gadget.getEstimate(); } /** * Gets the effective <i>lgConfigK</i> for the union operator, which may be less than * <i>lgMaxK</i>. * @return the <i>lgConfigK</i>. */ @Override public int getLgConfigK() { return gadget.getLgConfigK(); } @Override public double getLowerBound(final int numStdDev) { checkRebuildCurMinNumKxQ(gadget); return gadget.getLowerBound(numStdDev); } /** * Returns the maximum size in bytes that this union operator can grow to given a lgK. * * @param lgK The maximum Log2 of K for this union operator. This value must be * between 4 and 21 inclusively. * @return the maximum size in bytes that this union operator can grow to. */ public static int getMaxSerializationBytes(final int lgK) { return HllSketch.getMaxUpdatableSerializationBytes(lgK, TgtHllType.HLL_8); } /** * Return the result of this union operator as an HLL_4 sketch. * @return the result of this union operator as an HLL_4 sketch. */ public HllSketch getResult() { return getResult(HllSketch.DEFAULT_HLL_TYPE); } /** * Return the result of this union operator with the specified {@link TgtHllType} * @param tgtHllType the TgtHllType enum * @return the result of this union operator with the specified TgtHllType */ public HllSketch getResult(final TgtHllType tgtHllType) { checkRebuildCurMinNumKxQ(gadget); return gadget.copyAs(tgtHllType); } @Override public TgtHllType getTgtHllType() { return TgtHllType.HLL_8; } @Override public int getUpdatableSerializationBytes() { return gadget.getUpdatableSerializationBytes(); } @Override public double getUpperBound(final int numStdDev) { checkRebuildCurMinNumKxQ(gadget); return gadget.getUpperBound(numStdDev); } @Override public boolean isCompact() { return gadget.isCompact(); } @Override public boolean isEmpty() { return gadget.isEmpty(); } @Override public boolean isMemory() { return gadget.isMemory(); } @Override public boolean isOffHeap() { return gadget.isOffHeap(); } @Override boolean isOutOfOrder() { return gadget.isOutOfOrder(); } @Override public boolean isSameResource(final Memory mem) { return gadget.isSameResource(mem); } boolean isRebuildCurMinNumKxQFlag() { return gadget.hllSketchImpl.isRebuildCurMinNumKxQFlag(); } void putRebuildCurMinNumKxQFlag(final boolean rebuild) { gadget.hllSketchImpl.putRebuildCurMinNumKxQFlag(rebuild); } /** * Resets to empty and retains the current lgK, but does not change the configured value of * lgMaxK. */ @Override public void reset() { gadget.reset(); } /** * Gets the serialization of this union operator as a byte array in compact form, which is * designed to be heapified only. It is not directly updatable. * For the Union operator, this is the serialization of the internal state of * the union operator as a sketch. * @return the serialization of this union operator as a byte array. */ @Override public byte[] toCompactByteArray() { checkRebuildCurMinNumKxQ(gadget); return gadget.toCompactByteArray(); } @Override public byte[] toUpdatableByteArray() { checkRebuildCurMinNumKxQ(gadget); return gadget.toUpdatableByteArray(); } @Override public String toString(final boolean summary, final boolean hllDetail, final boolean auxDetail, final boolean all) { checkRebuildCurMinNumKxQ(gadget); return gadget.toString(summary, hllDetail, auxDetail, all); } /** * Update this union operator with the given sketch. * @param sketch the given sketch. */ public void update(final HllSketch sketch) { gadget.hllSketchImpl = unionImpl(sketch, gadget, lgMaxK); } @Override void couponUpdate(final int coupon) { if (coupon == EMPTY) { return; } gadget.hllSketchImpl = gadget.hllSketchImpl.couponUpdate(coupon); } // Union operator logic /** * Union the given source and destination sketches. This static method examines the state of * the current internal gadget and the incoming sketch and determines the optimum way to * perform the union. This may involve swapping the merge order, downsampling, transforming, * and / or copying one of the arguments and may completely replace the internals of the union. * * <p>If the union gadget is empty, the source sketch is effectively copied to the union gadget * after any required transformations. * * <p>The direction of the merge is reversed if the union gadget is in LIST or SET mode, and the * source sketch is in HLL mode. This is done to maintain maximum accuracy of the union process. * * <p>The source sketch is downsampled if the source LgK is larger than maxLgK and in HLL mode. * * <p>The union gadget is downsampled if both source and union gadget are in HLL mode * and the source LgK <b>less than</b> the union gadget LgK. * * @param source the given incoming sketch, which cannot be modified. * @param gadget the given gadget sketch, which has a target of HLL_8 and holds the result. * @param lgMaxK the maximum value of log2 K for this union. * @return the union of the two sketches in the form of the internal HllSketchImpl, which is * always in HLL_8 form. */ private static HllSketchImpl unionImpl(final HllSketch source, final HllSketch gadget, final int lgMaxK) { assert gadget.getTgtHllType() == HLL_8; if ((source == null) || source.isEmpty()) { return gadget.hllSketchImpl; } final CurMode srcMode = source.getCurMode(); if (srcMode == CurMode.LIST ) { source.mergeTo(gadget); return gadget.hllSketchImpl; } final int srcLgK = source.getLgConfigK(); final int gadgetLgK = gadget.getLgConfigK(); final boolean srcIsMem = source.isMemory(); final boolean gdtIsMem = gadget.isMemory(); final boolean gdtEmpty = gadget.isEmpty(); if (srcMode == CurMode.SET ) { if (gdtEmpty && (srcLgK == gadgetLgK) && (!srcIsMem) && (!gdtIsMem)) { gadget.hllSketchImpl = source.copyAs(HLL_8).hllSketchImpl; return gadget.hllSketchImpl; } source.mergeTo(gadget); return gadget.hllSketchImpl; } //Hereafter, the source is in HLL mode. final int bit0 = gdtIsMem ? 1 : 0; final int bits1_2 = (gdtEmpty ? 3 : gadget.getCurMode().ordinal()) << 1; final int bit3 = (srcLgK < gadgetLgK) ? 8 : 0; final int bit4 = (srcLgK > lgMaxK) ? 16 : 0; final int sw = bit4 | bit3 | bits1_2 | bit0; HllSketchImpl hllSketchImpl = null; //never returned as null switch (sw) { case 0: //src <= max, src >= gdt, gdtLIST, gdtHeap case 8: //src <= max, src < gdt, gdtLIST, gdtHeap case 2: //src <= max, src >= gdt, gdtSET, gdtHeap case 10://src <= max, src < gdt, gdtSET, gdtHeap { //Action: copy src, reverse merge w/autofold, ooof=src final HllSketch srcHll8Heap = source.copyAs(HLL_8); gadget.mergeTo(srcHll8Heap); //merge gdt(Hll8,heap,list/set) -> src(Hll8,heap,hll) hllSketchImpl = srcHll8Heap.hllSketchImpl; break; } case 16://src > max, src >= gdt, gdtList, gdtHeap case 18://src > max, src >= gdt, gdtSet, gdtHeap { //Action: downsample src to MaxLgK, reverse merge w/autofold, ooof=src final HllSketch srcHll8Heap = downsample(source, lgMaxK); gadget.mergeTo(srcHll8Heap); //merge gdt(Hll8,heap,list/set) -> src(Hll8,heap,hll) hllSketchImpl = srcHll8Heap.hllSketchImpl; break; } case 1: //src <= max, src >= gdt, gdtLIST, gdtMemory case 9: //src <= max, src < gdt, gdtLIST, gdtMemory case 3: //src <= max, src >= gdt, gdtSET, gdtMemory case 11://src <= max, src < gdt, gdtSET, gdtMemory { //Action: copy src, reverse merge w/autofold, use gdt memory, ooof=src final HllSketch srcHll8Heap = source.copyAs(HLL_8); gadget.mergeTo(srcHll8Heap); //merge gdt(Hll8,mem,list/set) -> src(Hll8,heap,hll) hllSketchImpl = useGadgetMemory(gadget, srcHll8Heap, false).hllSketchImpl; break; } case 17://src > max, src >= gdt, gdtList, gdtMemory case 19://src > max, src >= gdt, gdtSet, gdtMemory { //Action: downsample src to MaxLgK, reverse merge w/autofold, use gdt memory, ooof=src final HllSketch srcHll8Heap = downsample(source, lgMaxK); gadget.mergeTo(srcHll8Heap); //merge gdt(Hll8,mem,list/set) -> src(Hll8,heap,hll), autofold hllSketchImpl = useGadgetMemory(gadget, srcHll8Heap, false).hllSketchImpl; break; } case 4: //src <= max, src >= gdt, gdtHLL, gdtHeap case 20://src > max, src >= gdt, gdtHLL, gdtHeap case 5: //src <= max, src >= gdt, gdtHLL, gdtMemory case 21://src > max, src >= gdt, gdtHLL, gdtMemory { //Action: forward HLL merge w/autofold, ooof=True //merge src(Hll4,6,8,heap/mem,Mode=HLL) -> gdt(Hll8,heap,Mode=HLL) mergeHlltoHLLmode(source, gadget, srcLgK, gadgetLgK, srcIsMem, gdtIsMem); hllSketchImpl = gadget.putOutOfOrderFlag(true).hllSketchImpl; break; } case 12://src <= max, src < gdt, gdtHLL, gdtHeap { //Action: downsample gdt to srcLgK, forward HLL merge w/autofold, ooof=True final HllSketch gdtHll8Heap = downsample(gadget, srcLgK); //merge src(Hll4,6,8;heap/mem,Mode=HLL) -> gdt(Hll8,heap,hll) mergeHlltoHLLmode(source, gdtHll8Heap, srcLgK, gadgetLgK, srcIsMem, false); hllSketchImpl = gdtHll8Heap.putOutOfOrderFlag(true).hllSketchImpl; break; } case 13://src <= max, src < gdt, gdtHLL, gdtMemory { //Action: downsample gdt to srcLgK, forward HLL merge w/autofold, use gdt memory, ooof=True final HllSketch gdtHll8Heap = downsample(gadget, srcLgK); //merge src(Hll4,6,8;heap/mem;Mode=HLL) -> gdt(Hll8,heap,Mode=HLL) mergeHlltoHLLmode(source, gdtHll8Heap, srcLgK, gadgetLgK, srcIsMem, false); hllSketchImpl = useGadgetMemory(gadget, gdtHll8Heap, true).hllSketchImpl; break; } case 6: //src <= max, src >= gdt, gdtEmpty, gdtHeap case 14://src <= max, src < gdt, gdtEmpty, gdtHeap { //Action: copy src, replace gdt, ooof=src final HllSketch srcHll8Heap = source.copyAs(HLL_8); hllSketchImpl = srcHll8Heap.hllSketchImpl; break; } case 22://src > max, src >= gdt, gdtEmpty, gdtHeap { //Action: downsample src to lgMaxK, replace gdt, ooof=src final HllSketch srcHll8Heap = downsample(source, lgMaxK); hllSketchImpl = srcHll8Heap.hllSketchImpl; break; } case 7: //src <= max, src >= gdt, gdtEmpty, gdtMemory case 15://src <= max, src < gdt, gdtEmpty, gdtMemory { //Action: copy src, use gdt memory, ooof=src final HllSketch srcHll8Heap = source.copyAs(HLL_8); hllSketchImpl = useGadgetMemory(gadget, srcHll8Heap, false).hllSketchImpl; break; } case 23://src > max, src >= gdt, gdtEmpty, gdtMemory, replace mem, downsample src, ooof=src { //Action: downsample src to lgMaxK, use gdt memory, ooof=src final HllSketch srcHll8Heap = downsample(source, lgMaxK); hllSketchImpl = useGadgetMemory(gadget, srcHll8Heap, false).hllSketchImpl; break; } default: return gadget.hllSketchImpl; //not possible } return hllSketchImpl; } private static final HllSketch useGadgetMemory( final HllSketch gadget, final HllSketch hll8Heap, final boolean setOooFlag) { final WritableMemory wmem = gadget.getWritableMemory(); //use the gdt wmem final byte[] byteArr = hll8Heap.toUpdatableByteArray(); //serialize srcCopy wmem.putByteArray(0, byteArr, 0, byteArr.length); //replace old data with new return (setOooFlag) ? HllSketch.writableWrap(wmem, false).putOutOfOrderFlag(true) //wrap, set oooflag, return : HllSketch.writableWrap(wmem, false); //wrap & return } private static final void mergeHlltoHLLmode(final HllSketch src, final HllSketch tgt, final int srcLgK, final int tgtLgK, final boolean srcIsMem, final boolean tgtIsMem) { final int sw = (tgtIsMem ? 1 : 0) | (srcIsMem ? 2 : 0) | ((srcLgK > tgtLgK) ? 4 : 0) | ((src.getTgtHllType() != HLL_8) ? 8 : 0); final int srcK = 1 << srcLgK; switch (sw) { case 0: { //HLL_8, srcLgK=tgtLgK, src=heap, tgt=heap final byte[] srcArr = ((Hll8Array) src.hllSketchImpl).hllByteArr; final byte[] tgtArr = ((Hll8Array) tgt.hllSketchImpl).hllByteArr; for (int i = 0; i < srcK; i++) { final byte srcV = srcArr[i]; final byte tgtV = tgtArr[i]; tgtArr[i] = (byte) Math.max(srcV, tgtV); } break; } case 1: { //HLL_8, srcLgK=tgtLgK, src=heap, tgt=mem final byte[] srcArr = ((Hll8Array) src.hllSketchImpl).hllByteArr; final WritableMemory tgtMem = tgt.getWritableMemory(); for (int i = 0; i < srcK; i++) { final byte srcV = srcArr[i]; final byte tgtV = tgtMem.getByte(HLL_BYTE_ARR_START + i); tgtMem.putByte(HLL_BYTE_ARR_START + i, (byte) Math.max(srcV, tgtV)); } break; } case 2: { //HLL_8, srcLgK=tgtLgK, src=mem, tgt=heap final Memory srcMem = src.getMemory(); final byte[] tgtArr = ((Hll8Array) tgt.hllSketchImpl).hllByteArr; for (int i = 0; i < srcK; i++) { final byte srcV = srcMem.getByte(HLL_BYTE_ARR_START + i); final byte tgtV = tgtArr[i]; tgtArr[i] = (byte) Math.max(srcV, tgtV); } break; } case 3: { //HLL_8, srcLgK=tgtLgK, src=mem, tgt=mem final Memory srcMem = src.getMemory(); final WritableMemory tgtMem = tgt.getWritableMemory(); for (int i = 0; i < srcK; i++) { final byte srcV = srcMem.getByte(HLL_BYTE_ARR_START + i); final byte tgtV = tgtMem.getByte(HLL_BYTE_ARR_START + i); tgtMem.putByte(HLL_BYTE_ARR_START + i, (byte) Math.max(srcV, tgtV)); } break; } case 4: { //HLL_8, srcLgK>tgtLgK, src=heap, tgt=heap final int tgtKmask = (1 << tgtLgK) - 1; final byte[] srcArr = ((Hll8Array) src.hllSketchImpl).hllByteArr; final byte[] tgtArr = ((Hll8Array) tgt.hllSketchImpl).hllByteArr; for (int i = 0; i < srcK; i++) { final byte srcV = srcArr[i]; final int j = i & tgtKmask; final byte tgtV = tgtArr[j]; tgtArr[j] = (byte) Math.max(srcV, tgtV); } break; } case 5: { //HLL_8, srcLgK>tgtLgK, src=heap, tgt=mem final int tgtKmask = (1 << tgtLgK) - 1; final byte[] srcArr = ((Hll8Array) src.hllSketchImpl).hllByteArr; final WritableMemory tgtMem = tgt.getWritableMemory(); for (int i = 0; i < srcK; i++) { final byte srcV = srcArr[i]; final int j = i & tgtKmask; final byte tgtV = tgtMem.getByte(HLL_BYTE_ARR_START + j); tgtMem.putByte(HLL_BYTE_ARR_START + j, (byte) Math.max(srcV, tgtV)); } break; } case 6: { //HLL_8, srcLgK>tgtLgK, src=mem, tgt=heap final int tgtKmask = (1 << tgtLgK) - 1; final Memory srcMem = src.getMemory(); final byte[] tgtArr = ((Hll8Array) tgt.hllSketchImpl).hllByteArr; for (int i = 0; i < srcK; i++) { final byte srcV = srcMem.getByte(HLL_BYTE_ARR_START + i); final int j = i & tgtKmask; final byte tgtV = tgtArr[j]; tgtArr[j] = (byte) Math.max(srcV, tgtV); } break; } case 7: { //HLL_8, srcLgK>tgtLgK, src=mem, tgt=mem final int tgtKmask = (1 << tgtLgK) - 1; final Memory srcMem = src.getMemory(); final WritableMemory tgtMem = tgt.getWritableMemory(); for (int i = 0; i < srcK; i++) { final byte srcV = srcMem.getByte(HLL_BYTE_ARR_START + i); final int j = i & tgtKmask; final byte tgtV = tgtMem.getByte(HLL_BYTE_ARR_START + j); tgtMem.putByte(HLL_BYTE_ARR_START + j, (byte) Math.max(srcV, tgtV)); } break; } case 8: case 9: { //!HLL_8, srcLgK=tgtLgK, src=heap, tgt=heap/mem final AbstractHllArray tgtAbsHllArr = (AbstractHllArray)(tgt.hllSketchImpl); if (src.getTgtHllType() == HLL_4) { final Hll4Array src4 = (Hll4Array) src.hllSketchImpl; final AuxHashMap auxHashMap = src4.getAuxHashMap(); final int curMin = src4.getCurMin(); int i = 0; int j = 0; while (j < srcK) { final byte b = src4.hllByteArr[i++]; int value = Byte.toUnsignedInt(b) & loNibbleMask; tgtAbsHllArr.updateSlotNoKxQ(j, value == AUX_TOKEN ? auxHashMap.mustFindValueFor(j) : value + curMin); j++; value = Byte.toUnsignedInt(b) >>> 4; tgtAbsHllArr.updateSlotNoKxQ(j, value == AUX_TOKEN ? auxHashMap.mustFindValueFor(j) : value + curMin); j++; } } else { final Hll6Array src6 = (Hll6Array) src.hllSketchImpl; int i = 0; int j = 0; while (j < srcK) { final byte b1 = src6.hllByteArr[i++]; final byte b2 = src6.hllByteArr[i++]; final byte b3 = src6.hllByteArr[i++]; int value = Byte.toUnsignedInt(b1) & 0x3f; tgtAbsHllArr.updateSlotNoKxQ(j++, value); value = Byte.toUnsignedInt(b1) >>> 6; value |= (Byte.toUnsignedInt(b2) & 0x0f) << 2; tgtAbsHllArr.updateSlotNoKxQ(j++, value); value = Byte.toUnsignedInt(b2) >>> 4; value |= (Byte.toUnsignedInt(b3) & 3) << 4; tgtAbsHllArr.updateSlotNoKxQ(j++, value); value = Byte.toUnsignedInt(b3) >>> 2; tgtAbsHllArr.updateSlotNoKxQ(j++, value); } } break; } case 10: case 11: { //!HLL_8, srcLgK=tgtLgK, src=mem, tgt=heap/mem final AbstractHllArray tgtAbsHllArr = (AbstractHllArray)(tgt.hllSketchImpl); if (src.getTgtHllType() == HLL_4) { final DirectHll4Array src4 = (DirectHll4Array) src.hllSketchImpl; final AuxHashMap auxHashMap = src4.getAuxHashMap(); final int curMin = src4.getCurMin(); int i = 0; int j = 0; while (j < srcK) { final byte b = src4.mem.getByte(HLL_BYTE_ARR_START + i++); int value = Byte.toUnsignedInt(b) & loNibbleMask; tgtAbsHllArr.updateSlotNoKxQ(j, value == AUX_TOKEN ? auxHashMap.mustFindValueFor(j) : value + curMin); j++; value = Byte.toUnsignedInt(b) >>> 4; tgtAbsHllArr.updateSlotNoKxQ(j, value == AUX_TOKEN ? auxHashMap.mustFindValueFor(j) : value + curMin); j++; } } else { final DirectHll6Array src6 = (DirectHll6Array) src.hllSketchImpl; int i = 0; int offset = HLL_BYTE_ARR_START; while (i < srcK) { final byte b1 = src6.mem.getByte(offset++); final byte b2 = src6.mem.getByte(offset++); final byte b3 = src6.mem.getByte(offset++); int value = Byte.toUnsignedInt(b1) & 0x3f; tgtAbsHllArr.updateSlotNoKxQ(i++, value); value = Byte.toUnsignedInt(b1) >>> 6; value |= (Byte.toUnsignedInt(b2) & 0x0f) << 2; tgtAbsHllArr.updateSlotNoKxQ(i++, value); value = Byte.toUnsignedInt(b2) >>> 4; value |= (Byte.toUnsignedInt(b3) & 3) << 4; tgtAbsHllArr.updateSlotNoKxQ(i++, value); value = Byte.toUnsignedInt(b3) >>> 2; tgtAbsHllArr.updateSlotNoKxQ(i++, value); } } break; } case 12: case 13: { //!HLL_8, srcLgK>tgtLgK, src=heap, tgt=heap/mem final int tgtKmask = (1 << tgtLgK) - 1; final AbstractHllArray tgtAbsHllArr = (AbstractHllArray)(tgt.hllSketchImpl); if (src.getTgtHllType() == HLL_4) { final Hll4Array src4 = (Hll4Array) src.hllSketchImpl; final AuxHashMap auxHashMap = src4.getAuxHashMap(); final int curMin = src4.getCurMin(); int i = 0; int j = 0; while (j < srcK) { final byte b = src4.hllByteArr[i++]; int value = Byte.toUnsignedInt(b) & loNibbleMask; tgtAbsHllArr.updateSlotNoKxQ(j & tgtKmask, value == AUX_TOKEN ? auxHashMap.mustFindValueFor(j) : value + curMin); j++; value = Byte.toUnsignedInt(b) >>> 4; tgtAbsHllArr.updateSlotNoKxQ(j & tgtKmask, value == AUX_TOKEN ? auxHashMap.mustFindValueFor(j) : value + curMin); j++; } } else { final Hll6Array src6 = (Hll6Array) src.hllSketchImpl; int i = 0; int j = 0; while (j < srcK) { final byte b1 = src6.hllByteArr[i++]; final byte b2 = src6.hllByteArr[i++]; final byte b3 = src6.hllByteArr[i++]; int value = Byte.toUnsignedInt(b1) & 0x3f; tgtAbsHllArr.updateSlotNoKxQ(j++ & tgtKmask, value); value = Byte.toUnsignedInt(b1) >>> 6; value |= (Byte.toUnsignedInt(b2) & 0x0f) << 2; tgtAbsHllArr.updateSlotNoKxQ(j++ & tgtKmask, value); value = Byte.toUnsignedInt(b2) >>> 4; value |= (Byte.toUnsignedInt(b3) & 3) << 4; tgtAbsHllArr.updateSlotNoKxQ(j++ & tgtKmask, value); value = Byte.toUnsignedInt(b3) >>> 2; tgtAbsHllArr.updateSlotNoKxQ(j++ & tgtKmask, value); } } break; } case 14: case 15: { //!HLL_8, srcLgK>tgtLgK, src=mem, tgt=heap/mem final int tgtKmask = (1 << tgtLgK) - 1; final AbstractHllArray tgtAbsHllArr = (AbstractHllArray)(tgt.hllSketchImpl); if (src.getTgtHllType() == HLL_4) { final DirectHll4Array src4 = (DirectHll4Array) src.hllSketchImpl; final AuxHashMap auxHashMap = src4.getAuxHashMap(); final int curMin = src4.getCurMin(); int i = 0; int j = 0; while (j < srcK) { final byte b = src4.mem.getByte(HLL_BYTE_ARR_START + i++); int value = Byte.toUnsignedInt(b) & loNibbleMask; tgtAbsHllArr.updateSlotNoKxQ(j & tgtKmask, value == AUX_TOKEN ? auxHashMap.mustFindValueFor(j) : value + curMin); j++; value = Byte.toUnsignedInt(b) >>> 4; tgtAbsHllArr.updateSlotNoKxQ(j & tgtKmask, value == AUX_TOKEN ? auxHashMap.mustFindValueFor(j) : value + curMin); j++; } } else { final DirectHll6Array src6 = (DirectHll6Array) src.hllSketchImpl; int i = 0; int offset = HLL_BYTE_ARR_START; while (i < srcK) { final byte b1 = src6.mem.getByte(offset++); final byte b2 = src6.mem.getByte(offset++); final byte b3 = src6.mem.getByte(offset++); int value = Byte.toUnsignedInt(b1) & 0x3f; tgtAbsHllArr.updateSlotNoKxQ(i++ & tgtKmask, value); value = Byte.toUnsignedInt(b1) >>> 6; value |= (Byte.toUnsignedInt(b2) & 0x0f) << 2; tgtAbsHllArr.updateSlotNoKxQ(i++ & tgtKmask, value); value = Byte.toUnsignedInt(b2) >>> 4; value |= (Byte.toUnsignedInt(b3) & 3) << 4; tgtAbsHllArr.updateSlotNoKxQ(i++ & tgtKmask, value); value = Byte.toUnsignedInt(b3) >>> 2; tgtAbsHllArr.updateSlotNoKxQ(i++ & tgtKmask, value); } } break; } default: break; //not possible } tgt.hllSketchImpl.putRebuildCurMinNumKxQFlag(true); } //Used by union operator. Always copies or downsamples to Heap HLL_8. //Caller must ultimately manage oooFlag, as caller has more context. /** * Copies or downsamples the given candidate HLLmode sketch to tgtLgK, HLL_8, on the heap. * * @param candidate the HllSketch to downsample, must be in HLL mode. * @param tgtLgK the LgK to downsample to. * @return the downsampled HllSketch. */ private static final HllSketch downsample(final HllSketch candidate, final int tgtLgK) { final AbstractHllArray candArr = (AbstractHllArray) candidate.hllSketchImpl; final HllArray tgtHllArr = HllArray.newHeapHll(tgtLgK, TgtHllType.HLL_8); final PairIterator candItr = candArr.iterator(); while (candItr.nextValid()) { tgtHllArr.couponUpdate(candItr.getPair()); //rebuilds KxQ, etc. } //both of these are required for isomorphism tgtHllArr.putHipAccum(candArr.getHipAccum()); tgtHllArr.putOutOfOrder(candidate.isOutOfOrder()); tgtHllArr.putRebuildCurMinNumKxQFlag(false); return new HllSketch(tgtHllArr); } //Used to rebuild curMin, numAtCurMin and KxQ registers, due to high performance merge operation static final void checkRebuildCurMinNumKxQ(final HllSketch sketch) { final HllSketchImpl hllSketchImpl = sketch.hllSketchImpl; final CurMode curMode = sketch.getCurMode(); final TgtHllType tgtHllType = sketch.getTgtHllType(); final boolean rebuild = hllSketchImpl.isRebuildCurMinNumKxQFlag(); if ( !rebuild || (curMode != CurMode.HLL) || (tgtHllType != HLL_8) ) { return; } final AbstractHllArray absHllArr = (AbstractHllArray)(hllSketchImpl); int curMin = 64; int numAtCurMin = 0; double kxq0 = 1 << absHllArr.getLgConfigK(); double kxq1 = 0; final PairIterator itr = absHllArr.iterator(); while (itr.nextAll()) { final int v = itr.getValue(); if (v > 0) { if (v < 32) { kxq0 += invPow2(v) - 1.0; } else { kxq1 += invPow2(v) - 1.0; } } if (v > curMin) { continue; } if (v < curMin) { curMin = v; numAtCurMin = 1; } else { numAtCurMin++; } } absHllArr.putKxQ0(kxq0); absHllArr.putKxQ1(kxq1); absHllArr.putCurMin(curMin); absHllArr.putNumAtCurMin(numAtCurMin); absHllArr.putRebuildCurMinNumKxQFlag(false); //HipAccum is not affected } }
2,504
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/PairIterator.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; /** * @author Lee Rhodes */ abstract class PairIterator { /** * Gets the header string for a list of pairs * @return the header string for a list of pairs */ String getHeader() { return String.format("%10s%10s%10s%6s", "Index", "Key", "Slot", "Value"); } /** * In LIST and SET modes, this gets the iterating index into the integer array of HLL key/value * pairs. * In HLL mode, this is the iterating index into the hypothetical array of HLL values, which may * be physically contructed differently based on the compaction scheme (HLL_4, HLL_6, HLL_8). * @return the index. */ abstract int getIndex(); /** * Gets the key, the low 26 bits of an pair, and can be up to 26 bits in length. * @return the key */ abstract int getKey(); /** * Gets the key, value pair as a single int where the key is the lower 26 bits * and the value is in the upper 6 bits. * @return the key, value pair. */ abstract int getPair(); /** * Gets the target or actual HLL slot number, which is derived from the key and LgConfigK. * The slot number is the index into a hypothetical array of length K and has LgConfigK bits. * If in LIST or SET mode this is the index into the hypothetical target HLL array of size K. * In HLL mode, this will be the actual index into the hypothetical target HLL array of size K. * @return the target or actual HLL slot number. */ abstract int getSlot(); /** * Gets the current pair as a string * @return the current pair as a string */ String getString() { final int index = getIndex(); final int key = getKey(); final int slot = getSlot(); final int value = getValue(); return String.format("%10d%10d%10d%6d", index, key, slot, value); } /** * Gets the HLL value of a particular slot or pair. * @return the HLL value of a particular slot or pair. */ abstract int getValue(); /** * Returns true at the next pair in sequence. * If false, the iteration is done. * @return true at the next pair in sequence. */ abstract boolean nextAll(); /** * Returns true at the next pair where getKey() and getValue() are valid. * If false, the iteration is done. * @return true at the next pair where getKey() and getValue() are valid. */ abstract boolean nextValid(); }
2,505
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/CurMode.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; /** * Represents the three fundamental modes of the HLL Sketch. * * @author Lee Rhodes * @author Kevin Lang */ enum CurMode { LIST, SET, HLL; //do not change the order. public static final CurMode values[] = values(); /** * Returns the CurMode given its ordinal * @param ordinal the order of appearance in the enum definition. * @return the CurMode given its ordinal */ public static CurMode fromOrdinal(final int ordinal) { return values[ordinal]; } }
2,506
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/DirectHll6Array.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.HllUtil.VAL_MASK_6; import static org.apache.datasketches.hll.HllUtil.noWriteAccess; import static org.apache.datasketches.hll.PreambleUtil.HLL_BYTE_ARR_START; import org.apache.datasketches.common.SketchesStateException; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.memory.WritableMemory; /** * @author Lee Rhodes */ final class DirectHll6Array extends DirectHllArray { //Called by HllSketch.writableWrap(), DirectCouponList.promoteListOrSetToHll DirectHll6Array(final int lgConfigK, final WritableMemory wmem) { super(lgConfigK, TgtHllType.HLL_6, wmem); } //Called by HllSketch.wrap(Memory) DirectHll6Array(final int lgConfigK, final Memory mem) { super(lgConfigK, TgtHllType.HLL_6, mem); } @Override HllSketchImpl copy() { return Hll6Array.heapify(mem); } @Override HllSketchImpl couponUpdate(final int coupon) { if (wmem == null) { noWriteAccess(); } final int newValue = HllUtil.getPairValue(coupon); final int configKmask = (1 << getLgConfigK()) - 1; final int slotNo = HllUtil.getPairLow26(coupon) & configKmask; updateSlotWithKxQ(slotNo, newValue); return this; } @Override int getHllByteArrBytes() { return hll6ArrBytes(lgConfigK); } @Override int getNibble(final int slotNo) { throw new SketchesStateException("Improper access."); } @Override final int getSlotValue(final int slotNo) { return get6Bit(mem, HLL_BYTE_ARR_START, slotNo); } @Override PairIterator iterator() { return new DirectHll6Iterator(1 << lgConfigK); } @Override void putNibble(final int slotNo, final int nibValue) { throw new SketchesStateException("Improper access."); } @Override //Would be used by Union, but not used because the gadget is always HLL8 type final void updateSlotNoKxQ(final int slotNo, final int newValue) { throw new SketchesStateException("Improper access."); } @Override //Used by this couponUpdate() //updates HipAccum, CurMin, NumAtCurMin, KxQs and checks newValue > oldValue final void updateSlotWithKxQ(final int slotNo, final int newValue) { final int oldValue = getSlotValue(slotNo); if (newValue > oldValue) { put6Bit(wmem, HLL_BYTE_ARR_START, slotNo, newValue); hipAndKxQIncrementalUpdate(this, oldValue, newValue); if (oldValue == 0) { decNumAtCurMin(); //overloaded as num zeros assert getNumAtCurMin() >= 0; } } } //off-heap / direct private static final void put6Bit(final WritableMemory wmem, final int offsetBytes, final int slotNo, final int newValue) { final int startBit = slotNo * 6; final int shift = startBit & 0X7; final int byteIdx = (startBit >>> 3) + offsetBytes; final int valShifted = (newValue & 0X3F) << shift; final int curMasked = wmem.getShort(byteIdx) & (~(VAL_MASK_6 << shift)); final short insert = (short) (curMasked | valShifted); wmem.putShort(byteIdx, insert); } //off-heap / direct private static final int get6Bit(final Memory mem, final int offsetBytes, final int slotNo) { final int startBit = slotNo * 6; final int shift = startBit & 0X7; final int byteIdx = (startBit >>> 3) + offsetBytes; return (byte) ((mem.getShort(byteIdx) >>> shift) & 0X3F); } //ITERATOR final class DirectHll6Iterator extends HllPairIterator { int bitOffset; DirectHll6Iterator(final int lengthPairs) { super(lengthPairs); bitOffset = -6; } @Override int value() { bitOffset += 6; final int tmp = mem.getShort(HLL_BYTE_ARR_START + (bitOffset / 8)); final int shift = (bitOffset % 8) & 0X7; return (tmp >>> shift) & VAL_MASK_6; } } }
2,507
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/CouponHashSet.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.HllUtil.EMPTY; import static org.apache.datasketches.hll.HllUtil.LG_INIT_SET_SIZE; import static org.apache.datasketches.hll.HllUtil.RESIZE_DENOM; import static org.apache.datasketches.hll.HllUtil.RESIZE_NUMER; import static org.apache.datasketches.hll.PreambleUtil.HASH_SET_INT_ARR_START; import static org.apache.datasketches.hll.PreambleUtil.HASH_SET_PREINTS; import static org.apache.datasketches.hll.PreambleUtil.LIST_INT_ARR_START; import static org.apache.datasketches.hll.PreambleUtil.computeLgArr; import static org.apache.datasketches.hll.PreambleUtil.extractCompactFlag; import static org.apache.datasketches.hll.PreambleUtil.extractCurMode; import static org.apache.datasketches.hll.PreambleUtil.extractHashSetCount; import static org.apache.datasketches.hll.PreambleUtil.extractInt; import static org.apache.datasketches.hll.PreambleUtil.extractLgArr; import static org.apache.datasketches.hll.PreambleUtil.extractLgK; import static org.apache.datasketches.hll.PreambleUtil.extractTgtHllType; import org.apache.datasketches.common.SketchesStateException; import org.apache.datasketches.memory.Memory; /** * @author Lee Rhodes * @author Kevin Lang */ class CouponHashSet extends CouponList { /** * Constructs this sketch with the intent of loading it with data * @param lgConfigK the configured Lg K * @param tgtHllType the new target Hll type */ CouponHashSet(final int lgConfigK, final TgtHllType tgtHllType) { super(lgConfigK, tgtHllType, CurMode.SET); assert lgConfigK > 7; } /** * Copy constructor * @param that another CouponHashSet */ CouponHashSet(final CouponHashSet that) { super(that); } /** * Copy As constructor. * @param that another CouponHashSet * @param tgtHllType the new target Hll type */ CouponHashSet(final CouponHashSet that, final TgtHllType tgtHllType) { super(that, tgtHllType); } //will also accept List, but results in a Set static final CouponHashSet heapifySet(final Memory mem) { final int lgConfigK = extractLgK(mem); final TgtHllType tgtHllType = extractTgtHllType(mem); final CurMode curMode = extractCurMode(mem); final int memArrStart = (curMode == CurMode.LIST) ? LIST_INT_ARR_START : HASH_SET_INT_ARR_START; final CouponHashSet set = new CouponHashSet(lgConfigK, tgtHllType); final boolean memIsCompact = extractCompactFlag(mem); final int couponCount = extractHashSetCount(mem); int lgCouponArrInts = extractLgArr(mem); if (lgCouponArrInts < LG_INIT_SET_SIZE) { lgCouponArrInts = computeLgArr(mem, couponCount, lgConfigK); } if (memIsCompact) { for (int i = 0; i < couponCount; i++) { set.couponUpdate(extractInt(mem, memArrStart + (i << 2))); } } else { //updatable set.couponCount = couponCount; set.lgCouponArrInts = lgCouponArrInts; final int couponArrInts = 1 << lgCouponArrInts; set.couponIntArr = new int[couponArrInts]; mem.getIntArray(HASH_SET_INT_ARR_START, set.couponIntArr, 0, couponArrInts); } return set; } @Override CouponHashSet copy() { return new CouponHashSet(this); } @Override CouponHashSet copyAs(final TgtHllType tgtHllType) { return new CouponHashSet(this, tgtHllType); } @Override HllSketchImpl couponUpdate(final int coupon) { final int index = find(couponIntArr, lgCouponArrInts, coupon); if (index >= 0) { return this; //found duplicate, ignore } couponIntArr[~index] = coupon; //found empty couponCount++; if (checkGrowOrPromote()) { return promoteHeapListOrSetToHll(this); } return this; } @Override int getMemDataStart() { return HASH_SET_INT_ARR_START; } @Override int getPreInts() { return HASH_SET_PREINTS; } private boolean checkGrowOrPromote() { if ((RESIZE_DENOM * couponCount) > (RESIZE_NUMER * (1 << lgCouponArrInts))) { if (lgCouponArrInts == (lgConfigK - 3)) { //at max size return true; // promote to HLL } couponIntArr = growHashSet(couponIntArr, ++lgCouponArrInts); } return false; } private static final int[] growHashSet(final int[] coupIntArr, final int tgtLgCoupArrSize) { final int[] tgtCouponIntArr = new int[1 << tgtLgCoupArrSize]; //create tgt final int len = coupIntArr.length; for (int i = 0; i < len; i++) { //scan input arr for non-zero values final int fetched = coupIntArr[i]; if (fetched != EMPTY) { final int idx = find(tgtCouponIntArr, tgtLgCoupArrSize, fetched); //find empty in tgt if (idx < 0) { //found EMPTY tgtCouponIntArr[~idx] = fetched; //insert continue; } throw new SketchesStateException("Error: found duplicate."); } } return tgtCouponIntArr; } }
2,508
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/AbstractHllArray.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.common.Util.invPow2; import static org.apache.datasketches.hll.PreambleUtil.HLL_BYTE_ARR_START; import static org.apache.datasketches.hll.PreambleUtil.HLL_PREINTS; import static org.apache.datasketches.hll.TgtHllType.HLL_4; import static org.apache.datasketches.hll.TgtHllType.HLL_6; import org.apache.datasketches.common.SketchesStateException; /** * @author Lee Rhodes */ abstract class AbstractHllArray extends HllSketchImpl { AuxHashMap auxHashMap = null; //used for both heap and direct HLL4 final int auxStart; //used for direct HLL4 AbstractHllArray(final int lgConfigK, final TgtHllType tgtHllType, final CurMode curMode) { super(lgConfigK, tgtHllType, curMode); auxStart = HLL_BYTE_ARR_START + hll4ArrBytes(lgConfigK); } abstract void addToHipAccum(double delta); @Override HllArray copyAs(final TgtHllType tgtHllType) { if (tgtHllType == getTgtHllType()) { return (HllArray) copy(); } if (tgtHllType == HLL_4) { return Conversions.convertToHll4(this); } if (tgtHllType == HLL_6) { return Conversions.convertToHll6(this); } return Conversions.convertToHll8(this); } abstract void decNumAtCurMin(); AuxHashMap getAuxHashMap() { return auxHashMap; } PairIterator getAuxIterator() { return (auxHashMap == null) ? null : auxHashMap.getIterator(); } @Override int getCompactSerializationBytes() { final AuxHashMap auxHashMap = getAuxHashMap(); final int auxCountBytes = (auxHashMap == null) ? 0 : auxHashMap.getAuxCount() << 2; return HLL_BYTE_ARR_START + getHllByteArrBytes() + auxCountBytes; } /** * This is the (non-HIP) estimator. * It is called "composite" because multiple estimators are pasted together. * @return the composite estimate */ //In C: again-two-registers.c hhb_get_composite_estimate L1489 @Override double getCompositeEstimate() { return HllEstimators.hllCompositeEstimate(this); } abstract int getCurMin(); @Override double getEstimate() { if (isOutOfOrder()) { return getCompositeEstimate(); } return getHipAccum(); } /** * For each actual update of the sketch, where the state of the sketch is changed, this register * tracks the Historical Inverse Probability or HIP. Before the update is recorded this register * is incremented by adding the inverse probability 1/Q (defined below). Since KxQ is scaled by K, * the actual increment is K/KxQ as can be seen in the hipAndKxQIncrementalUpdate(...) method * below. * @return the HIP Accumulator */ abstract double getHipAccum(); @Override double getHipEstimate() { return getHipAccum(); } abstract int getHllByteArrBytes(); /** * Q = KxQ/K is the probability that an incoming event can modify the state of the sketch. * KxQ is literally K times Q. The HIP estimator is based on tracking this probability as the * sketch gets populated. It is tracked in the hipAccum register. * * <p>The KxQ registers serve dual purposes: They are used in the HIP estimator and in * the "raw" HLL estimator defined in the Flajolet, et al, 2007 HLL paper. In order to do this, * the way the KxQ registers are computed here differ from how they are defined in the paper.</p> * * <p>The paper Fig 2 defines</p> * <pre>Z := ( sum[j=1,m](2^(-M[j])) )^(-1).</pre> * But the HIP estimator requires a computation of the probability defined above. * We accomplish both by redefining Z as * <pre>Z := ( m + sum[j=1,m](2^(-M[j] - 1)) )^(-1).</pre> * They are mathematically equivalent since: * <pre>m + sum[j=1,m](2^(-M[j] - 1)) == m + sum[j=1,m](2^(-M[j])) - m == sum[j=1,m](2^(-M[j])).</pre> * * @return KxQ0 */ abstract double getKxQ0(); /** * This second KxQ register is shifted by 32 bits to give us more than 90 bits of mantissa * precision, which produces more accurate results for very large counts. * @return KxQ1 */ abstract double getKxQ1(); @Override double getLowerBound(final int numStdDev) { HllUtil.checkNumStdDev(numStdDev); return HllEstimators.hllLowerBound(this, numStdDev); } @Override int getMemDataStart() { return HLL_BYTE_ARR_START; } abstract AuxHashMap getNewAuxHashMap(); /** * Returns the number of slots that have the value CurMin. * If CurMin is 0, then it returns the number of zeros in the array. * @return the number of slots that have the value CurMin. */ abstract int getNumAtCurMin(); @Override int getPreInts() { return HLL_PREINTS; } //overridden by Hll4Array and DirectHll4Array abstract int getNibble(int slotNo); abstract int getSlotValue(int slotNo); @Override //used by HLL6 and HLL8, Overridden by HLL4 int getUpdatableSerializationBytes() { return HLL_BYTE_ARR_START + getHllByteArrBytes(); } @Override double getUpperBound(final int numStdDev) { HllUtil.checkNumStdDev(numStdDev); return HllEstimators.hllUpperBound(this, numStdDev); } @Override abstract PairIterator iterator(); @Override void mergeTo(final HllSketch that) { throw new SketchesStateException("Possible Corruption, improper access."); } abstract void putAuxHashMap(AuxHashMap auxHashMap, boolean compact); abstract void putCurMin(int curMin); abstract void putHipAccum(double hipAccum); abstract void putKxQ0(double kxq0); abstract void putKxQ1(double kxq1); //overridden by Hll4Array and DirectHll4Array abstract void putNibble(int slotNo, int nibValue); abstract void putNumAtCurMin(int numAtCurMin); abstract void updateSlotWithKxQ(final int slotNo, final int value); abstract void updateSlotNoKxQ(final int slotNo, final int value); //Compute HLL byte array lengths, used by both heap and direct. static final int hll4ArrBytes(final int lgConfigK) { return 1 << (lgConfigK - 1); } static final int hll6ArrBytes(final int lgConfigK) { final int numSlots = 1 << lgConfigK; return ((numSlots * 3) >>> 2) + 1; } static final int hll8ArrBytes(final int lgConfigK) { return 1 << lgConfigK; } /** * Common HIP and KxQ incremental update for all heap and direct Hll. * This is used when incrementally updating an existing array with non-zero values. * @param host the origin implementation * @param oldValue old value * @param newValue new value */ //Called here and by Heap and Direct 6 and 8 bit implementations //In C: again-two-registers.c Lines 851 to 871 static final void hipAndKxQIncrementalUpdate(final AbstractHllArray host, final int oldValue, final int newValue) { assert newValue > oldValue; final double kxq0 = host.getKxQ0(); final double kxq1 = host.getKxQ1(); //update hipAccum BEFORE updating kxq0 and kxq1 host.addToHipAccum((1 << host.getLgConfigK()) / (kxq0 + kxq1)); incrementalUpdateKxQ(host, oldValue, newValue, kxq0, kxq1); } //separate KxQ updates static final void incrementalUpdateKxQ(final AbstractHllArray host, final int oldValue, final int newValue, double kxq0, double kxq1) { //update kxq0 and kxq1; subtract first, then add. if (oldValue < 32) { host.putKxQ0(kxq0 -= invPow2(oldValue)); } else { host.putKxQ1(kxq1 -= invPow2(oldValue)); } if (newValue < 32) { host.putKxQ0(kxq0 += invPow2(newValue)); } else { host.putKxQ1(kxq1 += invPow2(newValue)); } } }
2,509
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/Hll8Array.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.HllUtil.EMPTY; import static org.apache.datasketches.hll.HllUtil.KEY_BITS_26; import static org.apache.datasketches.hll.HllUtil.VAL_MASK_6; import static org.apache.datasketches.hll.PreambleUtil.extractLgK; import org.apache.datasketches.common.SketchesStateException; import org.apache.datasketches.memory.Memory; /** * Uses 8 bits per slot in a byte array. * @author Lee Rhodes */ class Hll8Array extends HllArray { /** * Standard constructor for new instance * @param lgConfigK the configured Lg K */ Hll8Array(final int lgConfigK) { super(lgConfigK, TgtHllType.HLL_8); hllByteArr = new byte[hll8ArrBytes(lgConfigK)]; } /** * Copy constructor * @param that another Hll8Array */ Hll8Array(final Hll8Array that) { super(that); } static final Hll8Array heapify(final Memory mem) { final int lgConfigK = extractLgK(mem); final Hll8Array hll8Array = new Hll8Array(lgConfigK); HllArray.extractCommonHll(mem, hll8Array); return hll8Array; } @Override Hll8Array copy() { return new Hll8Array(this); } @Override HllSketchImpl couponUpdate(final int coupon) { final int newValue = coupon >>> KEY_BITS_26; final int configKmask = (1 << lgConfigK) - 1; final int slotNo = coupon & configKmask; updateSlotWithKxQ(slotNo, newValue); return this; } @Override int getNibble(final int slotNo) { throw new SketchesStateException("Improper access."); } @Override final int getSlotValue(final int slotNo) { return hllByteArr[slotNo] & VAL_MASK_6; } @Override PairIterator iterator() { return new HeapHll8Iterator(1 << lgConfigK); } @Override void putNibble(final int slotNo, final int nibValue) { throw new SketchesStateException("Improper access."); } @Override //Used by Union when source is not HLL8 final void updateSlotNoKxQ(final int slotNo, final int newValue) { final int oldValue = getSlotValue(slotNo); hllByteArr[slotNo] = (byte) Math.max(newValue, oldValue); } @Override //Used by this couponUpdate() //updates HipAccum, CurMin, NumAtCurMin, KxQs and checks newValue > oldValue final void updateSlotWithKxQ(final int slotNo, final int newValue) { final int oldValue = getSlotValue(slotNo); if (newValue > oldValue) { hllByteArr[slotNo] = (byte) (newValue & VAL_MASK_6); hipAndKxQIncrementalUpdate(this, oldValue, newValue); if (oldValue == 0) { numAtCurMin--; //interpret numAtCurMin as num Zeros assert getNumAtCurMin() >= 0; } } } //ITERATOR final class HeapHll8Iterator extends HllPairIterator { HeapHll8Iterator(final int lengthPairs) { super(lengthPairs); } @Override int value() { return hllByteArr[index] & VAL_MASK_6; } @Override public boolean nextValid() { while (++index < lengthPairs) { value = hllByteArr[index] & VAL_MASK_6; if (value != EMPTY) { return true; } } return false; } } }
2,510
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/TgtHllType.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; /** * Specifies the target type of HLL sketch to be created. It is a target in that the actual * allocation of the HLL array is deferred until sufficient number of items have been received by * the warm-up phases. * * <p>These three target types are isomorphic representations of the same underlying HLL algorithm. * Thus, given the same value of <i>lgConfigK</i> and the same input, all three HLL target types * will produce identical estimates and have identical error distributions.</p> * * <p>The memory (and also the serialization) of the sketch during this early warmup phase starts * out very small (8 bytes, when empty) and then grows in increments of 4 bytes as required * until the full HLL array is allocated. This transition point occurs at about 10% of K for * sketches where lgConfigK is &gt; 8.</p> * * <ul> * <li><b>HLL 8</b> This uses an 8-bit byte per HLL bucket. It is generally the * fastest in terms of update time, but has the largest storage footprint of about * <i>K</i> bytes.</li> * * <li><b>HLL 6</b> This uses a 6-bit field per HLL bucket. It is the generally the next fastest * in terms of update time with a storage footprint of about <i>3/4 * K</i> bytes.</li> * * <li><b>HLL 4</b> This uses a 4-bit field per HLL bucket and for large counts may require * the use of a small internal auxiliary array for storing statistical exceptions, which are rare. * For the values of <i>lgConfigK &gt; 13</i> (<i>K</i> = 8192), * this additional array adds about 3% to the overall storage. It is generally the slowest in * terms of update time, but has the smallest storage footprint of about * <i>K/2 * 1.03</i> bytes.</li> * </ul> * @author Lee Rhodes */ public enum TgtHllType { HLL_4, HLL_6, HLL_8; private static final TgtHllType values[] = values(); public static final TgtHllType fromOrdinal(final int typeId) { return values[typeId]; } }
2,511
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/DirectCouponList.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.HllUtil.EMPTY; import static org.apache.datasketches.hll.HllUtil.LG_INIT_LIST_SIZE; import static org.apache.datasketches.hll.HllUtil.LG_INIT_SET_SIZE; import static org.apache.datasketches.hll.HllUtil.noWriteAccess; import static org.apache.datasketches.hll.PreambleUtil.EMPTY_FLAG_MASK; import static org.apache.datasketches.hll.PreambleUtil.HASH_SET_PREINTS; import static org.apache.datasketches.hll.PreambleUtil.HLL_PREINTS; import static org.apache.datasketches.hll.PreambleUtil.LIST_INT_ARR_START; import static org.apache.datasketches.hll.PreambleUtil.LIST_PREINTS; import static org.apache.datasketches.hll.PreambleUtil.computeLgArr; import static org.apache.datasketches.hll.PreambleUtil.extractCompactFlag; import static org.apache.datasketches.hll.PreambleUtil.extractInt; import static org.apache.datasketches.hll.PreambleUtil.extractLgArr; import static org.apache.datasketches.hll.PreambleUtil.extractListCount; import static org.apache.datasketches.hll.PreambleUtil.insertCurMin; import static org.apache.datasketches.hll.PreambleUtil.insertCurMode; import static org.apache.datasketches.hll.PreambleUtil.insertEmptyFlag; import static org.apache.datasketches.hll.PreambleUtil.insertFamilyId; import static org.apache.datasketches.hll.PreambleUtil.insertFlags; import static org.apache.datasketches.hll.PreambleUtil.insertInt; import static org.apache.datasketches.hll.PreambleUtil.insertKxQ0; import static org.apache.datasketches.hll.PreambleUtil.insertLgArr; import static org.apache.datasketches.hll.PreambleUtil.insertLgK; import static org.apache.datasketches.hll.PreambleUtil.insertListCount; import static org.apache.datasketches.hll.PreambleUtil.insertModes; import static org.apache.datasketches.hll.PreambleUtil.insertNumAtCurMin; import static org.apache.datasketches.hll.PreambleUtil.insertPreInts; import static org.apache.datasketches.hll.PreambleUtil.insertSerVer; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.common.SketchesStateException; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.memory.WritableMemory; /** * @author Lee Rhodes */ class DirectCouponList extends AbstractCoupons { WritableMemory wmem; Memory mem; final boolean compact; //called from newInstance, writableWrap and DirectCouponHashSet DirectCouponList(final int lgConfigK, final TgtHllType tgtHllType, final CurMode curMode, final WritableMemory wmem) { super(lgConfigK, tgtHllType, curMode); this.wmem = wmem; mem = wmem; compact = extractCompactFlag(wmem); assert !compact; } //called from HllSketch.wrap and from DirectCouponHashSet constructor, may be compact DirectCouponList(final int lgConfigK, final TgtHllType tgtHllType, final CurMode curMode, final Memory mem) { super(lgConfigK, tgtHllType, curMode); wmem = null; this.mem = mem; compact = extractCompactFlag(mem); } /** * Standard factory for new DirectCouponList. * This initializes the given WritableMemory. * @param lgConfigK the configured Lg K * @param tgtHllType the configured HLL target * @param dstMem the destination memory for the sketch. * @return a new DirectCouponList */ static DirectCouponList newInstance(final int lgConfigK, final TgtHllType tgtHllType, final WritableMemory dstMem) { insertPreInts(dstMem, LIST_PREINTS); insertSerVer(dstMem); insertFamilyId(dstMem); insertLgK(dstMem, lgConfigK); insertLgArr(dstMem, LG_INIT_LIST_SIZE); insertFlags(dstMem, EMPTY_FLAG_MASK); //empty and not compact insertListCount(dstMem, 0); insertModes(dstMem, tgtHllType, CurMode.LIST); return new DirectCouponList(lgConfigK, tgtHllType, CurMode.LIST, dstMem); } @Override //returns on-heap List CouponList copy() { return CouponList.heapifyList(mem); } @Override //returns on-heap List CouponList copyAs(final TgtHllType tgtHllType) { final CouponList clist = CouponList.heapifyList(mem); return new CouponList(clist, tgtHllType); } @Override HllSketchImpl couponUpdate(final int coupon) { if (wmem == null) { noWriteAccess(); } final int len = 1 << getLgCouponArrInts(); for (int i = 0; i < len; i++) { //search for empty slot and duplicates final int couponAtIdx = extractInt(mem, LIST_INT_ARR_START + (i << 2)); if (couponAtIdx == EMPTY) { insertInt(wmem, LIST_INT_ARR_START + (i << 2), coupon); int couponCount = extractListCount(mem); insertListCount(wmem, ++couponCount); insertEmptyFlag(wmem, false); //only first time if (couponCount >= len) { //array full if (lgConfigK < 8) { return promoteListOrSetToHll(this);//oooFlag = false } return promoteListToSet(this); //oooFlag = true } return this; } //cell not empty if (couponAtIdx == coupon) { return this; } //return if duplicate //cell not empty & not a duplicate, continue } //end for throw new SketchesStateException("Invalid State: no empties & no duplicates"); } @Override int getCompactSerializationBytes() { return getMemDataStart() + (getCouponCount() << 2); } @Override //Overridden by DirectCouponHashSet int getCouponCount() { return extractListCount(mem); } @Override int[] getCouponIntArr() { //here only to satisfy the abstract, should not be used return null; } @Override int getLgCouponArrInts() { final int lgArr = extractLgArr(mem); if (lgArr >= LG_INIT_LIST_SIZE) { return lgArr; } //early versions of compact images did not use this lgArr field final int coupons = getCouponCount(); return computeLgArr(mem, coupons, lgConfigK); } @Override int getMemDataStart() { return LIST_INT_ARR_START; } @Override Memory getMemory() { return mem; } @Override int getPreInts() { return LIST_PREINTS; } @Override WritableMemory getWritableMemory() { return wmem; } @Override boolean isCompact() { return compact; } @Override boolean isMemory() { return true; } @Override boolean isOffHeap() { return mem.isDirect(); } @Override boolean isSameResource(final Memory mem) { return this.mem.isSameResource(mem); } @Override PairIterator iterator() { final long dataStart = getMemDataStart(); final int lenInts = (compact) ? getCouponCount() : 1 << getLgCouponArrInts(); return new IntMemoryPairIterator(mem, dataStart, lenInts, lgConfigK); } @Override void mergeTo(final HllSketch that) { final int lenInts = (compact) ? getCouponCount() : 1 << getLgCouponArrInts(); final int dataStart = getMemDataStart(); for (int i = 0; i < lenInts; i++) { final int pair = mem.getInt(dataStart + (i << 2)); if (pair == 0) { continue; } that.couponUpdate(pair); } } @Override DirectCouponList reset() { if (wmem == null) { throw new SketchesArgumentException("Cannot reset a read-only sketch"); } insertEmptyFlag(wmem, true); final int bytes = HllSketch.getMaxUpdatableSerializationBytes(lgConfigK, tgtHllType); wmem.clear(0, bytes); return DirectCouponList.newInstance(lgConfigK, tgtHllType, wmem); } //Called by DirectCouponList.couponUpdate() static final DirectCouponHashSet promoteListToSet(final DirectCouponList src) { final WritableMemory wmem = src.wmem; //get the data from the current memory HllUtil.checkPreamble(wmem); //sanity check final int lgConfigK = src.lgConfigK; final TgtHllType tgtHllType = src.tgtHllType; final int srcOffset = LIST_INT_ARR_START; final int couponArrInts = 1 << src.getLgCouponArrInts(); final int[] couponArr = new int[couponArrInts]; //buffer wmem.getIntArray(srcOffset, couponArr, 0, couponArrInts); //rewrite the memory image as a SET: insertPreInts(wmem, HASH_SET_PREINTS); //SerVer, FamID, LgK should be OK insertLgArr(wmem, LG_INIT_SET_SIZE); insertCurMin(wmem, 0); //was list count insertCurMode(wmem, CurMode.SET); //tgtHllType should already be ok final int maxBytes = HllSketch.getMaxUpdatableSerializationBytes(lgConfigK, tgtHllType); wmem.clear(LIST_INT_ARR_START, maxBytes - LIST_INT_ARR_START); //clear all past first 8 //create the tgt final DirectCouponHashSet dchSet = new DirectCouponHashSet(src.lgConfigK, src.tgtHllType, src.wmem); //now reload the coupon data into the set for (int i = 0; i < couponArrInts; i++) { final int coupon = couponArr[i]; if (coupon != EMPTY) { dchSet.couponUpdate(coupon); } } return dchSet; } static final DirectHllArray promoteListOrSetToHll(final DirectCouponList src) { final WritableMemory wmem = src.wmem; //get the data from the current list or set memory HllUtil.checkPreamble(wmem); //sanity check final int lgConfigK = src.lgConfigK; final TgtHllType tgtHllType = src.tgtHllType; final int srcMemDataStart = src.getMemDataStart(); final double est = src.getEstimate(); final int couponArrInts = 1 << src.getLgCouponArrInts(); final int[] couponArr = new int[couponArrInts]; //buffer wmem.getIntArray(srcMemDataStart, couponArr, 0, couponArrInts); //rewrite the memory image as an HLL insertPreInts(wmem, HLL_PREINTS); //SerVer, FamID, LgK should be OK insertLgArr(wmem, 0); //no Aux possible yet insertFlags(wmem, 0); //clear all flags insertCurMin(wmem, 0); insertCurMode(wmem, CurMode.HLL); //tgtHllType should already be set //we update HipAccum at the end //clear KxQ0, KxQ1, NumAtCurMin, AuxCount, hllArray, auxArr final int maxBytes = HllSketch.getMaxUpdatableSerializationBytes(lgConfigK, tgtHllType); wmem.clear(LIST_INT_ARR_START, maxBytes - LIST_INT_ARR_START); //clear all past first 8 insertNumAtCurMin(wmem, 1 << lgConfigK); //set numAtCurMin insertKxQ0(wmem, 1 << lgConfigK); //choose the tgt final DirectHllArray dirHllArr; if (tgtHllType == TgtHllType.HLL_4) { dirHllArr = new DirectHll4Array(lgConfigK, wmem); } else if (tgtHllType == TgtHllType.HLL_6) { dirHllArr = new DirectHll6Array(lgConfigK, wmem); } else { //Hll_8 dirHllArr = new DirectHll8Array(lgConfigK, wmem); } //now load the coupon data into HLL for (int i = 0; i < couponArrInts; i++) { final int coupon = couponArr[i]; if (coupon != EMPTY) { dirHllArr.couponUpdate(coupon); } } dirHllArr.putHipAccum(est); return dirHllArr; } }
2,512
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/HllArray.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.HllUtil.LG_AUX_ARR_INTS; import static org.apache.datasketches.hll.PreambleUtil.HLL_BYTE_ARR_START; import static org.apache.datasketches.hll.PreambleUtil.extractCurMin; import static org.apache.datasketches.hll.PreambleUtil.extractEmptyFlag; import static org.apache.datasketches.hll.PreambleUtil.extractHipAccum; import static org.apache.datasketches.hll.PreambleUtil.extractKxQ0; import static org.apache.datasketches.hll.PreambleUtil.extractKxQ1; import static org.apache.datasketches.hll.PreambleUtil.extractNumAtCurMin; import static org.apache.datasketches.hll.PreambleUtil.extractOooFlag; import static org.apache.datasketches.hll.PreambleUtil.extractRebuildCurMinNumKxQFlag; import static org.apache.datasketches.hll.TgtHllType.HLL_4; import static org.apache.datasketches.hll.TgtHllType.HLL_6; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.memory.WritableMemory; /** * @author Lee Rhodes * @author Kevin Lang */ abstract class HllArray extends AbstractHllArray { boolean oooFlag = false; //Out-Of-Order Flag boolean rebuildCurMinNumKxQ = false; int curMin; //always zero for Hll6 and Hll8, only used by Hll4Array int numAtCurMin; //# of values at curMin. If curMin = 0, it is # of zeros double hipAccum; double kxq0; double kxq1; byte[] hllByteArr = null; //init by sub-classes /** * Standard constructor for new instance * @param lgConfigK the configured Lg K * @param tgtHllType the type of target HLL sketch */ HllArray(final int lgConfigK, final TgtHllType tgtHllType) { super(lgConfigK, tgtHllType, CurMode.HLL); curMin = 0; numAtCurMin = 1 << lgConfigK; hipAccum = 0; kxq0 = 1 << lgConfigK; kxq1 = 0; } /** * Copy constructor * @param that another HllArray */ HllArray(final HllArray that) { super(that.getLgConfigK(), that.getTgtHllType(), CurMode.HLL); oooFlag = that.isOutOfOrder(); rebuildCurMinNumKxQ = that.isRebuildCurMinNumKxQFlag(); curMin = that.getCurMin(); numAtCurMin = that.getNumAtCurMin(); hipAccum = that.getHipAccum(); kxq0 = that.getKxQ0(); kxq1 = that.getKxQ1(); hllByteArr = that.hllByteArr.clone(); //that.hllByteArr should never be null. final AuxHashMap thatAuxMap = that.getAuxHashMap(); if (thatAuxMap != null) { putAuxHashMap(thatAuxMap.copy(), false); } else { putAuxHashMap(null, false); } } static final HllArray newHeapHll(final int lgConfigK, final TgtHllType tgtHllType) { if (tgtHllType == HLL_4) { return new Hll4Array(lgConfigK); } if (tgtHllType == HLL_6) { return new Hll6Array(lgConfigK); } return new Hll8Array(lgConfigK); } @Override void addToHipAccum(final double delta) { hipAccum += delta; } @Override void decNumAtCurMin() { numAtCurMin--; } @Override int getCurMin() { return curMin; } @Override CurMode getCurMode() { return curMode; } @Override double getHipAccum() { return hipAccum; } @Override int getHllByteArrBytes() { return hllByteArr.length; } @Override double getKxQ0() { return kxq0; } @Override double getKxQ1() { return kxq1; } @Override int getLgConfigK() { return lgConfigK; } @Override Memory getMemory() { return null; } @Override AuxHashMap getNewAuxHashMap() { return new HeapAuxHashMap(LG_AUX_ARR_INTS[lgConfigK], lgConfigK); } @Override int getNumAtCurMin() { return numAtCurMin; } @Override WritableMemory getWritableMemory() { return null; } @Override boolean isCompact() { return false; } @Override boolean isEmpty() { return false; //because there should be no normal way to create an HllArray that is empty } @Override boolean isMemory() { return false; } @Override boolean isOffHeap() { return false; } @Override boolean isOutOfOrder() { return oooFlag; } @Override boolean isSameResource(final Memory mem) { return false; } @Override boolean isRebuildCurMinNumKxQFlag() { return rebuildCurMinNumKxQ; } @Override void putAuxHashMap(final AuxHashMap auxHashMap, final boolean compact) { this.auxHashMap = auxHashMap; } @Override void putCurMin(final int curMin) { this.curMin = curMin; } @Override void putEmptyFlag(final boolean empty) { } @Override void putHipAccum(final double value) { hipAccum = value; } @Override void putKxQ0(final double kxq0) { this.kxq0 = kxq0; } @Override void putKxQ1(final double kxq1) { this.kxq1 = kxq1; } @Override void putNumAtCurMin(final int numAtCurMin) { this.numAtCurMin = numAtCurMin; } @Override void putOutOfOrder(final boolean oooFlag) { if (oooFlag) { putHipAccum(0); } this.oooFlag = oooFlag; } @Override void putRebuildCurMinNumKxQFlag(final boolean rebuild) { rebuildCurMinNumKxQ = rebuild; } @Override HllSketchImpl reset() { return new CouponList(lgConfigK, tgtHllType, CurMode.LIST); } @Override //used by HLL6 and HLL8, overridden by HLL4 byte[] toCompactByteArray() { return toUpdatableByteArray(); //indistinguishable for HLL6 and HLL8 } @Override //used by HLL4, HLL6 and HLL8 byte[] toUpdatableByteArray() { return ToByteArrayImpl.toHllByteArray(this, false); } //used by heapify by all Heap HLL static final void extractCommonHll(final Memory srcMem, final HllArray hllArray) { hllArray.putOutOfOrder(extractOooFlag(srcMem)); hllArray.putEmptyFlag(extractEmptyFlag(srcMem)); hllArray.putCurMin(extractCurMin(srcMem)); hllArray.putHipAccum(extractHipAccum(srcMem)); hllArray.putKxQ0(extractKxQ0(srcMem)); hllArray.putKxQ1(extractKxQ1(srcMem)); hllArray.putNumAtCurMin(extractNumAtCurMin(srcMem)); hllArray.putRebuildCurMinNumKxQFlag(extractRebuildCurMinNumKxQFlag(srcMem)); //load Hll array srcMem.getByteArray(HLL_BYTE_ARR_START, hllArray.hllByteArr, 0, hllArray.hllByteArr.length); } }
2,513
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/CouponList.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.HllUtil.EMPTY; import static org.apache.datasketches.hll.HllUtil.LG_INIT_LIST_SIZE; import static org.apache.datasketches.hll.HllUtil.LG_INIT_SET_SIZE; import static org.apache.datasketches.hll.PreambleUtil.LIST_INT_ARR_START; import static org.apache.datasketches.hll.PreambleUtil.LIST_PREINTS; import static org.apache.datasketches.hll.PreambleUtil.extractLgK; import static org.apache.datasketches.hll.PreambleUtil.extractListCount; import static org.apache.datasketches.hll.PreambleUtil.extractTgtHllType; import org.apache.datasketches.common.SketchesStateException; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.memory.WritableMemory; /** * @author Lee Rhodes * @author Kevin Lang */ class CouponList extends AbstractCoupons { int lgCouponArrInts; int couponCount; int[] couponIntArr; /** * New instance constructor for LIST or SET. * @param lgConfigK the configured Lg K * @param tgtHllType the configured HLL target * @param curMode LIST or SET */ CouponList(final int lgConfigK, final TgtHllType tgtHllType, final CurMode curMode) { super(lgConfigK, tgtHllType, curMode); if (curMode == CurMode.LIST) { lgCouponArrInts = LG_INIT_LIST_SIZE; } else { //SET lgCouponArrInts = LG_INIT_SET_SIZE; assert lgConfigK > 7; } couponIntArr = new int[1 << lgCouponArrInts]; couponCount = 0; } /** * Copy Constructor * @param that another CouponArray */ CouponList(final CouponList that) { super(that.lgConfigK, that.tgtHllType, that.curMode); lgCouponArrInts = that.lgCouponArrInts; couponCount = that.couponCount; couponIntArr = that.couponIntArr.clone(); } /** * Copy As constructor. * @param that another CouponList * @param tgtHllType the new target Hll type */ //also used by CouponHashSet CouponList(final CouponList that, final TgtHllType tgtHllType) { super(that.lgConfigK, tgtHllType, that.curMode); lgCouponArrInts = that.lgCouponArrInts; couponCount = that.couponCount; couponIntArr = that.couponIntArr.clone(); } static final CouponList heapifyList(final Memory mem) { final int lgConfigK = extractLgK(mem); final TgtHllType tgtHllType = extractTgtHllType(mem); final CouponList list = new CouponList(lgConfigK, tgtHllType, CurMode.LIST); final int couponCount = extractListCount(mem); mem.getIntArray(LIST_INT_ARR_START, list.couponIntArr, 0, couponCount); list.couponCount = couponCount; return list; } @Override CouponList copy() { return new CouponList(this); } @Override CouponList copyAs(final TgtHllType tgtHllType) { return new CouponList(this, tgtHllType); } @Override HllSketchImpl couponUpdate(final int coupon) { final int len = 1 << lgCouponArrInts; for (int i = 0; i < len; i++) { //search for empty slot final int couponAtIdx = couponIntArr[i]; if (couponAtIdx == EMPTY) { couponIntArr[i] = coupon; //update couponCount++; if (couponCount >= len) { //array full if (lgConfigK < 8) { return promoteHeapListOrSetToHll(this); //oooFlag = false } return promoteHeapListToSet(this); //oooFlag = true } return this; } //cell not empty if (couponAtIdx == coupon) { return this; //duplicate } //cell not empty & not a duplicate, continue } //end for throw new SketchesStateException("Array invalid: no empties & no duplicates"); } @Override int getCompactSerializationBytes() { return getMemDataStart() + (couponCount << 2); } @Override int getCouponCount() { return couponCount; } @Override int[] getCouponIntArr() { return couponIntArr; } @Override int getLgCouponArrInts() { return lgCouponArrInts; } @Override int getMemDataStart() { return LIST_INT_ARR_START; } @Override Memory getMemory() { return null; } @Override int getPreInts() { return LIST_PREINTS; } @Override WritableMemory getWritableMemory() { return null; } @Override boolean isCompact() { return false; } @Override boolean isMemory() { return false; } @Override boolean isOffHeap() { return false; } @Override boolean isSameResource(final Memory mem) { return false; } @Override PairIterator iterator() { return new IntArrayPairIterator(couponIntArr, lgConfigK); } @Override void mergeTo(final HllSketch that) { final int arrLen = couponIntArr.length; for (int i = 0; i < arrLen; i++) { final int pair = couponIntArr[i]; if (pair == 0) { continue; } that.couponUpdate(pair); } } @Override CouponList reset() { return new CouponList(lgConfigK, tgtHllType, CurMode.LIST); } static final HllSketchImpl promoteHeapListToSet(final CouponList list) { final int couponCount = list.couponCount; final int[] arr = list.couponIntArr; final CouponHashSet chSet = new CouponHashSet(list.lgConfigK, list.tgtHllType); for (int i = 0; i < couponCount; i++) { chSet.couponUpdate(arr[i]); } return chSet; } //Promotional move of coupons to an HllSketch from either List or Set. //called by CouponHashSet.couponUpdate() //called by CouponList.couponUpdate() static final HllSketchImpl promoteHeapListOrSetToHll(final CouponList src) { final HllArray tgtHllArr = HllArray.newHeapHll(src.lgConfigK, src.tgtHllType); final PairIterator srcItr = src.iterator(); tgtHllArr.putKxQ0(1 << src.lgConfigK); while (srcItr.nextValid()) { tgtHllArr.couponUpdate(srcItr.getPair()); } tgtHllArr.putHipAccum(src.getEstimate()); tgtHllArr.putOutOfOrder(false); return tgtHllArr; } }
2,514
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/HllSketch.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.common.Util.checkBounds; import static org.apache.datasketches.hll.HllUtil.EMPTY; import static org.apache.datasketches.hll.HllUtil.KEY_BITS_26; import static org.apache.datasketches.hll.HllUtil.LG_AUX_ARR_INTS; import static org.apache.datasketches.hll.HllUtil.checkPreamble; import static org.apache.datasketches.hll.PreambleUtil.HLL_BYTE_ARR_START; import static org.apache.datasketches.hll.PreambleUtil.extractCompactFlag; import static org.apache.datasketches.hll.PreambleUtil.extractLgK; import static org.apache.datasketches.hll.PreambleUtil.extractTgtHllType; import java.util.Objects; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.memory.WritableMemory; /** * The HllSketch is actually a collection of compact implementations of Phillipe Flajolet’s HyperLogLog (HLL) * sketch but with significantly improved error behavior and excellent speed performance. * * <p>If the use case for sketching is primarily counting uniques and merging, the HLL sketch is the 2nd highest * performing in terms of accuracy for storage space consumed in the DataSketches library * (the new CPC sketch developed by Kevin J. Lang now beats HLL in terms of accuracy / space). * For large counts, HLL sketches can be 2 to 8 times smaller for the same accuracy than the DataSketches Theta * Sketches when serialized, but the Theta sketches can do set intersections and differences while HLL and CPC cannot. * The CPC sketch and HLL share similar use cases, but the CPC sketch is about 30 to 40% smaller than the HLL sketch * when serialized and larger than the HLL when active in memory. Choose your weapons!</p> * * <p>A new HLL sketch is created with a simple constructor:</p> * <pre>{@code * int lgK = 12; //This is log-base2 of k, so k = 4096. lgK can be from 4 to 21 * HllSketch sketch = new HllSketch(lgK); //TgtHllType.HLL_4 is the default * //OR * HllSketch sketch = new HllSketch(lgK, TgtHllType.HLL_6); * //OR * HllSketch sketch = new HllSketch(lgK, TgtHllType.HLL_8); * }</pre> * * <p>All three different sketch types are targets in that the sketches start out in a warm-up mode that is small in * size and gradually grows as needed until the full HLL array is allocated. The HLL_4, HLL_6 and HLL_8 represent * different levels of compression of the final HLL array where the 4, 6 and 8 refer to the number of bits each * bucket of the HLL array is compressed down to. * The HLL_4 is the most compressed but generally slower than the other two, especially during union operations.</p> * * <p>All three types share the same API. Updating the HllSketch is very simple:</p> * * <pre>{@code * long n = 1000000; * for (int i = 0; i < n; i++) { * sketch.update(i); * } * }</pre> * * <p>Each of the presented integers above are first hashed into 128-bit hash values that are used by the sketch * HLL algorithm, so the above loop is essentially equivalent to using a random number generator initialized with a * seed so that the sequence is deterministic and random.</p> * * <p>Obtaining the cardinality results from the sketch is also simple:</p> * * <pre>{@code * double estimate = sketch.getEstimate(); * double estUB = sketch.getUpperBound(1.0); //the upper bound at 1 standard deviation. * double estLB = sketch.getLowerBound(1.0); //the lower bound at 1 standard deviation. * //OR * System.out.println(sketch.toString()); //will output a summary of the sketch. * }</pre> * * <p>Which produces a console output something like this:</p> * * <pre>{@code * ### HLL SKETCH SUMMARY: * Log Config K : 12 * Hll Target : HLL_4 * Current Mode : HLL * LB : 977348.7024560181 * Estimate : 990116.6007366662 * UB : 1003222.5095308956 * OutOfOrder Flag: false * CurMin : 5 * NumAtCurMin : 1 * HipAccum : 990116.6007366662 * }</pre> * * @author Lee Rhodes * @author Kevin Lang */ public class HllSketch extends BaseHllSketch { /** * The default Log_base2 of K */ public static final int DEFAULT_LG_K = 12; /** * The default HLL-TYPE is HLL_4 */ public static final TgtHllType DEFAULT_HLL_TYPE = TgtHllType.HLL_4; private static final String LS = System.getProperty("line.separator"); HllSketchImpl hllSketchImpl = null; /** * Constructs a new on-heap sketch with the default lgConfigK and tgtHllType. */ public HllSketch() { this(DEFAULT_LG_K, DEFAULT_HLL_TYPE); } /** * Constructs a new on-heap sketch with the default tgtHllType. * @param lgConfigK The Log2 of K for the target HLL sketch. This value must be * between 4 and 21 inclusively. */ public HllSketch(final int lgConfigK) { this(lgConfigK, DEFAULT_HLL_TYPE); } /** * Constructs a new on-heap sketch with the type of HLL sketch to configure. * @param lgConfigK The Log2 of K for the target HLL sketch. This value must be * between 4 and 21 inclusively. * @param tgtHllType the desired HLL type. */ public HllSketch(final int lgConfigK, final TgtHllType tgtHllType) { hllSketchImpl = new CouponList(HllUtil.checkLgK(lgConfigK), tgtHllType, CurMode.LIST); } /** * Constructs a new sketch with the type of HLL sketch to configure and the given * WritableMemory as the destination for the sketch. This WritableMemory is usually configured * for off-heap memory. What remains on the java heap is a thin wrapper object that reads and * writes to the given WritableMemory. * * <p>The given <i>dstMem</i> is checked for the required capacity as determined by * {@link #getMaxUpdatableSerializationBytes(int, TgtHllType)}. * @param lgConfigK The Log2 of K for the target HLL sketch. This value must be * between 4 and 21 inclusively. * @param tgtHllType the desired HLL type. * @param dstMem the destination memory for the sketch. */ public HllSketch(final int lgConfigK, final TgtHllType tgtHllType, final WritableMemory dstMem) { Objects.requireNonNull(dstMem, "Destination Memory must not be null"); final long minBytes = getMaxUpdatableSerializationBytes(lgConfigK, tgtHllType); final long capBytes = dstMem.getCapacity(); HllUtil.checkMemSize(minBytes, capBytes); dstMem.clear(0, minBytes); hllSketchImpl = DirectCouponList.newInstance(lgConfigK, tgtHllType, dstMem); } /** * Copy constructor used by copy(). * @param that another HllSketch */ HllSketch(final HllSketch that) { hllSketchImpl = that.hllSketchImpl.copy(); } /** * Special constructor used by copyAs, heapify * @param that another HllSketchImpl, which must already be a copy */ HllSketch(final HllSketchImpl that) { hllSketchImpl = that; } /** * Heapify the given byte array, which must be a valid HllSketch image and may have data. * @param byteArray the given byte array. This byteArray is not modified and is not retained * by the on-heap sketch. * @return an HllSketch on the java heap. */ public static final HllSketch heapify(final byte[] byteArray) { return heapify(Memory.wrap(byteArray)); } /** * Heapify the given Memory, which must be a valid HllSketch image and may have data. * @param srcMem the given Memory, which is read-only. * @return an HllSketch on the java heap. */ public static final HllSketch heapify(final Memory srcMem) { return heapify(srcMem, true); } //used by union and above static final HllSketch heapify(final Memory srcMem, final boolean checkRebuild) { Objects.requireNonNull(srcMem, "Source Memory must not be null"); checkBounds(0, 8, srcMem.getCapacity()); //need min 8 bytes final CurMode curMode = checkPreamble(srcMem); final HllSketch heapSketch; if (curMode == CurMode.HLL) { final TgtHllType tgtHllType = extractTgtHllType(srcMem); if (tgtHllType == TgtHllType.HLL_4) { heapSketch = new HllSketch(Hll4Array.heapify(srcMem)); } else if (tgtHllType == TgtHllType.HLL_6) { heapSketch = new HllSketch(Hll6Array.heapify(srcMem)); } else { //Hll_8 heapSketch = new HllSketch(Hll8Array.heapify(srcMem)); if (checkRebuild) { Union.checkRebuildCurMinNumKxQ(heapSketch); } } } else if (curMode == CurMode.LIST) { heapSketch = new HllSketch(CouponList.heapifyList(srcMem)); } else { heapSketch = new HllSketch(CouponHashSet.heapifySet(srcMem)); } return heapSketch; } /** * Wraps the given WritableMemory, which must be a image of a valid updatable sketch, * and may have data. What remains on the java heap is a * thin wrapper object that reads and writes to the given WritableMemory, which, depending on * how the user configures the WritableMemory, may actually reside on the Java heap or off-heap. * * <p>The given <i>dstMem</i> is checked for the required capacity as determined by * {@link #getMaxUpdatableSerializationBytes(int, TgtHllType)}. * @param srcWmem an writable image of a valid source sketch with data. * @return an HllSketch where the sketch data is in the given dstMem. */ public static final HllSketch writableWrap(final WritableMemory srcWmem) { return writableWrap(srcWmem, true); } //used by union and above static final HllSketch writableWrap( final WritableMemory srcWmem, final boolean checkRebuild) { Objects.requireNonNull(srcWmem, "Source Memory must not be null"); checkBounds(0, 8, srcWmem.getCapacity()); //need min 8 bytes if (extractCompactFlag(srcWmem)) { throw new SketchesArgumentException( "Cannot perform a writableWrap of a writable sketch image that is in compact form. " + "Compact sketches are by definition immutable."); } final int lgConfigK = extractLgK(srcWmem); final TgtHllType tgtHllType = extractTgtHllType(srcWmem); final long minBytes = getMaxUpdatableSerializationBytes(lgConfigK, tgtHllType); final long capBytes = srcWmem.getCapacity(); HllUtil.checkMemSize(minBytes, capBytes); final CurMode curMode = checkPreamble(srcWmem); final HllSketch directSketch; if (curMode == CurMode.HLL) { if (tgtHllType == TgtHllType.HLL_4) { directSketch = new HllSketch(new DirectHll4Array(lgConfigK, srcWmem)); } else if (tgtHllType == TgtHllType.HLL_6) { directSketch = new HllSketch(new DirectHll6Array(lgConfigK, srcWmem)); } else { //Hll_8 directSketch = new HllSketch(new DirectHll8Array(lgConfigK, srcWmem)); if (checkRebuild) { //union only uses HLL_8, we allow non-finalized from a union call. Union.checkRebuildCurMinNumKxQ(directSketch); } } } else if (curMode == CurMode.LIST) { directSketch = new HllSketch(new DirectCouponList(lgConfigK, tgtHllType, curMode, srcWmem)); } else { //SET directSketch = new HllSketch(new DirectCouponHashSet(lgConfigK, tgtHllType, srcWmem)); } return directSketch; } /** * Wraps the given read-only Memory that must be a image of a valid sketch, * which may be in compact or updatable form, and should have data. Any attempt to update the * given source Memory will throw an exception. * @param srcMem a read-only image of a valid source sketch. * @return an HllSketch, where the read-only data of the sketch is in the given srcMem. * */ public static final HllSketch wrap(final Memory srcMem) { Objects.requireNonNull(srcMem, "Source Memory must not be null"); checkBounds(0, 8, srcMem.getCapacity()); //need min 8 bytes final int lgConfigK = extractLgK(srcMem); final TgtHllType tgtHllType = extractTgtHllType(srcMem); final CurMode curMode = checkPreamble(srcMem); final HllSketch directSketch; if (curMode == CurMode.HLL) { if (tgtHllType == TgtHllType.HLL_4) { directSketch = new HllSketch(new DirectHll4Array(lgConfigK, srcMem)); } else if (tgtHllType == TgtHllType.HLL_6) { directSketch = new HllSketch(new DirectHll6Array(lgConfigK, srcMem)); } else { //Hll_8 directSketch = new HllSketch(new DirectHll8Array(lgConfigK, srcMem)); //rebuild if srcMem came from a union and was not finalized, rather than throw exception. Union.checkRebuildCurMinNumKxQ(directSketch); } } else if (curMode == CurMode.LIST) { directSketch = new HllSketch(new DirectCouponList(lgConfigK, tgtHllType, curMode, srcMem)); } else { //SET directSketch = new HllSketch(new DirectCouponHashSet(lgConfigK, tgtHllType, srcMem)); } return directSketch; } /** * Return a copy of this sketch onto the Java heap. * @return a copy of this sketch onto the Java heap. */ public HllSketch copy() { return new HllSketch(this); } /** * Return a deep copy of this sketch onto the Java heap with the specified TgtHllType. * @param tgtHllType the TgtHllType enum * @return a deep copy of this sketch with the specified TgtHllType. */ public HllSketch copyAs(final TgtHllType tgtHllType) { return new HllSketch(hllSketchImpl.copyAs(tgtHllType)); } @Override public double getCompositeEstimate() { return hllSketchImpl.getCompositeEstimate(); } @Override public double getEstimate() { return hllSketchImpl.getEstimate(); } double getHipEstimate() { return hllSketchImpl.getHipEstimate(); } @Override public int getLgConfigK() { return hllSketchImpl.getLgConfigK(); } @Override public int getCompactSerializationBytes() { return hllSketchImpl.getCompactSerializationBytes(); } @Override public double getLowerBound(final int numStdDev) { return hllSketchImpl.getLowerBound(numStdDev); } /** * Returns the maximum size in bytes that this sketch can grow to given lgConfigK. * However, for the HLL_4 sketch type, this value can be exceeded in extremely rare cases. * If exceeded, it will be larger by only a few percent. * * @param lgConfigK The Log2 of K for the target HLL sketch. This value must be * between 4 and 21 inclusively. * @param tgtHllType the desired Hll type * @return the maximum size in bytes that this sketch can grow to. */ public static final int getMaxUpdatableSerializationBytes(final int lgConfigK, final TgtHllType tgtHllType) { final int arrBytes; if (tgtHllType == TgtHllType.HLL_4) { final int auxBytes = 4 << LG_AUX_ARR_INTS[lgConfigK]; arrBytes = AbstractHllArray.hll4ArrBytes(lgConfigK) + auxBytes; } else if (tgtHllType == TgtHllType.HLL_6) { arrBytes = AbstractHllArray.hll6ArrBytes(lgConfigK); } else { //HLL_8 arrBytes = AbstractHllArray.hll8ArrBytes(lgConfigK); } return HLL_BYTE_ARR_START + arrBytes; } Memory getMemory() { return hllSketchImpl.getMemory(); } @Override public TgtHllType getTgtHllType() { return hllSketchImpl.getTgtHllType(); } @Override public int getUpdatableSerializationBytes() { return hllSketchImpl.getUpdatableSerializationBytes(); } WritableMemory getWritableMemory() { return hllSketchImpl.getWritableMemory(); } @Override public double getUpperBound(final int numStdDev) { return hllSketchImpl.getUpperBound(numStdDev); } @Override public boolean isCompact() { return hllSketchImpl.isCompact(); } @Override public boolean isEmpty() { return hllSketchImpl.isEmpty(); } @Override public boolean isMemory() { return hllSketchImpl.isMemory(); } @Override public boolean isOffHeap() { return hllSketchImpl.isOffHeap(); } @Override boolean isOutOfOrder() { return hllSketchImpl.isOutOfOrder(); } @Override public boolean isSameResource(final Memory mem) { return hllSketchImpl.isSameResource(mem); } void mergeTo(final HllSketch that) { hllSketchImpl.mergeTo(that); } HllSketch putOutOfOrderFlag(final boolean oooFlag) { hllSketchImpl.putOutOfOrder(oooFlag); return this; } @Override public void reset() { hllSketchImpl = hllSketchImpl.reset(); } @Override public byte[] toCompactByteArray() { return hllSketchImpl.toCompactByteArray(); } @Override public byte[] toUpdatableByteArray() { return hllSketchImpl.toUpdatableByteArray(); } @Override public String toString(final boolean summary, final boolean detail, final boolean auxDetail, final boolean all) { final StringBuilder sb = new StringBuilder(); if (summary) { sb.append("### HLL SKETCH SUMMARY: ").append(LS); sb.append(" Log Config K : ").append(getLgConfigK()).append(LS); sb.append(" Hll Target : ").append(getTgtHllType()).append(LS); sb.append(" Current Mode : ").append(getCurMode()).append(LS); sb.append(" Memory : ").append(isMemory()).append(LS); sb.append(" LB : ").append(getLowerBound(1)).append(LS); sb.append(" Estimate : ").append(getEstimate()).append(LS); sb.append(" UB : ").append(getUpperBound(1)).append(LS); sb.append(" OutOfOrder Flag: ").append(isOutOfOrder()).append(LS); if (getCurMode() == CurMode.HLL) { final AbstractHllArray absHll = (AbstractHllArray) hllSketchImpl; sb.append(" CurMin : ").append(absHll.getCurMin()).append(LS); sb.append(" NumAtCurMin : ").append(absHll.getNumAtCurMin()).append(LS); sb.append(" HipAccum : ").append(absHll.getHipAccum()).append(LS); sb.append(" KxQ0 : ").append(absHll.getKxQ0()).append(LS); sb.append(" KxQ1 : ").append(absHll.getKxQ1()).append(LS); sb.append(" Rebuild KxQ Flg: ").append(absHll.isRebuildCurMinNumKxQFlag()).append(LS); } else { sb.append(" Coupon Count : ") .append(((AbstractCoupons)hllSketchImpl).getCouponCount()).append(LS); } } if (detail) { sb.append("### HLL SKETCH DATA DETAIL: ").append(LS); final PairIterator pitr = iterator(); sb.append(pitr.getHeader()).append(LS); if (all) { while (pitr.nextAll()) { sb.append(pitr.getString()).append(LS); } } else { while (pitr.nextValid()) { sb.append(pitr.getString()).append(LS); } } } if (auxDetail) { if ((getCurMode() == CurMode.HLL) && (getTgtHllType() == TgtHllType.HLL_4)) { final AbstractHllArray absHll = (AbstractHllArray) hllSketchImpl; final PairIterator auxItr = absHll.getAuxIterator(); if (auxItr != null) { sb.append("### HLL SKETCH AUX DETAIL: ").append(LS); sb.append(auxItr.getHeader()).append(LS); if (all) { while (auxItr.nextAll()) { sb.append(auxItr.getString()).append(LS); } } else { while (auxItr.nextValid()) { sb.append(auxItr.getString()).append(LS); } } } } } return sb.toString(); } /** * Returns a human readable string of the preamble of a byte array image of an HllSketch. * @param byteArr the given byte array * @return a human readable string of the preamble of a byte array image of an HllSketch. */ public static String toString(final byte[] byteArr) { return PreambleUtil.toString(byteArr); } /** * Returns a human readable string of the preamble of a Memory image of an HllSketch. * @param mem the given Memory object * @return a human readable string of the preamble of a Memory image of an HllSketch. */ public static String toString(final Memory mem) { return PreambleUtil.toString(mem); } //restricted methods /** * Returns a PairIterator over the key, value pairs of the HLL array. * @return a PairIterator over the key, value pairs of the HLL array. */ PairIterator iterator() { return hllSketchImpl.iterator(); } @Override CurMode getCurMode() { return hllSketchImpl.getCurMode(); } @Override void couponUpdate(final int coupon) { if ((coupon >>> KEY_BITS_26 ) == EMPTY) { return; } hllSketchImpl = hllSketchImpl.couponUpdate(coupon); } }
2,515
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/CouponMapping.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; /** * @author Lee Rhodes * @author Kevin Lang * */ final class CouponMapping { //Computed for Coupon lgK = 26 ONLY. Designed for the cubic interpolator function. static final double[] xArr = new double[] { 0.0, 1.0, 20.0, 400.0, 8000.0, 160000.0, 300000.0, 600000.0, 900000.0, 1200000.0, 1500000.0, 1800000.0, 2100000.0, 2400000.0, 2700000.0, 3000000.0, 3300000.0, 3600000.0, 3900000.0, 4200000.0, 4500000.0, 4800000.0, 5100000.0, 5400000.0, 5700000.0, 6000000.0, 6300000.0, 6600000.0, 6900000.0, 7200000.0, 7500000.0, 7800000.0, 8100000.0, 8400000.0, 8700000.0, 9000000.0, 9300000.0, 9600000.0, 9900000.0, 10200000.0 }; // CHECKSTYLE:OFF LineLength //Computed for Coupon lgK = 26 ONLY. Designed for the cubic interpolator function. static final double[] yArr = new double[] { 0.0000000000000000, 1.0000000000000000, 20.0000009437402611, 400.0003963713384110, 8000.1589294602090376, 160063.6067763759638183, 300223.7071597663452849, 600895.5933856170158833, 902016.8065120954997838, 1203588.4983199508860707, 1505611.8245524743106216, 1808087.9449319066479802, 2111018.0231759352609515, 2414403.2270142501220107, 2718244.7282051891088486, 3022543.7025524540804327, 3327301.3299219091422856, 3632518.7942584538832307, 3938197.2836029687896371, 4244337.9901093561202288, 4550942.1100616492331028, 4858010.8438911894336343, 5165545.3961938973516226, 5473546.9757476449012756, 5782016.7955296505242586, 6090956.0727340159937739, 6400366.0287892958149314, 6710247.8893762007355690, 7020602.8844453142955899, 7331432.2482349723577499, 7642737.2192891482263803, 7954519.0404754765331745, 8266778.9590033423155546, 8579518.2264420464634895, 8892738.0987390466034412, 9206439.8362383283674717, 9520624.7036988288164139, 9835293.9703129194676876, 10150448.9097250290215015, 10466090.8000503256917000 }; // CHECKSTYLE:ON LineLength }
2,516
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/Hll6Array.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.common.ByteArrayUtil.getShortLE; import static org.apache.datasketches.common.ByteArrayUtil.putShortLE; import static org.apache.datasketches.hll.HllUtil.KEY_BITS_26; import static org.apache.datasketches.hll.HllUtil.VAL_MASK_6; import static org.apache.datasketches.hll.PreambleUtil.extractLgK; import org.apache.datasketches.common.SketchesStateException; import org.apache.datasketches.memory.Memory; /** * Uses 6 bits per slot in a packed byte array. * @author Lee Rhodes */ class Hll6Array extends HllArray { /** * Standard constructor for new instance * @param lgConfigK the configured Lg K */ Hll6Array(final int lgConfigK) { super(lgConfigK, TgtHllType.HLL_6); hllByteArr = new byte[hll6ArrBytes(lgConfigK)]; } /** * Copy constructor * @param that another Hll6Array */ Hll6Array(final Hll6Array that) { super(that); } static final Hll6Array heapify(final Memory mem) { final int lgConfigK = extractLgK(mem); final Hll6Array hll6Array = new Hll6Array(lgConfigK); HllArray.extractCommonHll(mem, hll6Array); return hll6Array; } @Override Hll6Array copy() { return new Hll6Array(this); } @Override HllSketchImpl couponUpdate(final int coupon) { final int newValue = coupon >>> KEY_BITS_26; final int configKmask = (1 << lgConfigK) - 1; final int slotNo = coupon & configKmask; updateSlotWithKxQ(slotNo, newValue); return this; } @Override int getNibble(final int slotNo) { throw new SketchesStateException("Improper access."); } @Override final int getSlotValue(final int slotNo) { return get6Bit(hllByteArr, 0, slotNo); } @Override PairIterator iterator() { return new HeapHll6Iterator(1 << lgConfigK); } @Override void putNibble(final int slotNo, final int nibValue) { throw new SketchesStateException("Improper access."); } @Override //Would be used by Union, but not used because the gadget is always HLL8 type final void updateSlotNoKxQ(final int slotNo, final int newValue) { throw new SketchesStateException("Improper access."); } @Override //Used by this couponUpdate() //updates HipAccum, CurMin, NumAtCurMin, KxQs and checks newValue > oldValue final void updateSlotWithKxQ(final int slotNo, final int newValue) { final int oldValue = getSlotValue(slotNo); if (newValue > oldValue) { put6Bit(hllByteArr, 0, slotNo, newValue); hipAndKxQIncrementalUpdate(this, oldValue, newValue); if (oldValue == 0) { numAtCurMin--; //interpret numAtCurMin as num Zeros assert getNumAtCurMin() >= 0; } } } //on-heap private static final void put6Bit(final byte[] arr, final int offsetBytes, final int slotNo, final int newValue) { final int startBit = slotNo * 6; final int shift = startBit & 0X7; final int byteIdx = (startBit >>> 3) + offsetBytes; final int valShifted = (newValue & 0X3F) << shift; final int curMasked = getShortLE(arr, byteIdx) & (~(VAL_MASK_6 << shift)); final short insert = (short) (curMasked | valShifted); putShortLE(arr, byteIdx, insert); } //on-heap private static final int get6Bit(final byte[] arr, final int offsetBytes, final int slotNo) { final int startBit = slotNo * 6; final int shift = startBit & 0X7; final int byteIdx = (startBit >>> 3) + offsetBytes; return (byte) ((getShortLE(arr, byteIdx) >>> shift) & 0X3F); } //ITERATOR private final class HeapHll6Iterator extends HllPairIterator { int bitOffset; HeapHll6Iterator(final int lengthPairs) { super(lengthPairs); bitOffset = - 6; } @Override int value() { bitOffset += 6; final int tmp = getShortLE(hllByteArr, bitOffset / 8); final int shift = (bitOffset % 8) & 0X7; return (tmp >>> shift) & VAL_MASK_6; } } }
2,517
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/IntMemoryPairIterator.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.HllUtil.EMPTY; import org.apache.datasketches.memory.Memory; /** * Iterates within a given Memory extracting integer pairs. * * @author Lee Rhodes */ class IntMemoryPairIterator extends PairIterator { private final Memory mem; private final long offsetBytes; private final int arrLen; private final int slotMask; private int index; private int pair; //Used by DirectAuxHashMap, DirectCouponList IntMemoryPairIterator( final Memory mem, final long offsetBytes, final int arrayLength, final int lgConfigK) { this.mem = mem; this.offsetBytes = offsetBytes; this.arrLen = arrayLength; slotMask = (1 << lgConfigK) - 1; index = -1; } IntMemoryPairIterator(final byte[] byteArr, final long offsetBytes, final int lengthPairs, final int lgConfigK) { this(Memory.wrap(byteArr), offsetBytes, lengthPairs, lgConfigK); } @Override public int getIndex() { return index; } @Override public int getKey() { return HllUtil.getPairLow26(pair); } @Override public int getPair() { return pair; } @Override public int getSlot() { return getKey() & slotMask; } @Override public int getValue() { return HllUtil.getPairValue(pair); } @Override public boolean nextAll() { if (++index < arrLen) { pair = pair(); return true; } return false; } @Override public boolean nextValid() { while (++index < arrLen) { final int pair = pair(); if (pair != EMPTY) { this.pair = pair; return true; } } return false; } int pair() { return mem.getInt(offsetBytes + (index << 2)); } }
2,518
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/HeapAuxHashMap.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.HllUtil.EMPTY; import static org.apache.datasketches.hll.HllUtil.RESIZE_DENOM; import static org.apache.datasketches.hll.HllUtil.RESIZE_NUMER; import static org.apache.datasketches.hll.PreambleUtil.extractInt; import static org.apache.datasketches.hll.PreambleUtil.extractLgArr; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.common.SketchesStateException; import org.apache.datasketches.memory.Memory; /** * @author Lee Rhodes * @author Kevin Lang */ class HeapAuxHashMap implements AuxHashMap { private final int lgConfigK; //required for #slot bits private int lgAuxArrInts; private int auxCount; private int[] auxIntArr; //used by Hll4Array /** * Standard constructor * @param lgAuxArrInts the log size of the aux integer array * @param lgConfigK must be 7 to 21 */ HeapAuxHashMap(final int lgAuxArrInts, final int lgConfigK) { this.lgConfigK = lgConfigK; this.lgAuxArrInts = lgAuxArrInts; auxIntArr = new int[1 << lgAuxArrInts]; } /** * Copy constructor * @param that another AuxHashMap */ HeapAuxHashMap(final HeapAuxHashMap that) { lgConfigK = that.lgConfigK; lgAuxArrInts = that.lgAuxArrInts; auxCount = that.auxCount; auxIntArr = that.auxIntArr.clone(); } static final HeapAuxHashMap heapify(final Memory mem, final long offset, final int lgConfigK, final int auxCount, final boolean srcCompact) { final int lgAuxArrInts; final HeapAuxHashMap auxMap; if (srcCompact) { //early versions did not use LgArr byte field lgAuxArrInts = PreambleUtil.computeLgArr(mem, auxCount, lgConfigK); } else { //updatable lgAuxArrInts = extractLgArr(mem); } auxMap = new HeapAuxHashMap(lgAuxArrInts, lgConfigK); final int configKmask = (1 << lgConfigK) - 1; if (srcCompact) { for (int i = 0; i < auxCount; i++) { final int pair = extractInt(mem, offset + (i << 2)); final int slotNo = HllUtil.getPairLow26(pair) & configKmask; final int value = HllUtil.getPairValue(pair); auxMap.mustAdd(slotNo, value); //increments count } } else { //updatable final int auxArrInts = 1 << lgAuxArrInts; for (int i = 0; i < auxArrInts; i++) { final int pair = extractInt(mem, offset + (i << 2)); if (pair == EMPTY) { continue; } final int slotNo = HllUtil.getPairLow26(pair) & configKmask; final int value = HllUtil.getPairValue(pair); auxMap.mustAdd(slotNo, value); //increments count } } return auxMap; } @Override public HeapAuxHashMap copy() { return new HeapAuxHashMap(this); } @Override public int getAuxCount() { return auxCount; } @Override public int[] getAuxIntArr() { return auxIntArr; } @Override public int getCompactSizeBytes() { return auxCount << 2; } @Override public PairIterator getIterator() { return new IntArrayPairIterator(auxIntArr, lgConfigK); } @Override public int getLgAuxArrInts() { return lgAuxArrInts; } @Override public int getUpdatableSizeBytes() { return 4 << lgAuxArrInts; } @Override public boolean isMemory() { return false; } @Override public boolean isOffHeap() { return false; } //In C: two-registers.c Line 300. @Override public void mustAdd(final int slotNo, final int value) { final int index = find(auxIntArr, lgAuxArrInts, lgConfigK, slotNo); final int pair = HllUtil.pair(slotNo, value); if (index >= 0) { final String pairStr = HllUtil.pairString(pair); throw new SketchesStateException("Found a slotNo that should not be there: " + pairStr); } //Found empty entry auxIntArr[~index] = pair; auxCount++; checkGrow(); } //In C: two-registers.c Line 205 @Override public int mustFindValueFor(final int slotNo) { final int index = find(auxIntArr, lgAuxArrInts, lgConfigK, slotNo); if (index >= 0) { return HllUtil.getPairValue(auxIntArr[index]); } throw new SketchesStateException("SlotNo not found: " + slotNo); } //In C: two-registers.c Line 321. @Override public void mustReplace(final int slotNo, final int value) { final int idx = find(auxIntArr, lgAuxArrInts, lgConfigK, slotNo); if (idx >= 0) { auxIntArr[idx] = HllUtil.pair(slotNo, value); //replace return; } final String pairStr = HllUtil.pairString(HllUtil.pair(slotNo, value)); throw new SketchesStateException("Pair not found: " + pairStr); } //Searches the Aux arr hash table for an empty or a matching slotNo depending on the context. //If entire entry is empty, returns one's complement of index = found empty. //If entry contains given slotNo, returns its index = found slotNo. //Continues searching. //If the probe comes back to original index, throws an exception. private static final int find(final int[] auxArr, final int lgAuxArrInts, final int lgConfigK, final int slotNo) { assert lgAuxArrInts < lgConfigK; final int auxArrMask = (1 << lgAuxArrInts) - 1; final int configKmask = (1 << lgConfigK) - 1; int probe = slotNo & auxArrMask; final int loopIndex = probe; do { final int arrVal = auxArr[probe]; if (arrVal == EMPTY) { //Compares on entire entry return ~probe; //empty } else if (slotNo == (arrVal & configKmask)) { //Compares only on slotNo return probe; //found given slotNo, return probe = index into aux array } final int stride = (slotNo >>> lgAuxArrInts) | 1; probe = (probe + stride) & auxArrMask; } while (probe != loopIndex); throw new SketchesArgumentException("Key not found and no empty slots!"); } private void checkGrow() { if ((RESIZE_DENOM * auxCount) > (RESIZE_NUMER * auxIntArr.length)) { growAuxSpace(); //TODO if direct, ask for more memory } } private void growAuxSpace() { final int[] oldArray = auxIntArr; final int configKmask = (1 << lgConfigK) - 1; auxIntArr = new int[1 << ++lgAuxArrInts]; for (int i = 0; i < oldArray.length; i++) { final int fetched = oldArray[i]; if (fetched != EMPTY) { //find empty in new array final int idx = find(auxIntArr, lgAuxArrInts, lgConfigK, fetched & configKmask); auxIntArr[~idx] = fetched; } } } }
2,519
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/IntArrayPairIterator.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.HllUtil.EMPTY; /** * Iterates over an on-heap integer array of pairs extracting * the components of the pair at a given index. * * @author Lee Rhodes */ class IntArrayPairIterator extends PairIterator { private final int[] array; private final int arrLen; private final int slotMask; private int index; private int pair; //used by CouponList, HeapAuxHashMap IntArrayPairIterator(final int[] array, final int lgConfigK) { this.array = array; slotMask = (1 << lgConfigK) - 1; arrLen = array.length; index = - 1; } @Override public int getIndex() { return index; } @Override public int getKey() { return HllUtil.getPairLow26(pair); } @Override public int getPair() { return pair; } @Override public int getSlot() { return getKey() & slotMask; } @Override public int getValue() { return HllUtil.getPairValue(pair); } @Override public boolean nextAll() { if (++index < arrLen) { pair = array[index]; return true; } return false; } @Override public boolean nextValid() { while (++index < arrLen) { final int pair = array[index]; if (pair != EMPTY) { this.pair = pair; return true; } } return false; } }
2,520
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/DirectHll4Array.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.HllUtil.AUX_TOKEN; import static org.apache.datasketches.hll.HllUtil.KEY_BITS_26; import static org.apache.datasketches.hll.HllUtil.LG_AUX_ARR_INTS; import static org.apache.datasketches.hll.HllUtil.hiNibbleMask; import static org.apache.datasketches.hll.HllUtil.loNibbleMask; import static org.apache.datasketches.hll.HllUtil.noWriteAccess; import static org.apache.datasketches.hll.PreambleUtil.HLL_BYTE_ARR_START; import static org.apache.datasketches.hll.PreambleUtil.extractAuxCount; import static org.apache.datasketches.hll.PreambleUtil.extractCompactFlag; import static org.apache.datasketches.hll.PreambleUtil.insertAuxCount; import static org.apache.datasketches.hll.PreambleUtil.insertCompactFlag; import static org.apache.datasketches.hll.PreambleUtil.insertInt; import static org.apache.datasketches.hll.PreambleUtil.insertLgArr; import org.apache.datasketches.common.SketchesStateException; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.memory.WritableMemory; /** * @author Lee Rhodes */ final class DirectHll4Array extends DirectHllArray { //Called by HllSketch.writableWrap(), DirectCouponList.promoteListOrSetToHll DirectHll4Array(final int lgConfigK, final WritableMemory wmem) { super(lgConfigK, TgtHllType.HLL_4, wmem); if (extractAuxCount(mem) > 0) { putAuxHashMap(new DirectAuxHashMap(this, false), false); } } //Called by HllSketch.wrap(Memory) DirectHll4Array(final int lgConfigK, final Memory mem) { super(lgConfigK, TgtHllType.HLL_4, mem); final int auxCount = extractAuxCount(mem); if (auxCount > 0) { final boolean compact = extractCompactFlag(mem); final AuxHashMap auxHashMap; if (compact) { auxHashMap = HeapAuxHashMap.heapify(mem, auxStart, lgConfigK, auxCount, compact); } else { auxHashMap = new DirectAuxHashMap(this, false); //not compact } putAuxHashMap(auxHashMap, compact); } } @Override HllSketchImpl copy() { return Hll4Array.heapify(mem); } @Override HllSketchImpl couponUpdate(final int coupon) { if (wmem == null) { noWriteAccess(); } final int newValue = coupon >>> KEY_BITS_26; final int configKmask = (1 << getLgConfigK()) - 1; final int slotNo = coupon & configKmask; updateSlotWithKxQ(slotNo, newValue); return this; } @Override int getHllByteArrBytes() { return hll4ArrBytes(lgConfigK); } @Override int getNibble(final int slotNo) { final long offset = HLL_BYTE_ARR_START + (slotNo >>> 1); int theByte = mem.getByte(offset); if ((slotNo & 1) > 0) { //odd? theByte >>>= 4; } return theByte & loNibbleMask; } @Override int getSlotValue(final int slotNo) { final int nib = getNibble(slotNo); if (nib == AUX_TOKEN) { final AuxHashMap auxHashMap = getAuxHashMap(); return auxHashMap.mustFindValueFor(slotNo); //auxHashMap cannot be null here } else { return nib + getCurMin(); } } @Override int getUpdatableSerializationBytes() { final AuxHashMap auxHashMap = getAuxHashMap(); final int auxBytes; if (auxHashMap == null) { auxBytes = 4 << LG_AUX_ARR_INTS[lgConfigK]; } else { auxBytes = 4 << auxHashMap.getLgAuxArrInts(); } return HLL_BYTE_ARR_START + getHllByteArrBytes() + auxBytes; } @Override PairIterator iterator() { return new DirectHll4Iterator(1 << lgConfigK); } @Override void putNibble(final int slotNo, final int nibValue) { final long offset = HLL_BYTE_ARR_START + (slotNo >>> 1); final int oldValue = mem.getByte(offset); final byte value = ((slotNo & 1) == 0) //even? ? (byte) ((oldValue & hiNibbleMask) | (nibValue & loNibbleMask)) //set low nibble : (byte) ((oldValue & loNibbleMask) | ((nibValue << 4) & hiNibbleMask)); //set high nibble wmem.putByte(offset, value); } @Override //Would be used by Union, but not used because the gadget is always HLL8 type void updateSlotNoKxQ(final int slotNo, final int newValue) { throw new SketchesStateException("Improper access."); } @Override //Used by this couponUpdate() //updates HipAccum, CurMin, NumAtCurMin, KxQs and checks newValue > oldValue void updateSlotWithKxQ(final int slotNo, final int newValue) { Hll4Update.internalHll4Update(this, slotNo, newValue); } @Override byte[] toCompactByteArray() { final boolean srcMemIsCompact = extractCompactFlag(mem); final int totBytes = getCompactSerializationBytes(); final byte[] byteArr = new byte[totBytes]; final WritableMemory memOut = WritableMemory.writableWrap(byteArr); if (srcMemIsCompact) { //mem is already consistent with result mem.copyTo(0, memOut, 0, totBytes); return byteArr; } //everything but the aux array is consistent mem.copyTo(0, memOut, 0, auxStart); if (auxHashMap != null) { final int auxCount = auxHashMap.getAuxCount(); insertAuxCount(memOut, auxCount); insertLgArr(memOut, auxHashMap.getLgAuxArrInts()); //only used for direct HLL final PairIterator itr = auxHashMap.getIterator(); int cnt = 0; while (itr.nextValid()) { //works whether src has compact memory or not insertInt(memOut, auxStart + (cnt++ << 2), itr.getPair()); } assert cnt == auxCount; } insertCompactFlag(memOut, true); return byteArr; } @Override byte[] toUpdatableByteArray() { final boolean memIsCompact = extractCompactFlag(mem); final int totBytes = getUpdatableSerializationBytes(); final byte[] byteArr = new byte[totBytes]; final WritableMemory memOut = WritableMemory.writableWrap(byteArr); if (!memIsCompact) { //both mem and target are updatable mem.copyTo(0, memOut, 0, totBytes); return byteArr; } //mem is compact, need to handle auxArr. Easiest way: final HllSketch heapSk = HllSketch.heapify(mem); return heapSk.toUpdatableByteArray(); } //ITERATOR final class DirectHll4Iterator extends HllPairIterator { DirectHll4Iterator(final int lengthPairs) { super(lengthPairs); } @Override int value() { return getSlotValue(index); } } }
2,521
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/RelativeErrorTables.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; /** * @author Lee Rhodes * @author Kevin Lang */ final class RelativeErrorTables { /** * Return Relative Error for UB or LB for HIP or Non-HIP as a function of numStdDev. * @param upperBound true if for upper bound * @param oooFlag true if for Non-HIP * @param lgK must be between 4 and 12 inclusive * @param stdDev must be between 1 and 3 inclusive * @return Relative Error for UB or LB for HIP or Non-HIP as a function of numStdDev. */ static double getRelErr(final boolean upperBound, final boolean oooFlag, final int lgK, final int stdDev) { final int idx = ((lgK - 4) * 3) + (stdDev - 1); final int sw = (oooFlag ? 2 : 0) | (upperBound ? 1 : 0); double f = 0; switch (sw) { case 0 : { //HIP, LB f = HIP_LB[idx]; break; } case 1 : { //HIP, UB f = HIP_UB[idx]; break; } case 2 : { //NON_HIP, LB f = NON_HIP_LB[idx]; break; } case 3 : { //NON_HIP, UB f = NON_HIP_UB[idx]; break; } default: f = 0;//can't happen } return f; } //case 0 private static double[] HIP_LB = //sd 1, 2, 3 { //Q(.84134), Q(.97725), Q(.99865) respectively 0.207316195, 0.502865572, 0.882303765, //4 0.146981579, 0.335426881, 0.557052, //5 0.104026721, 0.227683872, 0.365888317, //6 0.073614601, 0.156781585, 0.245740374, //7 0.05205248, 0.108783763, 0.168030442, //8 0.036770852, 0.075727545, 0.11593785, //9 0.025990219, 0.053145536, 0.080772263, //10 0.018373987, 0.037266176, 0.056271814, //11 0.012936253, 0.02613829, 0.039387631, //12 }; //case 1 private static double[] HIP_UB = //sd 1, 2, 3 { //Q(.15866), Q(.02275), Q(.00135) respectively -0.207805347, -0.355574279, -0.475535095, //4 -0.146988328, -0.262390832, -0.360864026, //5 -0.103877775, -0.191503663, -0.269311582, //6 -0.073452978, -0.138513438, -0.198487447, //7 -0.051982806, -0.099703123, -0.144128618, //8 -0.036768609, -0.07138158, -0.104430324, //9 -0.025991325, -0.050854296, -0.0748143, //10 -0.01834533, -0.036121138, -0.05327616, //11 -0.012920332, -0.025572893, -0.037896952, //12 }; //case 2 private static double[] NON_HIP_LB = //sd 1, 2, 3 { //Q(.84134), Q(.97725), Q(.99865) respectively 0.254409839, 0.682266712, 1.304022158, //4 0.181817353, 0.443389054, 0.778776219, //5 0.129432281, 0.295782195, 0.49252279, //6 0.091640655, 0.201175925, 0.323664385, //7 0.064858051, 0.138523393, 0.218805328, //8 0.045851855, 0.095925072, 0.148635751, //9 0.032454144, 0.067009668, 0.102660669, //10 0.022921382, 0.046868565, 0.071307398, //11 0.016155679, 0.032825719, 0.049677541 //12 }; //case 3 private static double[] NON_HIP_UB = //sd 1, 2, 3 { //Q(.15866), Q(.02275), Q(.00135) respectively -0.256980172, -0.411905944, -0.52651057, //4 -0.182332109, -0.310275547, -0.412660505, //5 -0.129314228, -0.230142294, -0.315636197, //6 -0.091584836, -0.16834013, -0.236346847, //7 -0.06487411, -0.122045231, -0.174112107, //8 -0.04591465, -0.08784505, -0.126917615, //9 -0.032433119, -0.062897613, -0.091862929, //10 -0.022960633, -0.044875401, -0.065736049, //11 -0.016186662, -0.031827816, -0.046973459 //12 }; }
2,522
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/HllEstimators.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.HllUtil.MIN_LOG_K; /** * @author Lee Rhodes * @author Kevin Lang */ class HllEstimators { //HLL UPPER AND LOWER BOUNDS /* * The upper and lower bounds are not symmetric and thus are treated slightly differently. * For the lower bound, when the unique count is <= k, LB >= numNonZeros, where * numNonZeros = k - numAtCurMin AND curMin == 0. * * For HLL6 and HLL8, curMin is always 0 and numAtCurMin is initialized to k and is decremented * down for each valid update until it reaches 0, where it stays. Thus, for these two * isomorphs, when numAtCurMin = 0, means the true curMin is > 0 and the unique count must be * greater than k. * * HLL4 always maintains both curMin and numAtCurMin dynamically. Nonetheless, the rules for * the very small values <= k where curMin = 0 still apply. */ static final double hllLowerBound(final AbstractHllArray absHllArr, final int numStdDev) { final int lgConfigK = absHllArr.lgConfigK; final int configK = 1 << lgConfigK; final double numNonZeros = (absHllArr.getCurMin() == 0) ? configK - absHllArr.getNumAtCurMin() : configK; final double estimate = absHllArr.getEstimate(); final boolean oooFlag = absHllArr.isOutOfOrder(); final double relErr = BaseHllSketch.getRelErr(false, oooFlag, lgConfigK, numStdDev); return Math.max(estimate / (1.0 + relErr), numNonZeros); } static final double hllUpperBound(final AbstractHllArray absHllArr, final int numStdDev) { final int lgConfigK = absHllArr.lgConfigK; final double estimate = absHllArr.getEstimate(); final boolean oooFlag = absHllArr.isOutOfOrder(); final double relErr = BaseHllSketch.getRelErr(true, oooFlag, lgConfigK, numStdDev); return estimate / (1.0 - relErr); } //THE HLL COMPOSITE ESTIMATOR /** * This is the (non-HIP) estimator. * It is called "composite" because multiple estimators are pasted together. * @param absHllArr an instance of the AbstractHllArray class. * @return the composite estimate */ //In C: again-two-registers.c hhb_get_composite_estimate L1489 static final double hllCompositeEstimate(final AbstractHllArray absHllArr) { final int lgConfigK = absHllArr.getLgConfigK(); final double rawEst = getHllRawEstimate(lgConfigK, absHllArr.getKxQ0() + absHllArr.getKxQ1()); final double[] xArr = CompositeInterpolationXTable.xArrs[lgConfigK - MIN_LOG_K]; final double yStride = CompositeInterpolationXTable.yStrides[lgConfigK - MIN_LOG_K]; final int xArrLen = xArr.length; if (rawEst < xArr[0]) { return 0; } final int xArrLenM1 = xArrLen - 1; if (rawEst > xArr[xArrLenM1]) { final double finalY = yStride * (xArrLenM1); final double factor = finalY / xArr[xArrLenM1]; return rawEst * factor; } final double adjEst = CubicInterpolation.usingXArrAndYStride(xArr, yStride, rawEst); // We need to completely avoid the linear_counting estimator if it might have a crazy value. // Empirical evidence suggests that the threshold 3*k will keep us safe if 2^4 <= k <= 2^21. if (adjEst > (3 << lgConfigK)) { return adjEst; } //Alternate call //if ((adjEst > (3 << lgConfigK)) || ((curMin != 0) || (numAtCurMin == 0)) ) { return adjEst; } final double linEst = getHllBitMapEstimate(lgConfigK, absHllArr.getCurMin(), absHllArr.getNumAtCurMin()); // Bias is created when the value of an estimator is compared with a threshold to decide whether // to use that estimator or a different one. // We conjecture that less bias is created when the average of the two estimators // is compared with the threshold. Empirical measurements support this conjecture. final double avgEst = (adjEst + linEst) / 2.0; // The following constants comes from empirical measurements of the crossover point // between the average error of the linear estimator and the adjusted HLL estimator double crossOver = 0.64; if (lgConfigK == 4) { crossOver = 0.718; } else if (lgConfigK == 5) { crossOver = 0.672; } return (avgEst > (crossOver * (1 << lgConfigK))) ? adjEst : linEst; } /** * Estimator when N is small, roughly less than k log(k). * Refer to Wikipedia: Coupon Collector Problem * @param lgConfigK the current configured lgK of the sketch * @param curMin the current minimum value of the HLL window * @param numAtCurMin the current number of slots with the value curMin * @return the very low range estimate */ //In C: again-two-registers.c hhb_get_improved_linear_counting_estimate L1274 private static final double getHllBitMapEstimate( final int lgConfigK, final int curMin, final int numAtCurMin) { final int configK = 1 << lgConfigK; final int numUnhitBuckets = (curMin == 0) ? numAtCurMin : 0; //This will eventually go away. if (numUnhitBuckets == 0) { return configK * Math.log(configK / 0.5); } final int numHitBuckets = configK - numUnhitBuckets; return HarmonicNumbers.getBitMapEstimate(configK, numHitBuckets); } //In C: again-two-registers.c hhb_get_raw_estimate L1167 //This algorithm is from Flajolet's, et al, 2007 HLL paper, Fig 3. private static final double getHllRawEstimate(final int lgConfigK, final double kxqSum) { final int configK = 1 << lgConfigK; final double correctionFactor; if (lgConfigK == 4) { correctionFactor = 0.673; } else if (lgConfigK == 5) { correctionFactor = 0.697; } else if (lgConfigK == 6) { correctionFactor = 0.709; } else { correctionFactor = 0.7213 / (1.0 + (1.079 / configK)); } final double hyperEst = (correctionFactor * configK * configK) / kxqSum; return hyperEst; } }
2,523
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/DirectHllArray.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.PreambleUtil.CUR_MIN_COUNT_INT; import static org.apache.datasketches.hll.PreambleUtil.HIP_ACCUM_DOUBLE; import static org.apache.datasketches.hll.PreambleUtil.extractCompactFlag; import static org.apache.datasketches.hll.PreambleUtil.extractCurMin; import static org.apache.datasketches.hll.PreambleUtil.extractCurMode; import static org.apache.datasketches.hll.PreambleUtil.extractEmptyFlag; import static org.apache.datasketches.hll.PreambleUtil.extractHipAccum; import static org.apache.datasketches.hll.PreambleUtil.extractKxQ0; import static org.apache.datasketches.hll.PreambleUtil.extractKxQ1; import static org.apache.datasketches.hll.PreambleUtil.extractLgK; import static org.apache.datasketches.hll.PreambleUtil.extractNumAtCurMin; import static org.apache.datasketches.hll.PreambleUtil.extractOooFlag; import static org.apache.datasketches.hll.PreambleUtil.extractRebuildCurMinNumKxQFlag; import static org.apache.datasketches.hll.PreambleUtil.extractTgtHllType; import static org.apache.datasketches.hll.PreambleUtil.insertAuxCount; import static org.apache.datasketches.hll.PreambleUtil.insertCompactFlag; import static org.apache.datasketches.hll.PreambleUtil.insertCurMin; import static org.apache.datasketches.hll.PreambleUtil.insertEmptyFlag; import static org.apache.datasketches.hll.PreambleUtil.insertHipAccum; import static org.apache.datasketches.hll.PreambleUtil.insertKxQ0; import static org.apache.datasketches.hll.PreambleUtil.insertKxQ1; import static org.apache.datasketches.hll.PreambleUtil.insertLgArr; import static org.apache.datasketches.hll.PreambleUtil.insertNumAtCurMin; import static org.apache.datasketches.hll.PreambleUtil.insertOooFlag; import static org.apache.datasketches.hll.PreambleUtil.insertRebuildCurMinNumKxQFlag; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.memory.WritableMemory; /** * @author Lee Rhodes */ abstract class DirectHllArray extends AbstractHllArray { WritableMemory wmem; Memory mem; Object memObj; long memAdd; final boolean compact; //Memory must be already initialized and may have data DirectHllArray(final int lgConfigK, final TgtHllType tgtHllType, final WritableMemory wmem) { super(lgConfigK, tgtHllType, CurMode.HLL); this.wmem = wmem; mem = wmem; memObj = wmem.getArray(); memAdd = wmem.getCumulativeOffset(0L); compact = extractCompactFlag(mem); assert !compact; insertEmptyFlag(wmem, false); } //Memory must already be initialized and should have data DirectHllArray(final int lgConfigK, final TgtHllType tgtHllType, final Memory mem) { super(lgConfigK, tgtHllType, CurMode.HLL); wmem = null; this.mem = mem; memObj = ((WritableMemory) mem).getArray(); memAdd = mem.getCumulativeOffset(0L); compact = extractCompactFlag(mem); } //only called by DirectAuxHashMap final void updateMemory(final WritableMemory newWmem) { wmem = newWmem; mem = newWmem; memObj = wmem.getArray(); memAdd = wmem.getCumulativeOffset(0L); } @Override void addToHipAccum(final double delta) { checkReadOnly(wmem); final double hipAccum = mem.getDouble(HIP_ACCUM_DOUBLE); wmem.putDouble(HIP_ACCUM_DOUBLE, hipAccum + delta); } @Override void decNumAtCurMin() { checkReadOnly(wmem); int numAtCurMin = mem.getInt(CUR_MIN_COUNT_INT); wmem.putInt(CUR_MIN_COUNT_INT, --numAtCurMin); } @Override int getCurMin() { return extractCurMin(mem); } @Override CurMode getCurMode() { return extractCurMode(mem); } @Override double getHipAccum() { return extractHipAccum(mem); } @Override double getKxQ0() { return extractKxQ0(mem); } @Override double getKxQ1() { return extractKxQ1(mem); } @Override int getLgConfigK() { return extractLgK(mem); } @Override Memory getMemory() { return mem; } @Override AuxHashMap getNewAuxHashMap() { return new DirectAuxHashMap(this, true); } @Override int getNumAtCurMin() { return extractNumAtCurMin(mem); } @Override TgtHllType getTgtHllType() { return extractTgtHllType(mem); } @Override WritableMemory getWritableMemory() { return wmem; } @Override boolean isCompact() { return compact; } @Override boolean isEmpty() { return extractEmptyFlag(mem); } @Override boolean isMemory() { return true; } @Override boolean isOffHeap() { return mem.isDirect(); } @Override boolean isOutOfOrder() { return extractOooFlag(mem); } @Override boolean isSameResource(final Memory mem) { return this.mem.isSameResource(mem); } @Override boolean isRebuildCurMinNumKxQFlag() { return extractRebuildCurMinNumKxQFlag(mem); } @Override void putAuxHashMap(final AuxHashMap auxHashMap, final boolean compact) { if (auxHashMap instanceof HeapAuxHashMap) { if (compact) { this.auxHashMap = auxHashMap; //heap and compact } else { //heap and not compact final int[] auxArr = auxHashMap.getAuxIntArr(); wmem.putIntArray(auxStart, auxArr, 0, auxArr.length); insertLgArr(wmem, auxHashMap.getLgAuxArrInts()); insertAuxCount(wmem, auxHashMap.getAuxCount()); this.auxHashMap = new DirectAuxHashMap(this, false); } } else { //DirectAuxHashMap assert !compact; //must not be compact this.auxHashMap = auxHashMap; //In case of read-only this works. } } @Override void putCurMin(final int curMin) { checkReadOnly(wmem); insertCurMin(wmem, curMin); } @Override void putEmptyFlag(final boolean empty) { checkReadOnly(wmem); insertEmptyFlag(wmem, empty); } @Override void putHipAccum(final double hipAccum) { checkReadOnly(wmem); insertHipAccum(wmem, hipAccum); } @Override void putKxQ0(final double kxq0) { checkReadOnly(wmem); insertKxQ0(wmem, kxq0); } @Override //called very very very rarely void putKxQ1(final double kxq1) { checkReadOnly(wmem); insertKxQ1(wmem, kxq1); } @Override void putNumAtCurMin(final int numAtCurMin) { checkReadOnly(wmem); insertNumAtCurMin(wmem, numAtCurMin); } @Override //not used on the direct side void putOutOfOrder(final boolean oooFlag) { if (oooFlag) { putHipAccum(0); } checkReadOnly(wmem); insertOooFlag(wmem, oooFlag); } @Override void putRebuildCurMinNumKxQFlag(final boolean rebuild) { checkReadOnly(wmem); insertRebuildCurMinNumKxQFlag(wmem, rebuild); } @Override //used by HLL6 and HLL8, overridden by HLL4 byte[] toCompactByteArray() { return toUpdatableByteArray(); //indistinguishable for HLL6 and HLL8 } @Override //used by HLL6 and HLL8, overridden by HLL4 byte[] toUpdatableByteArray() { final int totBytes = getCompactSerializationBytes(); final byte[] byteArr = new byte[totBytes]; final WritableMemory memOut = WritableMemory.writableWrap(byteArr); mem.copyTo(0, memOut, 0, totBytes); insertCompactFlag(memOut, false); return byteArr; } @Override HllSketchImpl reset() { checkReadOnly(wmem); insertEmptyFlag(wmem, true); final int bytes = HllSketch.getMaxUpdatableSerializationBytes(lgConfigK, tgtHllType); wmem.clear(0, bytes); return DirectCouponList.newInstance(lgConfigK, tgtHllType, wmem); } private static final void checkReadOnly(final WritableMemory wmem) { if (wmem == null) { throw new SketchesArgumentException("Cannot modify a read-only sketch"); } } }
2,524
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/HllSketchImpl.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.memory.WritableMemory; /** * The Abstract HllSketch implementation * * @author Lee Rhodes */ abstract class HllSketchImpl { final int lgConfigK; final TgtHllType tgtHllType; final CurMode curMode; HllSketchImpl(final int lgConfigK, final TgtHllType tgtHllType, final CurMode curMode) { this.lgConfigK = lgConfigK; this.tgtHllType = tgtHllType; this.curMode = curMode; } /** * Returns a copy of this sketch on the Heap. * The LgConfigK, TgtHllType and CurMode are not changed. * @return Returns a copy of this sketch on the Heap. */ abstract HllSketchImpl copy(); /** * Returns a copy of this sketch on the Heap with the given TgtHllType. * The LgConfigK and CurMode are not changed. * @return Returns a copy of this sketch on the Heap with the given TgtHllType. */ abstract HllSketchImpl copyAs(TgtHllType tgtHllType); abstract HllSketchImpl couponUpdate(int coupon); abstract int getCompactSerializationBytes(); abstract double getCompositeEstimate(); CurMode getCurMode() { return curMode; } abstract double getEstimate(); abstract double getHipEstimate(); int getLgConfigK() { return lgConfigK; } abstract double getLowerBound(int numStdDev); abstract int getMemDataStart(); abstract Memory getMemory(); abstract int getPreInts(); TgtHllType getTgtHllType() { return tgtHllType; } abstract int getUpdatableSerializationBytes(); abstract double getUpperBound(int numStdDev); abstract WritableMemory getWritableMemory(); abstract boolean isCompact(); abstract boolean isEmpty(); abstract boolean isMemory(); abstract boolean isOffHeap(); abstract boolean isOutOfOrder(); abstract boolean isRebuildCurMinNumKxQFlag(); abstract boolean isSameResource(Memory mem); abstract PairIterator iterator(); abstract void mergeTo(HllSketch that); abstract void putEmptyFlag(boolean empty); abstract void putOutOfOrder(boolean oooFlag); abstract void putRebuildCurMinNumKxQFlag(boolean rebuild); abstract HllSketchImpl reset(); abstract byte[] toCompactByteArray(); abstract byte[] toUpdatableByteArray(); }
2,525
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/DirectAuxHashMap.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.HllUtil.EMPTY; import static org.apache.datasketches.hll.HllUtil.RESIZE_DENOM; import static org.apache.datasketches.hll.HllUtil.RESIZE_NUMER; import static org.apache.datasketches.hll.HllUtil.noWriteAccess; import static org.apache.datasketches.hll.PreambleUtil.extractAuxCount; import static org.apache.datasketches.hll.PreambleUtil.extractLgArr; import static org.apache.datasketches.hll.PreambleUtil.insertAuxCount; import static org.apache.datasketches.hll.PreambleUtil.insertLgArr; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.common.SketchesStateException; import org.apache.datasketches.memory.MemoryRequestServer; import org.apache.datasketches.memory.WritableMemory; /** * @author Lee Rhodes */ class DirectAuxHashMap implements AuxHashMap { private final DirectHllArray host; //hosts the WritableMemory and read-only Memory private final boolean readOnly; DirectAuxHashMap(final DirectHllArray host, final boolean initialize) { this.host = host; readOnly = (host.wmem == null); final int initLgArrInts = HllUtil.LG_AUX_ARR_INTS[host.lgConfigK]; if (initialize) { //must be writable if (readOnly) { noWriteAccess(); } insertLgArr(host.wmem, initLgArrInts); host.wmem.clear(host.auxStart, 4 << initLgArrInts); } else { if (extractLgArr(host.mem) < initLgArrInts) { if (readOnly) { throw new SketchesArgumentException( "Possible Memory image corruption, incorrect LgArr field in preamble."); } //insert the correct LgArr value final int lgArr = PreambleUtil.computeLgArr(host.wmem, host.auxHashMap.getAuxCount(), host.lgConfigK); insertLgArr(host.wmem, lgArr); } } } @Override public DirectAuxHashMap copy() { //a no-op return null; } @Override public int getAuxCount() { return extractAuxCount(host.mem); } @Override public int[] getAuxIntArr() { return null; } @Override public int getCompactSizeBytes() { return getAuxCount() << 2; } @Override public PairIterator getIterator() { return new IntMemoryPairIterator( host.mem, host.auxStart, 1 << getLgAuxArrInts(), host.lgConfigK); } @Override public int getLgAuxArrInts() { return extractLgArr(host.mem); } @Override public int getUpdatableSizeBytes() { return 4 << getLgAuxArrInts(); } @Override public boolean isMemory() { return true; } @Override public boolean isOffHeap() { return host.isOffHeap(); } @Override public void mustAdd(final int slotNo, final int value) { if (readOnly) { noWriteAccess(); } final int index = find(host, slotNo); final int pair = HllUtil.pair(slotNo, value); if (index >= 0) { final String pairStr = HllUtil.pairString(pair); throw new SketchesStateException("Found a slotNo that should not be there: " + pairStr); } //Found empty entry host.wmem.putInt(host.auxStart + (~index << 2), pair); int auxCount = extractAuxCount(host.mem); insertAuxCount(host.wmem, ++auxCount); final int lgAuxArrInts = extractLgArr(host.mem); if ((RESIZE_DENOM * auxCount) > (RESIZE_NUMER * (1 << lgAuxArrInts))) { grow(host, lgAuxArrInts); } } @Override public int mustFindValueFor(final int slotNo) { final int index = find(host, slotNo); if (index >= 0) { final int pair = host.mem.getInt(host.auxStart + (index << 2)); return HllUtil.getPairValue(pair); } throw new SketchesStateException("SlotNo not found: " + slotNo); } @Override public void mustReplace(final int slotNo, final int value) { if (readOnly) { noWriteAccess(); } final int index = find(host, slotNo); if (index >= 0) { host.wmem.putInt(host.auxStart + (index << 2), HllUtil.pair(slotNo, value)); return; } final String pairStr = HllUtil.pairString(HllUtil.pair(slotNo, value)); throw new SketchesStateException("Pair not found: " + pairStr); } //Searches the Aux arr hash table (embedded in Memory) for an empty or a matching slotNo // depending on the context. //If entire entry is empty, returns one's complement of index = found empty. //If entry contains given slotNo, returns its index = found slotNo. //Continues searching. //If the probe comes back to original index, throws an exception. private static final int find(final DirectHllArray host, final int slotNo) { final int lgAuxArrInts = extractLgArr(host.mem); assert lgAuxArrInts < host.lgConfigK : lgAuxArrInts; final int auxInts = 1 << lgAuxArrInts; final int auxArrMask = auxInts - 1; final int configKmask = (1 << host.lgConfigK) - 1; int probe = slotNo & auxArrMask; final int loopIndex = probe; do { final int arrVal = host.mem.getInt(host.auxStart + (probe << 2)); if (arrVal == EMPTY) { return ~probe; //empty } else if (slotNo == (arrVal & configKmask)) { //found given slotNo return probe; //return aux array index } final int stride = (slotNo >>> lgAuxArrInts) | 1; probe = (probe + stride) & auxArrMask; } while (probe != loopIndex); throw new SketchesArgumentException("Key not found and no empty slots!"); } private static final void grow(final DirectHllArray host, final int oldLgAuxArrInts) { if (host.wmem == null) { noWriteAccess(); } final int oldAuxArrInts = 1 << oldLgAuxArrInts; final int[] oldIntArray = new int[oldAuxArrInts]; //buffer old aux data host.wmem.getIntArray(host.auxStart, oldIntArray, 0, oldAuxArrInts); insertLgArr(host.wmem, oldLgAuxArrInts + 1); //update LgArr field final long newAuxBytes = oldAuxArrInts << 3; final long requestBytes = host.auxStart + newAuxBytes; final long oldCapBytes = host.wmem.getCapacity(); if (requestBytes > oldCapBytes) { final MemoryRequestServer svr = host.wmem.getMemoryRequestServer(); final WritableMemory newWmem = svr.request(host.wmem, requestBytes); host.wmem.copyTo(0, newWmem, 0, host.auxStart); newWmem.clear(host.auxStart, newAuxBytes); //clear space for new aux data svr.requestClose(host.wmem, newWmem); //old host.wmem is now invalid host.updateMemory(newWmem); } //rehash into larger aux array final int configKmask = (1 << host.lgConfigK) - 1; for (int i = 0; i < oldAuxArrInts; i++) { final int fetched = oldIntArray[i]; if (fetched != EMPTY) { //find empty in new array final int index = find(host, fetched & configKmask); host.wmem.putInt(host.auxStart + (~index << 2), fetched); } } } }
2,526
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/Conversions.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.HllUtil.AUX_TOKEN; import static org.apache.datasketches.hll.HllUtil.EMPTY; import static org.apache.datasketches.hll.HllUtil.LG_AUX_ARR_INTS; /** * Converters for one TgtHllType to another. The source can be heap or direct, but the result is * always on heap. These conversions only apply to sketches in HLL (dense) mode. * * @author Lee Rhodes */ class Conversions { static final Hll4Array convertToHll4(final AbstractHllArray srcAbsHllArr) { final int lgConfigK = srcAbsHllArr.getLgConfigK(); final Hll4Array hll4Array = new Hll4Array(lgConfigK); hll4Array.putOutOfOrder(srcAbsHllArr.isOutOfOrder()); //1st pass: compute starting curMin and numAtCurMin: final int pair = curMinAndNum(srcAbsHllArr); final int curMin = HllUtil.getPairValue(pair); final int numAtCurMin = HllUtil.getPairLow26(pair); //2nd pass: Must know curMin to create AuxHashMap. //Populate KxQ registers, build AuxHashMap if needed final PairIterator itr = srcAbsHllArr.iterator(); AuxHashMap auxHashMap = hll4Array.getAuxHashMap(); //may be null while (itr.nextValid()) { final int slotNo = itr.getIndex(); final int actualValue = itr.getValue(); AbstractHllArray.hipAndKxQIncrementalUpdate(hll4Array, 0, actualValue); if (actualValue >= (curMin + 15)) { hll4Array.putNibble(slotNo, AUX_TOKEN); if (auxHashMap == null) { auxHashMap = new HeapAuxHashMap(LG_AUX_ARR_INTS[lgConfigK], lgConfigK); hll4Array.putAuxHashMap(auxHashMap, false); } auxHashMap.mustAdd(slotNo, actualValue); } else { hll4Array.putNibble(slotNo, actualValue - curMin); } } hll4Array.putCurMin(curMin); hll4Array.putNumAtCurMin(numAtCurMin); hll4Array.putHipAccum(srcAbsHllArr.getHipAccum()); //intentional overwrite hll4Array.putRebuildCurMinNumKxQFlag(false); return hll4Array; } /** * This returns curMin and numAtCurMin as a pair and will be correct independent of the TgtHllType * of the input AbstractHllArray. * * <p>In general, it is always true that for HLL_6 and HLL_8, curMin is always 0, and numAtCurMin * is the number of zero slots. For these two types there is no need to track curMin nor to track * numAtCurMin once all the slots are filled. * * @param absHllArr an instance of AbstractHllArray * @return pair values representing numAtCurMin and curMin */ static final int curMinAndNum(final AbstractHllArray absHllArr) { int curMin = 64; int numAtCurMin = 0; final PairIterator itr = absHllArr.iterator(); while (itr.nextAll()) { final int v = itr.getValue(); if (v > curMin) { continue; } if (v < curMin) { curMin = v; numAtCurMin = 1; } else { numAtCurMin++; } } return HllUtil.pair(numAtCurMin, curMin); } static final Hll6Array convertToHll6(final AbstractHllArray srcAbsHllArr) { final int lgConfigK = srcAbsHllArr.lgConfigK; final Hll6Array hll6Array = new Hll6Array(lgConfigK); hll6Array.putOutOfOrder(srcAbsHllArr.isOutOfOrder()); int numZeros = 1 << lgConfigK; final PairIterator itr = srcAbsHllArr.iterator(); while (itr.nextAll()) { if (itr.getValue() != EMPTY) { numZeros--; hll6Array.couponUpdate(itr.getPair()); //couponUpdate creates KxQ registers } } hll6Array.putNumAtCurMin(numZeros); hll6Array.putHipAccum(srcAbsHllArr.getHipAccum()); //intentional overwrite hll6Array.putRebuildCurMinNumKxQFlag(false); return hll6Array; } static final Hll8Array convertToHll8(final AbstractHllArray srcAbsHllArr) { final int lgConfigK = srcAbsHllArr.lgConfigK; final Hll8Array hll8Array = new Hll8Array(lgConfigK); hll8Array.putOutOfOrder(srcAbsHllArr.isOutOfOrder()); int numZeros = 1 << lgConfigK; final PairIterator itr = srcAbsHllArr.iterator(); while (itr.nextAll()) { if (itr.getValue() != EMPTY) { numZeros--; hll8Array.couponUpdate(itr.getPair()); //creates KxQ registers } } hll8Array.putNumAtCurMin(numZeros); hll8Array.putHipAccum(srcAbsHllArr.getHipAccum()); //intentional overwrite hll8Array.putRebuildCurMinNumKxQFlag(false); return hll8Array; } }
2,527
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/CubicInterpolation.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import org.apache.datasketches.common.SketchesArgumentException; /** * @author Lee Rhodes * @author Kevin Lang */ final class CubicInterpolation { /** * Cubic interpolation using interpolation X and Y tables. * * @param xArr xArr * @param yArr yArr * @param x x * @return cubic interpolation */ //Used by AbstractCoupons //In C: again-two-registers cubic_interpolate_using_table L1377 static double usingXAndYTables(final double[] xArr, final double[] yArr, final double x) { assert (xArr.length >= 4) && (xArr.length == yArr.length); if ((x < xArr[0]) || (x > xArr[xArr.length - 1])) { throw new SketchesArgumentException("X value out of range: " + x); } if (x == xArr[xArr.length - 1]) { return yArr[yArr.length - 1]; // corner case } final int offset = findStraddle(xArr, x); //uses recursion assert (offset >= 0) && (offset <= (xArr.length - 2)); if (offset == 0) { return interpolateUsingXAndYTables(xArr, yArr, offset, x); // corner case } if (offset == (xArr.length - 2)) { return interpolateUsingXAndYTables(xArr, yArr, offset - 2, x); // corner case } return interpolateUsingXAndYTables(xArr, yArr, offset - 1, x); } // In C: again-two-registers cubic_interpolate_aux L1368 private static double interpolateUsingXAndYTables(final double[] xArr, final double[] yArr, final int offset, final double x) { return cubicInterpolate(xArr[offset], yArr[offset], xArr[offset + 1], yArr[offset + 1], xArr[offset + 2], yArr[offset + 2], xArr[offset + 3], yArr[offset + 3], x); } //Interpolate using X table and Y stride /** * Cubic interpolation using interpolation X table and Y stride. * * @param xArr The x array * @param yStride The y stride * @param x The value x * @return cubic interpolation */ //In C: again-two-registers cubic_interpolate_with_x_arr_and_y_stride L1411 // Used by HllEstimators static double usingXArrAndYStride( final double[] xArr, final double yStride, final double x) { final int xArrLen = xArr.length; final int xArrLenM1 = xArrLen - 1; final int offset; assert ((xArrLen >= 4) && (x >= xArr[0]) && (x <= xArr[xArrLenM1])); if (x == xArr[xArrLenM1]) { /* corner case */ return (yStride * (xArrLenM1)); } offset = findStraddle(xArr, x); //uses recursion final int xArrLenM2 = xArrLen - 2; assert ((offset >= 0) && (offset <= (xArrLenM2))); if (offset == 0) { /* corner case */ return (interpolateUsingXArrAndYStride(xArr, yStride, (offset - 0), x)); } else if (offset == xArrLenM2) { /* corner case */ return (interpolateUsingXArrAndYStride(xArr, yStride, (offset - 2), x)); } /* main case */ return (interpolateUsingXArrAndYStride(xArr, yStride, (offset - 1), x)); } //In C: again-two-registers cubic_interpolate_with_x_arr_and_y_stride_aux L1402 private static double interpolateUsingXArrAndYStride(final double[] xArr, final double yStride, final int offset, final double x) { return cubicInterpolate( xArr[offset + 0], yStride * (offset + 0), xArr[offset + 1], yStride * (offset + 1), xArr[offset + 2], yStride * (offset + 2), xArr[offset + 3], yStride * (offset + 3), x); } //Cubic Interpolation used by both methods // Interpolate using the cubic curve that passes through the four given points, using the // Lagrange interpolation formula. // In C: again-two-registers cubic_interpolate_aux_aux L1346 private static double cubicInterpolate(final double x0, final double y0, final double x1, final double y1, final double x2, final double y2, final double x3, final double y3, final double x) { final double l0Numer = (x - x1) * (x - x2) * (x - x3); final double l1Numer = (x - x0) * (x - x2) * (x - x3); final double l2Numer = (x - x0) * (x - x1) * (x - x3); final double l3Numer = (x - x0) * (x - x1) * (x - x2); final double l0Denom = (x0 - x1) * (x0 - x2) * (x0 - x3); final double l1Denom = (x1 - x0) * (x1 - x2) * (x1 - x3); final double l2Denom = (x2 - x0) * (x2 - x1) * (x2 - x3); final double l3Denom = (x3 - x0) * (x3 - x1) * (x3 - x2); final double term0 = (y0 * l0Numer) / l0Denom; final double term1 = (y1 * l1Numer) / l1Denom; final double term2 = (y2 * l2Numer) / l2Denom; final double term3 = (y3 * l3Numer) / l3Denom; return term0 + term1 + term2 + term3; } //In C: again-two-registers.c find_straddle L1335 private static int findStraddle(final double[] xArr, final double x) { assert ((xArr.length >= 2) && (x >= xArr[0]) && (x <= xArr[xArr.length - 1])); return (recursiveFindStraddle(xArr, 0, xArr.length - 1, x)); } //In C: again-two-registers.c find_straddle_aux L1322 private static int recursiveFindStraddle(final double[] xArr, final int left, final int right, final double x) { final int middle; assert (left < right); assert ((xArr[left] <= x) && (x < xArr[right])); /* the invariant */ if ((left + 1) == right) { return (left); } middle = left + ((right - left) / 2); if (xArr[middle] <= x) { return (recursiveFindStraddle(xArr, middle, right, x)); } else { return (recursiveFindStraddle(xArr, left, middle, x)); } } }
2,528
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/HllUtil.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static java.lang.Math.log; import static java.lang.Math.sqrt; import static org.apache.datasketches.common.Util.checkBounds; import static org.apache.datasketches.hll.PreambleUtil.HASH_SET_PREINTS; import static org.apache.datasketches.hll.PreambleUtil.HLL_PREINTS; import static org.apache.datasketches.hll.PreambleUtil.LIST_PREINTS; import static org.apache.datasketches.hll.PreambleUtil.extractCurMode; import static org.apache.datasketches.hll.PreambleUtil.extractFamilyId; import static org.apache.datasketches.hll.PreambleUtil.extractPreInts; import static org.apache.datasketches.hll.PreambleUtil.extractSerVer; import org.apache.datasketches.common.Family; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.common.SketchesReadOnlyException; import org.apache.datasketches.memory.Memory; /** * @author Lee Rhodes * @author Kevin Lang */ final class HllUtil { static final int KEY_BITS_26 = 26; static final int VAL_BITS_6 = 6; static final int KEY_MASK_26 = (1 << KEY_BITS_26) - 1; static final int VAL_MASK_6 = (1 << VAL_BITS_6) - 1; static final int EMPTY = 0; static final int MIN_LOG_K = 4; static final int MAX_LOG_K = 21; static final double HLL_HIP_RSE_FACTOR = sqrt(log(2.0)); //.8325546 static final double HLL_NON_HIP_RSE_FACTOR = sqrt((3.0 * log(2.0)) - 1.0); //1.03896 static final double COUPON_RSE_FACTOR = .409; //at transition point not the asymptote static final double COUPON_RSE = COUPON_RSE_FACTOR / (1 << 13); static final int LG_INIT_LIST_SIZE = 3; static final int LG_INIT_SET_SIZE = 5; static final int RESIZE_NUMER = 3; static final int RESIZE_DENOM = 4; static final int loNibbleMask = 0x0f; static final int hiNibbleMask = 0xf0; static final int AUX_TOKEN = 0xf; /** * Log2 table sizes for exceptions based on lgK from 0 to 26. * However, only lgK from 4 to 21 are used. */ static final int[] LG_AUX_ARR_INTS = new int[] { 0, 2, 2, 2, 2, 2, 2, 3, 3, 3, //0 - 9 4, 4, 5, 5, 6, 7, 8, 9, 10, 11, //10 - 19 12, 13, 14, 15, 16, 17, 18 //20 - 26 }; //Checks static final int checkLgK(final int lgK) { if ((lgK >= MIN_LOG_K) && (lgK <= MAX_LOG_K)) { return lgK; } throw new SketchesArgumentException( "Log K must be between 4 and 21, inclusive: " + lgK); } static void checkMemSize(final long minBytes, final long capBytes) { if (capBytes < minBytes) { throw new SketchesArgumentException( "Given WritableMemory is not large enough: " + capBytes); } } static final void checkNumStdDev(final int numStdDev) { if ((numStdDev < 1) || (numStdDev > 3)) { throw new SketchesArgumentException( "NumStdDev may not be less than 1 or greater than 3."); } } static CurMode checkPreamble(final Memory mem) { checkBounds(0, 8, mem.getCapacity()); //need min 8 bytes final int preInts = extractPreInts(mem); checkBounds(0, (long)preInts * Integer.BYTES, mem.getCapacity()); final int serVer = extractSerVer(mem); final int famId = extractFamilyId(mem); final CurMode curMode = extractCurMode(mem); if ( (famId != Family.HLL.getID()) || (serVer != 1) || ((preInts != LIST_PREINTS) && (preInts != HASH_SET_PREINTS) && (preInts != HLL_PREINTS)) || ((curMode == CurMode.LIST) && (preInts != LIST_PREINTS)) || ((curMode == CurMode.SET) && (preInts != HASH_SET_PREINTS)) || ((curMode == CurMode.HLL) && (preInts != HLL_PREINTS)) ) { throw new SketchesArgumentException("Possible Corruption, Invalid Preamble:" + PreambleUtil.toString(mem)); } return curMode; } //Exceptions static final void noWriteAccess() { throw new SketchesReadOnlyException( "This sketch is compact or does not have write access to the underlying resource."); } //Used for thrown exceptions static String pairString(final int pair) { return "SlotNo: " + getPairLow26(pair) + ", Value: " + getPairValue(pair); } //Pairs static int pair(final int slotNo, final int value) { return (value << KEY_BITS_26) | (slotNo & KEY_MASK_26); } static final int getPairLow26(final int coupon) { return coupon & KEY_MASK_26; } static final int getPairValue(final int coupon) { return coupon >>> KEY_BITS_26; } }
2,529
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/DirectCouponHashSet.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.HllUtil.EMPTY; import static org.apache.datasketches.hll.HllUtil.KEY_MASK_26; import static org.apache.datasketches.hll.HllUtil.RESIZE_DENOM; import static org.apache.datasketches.hll.HllUtil.RESIZE_NUMER; import static org.apache.datasketches.hll.HllUtil.noWriteAccess; import static org.apache.datasketches.hll.PreambleUtil.HASH_SET_INT_ARR_START; import static org.apache.datasketches.hll.PreambleUtil.HASH_SET_PREINTS; import static org.apache.datasketches.hll.PreambleUtil.LG_K_BYTE; import static org.apache.datasketches.hll.PreambleUtil.extractHashSetCount; import static org.apache.datasketches.hll.PreambleUtil.extractInt; import static org.apache.datasketches.hll.PreambleUtil.extractLgArr; import static org.apache.datasketches.hll.PreambleUtil.insertHashSetCount; import static org.apache.datasketches.hll.PreambleUtil.insertInt; import static org.apache.datasketches.hll.PreambleUtil.insertLgArr; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.common.SketchesStateException; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.memory.WritableMemory; /** * @author Lee Rhodes */ class DirectCouponHashSet extends DirectCouponList { //Constructs this sketch with data. DirectCouponHashSet(final int lgConfigK, final TgtHllType tgtHllType, final WritableMemory wmem) { super(lgConfigK, tgtHllType, CurMode.SET, wmem); assert wmem.getByte(LG_K_BYTE) > 7; } //Constructs this sketch with read-only data, may be compact. DirectCouponHashSet(final int lgConfigK, final TgtHllType tgtHllType, final Memory mem) { super(lgConfigK, tgtHllType, CurMode.SET, mem); assert mem.getByte(LG_K_BYTE) > 7; } @Override //returns on-heap Set CouponHashSet copy() { return CouponHashSet.heapifySet(mem); } @Override //returns on-heap Set CouponHashSet copyAs(final TgtHllType tgtHllType) { final CouponHashSet clist = CouponHashSet.heapifySet(mem); return new CouponHashSet(clist, tgtHllType); } @Override HllSketchImpl couponUpdate(final int coupon) { if (wmem == null) { noWriteAccess(); } //avoid array copy final int index = find(mem, getLgCouponArrInts(), coupon); if (index >= 0) { return this; //found duplicate, ignore } insertInt(wmem, HASH_SET_INT_ARR_START + (~index << 2), coupon); insertHashSetCount(wmem, getCouponCount() + 1); final boolean promote = checkGrowOrPromote(); if (!promote) { return this; } return promoteListOrSetToHll(this); } @Override int getCouponCount() { return extractHashSetCount(mem); } @Override int getMemDataStart() { return HASH_SET_INT_ARR_START; } @Override int getPreInts() { return HASH_SET_PREINTS; } private boolean checkGrowOrPromote() { int lgCouponArrInts = getLgCouponArrInts(); if ((RESIZE_DENOM * getCouponCount()) > (RESIZE_NUMER * (1 << lgCouponArrInts))) { if (lgCouponArrInts == (getLgConfigK() - 3)) { return true; // promote } //TODO if direct, ask for more memory insertLgArr(wmem, ++lgCouponArrInts); growHashSet(wmem, lgCouponArrInts); } return false; } private static final void growHashSet(final WritableMemory wmem, final int tgtLgCouponArrSize) { final int tgtArrSize = 1 << tgtLgCouponArrSize; final int[] tgtCouponIntArr = new int[tgtArrSize]; final int oldLen = 1 << extractLgArr(wmem); for (int i = 0; i < oldLen; i++) { final int fetched = extractInt(wmem, HASH_SET_INT_ARR_START + (i << 2)); if (fetched != EMPTY) { final int idx = find(tgtCouponIntArr, tgtLgCouponArrSize, fetched); if (idx < 0) { //found EMPTY tgtCouponIntArr[~idx] = fetched; //insert continue; } throw new SketchesStateException("Error: found duplicate."); } } wmem.clear(HASH_SET_INT_ARR_START, tgtArrSize << 2); wmem.putIntArray(HASH_SET_INT_ARR_START, tgtCouponIntArr, 0, tgtArrSize); } //Searches the Coupon hash table (embedded in Memory) for an empty slot // or a duplicate depending on the context. //If entire entry is empty, returns one's complement of index = found empty. //If entry equals given coupon, returns its index = found duplicate coupon //Continues searching //If the probe comes back to original index, throws an exception. private static final int find(final Memory mem, final int lgArr, final int coupon) { final int arrMask = (1 << lgArr) - 1; int probe = coupon & arrMask; final int loopIndex = probe; do { final int couponAtIndex = extractInt(mem, HASH_SET_INT_ARR_START + (probe << 2)); if (couponAtIndex == EMPTY) { return ~probe; } //empty else if (coupon == couponAtIndex) { return probe; } //duplicate final int stride = ((coupon & KEY_MASK_26) >>> lgArr) | 1; probe = (probe + stride) & arrMask; } while (probe != loopIndex); throw new SketchesArgumentException("Key not found and no empty slots!"); } }
2,530
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/BaseHllSketch.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static java.nio.charset.StandardCharsets.UTF_8; import static org.apache.datasketches.hash.MurmurHash3.hash; import static org.apache.datasketches.hll.HllUtil.HLL_HIP_RSE_FACTOR; import static org.apache.datasketches.hll.HllUtil.HLL_NON_HIP_RSE_FACTOR; import static org.apache.datasketches.hll.HllUtil.KEY_BITS_26; import static org.apache.datasketches.hll.HllUtil.KEY_MASK_26; import java.nio.ByteBuffer; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.thetacommon.ThetaUtil; /** * Although this class is package-private, it provides a single place to define and document * the common public API for both HllSketch and Union. * @author Lee Rhodes * @author Kevin Lang */ abstract class BaseHllSketch { abstract void couponUpdate(int coupon); /** * Gets the size in bytes of the current sketch when serialized using * <i>toCompactByteArray()</i>. * @return the size in bytes of the current sketch when serialized using * <i>toCompactByteArray()</i>. */ public abstract int getCompactSerializationBytes(); /** * This is less accurate than the <i>getEstimate()</i> method and is automatically used * when the sketch has gone through union operations where the more accurate HIP estimator * cannot be used. * This is made public only for error characterization software that exists in separate * packages and is not intended for normal use. * @return the composite estimate */ public abstract double getCompositeEstimate(); /** * Returns the current mode of the sketch: LIST, SET, HLL * @return the current mode of the sketch: LIST, SET, HLL */ abstract CurMode getCurMode(); /** * Return the cardinality estimate * @return the cardinality estimate */ public abstract double getEstimate(); /** * Gets the {@link TgtHllType} * @return the TgtHllType enum value */ public abstract TgtHllType getTgtHllType(); /** * Gets the <i>lgConfigK</i>. * @return the <i>lgConfigK</i>. */ public abstract int getLgConfigK(); /** * Gets the approximate lower error bound given the specified number of Standard Deviations. * * @param numStdDev This must be an integer between 1 and 3, inclusive. * <a href="{@docRoot}/resources/dictionary.html#numStdDev">See Number of Standard Deviations</a> * @return the lower bound. */ public abstract double getLowerBound(int numStdDev); /** * Returns the current serialization version. * @return the current serialization version. */ public static final int getSerializationVersion() { return PreambleUtil.SER_VER; } /** * Returns the current serialization version of the given Memory. * @param mem the given Memory containing a serialized HllSketch image. * @return the current serialization version. */ public static final int getSerializationVersion(final Memory mem) { return mem.getByte(PreambleUtil.SER_VER_BYTE) & 0XFF; } /** * Gets the current (approximate) Relative Error (RE) asymptotic values given several * parameters. This is used primarily for testing. * @param upperBound return the RE for the Upper Bound, otherwise for the Lower Bound. * @param oooFlag set true if the sketch is the result of a non qualifying union operation. * @param lgConfigK the configured value for the sketch. * @param numStdDev the given number of Standard Deviations. This must be an integer between * 1 and 3, inclusive. * <a href="{@docRoot}/resources/dictionary.html#numStdDev">Number of Standard Deviations</a> * @return the current (approximate) RelativeError */ public static double getRelErr(final boolean upperBound, final boolean oooFlag, final int lgConfigK, final int numStdDev) { HllUtil.checkLgK(lgConfigK); if (lgConfigK > 12) { final double rseFactor = oooFlag ? HLL_NON_HIP_RSE_FACTOR : HLL_HIP_RSE_FACTOR; final int configK = 1 << lgConfigK; return (numStdDev * rseFactor) / Math.sqrt(configK); } return Math.abs(RelativeErrorTables.getRelErr(upperBound, oooFlag, lgConfigK, numStdDev)); } /** * Gets the size in bytes of the current sketch when serialized using * <i>toUpdatableByteArray()</i>. * @return the size in bytes of the current sketch when serialized using * <i>toUpdatableByteArray()</i>. */ public abstract int getUpdatableSerializationBytes(); /** * Gets the approximate upper error bound given the specified number of Standard Deviations. * * @param numStdDev This must be an integer between 1 and 3, inclusive. * <a href="{@docRoot}/resources/dictionary.html#numStdDev">Number of Standard Deviations</a> * @return the upper bound. */ public abstract double getUpperBound(int numStdDev); /** * Returns true if empty * @return true if empty */ public abstract boolean isEmpty(); /** * Returns true if the backing memory of this sketch is in compact form. * @return true if the backing memory of this sketch is in compact form. */ public abstract boolean isCompact(); /** * This HLL family of sketches and operators is always estimating, even for very small values. * @return true */ public boolean isEstimationMode() { return true; } /** * Returns true if this sketch was created using Memory. * @return true if this sketch was created using Memory. */ public abstract boolean isMemory(); /** * Returns true if the backing memory for this sketch is off-heap. * @return true if the backing memory for this sketch is off-heap. */ public abstract boolean isOffHeap(); /** * Gets the Out-of-order flag. * @return true if the current estimator is the non-HIP estimator, which is slightly less * accurate than the HIP estimator. */ abstract boolean isOutOfOrder(); /** * Returns true if the given Memory refers to the same underlying resource as this sketch. * The capacities must be the same. If <i>this</i> is a region, * the region offset must also be the same. * * <p>This is only relevant for HLL_4 sketches that have been configured for off-heap * using WritableMemory or Memory. For on-heap sketches or unions this will return false. * * <p>It is rare, but possible, the the off-heap memory that has been allocated to an HLL_4 * sketch may not be large enough. If this should happen, the sketch makes a request for more * memory from the owner of the resource and then moves itself to this new location. This all * happens transparently to the user. This method provides a means for the user to * inquire of the sketch if it has, in fact, moved itself. * * @param mem the given Memory * @return true if the given Memory refers to the same underlying resource as this sketch or * union. */ public abstract boolean isSameResource(Memory mem); /** * Resets to empty, but does not change the configured values of lgConfigK and tgtHllType. */ public abstract void reset(); /** * Serializes this sketch as a byte array in compact form. The compact form is smaller in size * than the updatable form and read-only. It can be used in union operations as follows: * <pre>{@code * Union union; HllSketch sk, sk2; * int lgK = 12; * sk = new HllSketch(lgK, TgtHllType.HLL_4); //can be 4, 6, or 8 * for (int i = 0; i < (2 << lgK); i++) { sk.update(i); } * byte[] arr = HllSketch.toCompactByteArray(); * //... * union = Union.heapify(arr); //initializes the union using data from the array. * //OR, if used in an off-heap environment: * union = Union.heapify(Memory.wrap(arr)); //same as above, except from Memory object. * * //To recover an updatable heap sketch: * sk2 = HllSketch.heapify(arr); * //OR, if used in an off-heap environment: * sk2 = HllSketch.heapify(Memory.wrap(arr)); * }</pre> * * <p>The sketch "wrapping" operation skips actual deserialization thus is quite fast. However, * any attempt to update the derived HllSketch will result in a Read-only exception.</p> * * <p>Note that in some cases, based on the state of the sketch, the compact form is * indistiguishable from the updatable form. In these cases the updatable form is returned * and the compact flag bit will not be set.</p> * @return this sketch as a compact byte array. */ public abstract byte[] toCompactByteArray(); /** * Serializes this sketch as a byte array in an updatable form. The updatable form is larger than * the compact form. The use of this form is primarily in environments that support updating * sketches in off-heap memory. If the sketch is constructed using HLL_8, sketch updating and * union updating operations can actually occur in WritableMemory, which can be off-heap: * <pre>{@code * Union union; HllSketch sk; * int lgK = 12; * sk = new HllSketch(lgK, TgtHllType.HLL_8) //must be 8 * for (int i = 0; i < (2 << lgK); i++) { sk.update(i); } * byte[] arr = sk.toUpdatableByteArray(); * WritableMemory wmem = WritableMemory.wrap(arr); * //... * union = Union.writableWrap(wmem); //no deserialization! * }</pre> * @return this sketch as an updatable byte array. */ public abstract byte[] toUpdatableByteArray(); /** * Human readable summary as a string. * @return Human readable summary as a string. */ @Override public String toString() { return toString(true, false, false, false); } /** * Human readable summary with optional detail. Does not list empty entries. * @param summary if true, output the sketch summary * @param detail if true, output the internal data array * @param auxDetail if true, output the internal Aux array, if it exists. * @return human readable string with optional detail. */ public String toString(final boolean summary, final boolean detail, final boolean auxDetail) { return toString(summary, detail, auxDetail, false); } /** * Human readable summary with optional detail * @param summary if true, output the sketch summary * @param detail if true, output the internal data array * @param auxDetail if true, output the internal Aux array, if it exists. * @param all if true, outputs all entries including empty ones * @return human readable string with optional detail. */ public abstract String toString(boolean summary, boolean detail, boolean auxDetail, boolean all); /** * Present the given long as a potential unique item. * * @param datum The given long datum. */ public void update(final long datum) { final long[] data = { datum }; couponUpdate(coupon(hash(data, ThetaUtil.DEFAULT_UPDATE_SEED))); } /** * Present the given double (or float) datum as a potential unique item. * The double will be converted to a long using Double.doubleToLongBits(datum), * which normalizes all NaN values to a single NaN representation. * Plus and minus zero will be normalized to plus zero. * The special floating-point values NaN and +/- Infinity are treated as distinct. * * @param datum The given double datum. */ public void update(final double datum) { final double d = (datum == 0.0) ? 0.0 : datum; // canonicalize -0.0, 0.0 final long[] data = { Double.doubleToLongBits(d) };// canonicalize all NaN & +/- infinity forms couponUpdate(coupon(hash(data, ThetaUtil.DEFAULT_UPDATE_SEED))); } /** * Present the given String as a potential unique item. * 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: About 2X faster performance can be obtained by first converting the String to a * char[] and updating the sketch with that. This bypasses the complexity of the Java UTF_8 * encoding. This, of course, will not produce the same internal hash values as updating directly * with a String. So be consistent! Unioning two sketches, one fed with strings and the other * fed with char[] will be meaningless. * </p> * * @param datum The given String. */ public void update(final String datum) { if ((datum == null) || datum.isEmpty()) { return; } final byte[] data = datum.getBytes(UTF_8); couponUpdate(coupon(hash(data, ThetaUtil.DEFAULT_UPDATE_SEED))); } /** * Present the given byte buffer as a potential unique item. * Bytes are read from the current position of the buffer until its limit. * If the byte buffer is null or has no bytes remaining, no update attempt is made and the method returns. * * <p>This method will not modify the position, mark, limit, or byte order of the buffer.</p> * * <p>Little-endian order is preferred, but not required. This method may perform better if the provided byte * buffer is in little-endian order.</p> * * @param data The given byte buffer. */ public void update(final ByteBuffer data) { if ((data == null) || (data.remaining() == 0)) { return; } couponUpdate(coupon(hash(data, ThetaUtil.DEFAULT_UPDATE_SEED))); } /** * Present the given byte array as a potential unique item. * If the byte array is null or empty no update attempt is made and the method returns. * * @param data The given byte array. */ public void update(final byte[] data) { if ((data == null) || (data.length == 0)) { return; } couponUpdate(coupon(hash(data, ThetaUtil.DEFAULT_UPDATE_SEED))); } /** * Present the given char array as a potential unique item. * 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 <i>update(String)</i> * method but will be a little faster as it avoids the complexity of the UTF8 encoding.</p> * * @param data The given char array. */ public void update(final char[] data) { if ((data == null) || (data.length == 0)) { return; } couponUpdate(coupon(hash(data, ThetaUtil.DEFAULT_UPDATE_SEED))); } /** * Present the given integer array as a potential unique item. * If the integer array is null or empty no update attempt is made and the method returns. * * @param data The given int array. */ public void update(final int[] data) { if ((data == null) || (data.length == 0)) { return; } couponUpdate(coupon(hash(data, ThetaUtil.DEFAULT_UPDATE_SEED))); } /** * Present the given long array as a potential unique item. * If the long array is null or empty no update attempt is made and the method returns. * * @param data The given long array. */ public void update(final long[] data) { if ((data == null) || (data.length == 0)) { return; } couponUpdate(coupon(hash(data, ThetaUtil.DEFAULT_UPDATE_SEED))); } private static final int coupon(final long[] hash) { final int addr26 = (int) ((hash[0] & KEY_MASK_26)); final int lz = Long.numberOfLeadingZeros(hash[1]); final int value = ((lz > 62 ? 62 : lz) + 1); return (value << KEY_BITS_26) | addr26; } }
2,531
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/AbstractCoupons.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static java.lang.Math.max; import static org.apache.datasketches.hll.HllUtil.COUPON_RSE; import static org.apache.datasketches.hll.HllUtil.EMPTY; import static org.apache.datasketches.hll.HllUtil.KEY_MASK_26; import static org.apache.datasketches.hll.ToByteArrayImpl.toCouponByteArray; import org.apache.datasketches.common.SketchesArgumentException; /** * @author Lee Rhodes */ abstract class AbstractCoupons extends HllSketchImpl { AbstractCoupons(final int lgConfigK, final TgtHllType tgtHllType, final CurMode curMode) { super(lgConfigK, tgtHllType, curMode); } @Override double getCompositeEstimate() { return getEstimate(); } abstract int getCouponCount(); abstract int[] getCouponIntArr(); /** * This is the estimator for the Coupon List mode and Coupon Hash Set mode. * * <p>Note: This is an approximation to the true mapping from numCoupons to N, * which has a range of validity roughly from 0 to 6 million coupons.</p> * * <p>The k of the implied coupon sketch, which must not be confused with the k of the HLL * sketch. In this application k is always 2^26, which is the number of address bits of the * 32-bit coupon.</p> * @return the unique count estimate. */ @Override double getEstimate() { final int couponCount = getCouponCount(); final double est = CubicInterpolation.usingXAndYTables(CouponMapping.xArr, CouponMapping.yArr, couponCount); return max(est, couponCount); } @Override double getHipEstimate() { return getEstimate(); } abstract int getLgCouponArrInts(); @Override double getLowerBound(final int numStdDev) { HllUtil.checkNumStdDev(numStdDev); final int couponCount = getCouponCount(); final double est = CubicInterpolation.usingXAndYTables(CouponMapping.xArr, CouponMapping.yArr, couponCount); final double tmp = est / (1.0 + (numStdDev * COUPON_RSE)); return max(tmp, couponCount); } @Override double getUpperBound(final int numStdDev) { HllUtil.checkNumStdDev(numStdDev); final int couponCount = getCouponCount(); final double est = CubicInterpolation.usingXAndYTables(CouponMapping.xArr, CouponMapping.yArr, couponCount); final double tmp = est / (1.0 - (numStdDev * COUPON_RSE)); return max(tmp, couponCount); } @Override int getUpdatableSerializationBytes() { return getMemDataStart() + (4 << getLgCouponArrInts()); } @Override boolean isEmpty() { return getCouponCount() == 0; } @Override boolean isOutOfOrder() { return false; } @Override boolean isRebuildCurMinNumKxQFlag() { return false; } @Override void putEmptyFlag(final boolean empty) {} //no-op for coupons @Override void putOutOfOrder(final boolean outOfOrder) {} //no-op for coupons @Override void putRebuildCurMinNumKxQFlag(final boolean rebuild) {} //no-op for coupons @Override byte[] toCompactByteArray() { return toCouponByteArray(this, true); } @Override byte[] toUpdatableByteArray() { return toCouponByteArray(this, false); } //FIND for Heap and Direct //Searches the Coupon hash table for an empty slot or a duplicate depending on the context. //If entire entry is empty, returns one's complement of index = found empty. //If entry equals given coupon, returns its index = found duplicate coupon //Continues searching //If the probe comes back to original index, throws an exception. //Called by CouponHashSet.couponUpdate() //Called by CouponHashSet.growHashSet() //Called by DirectCouponHashSet.growHashSet() static final int find(final int[] array, final int lgArrInts, final int coupon) { final int arrMask = array.length - 1; int probe = coupon & arrMask; final int loopIndex = probe; do { final int couponAtIdx = array[probe]; if (couponAtIdx == EMPTY) { return ~probe; //empty } else if (coupon == couponAtIdx) { return probe; //duplicate } final int stride = ((coupon & KEY_MASK_26) >>> lgArrInts) | 1; probe = (probe + stride) & arrMask; } while (probe != loopIndex); throw new SketchesArgumentException("Key not found and no empty slots!"); } }
2,532
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/CompositeInterpolationXTable.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; /** * @author Lee Rhodes * @author Kevin Lang */ final class CompositeInterpolationXTable { //CHECKSTYLE.OFF: LineLength /** * 18 Values, index 0 is LgK = 4, index 17 is LgK = 21. */ static final int[] yStrides = {1, 2, 3, 5, 10, 20, 40, 80, 160, 320, 640, 1280, 2560, 5120, 10240, 20480, 40960, 81920 }; static final double xArrs[][] = { // log K = 4 { 10.767999803534, 11.237701481774, 11.722738717438, 12.223246391222, 12.739366773787, 13.271184824495, 13.818759686650, 14.382159835785, 14.961390904922, 15.556414447178, 16.167227058768, 16.793705840034, 17.435831011559, 18.093368824077, 18.766214022468, 19.454114555153, 20.156877376380, 20.874309118151, 21.606085061388, 22.351926834624, 23.111406651437, 23.884222842419, 24.669953053285, 25.468324695415, 26.278740385032, 27.100807701976, 27.934264623663, 28.778498012717, 29.633064579479, 30.497559091830, 31.371415928956, 32.254248998304, 33.145649456017, 34.045084140394, 34.952069032314, 35.866210502243, 36.787128772113, 37.714316067007, 38.647432342061, 39.586020708848, 40.529738186256, 41.478377527525, 42.431435504179, 43.388619067416, 44.349650659292, 45.314111448463, 46.281805382722, 47.252472240229, 48.225969481651, 49.202042512440, 50.180350422249, 51.160761480664, 52.143032445587, 53.127233235412, 54.112986437616, 55.100182392694, 56.088558030701, 57.078156023607, 58.068788167275, 59.060493030644, 60.053033784417, 61.046284072738, 62.040286486621, 63.034929032992, 64.030027371399, 65.025588917110, 66.021614945599, 67.018177418440, 68.015186786528, 69.012467972526, 70.010014322660, 71.007638150468, 72.005472777573, 73.003558069130, 74.001925061314, 75.000323809106, 75.998875046818, 76.997735309722, 77.996356820987, 78.995236661037, 79.994185446564, 80.993179953574, 81.991936172868, 82.991277726952, 83.990564675011, 84.990016215330, 85.989354175963, 86.988664031183, 87.988038383374, 88.987487931153, 89.987011275632, 90.986376635228, 91.985802667485, 92.985125933965, 93.984894049744, 94.984597185523, 95.984374633793, 96.983987641198, 97.983728001025, 98.983151114601, 99.982917278162, 100.982752268263, 101.982610588914, 102.982425129077, 103.982425883214, 104.982193814993, 105.982188025268, 106.982005912713, 107.981949640254, 108.981777381902, 109.981767708570, 110.981785341504, 111.981686149156, 112.981635012054, 113.981557303839, 114.981412817204, 115.981636136290, 116.981451847734, 117.981619278961, 118.981656205677, 119.981462628717, 120.981648197593, 121.981672839888, 122.981536601882, 123.981424780957, 124.981245233627, 125.981297203177, 126.981469882453, 127.981420558510, 128.981470958251, 129.981803124940, 130.981454077005, 131.981307772677, 132.981136552712, 133.981060440468, 134.980764897921, 135.980250123519, 136.979947889981, 137.979778713731, 138.979651075854, 139.979521006237, 140.979619490143, 141.979418214464, 142.979378742690, 143.979006714929, 144.978789085341, 145.978940618746, 146.978699584620, 147.978509489139, 148.978375065557, 149.978382007245, 150.978320879037, 151.978431501891, 152.978357310224, 153.978079953131, 154.977867908315, 155.977897282267, 156.977732141540, 157.977285841428, 158.977381755450, 159.977107808858, 160.976898092463, 161.976945065052, 162.976415653211, 163.976063254486, 164.976168718176, 165.976009634854, 166.975746910144, 167.975365498336, 168.975302031315, 169.975145815395, 170.975048401084, 171.974904933990, 172.974962856090, 173.974307743427, 174.974505556696, 175.974404122880, 176.974154794798, 177.973936891006, 178.973909768514, 179.973629325330, 180.973396428571, 181.973041798012, 182.972922327014, 183.972453839631, 184.972389341638, 185.971978290400, 186.971721489488, 187.971610992921, 188.971432541974, 189.970886522529, 190.970636748690, 191.970543849249, 192.970222044828, 193.969945219163, 194.969816174441, 195.969660267113, 196.969306217626, 197.969283141670, 198.968952697752, 199.968650726389, 200.968771498143, 201.969063388798, 202.968624157712, 203.968385122471, 204.968459873029, 205.968253426841, 206.968128284823, 207.968002441937, 208.967981887571, 209.967853843352, 210.967653026858, 211.967601511385, 212.967479077297, 213.967293002672, 214.967265475369, 215.967098197208, 216.966915139758, 217.966680984700, 218.966156974161, 219.965788928685, 220.965578199561, 221.965550145047, 222.965055562297, 223.965041228123, 224.965088576052, 225.964951649635, 226.964837079061, 227.964949815142, 228.964753278386, 229.964358666956, 230.964158288467, 231.964118659643, 232.964105599719, 233.964013041408, 234.963810178150, 235.963847138572, 236.963634735645, 237.963596910032, 238.963659218725, 239.963498440155, 240.963445597658, 241.963141291971, 242.962684055746, 243.962664757583, 244.962762174588, 245.962862664373, 246.962595237647, 247.962493432533, 248.962292518484, 249.962622934132, 250.962187495200, 251.962038306920, 252.961926735684, 253.961867248643, 254.961699581530, 255.961371028199 }, // log K = 5 { 22.304000309384, 23.261989079230, 24.249656493833, 25.267226901899, 26.314781734430, 27.392414068382, 28.500177845081, 29.638044883897, 30.805888087516, 32.003714838418, 33.231360682863, 34.488477823483, 35.774924122967, 37.090293268866, 38.434292452116, 39.806594197334, 41.206691028159, 42.634002763091, 44.088065403269, 45.568286128520, 47.074066947685, 48.604745105475, 50.159729872619, 51.738339282229, 53.339805618020, 54.963435280656, 56.608464119077, 58.274038434156, 59.959403900602, 61.663926660982, 63.386737477574, 65.127093833107, 66.884091606686, 68.656959698762, 70.445052178435, 72.247581860591, 74.063607663774, 75.892531705810, 77.733879994604, 79.586788509372, 81.450593478752, 83.324645303244, 85.208482496209, 87.101116685537, 89.002152917959, 90.911090999940, 92.827275073114, 94.750224301260, 96.679773715696, 98.615214511699, 100.556211548644, 102.502148428334, 104.452899214344, 106.407767504404, 108.366625832228, 110.329564692634, 112.295481742874, 114.264715056639, 116.236769512562, 118.211214593179, 120.188376370012, 122.167746134232, 124.148833002551, 126.131612799989, 128.116072530208, 130.102155735854, 132.089449244974, 134.077816832908, 136.067404409248, 138.057703527432, 140.049350564796, 142.041756984355, 144.034780624078, 146.028584459531, 148.022894428003, 150.017952482368, 152.013358245376, 154.009157360372, 156.005076955043, 158.001501688538, 159.998043891849, 161.995141096448, 163.992507770330, 165.990019741406, 167.987379363144, 169.985323168231, 171.983371903769, 173.981134136378, 175.979306888599, 177.977458741010, 179.975994045101, 181.974478508488, 183.973329438036, 185.972061903815, 187.971014174496, 189.969582278901, 191.968450510061, 193.967607022497, 195.966726931677, 197.965915341472, 199.964910588293, 201.964427852336, 203.964308576556, 205.963778587196, 207.963244179225, 209.962423156859, 211.961859295409, 213.961171237132, 215.960624048009, 217.959980906604, 219.959156277574, 221.958692298143, 223.958335010302, 225.958329061602, 227.957722024027, 229.957134954397, 231.956897929205, 233.956901572571, 235.956650770311, 237.956593290745, 239.956700470895, 241.956192761863, 243.956003327600, 245.955794901574, 247.955501305446, 249.954960233132, 251.954510187803, 253.954252376340, 255.954011943050, 257.953388008439, 259.953139524693, 261.952918466928, 263.952432910213, 265.952485387126, 267.952763908808, 269.952510701682, 271.952383356830, 273.951921008979, 275.951766755515, 277.951697578478, 279.951700548755, 281.951805228218, 283.951934023417, 285.952140722678, 287.952063941231, 289.951717207670, 291.951294241955, 293.950937111819, 295.950900506385, 297.950320403388, 299.949546547128, 301.949474819234, 303.949155377395, 305.948725010033, 307.948408553357, 309.948120512731, 311.947897212148, 313.947738181662, 315.947255247579, 317.946631669691, 319.946302193308, 321.945855881155, 323.945603079048, 325.944930145876, 327.945048538923, 329.944795565500, 331.944227346318, 333.943705422222, 335.943231918970, 337.942333069602, 339.942354997227, 341.941890952026, 343.941346276351, 345.940653575739, 347.940019001109, 349.940026817951, 351.938944534275, 353.938172407151, 355.937584534690, 357.937041224148, 359.936673631279, 361.936179855216, 363.936105840596, 365.935367434940, 367.935295315770, 369.934793960370, 371.934314961518, 373.933949277377, 375.933420587284, 377.933148188654, 379.932379363314, 381.931900192237, 383.930951130857, 385.930667541762, 387.930025971711, 389.929529183966, 391.929116798157, 393.928361940635, 395.927673611921, 397.927111110912, 399.926483516685, 401.925799510661, 403.924918492428, 405.924540208915, 407.924014467935, 409.923779779206, 411.922979548363, 413.922515268061, 415.922148908645, 417.921643066153, 419.921126587639, 421.920766647684, 423.920642824255, 425.920411492484, 427.920603679205, 429.920445437023, 431.920286509746, 433.919789058392, 435.919408587813, 437.918873200300, 439.918417416574, 441.918192491069, 443.918376503567, 445.917692006023, 447.917132642691, 449.917217997363, 451.916924115487, 453.916662151474, 455.916485288107, 457.915879851633, 459.915842501592, 461.915547354915, 463.914995141359, 465.914585759187, 467.914144928428, 469.913956069337, 471.913046082896, 473.912468624617, 475.912322983182, 477.912118100240, 479.911923664773, 481.911518944025, 483.911023761757, 485.910386539830, 487.910092345284, 489.910153786770, 491.909575245360, 493.908973823502, 495.908577061168, 497.908550221055, 499.908127020659, 501.907710605296, 503.907806972566, 505.907336571662, 507.907081330259, 509.906648846180, 511.906201574892 }, // log K = 6 { 45.375999854524, 46.820948326168, 48.298703712049, 49.809402685314, 51.353165756275, 52.929933710602, 54.539893242468, 56.183033892089, 57.859217068495, 59.568519998303, 61.310801009963, 63.085886926328, 64.893798324934, 66.734230191052, 68.607173306372, 70.512210309984, 72.449265953981, 74.418007054647, 76.417910710559, 78.448929349152, 80.510585932256, 82.602514696500, 84.724061528721, 86.875166381330, 89.055217833675, 91.263852803721, 93.500342620773, 95.764451518395, 98.055566319691, 100.373234086043, 102.716794393386, 105.085599217566, 107.479363461004, 109.897282858060, 112.338907676111, 114.803401103124, 117.290259931022, 119.799265623507, 122.329323908482, 124.879681624600, 127.450299022993, 130.040239267142, 132.648756733593, 135.275366525219, 137.919720233989, 140.580778017562, 143.258128898839, 145.951552244700, 148.660145472460, 151.383257444852, 154.120434694643, 156.871202914301, 159.634634288797, 162.410377522877, 165.198249005575, 167.997420145534, 170.807579918337, 173.628020587905, 176.458633641294, 179.298282098581, 182.147449731726, 185.005094444186, 187.870899328937, 190.744709255561, 193.625572419776, 196.513740460772, 199.408599560293, 202.309940032246, 205.217132295894, 208.129828601313, 211.047995735211, 213.970919426104, 216.899109829571, 219.832024990852, 222.769136305806, 225.710258431247, 228.655217514772, 231.603984417526, 234.555642012353, 237.510635882749, 240.468820458907, 243.429903849174, 246.393192036307, 249.359251612075, 252.327416912482, 255.297598046767, 258.269777722144, 261.244337060733, 264.219958654591, 267.197643910088, 270.177202282129, 273.157339433786, 276.139287129066, 279.122276848731, 282.107226383752, 285.092136000673, 288.078230550129, 291.065256815999, 294.053160971001, 297.042401030971, 300.031784889467, 303.022502412860, 306.013581138287, 309.005124942334, 311.996910583050, 314.989635544162, 317.982189300799, 320.975762085573, 323.969719809182, 326.963672522149, 329.958419674913, 332.953046205989, 335.948338966110, 338.943201657934, 341.938798787180, 344.935321901459, 347.931089897905, 350.927355153871, 353.923731760523, 356.920383399325, 359.916863372939, 362.914245241722, 365.911216314347, 368.908636150615, 371.905028432797, 374.901981729273, 377.899718967915, 380.897391911925, 383.894945428721, 386.892974193389, 389.890517424427, 392.889050804732, 395.886893560179, 398.884852345719, 401.883235933616, 404.881805247652, 407.880049402262, 410.878410789472, 413.876755787854, 416.875140105536, 419.873758096660, 422.872739431833, 425.871627898739, 428.870663138631, 431.869475472357, 434.868378059774, 437.867537661532, 440.866274620049, 443.865086570094, 446.864282236320, 449.863630871851, 452.862772062678, 455.861877807413, 458.860680461949, 461.859728874700, 464.858478829514, 467.857314559298, 470.856707830759, 473.855641079729, 476.854880576501, 479.853812351976, 482.852666825246, 485.851471230749, 488.850997731659, 491.850420535006, 494.849118717584, 497.848645341646, 500.848230362352, 503.847546143645, 506.847285358235, 509.846275998715, 512.845599444922, 515.845441684852, 518.844522015985, 521.843923976006, 524.843281639782, 527.842399994830, 530.841152548296, 533.840788827465, 536.840058715879, 539.839578267778, 542.839076801367, 545.838441853502, 548.837925005355, 551.837456112430, 554.837041819817, 557.836916936797, 560.836116071833, 563.834832988710, 566.834107840572, 569.832763471368, 572.831700689757, 575.832050680783, 578.831190216307, 581.831249402778, 584.830325442080, 587.828993643112, 590.827962761832, 593.827454067745, 596.825833598259, 599.824193205007, 602.822806340644, 605.822276069352, 608.821423024191, 611.820006110523, 614.819016304854, 617.818220240160, 620.817311827644, 623.816286470744, 626.815211383752, 629.815077535810, 632.813925717940, 635.813392704099, 638.812424935595, 641.811289306882, 644.809932622788, 647.808609610738, 650.807771758015, 653.806851303878, 656.805452367508, 659.804133900742, 662.803103749985, 665.801910649522, 668.800969589926, 671.798796938405, 674.798110719805, 677.797843657187, 680.797711918663, 683.796449689643, 686.794998006462, 689.794205385109, 692.792836542916, 695.792195299304, 698.790778974184, 701.790704644168, 704.790168225574, 707.789261680438, 710.788674453657, 713.787207421297, 716.786043303658, 719.784740017888, 722.783523691482, 725.782739693893, 728.781695938105, 731.780295310459, 734.778719103047, 737.778056368816, 740.777014963309, 743.776470087777, 746.774710218241, 749.773897180146, 752.772555359445, 755.771799417720, 758.770328749569, 761.768940347768, 764.768630836408, 767.767399121425 }, // log K = 7 { 91.554623, 93.970317, 96.431113, 98.937264, 101.488909, 104.086010, 106.728780, 109.417062, 112.150914, 114.930279, 117.755065, 120.625143, 123.540431, 126.500927, 129.506371, 132.556551, 135.651450, 138.790477, 141.973357, 145.200176, 148.470504, 151.783478, 155.139376, 158.537455, 161.977357, 165.458477, 168.980630, 172.543258, 176.145951, 179.788203, 183.469266, 187.188166, 190.945533, 194.739618, 198.570460, 202.437447, 206.339379, 210.276276, 214.247092, 218.251243, 222.288169, 226.357728, 230.458810, 234.590263, 238.751826, 242.942859, 247.162518, 251.410098, 255.685517, 259.987202, 264.314569, 268.667458, 273.044873, 277.445418, 281.869296, 286.316796, 290.785471, 295.276055, 299.788143, 304.319743, 308.869691, 313.438842, 318.026761, 322.632499, 327.254588, 331.892954, 336.547483, 341.217702, 345.901880, 350.601214, 355.313731, 360.038868, 364.777348, 369.526476, 374.288795, 379.061811, 383.846122, 388.640850, 393.446218, 398.260691, 403.084626, 407.916053, 412.755659, 417.604951, 422.461448, 427.324128, 432.195028, 437.072269, 441.956190, 446.846167, 451.741463, 456.641868, 461.549478, 466.461280, 471.377098, 476.297446, 481.222717, 486.151860, 491.085033, 496.022133, 500.962856, 505.906586, 510.853868, 515.802981, 520.755460, 525.711123, 530.668833, 535.629910, 540.592334, 545.557764, 550.525455, 555.494082, 560.463996, 565.436164, 570.410847, 575.385933, 580.361996, 585.339352, 590.318151, 595.300216, 600.281543, 605.264482, 610.250114, 615.236608, 620.222025, 625.208433, 630.195367, 635.183997, 640.172271, 645.162589, 650.153647, 655.144276, 660.133923, 665.123818, 670.115789, 675.109169, 680.103270, 685.097349, 690.091960, 695.086028, 700.080326, 705.075670, 710.069697, 715.065265, 720.062426, 725.058529, 730.054916, 735.050560, 740.048508, 745.044790, 750.041067, 755.037833, 760.035090, 765.033371, 770.029865, 775.025946, 780.022408, 785.021534, 790.018699, 795.016742, 800.013947, 805.012680, 810.009483, 815.007921, 820.008589, 825.007658, 830.007411, 835.006892, 840.006917, 845.006362, 850.004606, 855.003367, 860.001219, 864.999491, 869.999131, 874.998190, 879.998460, 884.997482, 889.995952, 894.995130, 899.994555, 904.993474, 909.991434, 914.990519, 919.991564, 924.992234, 929.993901, 934.992310, 939.992013, 944.993341, 949.993576, 954.992981, 959.993548, 964.993781, 969.993129, 974.994050, 979.994706, 984.994372, 989.994395, 994.995339, 999.995544, 1004.995498, 1009.996864, 1014.997829, 1019.996531, 1024.996724, 1029.997574, 1034.998591, 1039.997807, 1044.998954, 1050.000113, 1055.000633, 1060.000845, 1065.000760, 1070.000907, 1075.001248, 1080.001187, 1084.999775, 1089.999105, 1094.999647, 1100.001983, 1105.003468, 1110.004542, 1115.005170, 1120.006517, 1125.006493, 1130.006837, 1135.008369, 1140.006966, 1145.009225, 1150.008497, 1155.009589, 1160.009558, 1165.011395, 1170.009739, 1175.011131, 1180.013303, 1185.012525, 1190.014190, 1195.016399, 1200.016087, 1205.016715, 1210.018008, 1215.018108, 1220.019677, 1225.020269, 1230.019468, 1235.020515, 1240.021850, 1245.020961, 1250.019785, 1255.020253, 1260.020623, 1265.020156, 1270.020625, 1275.019028, 1280.019972 }, // log K = 8 { 183.877784, 188.720097, 193.652407, 198.675131, 203.788397, 208.992238, 214.286620, 219.671580, 225.147277, 230.713559, 236.370208, 242.117078, 247.953569, 253.880078, 259.895869, 266.000405, 272.193694, 278.474457, 284.843102, 291.298228, 297.839692, 304.466434, 311.177471, 317.972674, 324.850719, 331.811076, 338.852902, 345.974709, 353.175880, 360.455237, 367.811613, 375.244484, 382.752472, 390.334218, 397.988777, 405.714596, 413.510671, 421.376147, 429.308973, 437.308197, 445.372302, 453.500121, 461.690796, 469.942249, 478.253652, 486.623402, 495.049662, 503.532193, 512.068670, 520.658371, 529.299186, 537.990886, 546.732097, 555.520093, 564.355935, 573.235689, 582.159345, 591.126063, 600.133321, 609.181348, 618.267848, 627.392384, 636.552822, 645.748152, 654.977601, 664.239902, 673.534497, 682.859448, 692.213983, 701.598606, 711.009376, 720.446781, 729.910995, 739.397888, 748.908546, 758.443812, 768.000572, 777.577584, 787.174815, 796.791250, 806.427107, 816.079660, 825.748493, 835.435992, 845.136828, 854.854141, 864.583886, 874.328396, 884.085720, 893.855568, 903.637568, 913.431080, 923.234553, 933.049830, 942.873895, 952.707793, 962.551743, 972.402421, 982.263001, 992.131464, 1002.006941, 1011.888460, 1021.777462, 1031.671376, 1041.569600, 1051.476632, 1061.386996, 1071.302834, 1081.224251, 1091.148847, 1101.080297, 1111.013589, 1120.951962, 1130.893360, 1140.838011, 1150.786112, 1160.737100, 1170.689817, 1180.646908, 1190.604979, 1200.565150, 1210.527894, 1220.492169, 1230.460128, 1240.429764, 1250.401749, 1260.373274, 1270.349608, 1280.325544, 1290.302087, 1300.279074, 1310.257850, 1320.238948, 1330.221273, 1340.203554, 1350.186669, 1360.171345, 1370.156233, 1380.142561, 1390.129535, 1400.119177, 1410.107938, 1420.097171, 1430.087586, 1440.077831, 1450.067996, 1460.060299, 1470.050059, 1480.042995, 1490.035101, 1500.026878, 1510.021128, 1520.014262, 1530.008487, 1540.002895, 1549.996342, 1559.991075, 1569.986665, 1579.981202, 1589.976021, 1599.969512, 1609.964051, 1619.959210, 1629.957092, 1639.953516, 1649.948113, 1659.946715, 1669.945253, 1679.941736, 1689.938651, 1699.937600, 1709.936396, 1719.933608, 1729.931040, 1739.930316, 1749.927141, 1759.922600, 1769.920847, 1779.918614, 1789.915707, 1799.913000, 1809.910958, 1819.908532, 1829.906905, 1839.903410, 1849.904217, 1859.901705, 1869.898815, 1879.898869, 1889.899032, 1899.899113, 1909.896164, 1919.893199, 1929.894515, 1939.894965, 1949.893184, 1959.892164, 1969.890794, 1979.889035, 1989.888005, 1999.888538, 2009.888252, 2019.888976, 2029.890242, 2039.890655, 2049.891065, 2059.891068, 2069.889204, 2079.891120, 2089.892462, 2099.892479, 2109.891814, 2119.893049, 2129.893705, 2139.893471, 2149.894162, 2159.894213, 2169.895041, 2179.895987, 2189.894721, 2199.894028, 2209.896005, 2219.898683, 2229.898673, 2239.898012, 2249.898018, 2259.898215, 2269.899380, 2279.902590, 2289.903144, 2299.902047, 2309.904214, 2319.903812, 2329.904180, 2339.902528, 2349.903057, 2359.903089, 2369.904512, 2379.902563, 2389.905522, 2399.903653, 2409.902313, 2419.904452, 2429.905949, 2439.905059, 2449.904954, 2459.904977, 2469.905111, 2479.904049, 2489.903762, 2499.902022, 2509.904352, 2519.903272, 2529.901643, 2539.900789, 2549.903045, 2559.904334 }, // log K = 9 { 368.528954, 378.224619, 388.100156, 398.155846, 408.391912, 418.808631, 429.406377, 440.185048, 451.144613, 462.284693, 473.604932, 485.105306, 496.784844, 508.643147, 520.679581, 532.893098, 545.282616, 557.848295, 570.587645, 583.500268, 596.584345, 609.838697, 623.261582, 636.851681, 650.607666, 664.527320, 678.608336, 692.849674, 707.248088, 721.803089, 736.511331, 751.372034, 766.381798, 781.538178, 796.839808, 812.283881, 827.867995, 843.588520, 859.444318, 875.432109, 891.550599, 907.795630, 924.165943, 940.657393, 957.268601, 973.995685, 990.837235, 1007.789667, 1024.850025, 1042.016602, 1059.286426, 1076.658199, 1094.126523, 1111.689346, 1129.345589, 1147.092119, 1164.926795, 1182.844471, 1200.846880, 1218.929006, 1237.088281, 1255.322930, 1273.630393, 1292.007269, 1310.452948, 1328.963430, 1347.538768, 1366.176566, 1384.871299, 1403.626249, 1422.434094, 1441.295238, 1460.208340, 1479.169853, 1498.179265, 1517.234855, 1536.335403, 1555.477305, 1574.659180, 1593.881592, 1613.139378, 1632.433516, 1651.764587, 1671.127255, 1690.519577, 1709.942250, 1729.393153, 1748.871689, 1768.377142, 1787.907209, 1807.464755, 1827.043146, 1846.641976, 1866.263022, 1885.903553, 1905.564908, 1925.240581, 1944.936723, 1964.648221, 1984.375489, 2004.117246, 2023.875053, 2043.644075, 2063.423514, 2083.218689, 2103.026148, 2122.840475, 2142.669046, 2162.505619, 2182.351758, 2202.205963, 2222.068658, 2241.939010, 2261.814547, 2281.701803, 2301.591894, 2321.490728, 2341.393208, 2361.298412, 2381.211528, 2401.132345, 2421.057721, 2440.985510, 2460.916691, 2480.851468, 2500.792645, 2520.738446, 2540.683395, 2560.634603, 2580.586307, 2600.540530, 2620.497170, 2640.458137, 2660.421783, 2680.386186, 2700.352664, 2720.321411, 2740.291944, 2760.264068, 2780.238910, 2800.212546, 2820.185829, 2840.163952, 2860.142145, 2880.120568, 2900.102466, 2920.081981, 2940.062964, 2960.046262, 2980.029101, 3000.012591, 3019.998424, 3039.983637, 3059.969573, 3079.955927, 3099.942114, 3119.930348, 3139.919614, 3159.909130, 3179.899816, 3199.892231, 3219.885316, 3239.878917, 3259.871675, 3279.865195, 3299.856210, 3319.848871, 3339.840385, 3359.834158, 3379.830660, 3399.826361, 3419.822855, 3439.814714, 3459.813765, 3479.808673, 3499.804339, 3519.797785, 3539.791012, 3559.789885, 3579.785152, 3599.780237, 3619.775480, 3639.772724, 3659.768920, 3679.765735, 3699.766361, 3719.765048, 3739.762845, 3759.761526, 3779.757598, 3799.755719, 3819.751149, 3839.750182, 3859.749673, 3879.749344, 3899.749939, 3919.749060, 3939.748385, 3959.750520, 3979.750055, 3999.750104, 4019.749963, 4039.748453, 4059.746737, 4079.744522, 4099.744430, 4119.741804, 4139.740704, 4159.742056, 4179.740063, 4199.739155, 4219.741718, 4239.743433, 4259.743115, 4279.743265, 4299.745518, 4319.746682, 4339.750491, 4359.748549, 4379.747226, 4399.750319, 4419.750379, 4439.752339, 4459.750561, 4479.750612, 4499.747862, 4519.750693, 4539.747476, 4559.749051, 4579.750603, 4599.750089, 4619.754781, 4639.755536, 4659.758879, 4679.762145, 4699.762199, 4719.763886, 4739.765081, 4759.765681, 4779.765427, 4799.767582, 4819.769915, 4839.770107, 4859.769398, 4879.768976, 4899.768288, 4919.768786, 4939.768607, 4959.772822, 4979.772833, 4999.775808, 5019.773468, 5039.776181, 5059.770860, 5079.773006, 5099.773136, 5119.772474 }, // log K = 10 { 737.833738, 757.236338, 776.997875, 797.119465, 817.602058, 838.445349, 859.650320, 881.216437, 903.143414, 925.430957, 948.079220, 971.086231, 994.451449, 1018.173846, 1042.251928, 1066.683804, 1091.467648, 1116.601416, 1142.083017, 1167.909918, 1194.079097, 1220.588887, 1247.435099, 1274.614860, 1302.125360, 1329.962942, 1358.123177, 1386.601609, 1415.396299, 1444.501692, 1473.913256, 1503.628515, 1533.642054, 1563.948888, 1594.544067, 1625.424305, 1656.582169, 1688.016543, 1719.718553, 1751.685990, 1783.912130, 1816.393610, 1849.122123, 1882.095124, 1915.305647, 1948.748605, 1982.419843, 2016.313904, 2050.422277, 2084.742618, 2119.270601, 2153.998538, 2188.920843, 2224.034736, 2259.333960, 2294.812594, 2330.467059, 2366.291844, 2402.281356, 2438.428954, 2474.734330, 2511.187839, 2547.786319, 2584.527356, 2621.406814, 2658.418551, 2695.554270, 2732.812564, 2770.190157, 2807.682744, 2845.287138, 2882.995251, 2920.807982, 2958.719996, 2996.728771, 3034.826885, 3073.014082, 3111.287116, 3149.642093, 3188.073159, 3226.577352, 3265.156836, 3303.803738, 3342.515111, 3381.291514, 3420.130671, 3459.024315, 3497.974631, 3536.978221, 3576.034737, 3615.138610, 3654.285859, 3693.480455, 3732.715296, 3771.991792, 3811.305300, 3850.654870, 3890.041152, 3929.457167, 3968.905021, 4008.381466, 4047.886976, 4087.419414, 4126.978997, 4166.562624, 4206.166726, 4245.793284, 4285.445151, 4325.113012, 4364.802795, 4404.506274, 4444.228475, 4483.965191, 4523.719569, 4563.487708, 4603.267460, 4643.060387, 4682.865430, 4722.680718, 4762.503248, 4802.341326, 4842.185529, 4882.039088, 4921.900432, 4961.772625, 5001.649566, 5041.534380, 5081.425809, 5121.323013, 5161.224908, 5201.130560, 5241.044625, 5280.962456, 5320.882373, 5360.810007, 5400.740058, 5440.673843, 5480.606748, 5520.547394, 5560.493520, 5600.438100, 5640.383853, 5680.335386, 5720.292400, 5760.250576, 5800.212705, 5840.172984, 5880.134991, 5920.097923, 5960.060294, 6000.031177, 6040.003398, 6079.981696, 6119.954349, 6159.929074, 6199.908569, 6239.886163, 6279.863659, 6319.838261, 6359.815405, 6399.795170, 6439.777225, 6479.755885, 6519.738857, 6559.723169, 6599.708549, 6639.695186, 6679.679788, 6719.663289, 6759.651052, 6799.634129, 6839.624763, 6879.615013, 6919.605584, 6959.591310, 6999.586006, 7039.577399, 7079.565236, 7119.557222, 7159.548481, 7199.533025, 7239.519648, 7279.511781, 7319.507639, 7359.506105, 7399.497488, 7439.494944, 7479.491545, 7519.488415, 7559.480534, 7599.475646, 7639.470315, 7679.468730, 7719.460928, 7759.455834, 7799.455686, 7839.451758, 7879.446841, 7919.443884, 7959.444138, 7999.443141, 8039.446339, 8079.445285, 8119.444120, 8159.443157, 8199.446480, 8239.438893, 8279.437597, 8319.439714, 8359.441566, 8399.440754, 8439.439156, 8479.440112, 8519.442365, 8559.444795, 8599.444497, 8639.446714, 8679.449177, 8719.447620, 8759.443423, 8799.445995, 8839.446170, 8879.449622, 8919.450486, 8959.450372, 8999.449731, 9039.448449, 9079.452579, 9119.451596, 9159.454020, 9199.456651, 9239.460902, 9279.460419, 9319.461322, 9359.464611, 9399.467790, 9439.471448, 9479.468426, 9519.462982, 9559.464881, 9599.462983, 9639.461706, 9679.457284, 9719.457023, 9759.455648, 9799.458786, 9839.456980, 9879.457411, 9919.454971, 9959.451735, 9999.451916, 10039.443939, 10079.435325, 10119.432142, 10159.431313, 10199.431306, 10239.429671 }, // log K = 11 { 1476.444530, 1515.260638, 1554.794859, 1595.048761, 1636.023652, 1677.720065, 1720.138513, 1763.279158, 1807.141823, 1851.724811, 1897.027530, 1943.048153, 1989.784818, 2037.234835, 2085.395149, 2134.263580, 2183.834818, 2234.105548, 2285.071077, 2336.726064, 2389.065940, 2442.084276, 2495.776521, 2550.134177, 2605.153465, 2660.825522, 2717.142881, 2774.098631, 2831.683790, 2889.889911, 2948.711233, 3008.137464, 3068.158605, 3128.766926, 3189.951984, 3251.704129, 3314.013826, 3376.873018, 3440.270569, 3504.194851, 3568.636295, 3633.586039, 3699.031630, 3764.962925, 3831.371336, 3898.245744, 3965.574384, 4033.345701, 4101.550888, 4170.180686, 4239.221393, 4308.664143, 4378.497100, 4448.712735, 4519.296212, 4590.243535, 4661.538747, 4733.174738, 4805.142002, 4877.427081, 4950.019513, 5022.915001, 5096.098561, 5169.567185, 5243.312012, 5317.314578, 5391.572253, 5466.077564, 5540.821377, 5615.792025, 5690.985894, 5766.392174, 5842.007403, 5917.816734, 5993.815607, 6069.999957, 6146.362662, 6222.895187, 6299.592875, 6376.440778, 6453.443173, 6530.588182, 6607.874560, 6685.288536, 6762.829840, 6840.490840, 6918.272948, 6996.164556, 7074.161694, 7152.259404, 7230.451098, 7308.737035, 7387.113244, 7465.575350, 7544.120230, 7622.733447, 7701.427750, 7780.188044, 7859.016102, 7937.904850, 8016.853624, 8095.860379, 8174.919884, 8254.034842, 8333.197794, 8412.405307, 8491.657429, 8570.946804, 8650.278289, 8729.648657, 8809.056098, 8888.496764, 8967.966663, 9047.466051, 9126.993636, 9206.547289, 9286.127523, 9365.733732, 9445.361201, 9525.008810, 9604.680107, 9684.363532, 9764.066435, 9843.787987, 9923.523567, 10003.272886, 10083.037524, 10162.811803, 10242.597755, 10322.402415, 10402.216522, 10482.042584, 10561.877077, 10641.722187, 10721.570740, 10801.435848, 10881.301249, 10961.178474, 11041.059765, 11120.951444, 11200.845806, 11280.746075, 11360.652145, 11440.567398, 11520.476709, 11600.395391, 11680.316855, 11760.241900, 11840.170134, 11920.103548, 12000.038206, 12079.975425, 12159.912459, 12239.857191, 12319.802744, 12399.754385, 12479.707055, 12559.658981, 12639.614004, 12719.566502, 12799.526877, 12879.488833, 12959.451231, 13039.416480, 13119.388158, 13199.360789, 13279.334923, 13359.298927, 13439.261641, 13519.235871, 13599.204919, 13679.181082, 13759.157391, 13839.137425, 13919.112545, 13999.096522, 14079.082789, 14159.060487, 14239.040581, 14319.025236, 14399.006995, 14478.994078, 14558.980576, 14638.962774, 14718.952192, 14798.938522, 14878.931501, 14958.919062, 15038.907891, 15118.897247, 15198.889129, 15278.881057, 15358.873272, 15438.867627, 15518.861931, 15598.858206, 15678.850014, 15758.849382, 15838.847872, 15918.837658, 15998.837920, 16078.831305, 16158.827345, 16238.815546, 16318.817302, 16398.814389, 16478.815820, 16558.811634, 16638.808126, 16718.807170, 16798.805652, 16878.813389, 16958.806543, 17038.807885, 17118.813146, 17198.813752, 17278.817574, 17358.820511, 17438.819443, 17518.814262, 17598.815511, 17678.814160, 17758.816726, 17838.817584, 17918.814197, 17998.821276, 18078.822342, 18158.828474, 18238.822064, 18318.826732, 18398.827758, 18478.827319, 18558.834153, 18638.835651, 18718.839105, 18798.840903, 18878.836463, 18958.836015, 19038.836351, 19118.832583, 19198.829856, 19278.822953, 19358.824866, 19438.823697, 19518.826538, 19598.826954, 19678.831393, 19758.822279, 19838.818757, 19918.808247, 19998.804516, 20078.809201, 20158.808900, 20238.806398, 20318.798516, 20398.802224, 20478.803179 }, // log K = 12 { 2953.666722, 3031.310137, 3110.389526, 3190.908699, 3272.865410, 3356.267166, 3441.114376, 3527.404284, 3615.136846, 3704.310976, 3794.924283, 3886.973846, 3980.451903, 4075.355844, 4171.681168, 4269.423939, 4368.569738, 4469.111760, 4571.043603, 4674.356370, 4779.038117, 4885.076002, 4992.460659, 5101.176875, 5211.214098, 5322.553631, 5435.184633, 5549.088369, 5664.258175, 5780.673987, 5898.312682, 6017.158593, 6137.195815, 6258.397951, 6380.761679, 6504.261630, 6628.870413, 6754.585867, 6881.374325, 7009.206926, 7138.080376, 7267.979175, 7398.866805, 7530.722851, 7663.531580, 7797.269872, 7931.907340, 8067.451667, 8203.855692, 8341.091306, 8479.155031, 8618.026003, 8757.682272, 8898.095612, 9039.253787, 9181.124777, 9323.694329, 9466.944202, 9610.861899, 9755.403639, 9900.578346, 10046.356477, 10192.718770, 10339.635914, 10487.081762, 10635.066232, 10783.560130, 10932.549371, 11082.024055, 11231.949071, 11382.332885, 11533.122831, 11684.327555, 11835.935533, 11987.926999, 12140.281619, 12292.994375, 12446.046449, 12599.411083, 12753.088314, 12907.079745, 13061.355923, 13215.895621, 13370.722085, 13525.795736, 13681.095971, 13836.645692, 13992.405906, 14148.380716, 14304.566860, 14460.959382, 14617.539754, 14774.296125, 14931.214508, 15088.271748, 15245.493870, 15402.867309, 15560.380994, 15718.035423, 15875.801492, 16033.687891, 16191.701231, 16349.808763, 16508.026001, 16666.336068, 16824.745049, 16983.259302, 17141.848044, 17300.504517, 17459.240272, 17618.060296, 17776.914688, 17935.848010, 18094.852645, 18253.914288, 18413.023567, 18572.168358, 18731.376016, 18890.631505, 19049.948196, 19209.279260, 19368.638871, 19528.043860, 19687.498901, 19846.941765, 20006.443084, 20165.973535, 20325.525957, 20485.109520, 20644.729917, 20804.363876, 20964.021778, 21123.698687, 21283.379344, 21443.080536, 21602.821439, 21762.568135, 21922.305338, 22082.065878, 22241.856145, 22401.629436, 22561.435266, 22721.262644, 22881.080478, 23040.916036, 23200.734053, 23360.583255, 23520.433188, 23680.275065, 23840.132678, 24000.012847, 24159.886135, 24319.788338, 24479.654024, 24639.540316, 24799.466449, 24959.369335, 25119.276072, 25279.186502, 25439.091982, 25599.020083, 25758.930671, 25918.847522, 26078.773060, 26238.704021, 26398.647566, 26558.603366, 26718.539855, 26878.464425, 27038.402892, 27198.344989, 27358.270906, 27518.203448, 27678.169545, 27838.133625, 27998.111630, 28158.064264, 28318.032491, 28477.994153, 28637.950838, 28797.910362, 28957.884994, 29117.861910, 29277.814384, 29437.774223, 29597.762256, 29757.729819, 29917.688497, 30077.675371, 30237.667941, 30397.639431, 30557.644527, 30717.622991, 30877.622641, 31037.598941, 31197.601527, 31357.599118, 31517.585221, 31677.569983, 31837.556138, 31997.527880, 32157.500622, 32317.491709, 32477.504936, 32637.501123, 32797.516330, 32957.547336, 33117.541765, 33277.561207, 33437.554332, 33597.564988, 33757.565321, 33917.561180, 34077.580598, 34237.597132, 34397.598246, 34557.617536, 34717.623318, 34877.649722, 35037.645029, 35197.681143, 35357.700478, 35517.701804, 35677.734383, 35837.736494, 35997.737103, 36157.732045, 36317.741539, 36477.735602, 36637.761491, 36797.742508, 36957.734137, 37117.742952, 37277.789218, 37437.793798, 37597.779066, 37757.796449, 37917.794116, 38077.804399, 38237.797507, 38397.788049, 38557.768583, 38717.768279, 38877.752553, 39037.747349, 39197.737646, 39357.774395, 39517.752643, 39677.753347, 39837.740038, 39997.719486, 40157.709578, 40317.708657, 40477.719261, 40637.726978, 40797.711139, 40957.707784 }, // log K = 13 { 5908.111420, 6063.410343, 6221.578483, 6382.623803, 6546.553966, 6713.369171, 6883.070010, 7055.656719, 7231.129758, 7409.486672, 7590.720089, 7774.822139, 7961.786850, 8151.601206, 8344.261218, 8539.750086, 8738.048786, 8939.141702, 9143.011205, 9349.645220, 9559.003557, 9771.078383, 9985.846791, 10203.277103, 10423.349219, 10646.031323, 10871.293001, 11099.111735, 11329.434411, 11562.255804, 11797.522109, 12035.204106, 12275.263649, 12517.670237, 12762.388671, 13009.375491, 13258.587519, 13510.006825, 13763.564399, 14019.227089, 14276.975295, 14536.745792, 14798.511796, 15062.215057, 15327.806593, 15595.257499, 15864.529774, 16135.586770, 16408.362074, 16682.849583, 16958.977019, 17236.708221, 17515.990972, 17796.798914, 18079.096585, 18362.834517, 18647.959152, 18934.452073, 19222.251998, 19511.366053, 19801.717928, 20093.269020, 20385.960319, 20679.772736, 20974.698498, 21270.684814, 21567.671010, 21865.649193, 22164.581341, 22464.423293, 22765.172894, 23066.785452, 23369.201089, 23672.387494, 23976.354000, 24281.072155, 24586.465328, 24892.557323, 25199.303712, 25506.654130, 25814.626835, 26123.148009, 26432.247046, 26741.889610, 27052.042135, 27362.685181, 27673.761784, 27985.292246, 28297.219809, 28609.572368, 28922.317633, 29235.448741, 29548.944253, 29862.766175, 30176.910610, 30491.363107, 30806.103787, 31121.126024, 31436.412153, 31751.975134, 32067.737444, 32383.768560, 32700.017044, 33016.458300, 33333.101934, 33649.904492, 33966.923435, 34284.105041, 34601.429152, 34918.876076, 35236.481998, 35554.235511, 35872.093847, 36190.101572, 36508.190892, 36826.420800, 37144.739624, 37463.138566, 37781.598984, 38100.191087, 38418.846511, 38737.587005, 39056.391321, 39375.255819, 39694.182864, 40013.173503, 40332.221146, 40651.311460, 40970.465534, 41289.697002, 41608.913381, 41928.242461, 42247.585089, 42566.988678, 42886.409594, 43205.849396, 43525.322840, 43844.784209, 44164.327769, 44483.869861, 44803.383648, 45122.975593, 45442.593765, 45762.258110, 46081.937012, 46401.605265, 46721.295022, 47041.004034, 47360.695978, 47680.435451, 48000.192231, 48319.953839, 48639.726508, 48959.506239, 49279.318744, 49599.117846, 49918.951926, 50238.772730, 50558.620076, 50878.455501, 51198.285377, 51518.135316, 51837.976134, 52157.809260, 52477.635805, 52797.492063, 53117.368983, 53437.262930, 53757.142921, 54077.026859, 54396.907345, 54716.835955, 55036.713062, 55356.640827, 55676.537369, 55996.488865, 56316.420754, 56636.355217, 56956.329593, 57276.245282, 57596.202473, 57916.159021, 58236.077854, 58556.010303, 58875.938110, 59195.924442, 59515.871947, 59835.838035, 60155.809948, 60475.808167, 60795.765545, 61115.766398, 61435.725825, 61755.677959, 62075.688445, 62395.647263, 62715.609853, 63035.591787, 63355.588060, 63675.579841, 63995.560980, 64315.552298, 64635.582956, 64955.538779, 65275.512154, 65595.490993, 65915.477332, 66235.470693, 66555.483426, 66875.455452, 67195.455039, 67515.435090, 67835.418300, 68155.401870, 68475.404599, 68795.452423, 69115.474466, 69435.450344, 69755.439668, 70075.479966, 70395.468204, 70715.475957, 71035.472807, 71355.513734, 71675.486047, 71995.450475, 72315.443410, 72635.425138, 72955.481520, 73275.482426, 73595.476688, 73915.469540, 74235.426693, 74555.381683, 74875.368981, 75195.429157, 75515.480428, 75835.517265, 76155.557945, 76475.556134, 76795.579451, 77115.605924, 77435.604178, 77755.572770, 78075.576305, 78395.562526, 78715.547599, 79035.551674, 79355.540615, 79675.575726, 79995.547586, 80315.544278, 80635.546227, 80955.555617, 81275.540591, 81595.549538, 81915.550079 }, // log K = 14 { 11817.000969, 12127.609164, 12443.958867, 12766.059919, 13093.926347, 13427.563687, 13766.972021, 14112.158775, 14463.118632, 14819.833221, 15182.310838, 15550.514164, 15924.452624, 16304.097682, 16689.414687, 17080.390156, 17476.985675, 17879.171048, 18286.913146, 18700.171223, 19118.909859, 19543.074108, 19972.606221, 20407.466793, 20847.600290, 21292.965548, 21743.489087, 22199.109276, 22659.760749, 23125.379711, 23595.914325, 24071.277989, 24551.407611, 25036.233071, 25525.671155, 26019.641791, 26518.074136, 27020.885189, 27528.004081, 28039.347478, 28554.818908, 29074.343878, 29597.869471, 30125.254970, 30656.449891, 31191.351228, 31729.908970, 32272.003587, 32817.586623, 33366.531359, 33918.769390, 34474.232727, 35032.798601, 35594.423192, 36159.024745, 36726.469786, 37296.712851, 37869.686035, 38445.278953, 39023.442285, 39604.084062, 40187.147209, 40772.503600, 41360.148758, 41949.978377, 42541.929077, 43135.892496, 43731.836459, 44329.664552, 44929.334224, 45530.772542, 46133.919152, 46738.746702, 47345.120215, 47953.051656, 48562.469011, 49173.281212, 49785.453501, 50398.908717, 51013.645932, 51629.563526, 52246.635759, 52864.820434, 53484.057447, 54104.303719, 54725.504675, 55347.680019, 55970.744363, 56594.688890, 57219.458967, 57844.949821, 58471.197998, 59098.180259, 59725.774793, 60354.058215, 60982.962558, 61612.427469, 62242.448368, 62873.013426, 63504.106743, 64135.672742, 64767.665213, 65400.082326, 66032.949331, 66666.219558, 67299.817447, 67933.801373, 68568.092593, 69202.717636, 69837.643507, 70472.852133, 71108.305774, 71744.023189, 72380.016332, 73016.168118, 73652.560876, 74289.210741, 74926.041959, 75563.048434, 76200.207724, 76837.505542, 77474.999283, 78112.608284, 78750.375633, 79388.254423, 80026.267074, 80664.419197, 81302.602941, 81940.909776, 82579.321050, 83217.801329, 83856.420605, 84495.098999, 85133.832545, 85772.588001, 86411.448347, 87050.399262, 87689.358100, 88328.463714, 88967.516990, 89606.618839, 90245.788934, 90884.986443, 91524.243962, 92163.523370, 92802.835394, 93442.198790, 94081.633840, 94721.043838, 95360.482304, 96000.002974, 96639.518339, 97279.069377, 97918.584802, 98558.189888, 99197.773242, 99837.390001, 100476.994827, 101116.655652, 101756.293703, 102395.977437, 103035.662337, 103675.360919, 104315.063845, 104954.783621, 105594.504004, 106234.229823, 106874.056079, 107513.846129, 108153.562857, 108793.393622, 109433.202056, 110072.966808, 110712.777026, 111352.577741, 111992.441538, 112632.256702, 113272.083743, 113911.943484, 114551.818432, 115191.687411, 115831.584194, 116471.522453, 117111.338979, 117751.192777, 118391.070556, 119030.991702, 119670.888488, 120310.801509, 120950.761000, 121590.691826, 122230.618394, 122870.618097, 123510.616056, 124150.552363, 124790.488946, 125430.445483, 126070.428297, 126710.409643, 127350.398562, 127990.397846, 128630.413222, 129270.354937, 129910.365912, 130550.332174, 131190.259646, 131830.280970, 132470.304278, 133110.295205, 133750.312426, 134390.364847, 135030.321000, 135670.319926, 136310.335570, 136950.324790, 137590.338289, 138230.368657, 138870.374084, 139510.447896, 140150.468353, 140790.441338, 141430.438424, 142070.440787, 142710.431348, 143350.433770, 143990.493350, 144630.548409, 145270.549935, 145910.530486, 146550.488808, 147190.510290, 147830.509426, 148470.575641, 149110.541845, 149750.592427, 150390.634921, 151030.706500, 151670.646574, 152310.624482, 152950.686127, 153590.659153, 154230.646236, 154870.720079, 155510.614028, 156150.596142, 156790.659641, 157430.633386, 158070.649047, 158710.591350, 159350.585980, 159990.557711, 160630.530183, 161270.499427, 161910.521323, 162550.516603, 163190.482792, 163830.422631 }, // log K = 15 { 23634.780143, 24256.008404, 24888.717596, 25532.934303, 26188.680791, 26855.965949, 27534.798395, 28225.179199, 28927.099457, 29640.545847, 30365.501013, 31101.936533, 31849.825685, 32609.111850, 33379.755958, 34161.706474, 34954.912131, 35759.291030, 36574.778310, 37401.280667, 38238.742234, 39087.060449, 39946.133936, 40815.861824, 41696.151280, 42586.877221, 43487.927203, 44399.169293, 45320.460825, 46251.693279, 47192.741847, 48143.464216, 49103.716679, 50073.349113, 51052.208336, 52040.138165, 53036.993192, 54042.604241, 55056.838449, 56079.501709, 57110.434199, 58149.484623, 59196.455710, 60251.226284, 61313.593108, 62383.385277, 63460.484960, 64544.666337, 65635.763495, 66733.629361, 67838.096489, 68948.987037, 70066.117363, 71189.338133, 72318.464573, 73453.423815, 74593.923438, 75739.864335, 76891.058098, 78047.391508, 79208.654009, 80374.753680, 81545.514022, 82720.817799, 83900.405263, 85084.273208, 86272.263471, 87464.134340, 88659.829437, 89859.204804, 91062.115479, 92268.425863, 93478.041907, 94690.804589, 95906.647693, 97125.398400, 98347.018145, 99571.305992, 100798.274503, 102027.737913, 103259.574749, 104493.727918, 105730.035415, 106968.465700, 108208.988306, 109451.445144, 110695.748927, 111941.827269, 113189.666448, 114439.093665, 115690.116530, 116942.608958, 118196.501294, 119451.722652, 120708.276299, 121966.046247, 123225.027480, 124485.082349, 125746.201609, 127008.357433, 128271.416054, 129535.417336, 130800.297278, 132066.008820, 133332.483576, 134599.733640, 135867.681429, 137136.303792, 138405.538969, 139675.413251, 140945.839375, 142216.787412, 143488.232199, 144760.234976, 146032.650196, 147305.459198, 148578.703708, 149852.356585, 151126.347770, 152400.641475, 153675.275168, 154950.179904, 156225.390849, 157500.864126, 158776.585926, 160052.504602, 161328.716398, 162605.171897, 163881.825931, 165158.677102, 166435.646121, 167712.814846, 168990.136047, 170267.619890, 171545.238277, 172823.011460, 174100.890250, 175378.821293, 176656.920467, 177935.093547, 179213.318173, 180491.692642, 181770.130424, 183048.727206, 184327.217835, 185605.921891, 186884.657773, 188163.418059, 189442.221838, 190721.122201, 192000.068271, 193279.087882, 194558.139554, 195837.195523, 197116.400538, 198395.612565, 199674.831001, 200954.101970, 202233.365695, 203512.639968, 204792.003319, 206071.379608, 207350.730914, 208630.080321, 209909.544216, 211189.014497, 212468.508049, 213748.055445, 215027.584964, 216307.216078, 217586.896296, 218866.477232, 220146.109506, 221425.767458, 222705.465633, 223985.129542, 225264.797531, 226544.544335, 227824.304232, 229104.031191, 230383.739676, 231663.424684, 232943.198829, 234222.906969, 235502.669185, 236782.475696, 238062.262723, 239342.090234, 240622.002668, 241901.911022, 243181.742902, 244461.690551, 245741.531359, 247021.432644, 248301.277604, 249581.168404, 250861.094078, 252141.038818, 253421.065594, 254701.020169, 255980.887518, 257260.848328, 258540.873565, 259820.905666, 261100.908594, 262380.854763, 263660.862699, 264940.834842, 266220.775656, 267500.745402, 268780.761926, 270060.823540, 271340.798715, 272620.823081, 273900.881190, 275180.928531, 276460.914606, 277740.919081, 279020.967358, 280301.002010, 281581.054314, 282861.097638, 284141.109728, 285421.181335, 286701.182750, 287981.231437, 289261.342952, 290541.328315, 291821.320901, 293101.395969, 294381.402286, 295661.374812, 296941.357112, 298221.462446, 299501.443884, 300781.429735, 302061.512800, 303341.459364, 304621.530042, 305901.507763, 307181.541613, 308461.506346, 309741.474814, 311021.458944, 312301.373851, 313581.258984, 314861.222655, 316141.192042, 317421.179698, 318701.162346, 319981.143104, 321261.185706, 322541.120379, 323821.065555, 325100.961639, 326380.920540, 327660.786948 }, // log K = 16 { 47270.338530, 48512.804588, 49778.235006, 51066.679846, 52378.178287, 53712.756179, 55070.430016, 56451.198432, 57855.044557, 59281.941569, 60731.855114, 62204.727445, 63700.498656, 65219.087806, 66760.381754, 68324.278827, 69910.681844, 71519.442760, 73150.426782, 74803.469853, 76478.400456, 78175.038414, 79893.184924, 81632.639705, 83393.214587, 85174.662642, 86976.739912, 88799.210643, 90641.829434, 92504.355605, 94386.478165, 96287.925119, 98208.422967, 100147.694840, 102105.387259, 104081.226364, 106074.916897, 108086.136683, 110114.560800, 112159.857579, 114221.724675, 116299.785348, 118393.731853, 120503.258946, 122627.958417, 124767.540962, 126921.668848, 129090.003274, 131272.204811, 133467.918004, 135676.870426, 137898.595565, 140132.829694, 142379.269230, 144637.536478, 146907.321805, 149188.299797, 151480.151490, 153782.571459, 156095.292246, 158417.834407, 160749.983694, 163091.465145, 165441.984077, 167801.236994, 170168.903670, 172544.767987, 174928.494171, 177319.918928, 179718.624717, 182124.426670, 184537.056818, 186956.260737, 189381.869001, 191813.551321, 194251.107949, 196694.280093, 199142.910671, 201596.663557, 204055.453662, 206519.090402, 208987.322132, 211460.079506, 213936.962995, 216417.945039, 218902.907481, 221391.594168, 223883.857806, 226379.519239, 228878.478995, 231380.515508, 233885.422831, 236393.251620, 238903.734661, 241416.885791, 243932.462444, 246450.361381, 248970.497360, 251492.768859, 254017.059221, 256543.244066, 259071.366198, 261601.098326, 264132.472390, 266665.477130, 269200.003838, 271735.898262, 274273.128446, 276811.699617, 279351.334667, 281892.176977, 284434.126935, 286977.141855, 289520.993664, 292065.816286, 294611.452586, 297157.986206, 299705.252956, 302253.226506, 304801.859484, 307351.170990, 309901.148395, 312451.594894, 315002.530644, 317553.961670, 320105.951895, 322658.553440, 325211.299379, 327764.626147, 330318.311300, 332872.331048, 335426.696106, 337981.358498, 340536.275072, 343091.446562, 345646.972942, 348202.757347, 350758.693296, 353314.808579, 355871.203754, 358427.702337, 360984.491079, 363541.449032, 366098.493281, 368655.687887, 371213.030472, 373770.561193, 376328.233445, 378886.001706, 381443.937538, 384001.856504, 386559.857503, 389117.926075, 391676.219835, 394234.530250, 396792.812601, 399351.291660, 401909.794770, 404468.330377, 407026.964781, 409585.614496, 412144.407471, 414703.175858, 417261.980659, 419820.958264, 422379.863083, 424938.751186, 427497.927620, 430056.993719, 432616.066348, 435175.266190, 437734.382772, 440293.531277, 442852.711332, 445412.016423, 447971.502143, 450530.798765, 453090.167199, 455649.502729, 458208.951251, 460768.459694, 463328.053373, 465887.570627, 468447.239271, 471006.792818, 473566.544068, 476126.194227, 478685.887605, 481245.577573, 483805.348711, 486365.028302, 488924.844568, 491484.612666, 494044.321666, 496604.011864, 499163.799789, 501723.629793, 504283.493711, 506843.290195, 509403.157856, 511962.990320, 514522.941350, 517082.791953, 519642.848656, 522202.833761, 524762.686522, 527322.632758, 529882.529037, 532442.420851, 535002.422317, 537562.386309, 540122.357425, 542682.434171, 545242.468829, 547802.466694, 550362.492718, 552922.461624, 555482.539495, 558042.531431, 560602.467437, 563162.648952, 565722.750610, 568282.702877, 570842.810092, 573402.917604, 575962.999235, 578523.081937, 581083.162584, 583643.265455, 586203.287883, 588763.260931, 591323.292027, 593883.252456, 596443.273935, 599003.353442, 601563.492606, 604123.421135, 606683.438234, 609243.605174, 611803.638038, 614363.628593, 616923.690580, 619483.675256, 622043.744171, 624603.673246, 627163.577236, 629723.677960, 632283.670157, 634843.692958, 637403.576097, 639963.511752, 642523.482942, 645083.419327, 647643.330711, 650203.272467, 652763.290376, 655323.267522 }, // log K = 17 { 94541.455324, 97026.403245, 99557.280165, 102134.178452, 104757.187033, 107426.362674, 110141.726029, 112903.278005, 115710.975458, 118564.785344, 121464.629896, 124410.377345, 127401.910279, 130439.087070, 133521.683723, 136649.499031, 139822.321627, 143039.848320, 146301.785984, 149607.844773, 152957.695333, 156350.945326, 159787.233040, 163266.157476, 166787.301371, 170350.201319, 173954.353438, 177599.302692, 181284.526367, 185009.507089, 188773.757457, 192576.638207, 196417.619925, 200296.089540, 204211.494530, 208163.221220, 212150.613086, 216173.056150, 220229.879308, 224320.462264, 228444.146459, 232600.284426, 236788.187347, 241007.264352, 245256.725247, 249535.938618, 253844.217048, 258180.876528, 262545.282090, 266936.686787, 271354.453768, 275797.958112, 280266.429404, 284759.298770, 289275.861948, 293815.386243, 298377.390708, 302961.087179, 307565.895452, 312191.173759, 316836.362522, 321500.721532, 326183.652758, 330884.723656, 335603.197219, 340338.617624, 345090.316855, 349857.860719, 354640.538362, 359438.001710, 364249.638745, 369074.940533, 373913.351331, 378764.438864, 383627.767868, 388502.854215, 393389.259584, 398286.483436, 403194.238263, 408111.948151, 413039.292303, 417975.909674, 422921.304084, 427875.195485, 432837.200959, 437807.062429, 442784.399966, 447768.726662, 452760.032525, 457757.794185, 462761.810279, 467771.737196, 472787.226777, 477808.283249, 482834.411114, 487865.521166, 492901.275392, 497941.616150, 502986.164281, 508034.749030, 513087.210697, 518143.298172, 523202.692981, 528265.454984, 533331.384435, 538400.437943, 543472.278711, 548546.790172, 553623.737338, 558703.047575, 563784.666463, 568868.524932, 573954.251207, 579041.935016, 584131.557779, 589222.910016, 594315.786547, 599410.314112, 604506.227555, 609603.610432, 614702.124262, 619801.918486, 624902.903258, 630005.085347, 635107.990499, 640211.847431, 645316.755403, 650422.424613, 655529.009211, 660636.166872, 665744.170495, 670852.806947, 675962.099161, 681072.042796, 686182.458370, 691293.269596, 696404.763335, 701516.635253, 706628.965299, 711741.575313, 716854.769083, 721968.220605, 727082.027784, 732196.114131, 737310.594653, 742425.339665, 747540.295793, 752655.505647, 757770.968129, 762886.636891, 768002.527720, 773118.559142, 778234.871441, 783351.273881, 788467.944809, 793584.599211, 798701.497594, 803818.484285, 808935.619316, 814053.067243, 819170.390229, 824287.946609, 829405.450988, 834523.054071, 839640.856519, 844758.800102, 849876.719924, 854994.831607, 860112.896788, 865231.116455, 870349.453898, 875467.910756, 880586.444976, 885705.102070, 890823.822978, 895942.513436, 901061.120048, 906179.716020, 911298.587548, 916417.583647, 921536.507373, 926655.570709, 931774.690027, 936893.879961, 942012.956398, 947132.142141, 952251.503479, 957370.815448, 962490.012685, 967609.521437, 972729.002837, 977848.339983, 982967.824448, 988087.392216, 993206.992691, 998326.635354, 1003446.304388, 1008565.985024, 1013685.706678, 1018805.581548, 1023925.239073, 1029045.157502, 1034165.035124, 1039284.843524, 1044404.676526, 1049524.620186, 1054644.441170, 1059764.409794, 1064884.318434, 1070004.338968, 1075124.268749, 1080244.471055, 1085364.528193, 1090484.511927, 1095604.595114, 1100724.598478, 1105844.666500, 1110964.804168, 1116084.864159, 1121204.874577, 1126325.062599, 1131445.152321, 1136565.301592, 1141685.638897, 1146805.806800, 1151925.790859, 1157045.932640, 1162166.112443, 1167286.221698, 1172406.273611, 1177526.581875, 1182646.832239, 1187766.882661, 1192886.884388, 1198006.962839, 1203127.084377, 1208246.959060, 1213366.976076, 1218486.763464, 1223606.758568, 1228726.721761, 1233846.593404, 1238966.815228, 1244086.865449, 1249206.854850, 1254326.996667, 1259447.092239, 1264567.198081, 1269687.499091, 1274807.474194, 1279927.244161, 1285047.273317, 1290167.090035, 1295287.035620, 1300406.888764, 1305526.667946, 1310646.540340 }, // log K = 18 { 189083.688921, 194053.597973, 199115.342781, 204269.157017, 209515.195566, 214853.553887, 220284.295855, 225807.399998, 231422.826900, 237130.469824, 242930.139690, 248821.634191, 254804.718978, 260879.073467, 267044.289594, 273299.936993, 279645.549742, 286080.586199, 292604.521050, 299216.651567, 305916.376160, 312702.908374, 319575.536730, 326533.420485, 333575.639430, 340701.388339, 347909.713977, 355199.642852, 362570.108280, 370020.154080, 377548.652175, 385154.413997, 392836.374125, 400593.371525, 408424.172521, 416327.671123, 424302.406749, 432347.292674, 440461.014310, 448642.241683, 456889.603709, 465201.946081, 473577.749277, 482015.802141, 490514.770444, 499073.243760, 507689.794308, 516363.149230, 525091.902823, 533874.850510, 542710.434311, 551597.433198, 560534.241273, 569520.019389, 578553.034265, 587632.180111, 596756.084542, 605923.440522, 615133.037840, 624383.626242, 633673.841133, 643002.674316, 652368.578283, 661770.556501, 671207.605795, 680678.417768, 690181.818697, 699716.780471, 709282.084973, 718876.864175, 728499.951143, 738150.280049, 747827.053968, 757529.236597, 767255.784062, 777006.100829, 786779.065927, 796573.507429, 806388.828153, 816223.976258, 826078.608350, 835951.751956, 845842.493689, 855750.225870, 865674.190527, 875613.809349, 885568.425527, 895537.272219, 905519.763307, 915515.310346, 925523.353103, 935543.172648, 945574.292664, 955616.280042, 965668.428231, 975730.535479, 985802.055447, 995882.596628, 1005971.583108, 1016068.664992, 1026173.649961, 1036285.774891, 1046404.955770, 1056530.588186, 1066662.648243, 1076800.535542, 1086944.269881, 1097093.058652, 1107247.076183, 1117405.990557, 1127569.310917, 1137737.011345, 1147908.717700, 1158084.362020, 1168263.501029, 1178446.221907, 1188632.206842, 1198821.131431, 1209013.020373, 1219207.570786, 1229404.854893, 1239604.423801, 1249806.433263, 1260010.619993, 1270216.819018, 1280424.774173, 1290634.560600, 1300846.097203, 1311059.128630, 1321273.628402, 1331489.832488, 1341707.300774, 1351925.947953, 1362145.802925, 1372366.802414, 1382588.547896, 1392811.264776, 1403035.227449, 1413259.922280, 1423485.322819, 1433711.916106, 1443938.981360, 1454166.596984, 1464394.761679, 1474623.684212, 1484853.156422, 1495083.020146, 1505313.735890, 1515544.731764, 1525776.086634, 1536007.742073, 1546239.679012, 1556472.212370, 1566704.988763, 1576937.965495, 1587171.446007, 1597405.292186, 1607639.395274, 1617873.660775, 1628108.037273, 1638342.741799, 1648577.832828, 1658813.325573, 1669048.803784, 1679284.679963, 1689520.927615, 1699757.050010, 1709993.263665, 1720229.557663, 1730466.102090, 1740702.604009, 1750939.618504, 1761176.930182, 1771414.210528, 1781651.713742, 1791889.218503, 1802126.805234, 1812364.257333, 1822602.054007, 1832839.687792, 1843077.871973, 1853315.944762, 1863554.202066, 1873792.633125, 1884030.883830, 1894269.182711, 1904507.712050, 1914746.469507, 1924985.484753, 1935224.245956, 1945463.225215, 1955702.268883, 1965941.321722, 1976180.037370, 1986419.052784, 1996658.429579, 2006897.450283, 2017136.904971, 2027376.240039, 2037615.549295, 2047855.197425, 2058094.765906, 2068334.279787, 2078573.820075, 2088813.469730, 2099053.348620, 2109293.174176, 2119533.012704, 2129772.820132, 2140012.744709, 2150252.607739, 2160492.533017, 2170732.406422, 2180972.400110, 2191212.436319, 2201452.637894, 2211692.339292, 2221932.460057, 2232172.746036, 2242412.892705, 2252653.174824, 2262893.432271, 2273134.002270, 2283374.311008, 2293614.537881, 2303854.703732, 2314095.191374, 2324335.236186, 2334575.624526, 2344816.313269, 2355056.224171, 2365296.361285, 2375537.048184, 2385777.280284, 2396017.222502, 2406257.145546, 2416497.272190, 2426737.212210, 2436977.500856, 2447217.897426, 2457458.423605, 2467698.536697, 2477938.448096, 2488178.544726, 2498418.640441, 2508658.160374, 2518898.274852, 2529138.509345, 2539378.310538, 2549617.989497, 2559857.504886, 2570097.363128, 2580337.195261, 2590576.878833, 2600816.572159, 2611055.930087, 2621295.625004 }, // log K = 19 { 378168.156126, 388107.986863, 398231.517433, 408539.178530, 419031.252008, 429707.954086, 440569.365328, 451615.642722, 462846.490174, 474261.790112, 485861.190310, 497644.259253, 509610.438730, 521759.095095, 534089.573442, 546600.981190, 559292.272697, 572162.409622, 585210.331052, 598434.669889, 611834.157416, 625407.271721, 639152.543961, 653068.262294, 667152.777393, 681404.241581, 695820.778241, 710400.501586, 725141.425404, 740041.558661, 755098.503731, 770310.107475, 785674.192935, 801188.297422, 816849.950298, 832656.623960, 848606.198026, 864696.151343, 880923.487841, 897285.841319, 913780.530043, 930405.131973, 947156.655545, 964032.974599, 981030.762278, 998147.499138, 1015380.659035, 1032727.149847, 1050184.616751, 1067750.330789, 1085421.716664, 1103195.771853, 1121069.796650, 1139041.324454, 1157107.373802, 1175265.377022, 1193513.201704, 1211847.872855, 1230267.133114, 1248768.293478, 1267348.634472, 1286005.987451, 1304738.187318, 1323542.421988, 1342416.458954, 1361357.807871, 1380364.530293, 1399434.095493, 1418564.587724, 1437754.116682, 1457000.254889, 1476301.275902, 1495654.977143, 1515059.332212, 1534512.821906, 1554013.130304, 1573558.585534, 1593147.244450, 1612777.978405, 1632448.600136, 1652157.704100, 1671904.308928, 1691686.012962, 1711501.716352, 1731349.849417, 1751229.053668, 1771138.423208, 1791076.196899, 1811041.338260, 1831032.177526, 1851048.091817, 1871088.096683, 1891150.317142, 1911234.619162, 1931339.423184, 1951463.947205, 1971606.994543, 1991767.606441, 2011945.852451, 2032140.098239, 2052349.709954, 2072574.274980, 2092812.387905, 2113063.537955, 2133327.750182, 2153603.787963, 2173890.933053, 2194188.225246, 2214496.333997, 2234814.120065, 2255140.753372, 2275476.675949, 2295820.689602, 2316172.220323, 2336530.258616, 2356894.932256, 2377266.633368, 2397645.173978, 2418028.419639, 2438417.586475, 2458812.422593, 2479211.306360, 2499615.253217, 2520023.375080, 2540436.051898, 2560851.967345, 2581271.569076, 2601694.318944, 2622120.693963, 2642549.418269, 2662981.070023, 2683415.520909, 2703852.500003, 2724291.876933, 2744733.213126, 2765177.477758, 2785623.320019, 2806070.985142, 2826520.234343, 2846971.203929, 2867423.303616, 2887876.396540, 2908331.739769, 2928788.290672, 2949245.276635, 2969704.620226, 2990164.657583, 3010625.841710, 3031087.855626, 3051550.619090, 3072014.642350, 3092479.163042, 3112944.278087, 3133410.101603, 3153876.719100, 3174343.414117, 3194810.517970, 3215278.762802, 3235747.174277, 3256216.390486, 3276685.652553, 3297155.394593, 3317625.911419, 3338096.547090, 3358567.567862, 3379038.637659, 3399510.649493, 3419983.382105, 3440456.231853, 3460929.496047, 3481403.703904, 3501877.874941, 3522351.898403, 3542825.527074, 3563300.038829, 3583775.102749, 3604249.896903, 3624724.967423, 3645201.027293, 3665676.534460, 3686152.061202, 3706628.308333, 3727104.271827, 3747580.803335, 3768057.710876, 3788534.660048, 3809011.720864, 3829489.164523, 3849967.204462, 3870445.043912, 3890923.080446, 3911400.767281, 3931879.061622, 3952357.218696, 3972836.284463, 3993314.516935, 4013793.278282, 4034272.661280, 4054751.814018, 4075230.503334, 4095708.557968, 4116187.919814, 4136667.714093, 4157147.152355, 4177627.302553, 4198107.383728, 4218587.174918, 4239066.631662, 4259545.994167, 4280026.100110, 4300506.380609, 4320986.617208, 4341466.696312, 4361945.995732, 4382426.527708, 4402907.058309, 4423386.556967, 4443867.498542, 4464348.284664, 4484828.344391, 4505309.026083, 4525789.774619, 4546270.396129, 4566750.818685, 4587231.362126, 4607711.356124, 4628191.883250, 4648673.308942, 4669153.953372, 4689634.118530, 4710114.637510, 4730595.217429, 4751075.358693, 4771555.492921, 4792035.313408, 4812516.057037, 4832995.761283, 4853475.977619, 4873956.732160, 4894436.879314, 4914917.149285, 4935397.069776, 4955877.550351, 4976357.852728, 4996838.246842, 5017318.023169, 5037798.008344, 5058278.290403, 5078757.968956, 5099237.419344, 5119716.759591, 5140195.935830, 5160675.636320, 5181154.980843, 5201633.780713, 5222113.031789, 5242591.881577 }, // log K = 20 { 756337.090537, 776216.744995, 796463.785646, 817079.035290, 838063.221300, 859416.678362, 881139.576270, 903232.151726, 925693.940877, 948524.600346, 971723.442348, 995289.632784, 1019222.059448, 1043519.402588, 1068180.163485, 1093202.854610, 1118585.515820, 1144326.031845, 1170421.828997, 1196870.617641, 1223669.453959, 1250815.679160, 1278306.298944, 1306137.726017, 1334306.782451, 1362809.672011, 1391643.048887, 1420802.665486, 1450284.419390, 1480084.556504, 1510198.362531, 1540621.709038, 1571349.988582, 1602377.885766, 1633700.853485, 1665314.443974, 1697213.607484, 1729393.242718, 1761847.799154, 1794572.464804, 1827562.002973, 1860811.213417, 1894314.858503, 1928067.079352, 1962062.774535, 1996296.400592, 2030762.354026, 2065455.981656, 2100370.818573, 2135502.224529, 2170844.722018, 2206392.452100, 2242140.795895, 2278083.565225, 2314215.470175, 2350532.238358, 2387028.028531, 2423697.739702, 2460535.617283, 2497537.908190, 2534699.627071, 2572014.508248, 2609478.240813, 2647086.878671, 2684834.738503, 2722717.495987, 2760730.659130, 2798869.445379, 2837130.937688, 2875509.782410, 2914002.086936, 2952603.395700, 2991310.971904, 3030119.629799, 3069026.891217, 3108027.543585, 3147118.485322, 3186296.724369, 3225557.893026, 3264899.405158, 3304317.806643, 3343809.532357, 3383372.875494, 3423003.239113, 3462699.855290, 3502458.588120, 3542276.984489, 3582151.461666, 3622080.455509, 3662062.087297, 3702093.774268, 3742173.351913, 3782298.542025, 3822466.599295, 3862676.059061, 3902925.301466, 3943212.313188, 3983534.655139, 4023890.682932, 4064278.948819, 4104698.644146, 4145147.867936, 4185624.200116, 4226127.607823, 4266655.471909, 4307207.261933, 4347781.577688, 4388376.075665, 4428991.084860, 4469625.512791, 4510278.322018, 4550949.135583, 4591635.061142, 4632337.290243, 4673054.034167, 4713783.999697, 4754527.016825, 4795282.199016, 4836049.304645, 4876828.217063, 4917617.432795, 4958416.033283, 4999223.332994, 5040040.200620, 5080865.836319, 5121698.006953, 5162537.663489, 5203383.861562, 5244235.368756, 5285093.542054, 5325957.172545, 5366826.006832, 5407700.602295, 5448579.444589, 5489462.237514, 5530348.767163, 5571240.286944, 5612135.490023, 5653033.590430, 5693935.090249, 5734840.012969, 5775747.413811, 5816658.159197, 5857571.727542, 5898486.810748, 5939403.918361, 5980324.159335, 6021245.922584, 6062167.462275, 6103093.061724, 6144019.021028, 6184948.477708, 6225878.111073, 6266809.926593, 6307741.380274, 6348674.587241, 6389609.653586, 6430546.024311, 6471482.661256, 6512419.658468, 6553358.313587, 6594299.462793, 6635239.780670, 6676180.809115, 6717122.614963, 6758064.640997, 6799009.935719, 6839954.567726, 6880900.950276, 6921847.965357, 6962794.580261, 7003742.234176, 7044691.155209, 7085639.223984, 7126587.984374, 7167538.597843, 7208487.508674, 7249437.307369, 7290389.448350, 7331341.752887, 7372292.872774, 7413245.082344, 7454198.512431, 7495151.878502, 7536105.815461, 7577059.755263, 7618013.175400, 7658967.579066, 7699922.475951, 7740878.015144, 7781833.511721, 7822789.986990, 7863745.987999, 7904702.500036, 7945659.757785, 7986615.385020, 8027573.168319, 8068531.917672, 8109489.438412, 8150447.742963, 8191403.350221, 8232362.807587, 8273321.255913, 8314281.043410, 8355240.292724, 8396199.214301, 8437156.471990, 8478114.862372, 8519073.457296, 8560034.832965, 8600993.512814, 8641954.368755, 8682914.667061, 8723875.362809, 8764837.809961, 8805797.690078, 8846758.789178, 8887720.671501, 8928680.126482, 8969640.467726, 9010601.160006, 9051561.769905, 9092522.487783, 9133484.594542, 9174446.122191, 9215406.809237, 9256366.837569, 9297328.747584, 9338288.901510, 9379250.948160, 9420212.503729, 9461173.228802, 9502133.928845, 9543094.489131, 9584055.513956, 9625015.618922, 9665975.737758, 9706937.403885, 9747896.035874, 9788856.165941, 9829816.994627, 9870777.228642, 9911737.327683, 9952698.937964, 9993660.635143, 10034620.747904, 10075579.526207, 10116539.739394, 10157498.625700, 10198458.468474, 10239418.486051, 10280376.431741, 10321335.494132, 10362295.312860, 10403254.402980, 10444214.519042, 10485173.027410 }, // log K = 21 { 1512674.959317, 1552434.197368, 1592928.155075, 1634158.593696, 1676126.959176, 1718834.017325, 1762279.726817, 1806464.610827, 1851388.162884, 1897049.337397, 1943446.849678, 1990579.312415, 2038444.063365, 2087038.766553, 2136360.248426, 2186405.470599, 2237170.640174, 2288651.015887, 2340842.752937, 2393739.986364, 2447337.958039, 2501630.515180, 2556611.361814, 2612274.289376, 2668612.210872, 2725618.064986, 2783284.549430, 2841603.566294, 2900566.895724, 2960167.093438, 3020394.610851, 3081240.447614, 3142696.078921, 3204751.105703, 3267397.808234, 3330624.834407, 3394422.872136, 3458781.966184, 3523690.805899, 3589140.856689, 3655119.540256, 3721617.907853, 3788624.486645, 3856129.175227, 3924119.940026, 3992586.658818, 4061519.544440, 4130906.734075, 4200737.500257, 4271000.909057, 4341685.842431, 4412781.306685, 4484277.199691, 4556162.287552, 4628426.495554, 4701059.642462, 4774050.909933, 4847389.117100, 4921065.536469, 4995068.178578, 5069389.529141, 5144019.928509, 5218947.440841, 5294164.568050, 5369660.364896, 5445425.915267, 5521454.727645, 5597734.642433, 5674257.411718, 5751014.463556, 5827999.822090, 5905203.759793, 5982617.522812, 6060234.561845, 6138048.162353, 6216049.451496, 6294232.713331, 6372588.108087, 6451109.903764, 6529792.611180, 6608629.912527, 6687615.097432, 6766740.086064, 6846000.116450, 6925391.839995, 7004908.093083, 7084544.127544, 7164293.822313, 7244153.392582, 7324115.814384, 7404179.031163, 7484336.380737, 7564586.128001, 7644922.247694, 7725340.819662, 7805840.541693, 7886412.416887, 7967054.301986, 8047764.087248, 8128539.447460, 8209376.477465, 8290273.766928, 8371225.884580, 8452230.183873, 8533283.959811, 8614388.863382, 8695539.350564, 8776730.375553, 8857961.046732, 8939230.757845, 9020535.369667, 9101877.322129, 9183251.420601, 9264654.743697, 9346087.109813, 9427548.372707, 9509034.441574, 9590547.753266, 9672081.682514, 9753639.600816, 9835216.139507, 9916813.989591, 9998428.682824, 10080058.312759, 10161706.673718, 10243372.018016, 10325050.507034, 10406744.529470, 10488447.296300, 10570163.109725, 10651893.512546, 10733632.712879, 10815382.384056, 10897140.573621, 10978907.646650, 11060681.383125, 11142464.138730, 11224254.549571, 11306053.275368, 11387858.162207, 11469666.329635, 11551480.634292, 11633302.025569, 11715128.710512, 11796961.403328, 11878795.670820, 11960634.652330, 12042477.348124, 12124323.950451, 12206172.128053, 12288028.811447, 12369886.306570, 12451744.123236, 12533607.183832, 12615473.707341, 12697338.733240, 12779207.987968, 12861080.507718, 12942957.260823, 13024834.007606, 13106715.252745, 13188597.806270, 13270478.933580, 13352362.482281, 13434247.263704, 13516133.477076, 13598020.834213, 13679909.264273, 13761799.592878, 13843689.299819, 13925583.582108, 14007479.173806, 14089371.780210, 14171268.621526, 14253167.561983, 14335070.527750, 14416971.620797, 14498873.784867, 14580778.789301, 14662681.662290, 14744586.311912, 14826493.286700, 14908400.702409, 14990305.605573, 15072214.379125, 15154123.120545, 15236031.901296, 15317939.866920, 15399849.993076, 15481761.939120, 15563672.325616, 15645585.312945, 15727497.406318, 15809411.780271, 15891325.875461, 15973239.145687, 16055155.936424, 16137069.783844, 16218984.663574, 16300899.732679, 16382815.849682, 16464730.053474, 16546645.675799, 16628563.444017, 16710481.533611, 16792400.824636, 16874319.061886, 16956235.859045, 17038151.394583, 17120072.629220, 17201992.200096, 17283914.365655, 17365836.161356, 17447756.880495, 17529681.545942, 17611601.675003, 17693522.079185, 17775441.939609, 17857361.777214, 17939284.144858, 18021205.445833, 18103125.675947, 18185051.132189, 18266974.391463, 18348894.689783, 18430817.526979, 18512736.992850, 18594657.001222, 18676577.636496, 18758497.698999, 18840421.426602, 18922345.360722, 19004266.906157, 19086186.184618, 19168105.320223, 19250029.336282, 19331950.350200, 19413874.580826, 19495794.431194, 19577715.745558, 19659638.264924, 19741557.363469, 19823478.431258, 19905400.862401, 19987318.682409, 20069238.585823, 20151157.549449, 20233076.181910, 20314994.521260, 20396915.069140, 20478836.108717, 20560753.915833, 20642672.570035, 20724588.867440, 20806507.038063, 20888426.370670, 20970344.006053 } }; //CHECKSTYLE.ON: LineLength }
2,533
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/ToByteArrayImpl.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.AbstractCoupons.find; import static org.apache.datasketches.hll.HllUtil.LG_AUX_ARR_INTS; import static org.apache.datasketches.hll.PreambleUtil.AUX_COUNT_INT; import static org.apache.datasketches.hll.PreambleUtil.HLL_BYTE_ARR_START; import static org.apache.datasketches.hll.PreambleUtil.insertAuxCount; import static org.apache.datasketches.hll.PreambleUtil.insertCompactFlag; import static org.apache.datasketches.hll.PreambleUtil.insertCurMin; import static org.apache.datasketches.hll.PreambleUtil.insertCurMode; import static org.apache.datasketches.hll.PreambleUtil.insertEmptyFlag; import static org.apache.datasketches.hll.PreambleUtil.insertFamilyId; import static org.apache.datasketches.hll.PreambleUtil.insertHashSetCount; import static org.apache.datasketches.hll.PreambleUtil.insertHipAccum; import static org.apache.datasketches.hll.PreambleUtil.insertInt; import static org.apache.datasketches.hll.PreambleUtil.insertKxQ0; import static org.apache.datasketches.hll.PreambleUtil.insertKxQ1; import static org.apache.datasketches.hll.PreambleUtil.insertLgArr; import static org.apache.datasketches.hll.PreambleUtil.insertLgK; import static org.apache.datasketches.hll.PreambleUtil.insertListCount; import static org.apache.datasketches.hll.PreambleUtil.insertNumAtCurMin; import static org.apache.datasketches.hll.PreambleUtil.insertOooFlag; import static org.apache.datasketches.hll.PreambleUtil.insertPreInts; import static org.apache.datasketches.hll.PreambleUtil.insertRebuildCurMinNumKxQFlag; import static org.apache.datasketches.hll.PreambleUtil.insertSerVer; import static org.apache.datasketches.hll.PreambleUtil.insertTgtHllType; import org.apache.datasketches.common.SketchesStateException; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.memory.WritableMemory; /** * @author Lee Rhodes */ class ToByteArrayImpl { // To byte array used by the heap HLL modes. static final byte[] toHllByteArray(final AbstractHllArray impl, final boolean compact) { int auxBytes = 0; if (impl.tgtHllType == TgtHllType.HLL_4) { final AuxHashMap auxHashMap = impl.getAuxHashMap(); if (auxHashMap != null) { auxBytes = (compact) ? auxHashMap.getCompactSizeBytes() : auxHashMap.getUpdatableSizeBytes(); } else { auxBytes = (compact) ? 0 : 4 << LG_AUX_ARR_INTS[impl.lgConfigK]; } } final int totBytes = HLL_BYTE_ARR_START + impl.getHllByteArrBytes() + auxBytes; final byte[] byteArr = new byte[totBytes]; final WritableMemory wmem = WritableMemory.writableWrap(byteArr); insertHll(impl, wmem, compact); return byteArr; } private static final void insertHll(final AbstractHllArray impl, final WritableMemory wmem, final boolean compact) { insertCommonHll(impl, wmem, compact); final byte[] hllByteArr = ((HllArray)impl).hllByteArr; wmem.putByteArray(HLL_BYTE_ARR_START, hllByteArr, 0, hllByteArr.length); if (impl.getAuxHashMap() != null) { insertAux(impl, wmem, compact); } else { wmem.putInt(AUX_COUNT_INT, 0); } } private static final void insertCommonHll(final AbstractHllArray srcImpl, final WritableMemory tgtWmem, final boolean compact) { insertPreInts(tgtWmem, srcImpl.getPreInts()); insertSerVer(tgtWmem); insertFamilyId(tgtWmem); insertLgK(tgtWmem, srcImpl.getLgConfigK()); insertEmptyFlag(tgtWmem, srcImpl.isEmpty()); insertCompactFlag(tgtWmem, compact); insertOooFlag(tgtWmem, srcImpl.isOutOfOrder()); insertCurMin(tgtWmem, srcImpl.getCurMin()); insertCurMode(tgtWmem, srcImpl.getCurMode()); insertTgtHllType(tgtWmem, srcImpl.getTgtHllType()); insertHipAccum(tgtWmem, srcImpl.getHipAccum()); insertKxQ0(tgtWmem, srcImpl.getKxQ0()); insertKxQ1(tgtWmem, srcImpl.getKxQ1()); insertNumAtCurMin(tgtWmem, srcImpl.getNumAtCurMin()); insertRebuildCurMinNumKxQFlag(tgtWmem, srcImpl.isRebuildCurMinNumKxQFlag()); } private static final void insertAux(final AbstractHllArray srcImpl, final WritableMemory tgtWmem, final boolean tgtCompact) { final AuxHashMap auxHashMap = srcImpl.getAuxHashMap(); final int auxCount = auxHashMap.getAuxCount(); insertAuxCount(tgtWmem, auxCount); insertLgArr(tgtWmem, auxHashMap.getLgAuxArrInts()); //only used for direct HLL final long auxStart = srcImpl.auxStart; if (tgtCompact) { final PairIterator itr = auxHashMap.getIterator(); int cnt = 0; while (itr.nextValid()) { //works whether src has compact memory or not insertInt(tgtWmem, auxStart + (cnt++ << 2), itr.getPair()); } assert cnt == auxCount; } else { //updatable final int auxInts = 1 << auxHashMap.getLgAuxArrInts(); final int[] auxArr = auxHashMap.getAuxIntArr(); tgtWmem.putIntArray(auxStart, auxArr, 0, auxInts); } } //To byte array for coupons static final byte[] toCouponByteArray(final AbstractCoupons impl, final boolean dstCompact) { final int srcCouponCount = impl.getCouponCount(); final int srcLgCouponArrInts = impl.getLgCouponArrInts(); final int srcCouponArrInts = 1 << srcLgCouponArrInts; final byte[] byteArrOut; final boolean list = impl.getCurMode() == CurMode.LIST; //prepare switch final int sw = (impl.isMemory() ? 0 : 4) | (impl.isCompact() ? 0 : 2) | (dstCompact ? 0 : 1); switch (sw) { case 0: { //Src Memory, Src Compact, Dst Compact final Memory srcMem = impl.getMemory(); final int bytesOut = impl.getMemDataStart() + (srcCouponCount << 2); byteArrOut = new byte[bytesOut]; srcMem.getByteArray(0, byteArrOut, 0, bytesOut); break; } case 1: { //Src Memory, Src Compact, Dst Updatable final int dataStart = impl.getMemDataStart(); final int bytesOut = dataStart + (srcCouponArrInts << 2); byteArrOut = new byte[bytesOut]; final WritableMemory memOut = WritableMemory.writableWrap(byteArrOut); copyCommonListAndSet(impl, memOut); insertCompactFlag(memOut, dstCompact); final int[] tgtCouponIntArr = new int[srcCouponArrInts]; final PairIterator itr = impl.iterator(); while (itr.nextValid()) { final int pair = itr.getPair(); final int idx = find(tgtCouponIntArr, srcLgCouponArrInts, pair); if (idx < 0) { //found EMPTY tgtCouponIntArr[~idx] = pair; //insert continue; } throw new SketchesStateException("Error: found duplicate."); } memOut.putIntArray(dataStart, tgtCouponIntArr, 0, srcCouponArrInts); if (list) { insertListCount(memOut, srcCouponCount); } else { insertHashSetCount(memOut, srcCouponCount); } break; } case 6: //Src Heap, Src Updatable, Dst Compact case 2: { //Src Memory, Src Updatable, Dst Compact final int dataStart = impl.getMemDataStart(); final int bytesOut = dataStart + (srcCouponCount << 2); byteArrOut = new byte[bytesOut]; final WritableMemory memOut = WritableMemory.writableWrap(byteArrOut); copyCommonListAndSet(impl, memOut); insertCompactFlag(memOut, dstCompact); final PairIterator itr = impl.iterator(); int cnt = 0; while (itr.nextValid()) { insertInt(memOut, dataStart + (cnt++ << 2), itr.getPair()); } if (list) { insertListCount(memOut, srcCouponCount); } else { insertHashSetCount(memOut, srcCouponCount); } break; } case 3: { //Src Memory, Src Updatable, Dst Updatable final Memory srcMem = impl.getMemory(); final int bytesOut = impl.getMemDataStart() + (srcCouponArrInts << 2); byteArrOut = new byte[bytesOut]; srcMem.getByteArray(0, byteArrOut, 0, bytesOut); break; } case 7: { //Src Heap, Src Updatable, Dst Updatable final int dataStart = impl.getMemDataStart(); final int bytesOut = dataStart + (srcCouponArrInts << 2); byteArrOut = new byte[bytesOut]; final WritableMemory memOut = WritableMemory.writableWrap(byteArrOut); copyCommonListAndSet(impl, memOut); memOut.putIntArray(dataStart, impl.getCouponIntArr(), 0, srcCouponArrInts); if (list) { insertListCount(memOut, srcCouponCount); } else { insertHashSetCount(memOut, srcCouponCount); } break; } default: throw new SketchesStateException("Corruption, should not happen: " + sw); } return byteArrOut; } private static final void copyCommonListAndSet(final AbstractCoupons impl, final WritableMemory wmem) { insertPreInts(wmem, impl.getPreInts()); insertSerVer(wmem); insertFamilyId(wmem); insertLgK(wmem, impl.getLgConfigK()); insertLgArr(wmem, impl.getLgCouponArrInts()); insertEmptyFlag(wmem, impl.isEmpty()); insertOooFlag(wmem, impl.isOutOfOrder()); insertCurMode(wmem, impl.getCurMode()); insertTgtHllType(wmem, impl.getTgtHllType()); } }
2,534
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/HllPairIterator.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.HllUtil.EMPTY; import static org.apache.datasketches.hll.HllUtil.pair; /** * Iterates over an on-heap HLL byte array producing pairs of index, value. * * @author Lee Rhodes */ abstract class HllPairIterator extends PairIterator { final int lengthPairs; int index; int value; //Used by Direct<4,6,8>Array, Heap<4,6,8>Array HllPairIterator(final int lengthPairs) { this.lengthPairs = lengthPairs; index = - 1; } @Override public String getHeader() { return String.format("%10s%6s", "Slot", "Value"); } @Override public int getIndex() { return index; } @Override public int getKey() { return index; } @Override public int getPair() { return pair(index, value); } @Override public int getSlot() { return index; } @Override public String getString() { final int slot = getSlot(); final int value = getValue(); return String.format("%10d%6d", slot, value); } @Override public int getValue() { return value; } @Override public boolean nextAll() { if (++index < lengthPairs) { value = value(); return true; } return false; } @Override public boolean nextValid() { while (++index < lengthPairs) { value = value(); if (value != EMPTY) { return true; } } return false; } abstract int value(); }
2,535
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/AuxHashMap.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import org.apache.datasketches.common.SketchesStateException; /** * @author Lee Rhodes */ interface AuxHashMap { AuxHashMap copy(); int getAuxCount(); int[] getAuxIntArr(); int getCompactSizeBytes(); PairIterator getIterator(); int getLgAuxArrInts(); int getUpdatableSizeBytes(); boolean isMemory(); boolean isOffHeap(); /** * Adds the slotNo and value to the aux array. * @param slotNo the index from the HLL array * @param value the HLL value at the slotNo. * @throws SketchesStateException if this slotNo already exists in the aux array. */ void mustAdd(int slotNo, int value); /** * Returns value given slotNo. If this fails an exception is thrown. * @param slotNo the index from the HLL array * @return value the HLL value at the slotNo * @throws SketchesStateException if valid slotNo and value is not found. */ int mustFindValueFor(int slotNo); /** * Replaces the entry at slotNo with the given value. * @param slotNo the index from the HLL array * @param value the HLL value at the slotNo * @throws SketchesStateException if a valid slotNo, value is not found. */ void mustReplace(int slotNo, int value); }
2,536
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/Hll4Update.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.HllUtil.AUX_TOKEN; import static org.apache.datasketches.hll.HllUtil.LG_AUX_ARR_INTS; import org.apache.datasketches.common.SketchesStateException; /** * Update process common to Heap Hll 4 and Direct Hll 4 * @author Lee Rhodes * @author Kevin Lang */ class Hll4Update { //Uses lgConfigK, curMin, numAtCurMin, auxMap //Only called by Hll4Array and DirectHll4Array //In C: two-registers.c Line 836 in "hhb_abstract_set_slot_if_new_value_bigger" non-sparse static final void internalHll4Update(final AbstractHllArray host, final int slotNo, final int newValue) { assert ((0 <= slotNo) && (slotNo < (1 << host.getLgConfigK()))); final int curMin = host.getCurMin(); final int rawStoredOldNibble = host.getNibble(slotNo); //could be 0 final int lbOnOldValue = rawStoredOldNibble + curMin; //provable lower bound, could be 0 if (newValue <= lbOnOldValue) { return; } //Thus: newValue > lbOnOldValue AND newValue > curMin AuxHashMap auxHashMap; // = host.getAuxHashMap(); final int actualOldValue; final int shiftedNewValue; //value - curMin //Based on whether we have an AUX_TOKEN and whether the shiftedNewValue is greater than // AUX_TOKEN, we have four cases for how to actually modify the data structure: // 1. (shiftedNewValue >= AUX_TOKEN) && (rawStoredOldNibble = AUX_TOKEN) //881: // The byte array already contains aux token // This is the case where old and new values are both exceptions. // Therefore, the 4-bit array already is AUX_TOKEN. Only need to update auxMap // 2. (shiftedNewValue < AUX_TOKEN) && (rawStoredOldNibble = AUX_TOKEN) //885 // This is the (hypothetical) case where old value is an exception and the new one is not, // which is impossible given that curMin has not changed here and the newValue > oldValue. // 3. (shiftedNewValue >= AUX_TOKEN) && (rawStoredOldNibble < AUX_TOKEN) //892 // This is the case where the old value is not an exception and the new value is. // Therefore the AUX_TOKEN must be stored in the 4-bit array and the new value // added to the exception table. // 4. (shiftedNewValue < AUX_TOKEN) && (rawStoredOldNibble < AUX_TOKEN) //897 // This is the case where neither the old value nor the new value is an exception. // Therefore we just overwrite the 4-bit array with the shifted new value. if (rawStoredOldNibble == AUX_TOKEN) { //846 Note: This is rare and really hard to test! auxHashMap = host.getAuxHashMap(); //auxHashMap must already exist. assert auxHashMap != null; actualOldValue = auxHashMap.mustFindValueFor(slotNo);//lgtm [java/dereferenced-value-may-be-null] if (newValue <= actualOldValue) { return; } //We know that the array will be changed, but we haven't actually updated yet. AbstractHllArray.hipAndKxQIncrementalUpdate(host, actualOldValue, newValue); shiftedNewValue = newValue - curMin; assert (shiftedNewValue >= 0); if (shiftedNewValue >= AUX_TOKEN) { //CASE 1: auxHashMap.mustReplace(slotNo, newValue); //lgtm [java/dereferenced-value-may-be-null] } //else //CASE 2: impossible } else { //rawStoredOldNibble < AUX_TOKEN actualOldValue = lbOnOldValue; //We know that the array will be changed, but we haven't actually updated yet. AbstractHllArray.hipAndKxQIncrementalUpdate(host, actualOldValue, newValue); shiftedNewValue = newValue - curMin; assert (shiftedNewValue >= 0); if (shiftedNewValue >= AUX_TOKEN) { //CASE 3: //892 host.putNibble(slotNo, AUX_TOKEN); auxHashMap = host.getAuxHashMap(); if (auxHashMap == null) { auxHashMap = host.getNewAuxHashMap(); host.putAuxHashMap(auxHashMap, false); } auxHashMap.mustAdd(slotNo, newValue); } else { // CASE 4: //897 host.putNibble(slotNo, shiftedNewValue); } } // We just changed the HLL array, so it might be time to change curMin if (actualOldValue == curMin) { //908 assert (host.getNumAtCurMin() >= 1); host.decNumAtCurMin(); while (host.getNumAtCurMin() == 0) { //increases curMin by 1, and builds a new aux table, // shifts values in 4-bit table, and recounts curMin shiftToBiggerCurMin(host); } } } //This scheme only works with two double registers (2 kxq values). // HipAccum, kxq0 and kxq1 remain untouched. // This changes curMin, numAtCurMin, hllByteArr and auxMap. //Entering this routine assumes that all slots have valid nibbles > 0 and <= 15. //An AuxHashMap must exist if any values in the current hllByteArray are already 15. //In C: again-two-registers.c Lines 710 "hhb_shift_to_bigger_curmin" private static final void shiftToBiggerCurMin(final AbstractHllArray host) { final int oldCurMin = host.getCurMin(); final int newCurMin = oldCurMin + 1; final int lgConfigK = host.getLgConfigK(); final int configK = 1 << lgConfigK; final int configKmask = configK - 1; int numAtNewCurMin = 0; int numAuxTokens = 0; // Walk through the slots of 4-bit array decrementing stored values by one unless it // equals AUX_TOKEN, where it is left alone but counted to be checked later. // If oldStoredValue is 0 it is an error. // If the decremented value is 0, we increment numAtNewCurMin. // Because getNibble is masked to 4 bits oldStoredValue can never be > 15 or negative for (int i = 0; i < configK; i++) { //724 int oldStoredNibble = host.getNibble(i); if (oldStoredNibble == 0) { throw new SketchesStateException("Array slots cannot be 0 at this point."); } if (oldStoredNibble < AUX_TOKEN) { host.putNibble(i, --oldStoredNibble); if (oldStoredNibble == 0) { numAtNewCurMin++; } } else { //oldStoredNibble == AUX_TOKEN numAuxTokens++; assert host.getAuxHashMap() != null : "AuxHashMap cannot be null at this point."; } } //If old AuxHashMap exists, walk through it updating some slots and build a new AuxHashMap // if needed. AuxHashMap newAuxMap = null; final AuxHashMap oldAuxMap = host.getAuxHashMap(); if (oldAuxMap != null) { int slotNum; int oldActualVal; int newShiftedVal; final PairIterator itr = oldAuxMap.getIterator(); while (itr.nextValid()) { slotNum = itr.getKey() & configKmask; oldActualVal = itr.getValue(); newShiftedVal = oldActualVal - newCurMin; assert newShiftedVal >= 0; assert host.getNibble(slotNum) == AUX_TOKEN : "Array slot != AUX_TOKEN: " + host.getNibble(slotNum); if (newShiftedVal < AUX_TOKEN) { //756 assert (newShiftedVal == 14); // The former exception value isn't one anymore, so it stays out of new AuxHashMap. // Correct the AUX_TOKEN value in the HLL array to the newShiftedVal (14). host.putNibble(slotNum, newShiftedVal); numAuxTokens--; } else { //newShiftedVal >= AUX_TOKEN // the former exception remains an exception, so must be added to the newAuxMap if (newAuxMap == null) { //Note: even in the direct case we use a heap aux map temporarily newAuxMap = new HeapAuxHashMap(LG_AUX_ARR_INTS[lgConfigK], lgConfigK); } newAuxMap.mustAdd(slotNum, oldActualVal); } } //end scan of oldAuxMap } //end if (auxHashMap != null) else { //oldAuxMap == null assert numAuxTokens == 0 : "auxTokens: " + numAuxTokens; } if (newAuxMap != null) { assert newAuxMap.getAuxCount() == numAuxTokens : "auxCount: " + newAuxMap.getAuxCount() + ", HLL tokens: " + numAuxTokens; } host.putAuxHashMap(newAuxMap, false); //if we are direct, this will do the right thing host.putCurMin(newCurMin); host.putNumAtCurMin(numAtNewCurMin); } //end of shiftToBiggerCurMin }
2,537
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/PreambleUtil.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.common.Util.ceilingIntPowerOf2; import static org.apache.datasketches.common.Util.exactLog2OfLong; import static org.apache.datasketches.common.Util.zeroPad; import static org.apache.datasketches.hll.HllUtil.LG_AUX_ARR_INTS; import static org.apache.datasketches.hll.HllUtil.LG_INIT_SET_SIZE; import static org.apache.datasketches.hll.HllUtil.RESIZE_DENOM; import static org.apache.datasketches.hll.HllUtil.RESIZE_NUMER; import java.nio.ByteOrder; import org.apache.datasketches.common.Family; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.memory.WritableMemory; //@formatter:off /** * <pre> * CouponList Layout * Long || Start Byte Adr, Big Endian Illustration * Adr: * || 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | * 0 || Mode | ListCnt| Flags | LgArr | lgK | FamID | SerVer | PI=2 | * * || 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | * 1 || |------Coupon Int List Start--------| * </pre> * * <pre> * CouponHashSet Layout * Long || Start Byte Adr, Big Endian Illustration * Adr: * || 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | * 0 || Mode | | Flags | LgArr | lgK | FamID | SerVer | PI=3 | * * || 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | * 1 ||-----Coupon Int Hash Set Start-----|---------Hash Set Count------------| * </pre> * * <pre> * HllArray Layout * Long || Start Byte Adr, Big Endian Illustration * Adr: * || 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | * 0 || Mode | CurMin | Flags | LgArr | lgK | FamID | SerVer | PI=10 | * * || 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | * 1 ||-------------------------------HIP Accum-------------------------------| * * || 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | * 2 ||----------------------------------KxQ0---------------------------------| * * || 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | * 3 ||----------------------------------KxQ1---------------------------------| * * || 39 | 38 | 37 | 36 | 35 | 34 | 33 | 32 | * 4 ||-------------Aux Count-------------|----------Num At Cur Min-----------| * * || 47 | 46 | 45 | 44 | 43 | 42 | 41 | 40 | * 5 ||...................................|------Start of HLL_X Byte Array----| * * N ||----End of Byte Array for HLL_4----|...................................| * N+1 ||...................................|-----Start of Aux Array for HLL_4--| * </pre> * If in compact form exceptions array will be compacted. * * @author Lee Rhodes */ final class PreambleUtil { private PreambleUtil() {} private static final String LS = System.getProperty("line.separator"); // ###### DO NOT MESS WITH THIS ... // Preamble byte start addresses // First 8 Bytes: static int PREAMBLE_INTS_BYTE = 0; static int SER_VER_BYTE = 1; static int FAMILY_BYTE = 2; static int LG_K_BYTE = 3; static int LG_ARR_BYTE = 4; //used for LIST, SET & HLL_4 static int FLAGS_BYTE = 5; static int LIST_COUNT_BYTE = 6; static int HLL_CUR_MIN_BYTE = 6; static int MODE_BYTE = 7; //lo2bits = curMode, next 2 bits = tgtHllType //mode encoding of combined CurMode and TgtHllType: // Dec Lo4Bits TgtHllType, CurMode // 0 0000 HLL_4, LIST // 1 0001 HLL_4, SET // 2 0010 HLL_4, HLL // 4 0100 HLL_6, LIST // 5 0101 HLL_6, SET // 6 0110 HLL_6, HLL // 8 1000 HLL_8, LIST // 9 1001 HLL_8, SET // 10 1010 HLL_8, HLL //Coupon List static int LIST_INT_ARR_START = 8; //Coupon Hash Set static int HASH_SET_COUNT_INT = 8; static int HASH_SET_INT_ARR_START = 12; //HLL static int HIP_ACCUM_DOUBLE = 8; static int KXQ0_DOUBLE = 16; static int KXQ1_DOUBLE = 24; static int CUR_MIN_COUNT_INT = 32; static int AUX_COUNT_INT = 36; static int HLL_BYTE_ARR_START = 40; //Flag bit masks static final int BIG_ENDIAN_FLAG_MASK = 1; //Set but not read. Reserved. static final int READ_ONLY_FLAG_MASK = 2; //Set but not read. Reserved. static final int EMPTY_FLAG_MASK = 4; static final int COMPACT_FLAG_MASK = 8; static final int OUT_OF_ORDER_FLAG_MASK = 16; static final int REBUILD_CURMIN_NUM_KXQ_MASK = 32; //used only by Union //Mode byte masks static final int CUR_MODE_MASK = 3; static final int TGT_HLL_TYPE_MASK = 12; //Other constants static final int SER_VER = 1; static final int FAMILY_ID = 7; static final int LIST_PREINTS = 2; static final int HASH_SET_PREINTS = 3; static final int HLL_PREINTS = 10; static final boolean NATIVE_ORDER_IS_BIG_ENDIAN = (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN); static String toString(final byte[] byteArr) { final Memory mem = Memory.wrap(byteArr); return toString(mem); } static String toString(final Memory mem) { //First 8 bytes final int preInts = mem.getByte(PREAMBLE_INTS_BYTE); final int serVer = mem.getByte(SER_VER_BYTE); final Family family = Family.idToFamily(mem.getByte(FAMILY_BYTE)); final int lgK = mem.getByte(LG_K_BYTE); final int lgArr = mem.getByte(LG_ARR_BYTE); final int flags = mem.getByte(FLAGS_BYTE); //Flags final String flagsStr = zeroPad(Integer.toBinaryString(flags), 8) + ", " + (flags); final boolean bigEndian = (flags & BIG_ENDIAN_FLAG_MASK) > 0; final String nativeOrder = ByteOrder.nativeOrder().toString(); final boolean compact = (flags & COMPACT_FLAG_MASK) > 0; final boolean oooFlag = (flags & OUT_OF_ORDER_FLAG_MASK) > 0; final boolean readOnly = (flags & READ_ONLY_FLAG_MASK) > 0; final boolean empty = (flags & EMPTY_FLAG_MASK) > 0; final boolean rebuildKxQ = (flags & REBUILD_CURMIN_NUM_KXQ_MASK) > 0; final int hllCurMin = mem.getByte(HLL_CUR_MIN_BYTE); final int listCount = hllCurMin; final int modeByte = mem.getByte(MODE_BYTE); final CurMode curMode = CurMode.fromOrdinal(modeByte & 3); final TgtHllType tgtHllType = TgtHllType.fromOrdinal((modeByte >>> 2) & 3); double hipAccum = 0; double kxq0 = 0; double kxq1 = 0; int hashSetCount = 0; int curMinCount = 0; int exceptionCount = 0; if (curMode == CurMode.SET) { hashSetCount = mem.getInt(HASH_SET_COUNT_INT); } else if (curMode == CurMode.HLL) { hipAccum = mem.getDouble(HIP_ACCUM_DOUBLE); kxq0 = mem.getDouble(KXQ0_DOUBLE); kxq1 = mem.getDouble(KXQ1_DOUBLE); curMinCount = mem.getInt(CUR_MIN_COUNT_INT); exceptionCount = mem.getInt(AUX_COUNT_INT); } final StringBuilder sb = new StringBuilder(); sb.append(LS); sb.append("### HLL SKETCH PREAMBLE:").append(LS); sb.append("Byte 0: Preamble Ints : ").append(preInts).append(LS); sb.append("Byte 1: SerVer : ").append(serVer).append(LS); sb.append("Byte 2: Family : ").append(family).append(LS); sb.append("Byte 3: lgK : ").append(lgK).append(LS); //expand byte 4: LgArr if (curMode == CurMode.LIST) { sb.append("Byte 4: LgArr: List Arr : ").append(lgArr).append(LS); } if (curMode == CurMode.SET) { sb.append("Byte 4: LgArr: Hash Set Arr : ").append(lgArr).append(LS); } if (curMode == CurMode.HLL) { sb.append("Byte 4: LgArr or Aux LgArr : ").append(lgArr).append(LS); } //expand byte 5: Flags sb.append("Byte 5: Flags: : ").append(flagsStr).append(LS); sb.append(" BIG_ENDIAN_STORAGE : ").append(bigEndian).append(LS); sb.append(" (Native Byte Order) : ").append(nativeOrder).append(LS); sb.append(" READ_ONLY : ").append(readOnly).append(LS); sb.append(" EMPTY : ").append(empty).append(LS); sb.append(" COMPACT : ").append(compact).append(LS); sb.append(" OUT_OF_ORDER : ").append(oooFlag).append(LS); sb.append(" REBUILD_KXQ : ").append(rebuildKxQ).append(LS); //expand byte 6: ListCount, CurMin if (curMode == CurMode.LIST) { sb.append("Byte 6: List Count/CurMin : ").append(listCount).append(LS); } if (curMode == CurMode.SET) { sb.append("Byte 6: (not used) : ").append(LS); } if (curMode == CurMode.HLL) { sb.append("Byte 6: Cur Min : ").append(hllCurMin).append(LS); } final String modes = curMode.toString() + ", " + tgtHllType.toString(); sb.append("Byte 7: Mode : ").append(modes).append(LS); if (curMode == CurMode.SET) { sb.append("Hash Set Count : ").append(hashSetCount).append(LS); } if (curMode == CurMode.HLL) { sb.append("HIP Accum : ").append(hipAccum).append(LS); sb.append("KxQ0 : ").append(kxq0).append(LS); sb.append("KxQ1 : ").append(kxq1).append(LS); sb.append("Num At Cur Min : ").append(curMinCount).append(LS); sb.append("Aux Count : ").append(exceptionCount).append(LS); } sb.append("### END HLL SKETCH PREAMBLE").append(LS); return sb.toString(); } //@formatter:on static int extractPreInts(final Memory mem) { return mem.getByte(PREAMBLE_INTS_BYTE) & 0X3F; } static void insertPreInts(final WritableMemory wmem, final int preInts) { wmem.putByte(PREAMBLE_INTS_BYTE, (byte) (preInts & 0X3F)); } static int extractSerVer(final Memory mem) { return mem.getByte(SER_VER_BYTE) & 0XFF; } static void insertSerVer(final WritableMemory wmem) { wmem.putByte(SER_VER_BYTE, (byte) SER_VER); } static int extractFamilyId(final Memory mem) { return mem.getByte(FAMILY_BYTE) & 0XFF; } static void insertFamilyId(final WritableMemory wmem) { wmem.putByte(FAMILY_BYTE, (byte) FAMILY_ID); } static int extractLgK(final Memory mem) { return mem.getByte(LG_K_BYTE) & 0XFF; } static void insertLgK(final WritableMemory wmem, final int lgK) { wmem.putByte(LG_K_BYTE, (byte) lgK); } static int extractLgArr(final Memory mem) { final int lgArr = mem.getByte(LG_ARR_BYTE) & 0XFF; return lgArr; } static void insertLgArr(final WritableMemory wmem, final int lgArr) { wmem.putByte(LG_ARR_BYTE, (byte) lgArr); } static int extractListCount(final Memory mem) { return mem.getByte(LIST_COUNT_BYTE) & 0XFF; } static void insertListCount(final WritableMemory wmem, final int listCnt) { wmem.putByte(LIST_COUNT_BYTE, (byte) listCnt); } static int extractCurMin(final Memory mem) { return mem.getByte(HLL_CUR_MIN_BYTE) & 0XFF; } static void insertCurMin(final WritableMemory wmem, final int curMin) { wmem.putByte(HLL_CUR_MIN_BYTE, (byte) curMin); } static double extractHipAccum(final Memory mem) { return mem.getDouble(HIP_ACCUM_DOUBLE); } static void insertHipAccum(final WritableMemory wmem, final double hipAccum) { wmem.putDouble(HIP_ACCUM_DOUBLE, hipAccum); } static double extractKxQ0(final Memory mem) { return mem.getDouble(KXQ0_DOUBLE); } static void insertKxQ0(final WritableMemory wmem, final double kxq0) { wmem.putDouble(KXQ0_DOUBLE, kxq0); } static double extractKxQ1(final Memory mem) { return mem.getDouble(KXQ1_DOUBLE); } static void insertKxQ1(final WritableMemory wmem, final double kxq1) { wmem.putDouble(KXQ1_DOUBLE, kxq1); } static int extractHashSetCount(final Memory mem) { return mem.getInt(HASH_SET_COUNT_INT); } static void insertHashSetCount(final WritableMemory wmem, final int hashSetCnt) { wmem.putInt(HASH_SET_COUNT_INT, hashSetCnt); } static int extractNumAtCurMin(final Memory mem) { return mem.getInt(CUR_MIN_COUNT_INT); } static void insertNumAtCurMin(final WritableMemory wmem, final int numAtCurMin) { wmem.putInt(CUR_MIN_COUNT_INT, numAtCurMin); } static int extractAuxCount(final Memory mem) { return mem.getInt(AUX_COUNT_INT); } static void insertAuxCount(final WritableMemory wmem, final int auxCount) { wmem.putInt(AUX_COUNT_INT, auxCount); } //Mode bits static void insertCurMode(final WritableMemory wmem, final CurMode curMode) { final int curModeId = curMode.ordinal(); int mode = wmem.getByte(MODE_BYTE) & ~CUR_MODE_MASK; //strip bits 0, 1 mode |= (curModeId & CUR_MODE_MASK); wmem.putByte(MODE_BYTE, (byte) mode); } static CurMode extractCurMode(final Memory mem) { final int curModeId = mem.getByte(MODE_BYTE) & CUR_MODE_MASK; return CurMode.fromOrdinal(curModeId); } static void insertTgtHllType(final WritableMemory wmem, final TgtHllType tgtHllType) { final int typeId = tgtHllType.ordinal(); int mode = wmem.getByte(MODE_BYTE) & ~TGT_HLL_TYPE_MASK; //strip bits 2, 3 mode |= (typeId << 2) & TGT_HLL_TYPE_MASK; wmem.putByte(MODE_BYTE, (byte) mode); } static TgtHllType extractTgtHllType(final Memory mem) { final int typeId = mem.getByte(MODE_BYTE) & TGT_HLL_TYPE_MASK; return TgtHllType.fromOrdinal(typeId >>> 2); } static void insertModes(final WritableMemory wmem, final TgtHllType tgtHllType, final CurMode curMode) { final int curModeId = curMode.ordinal() & 3; final int typeId = (tgtHllType.ordinal() & 3) << 2; final int mode = typeId | curModeId; wmem.putByte(MODE_BYTE, (byte) mode); } //Flags static void insertEmptyFlag(final WritableMemory wmem, final boolean empty) { int flags = wmem.getByte(FLAGS_BYTE); if (empty) { flags |= EMPTY_FLAG_MASK; } else { flags &= ~EMPTY_FLAG_MASK; } wmem.putByte(FLAGS_BYTE, (byte) flags); } static boolean extractEmptyFlag(final Memory mem) { final int flags = mem.getByte(FLAGS_BYTE); return (flags & EMPTY_FLAG_MASK) > 0; } static void insertCompactFlag(final WritableMemory wmem, final boolean compact) { int flags = wmem.getByte(FLAGS_BYTE); if (compact) { flags |= COMPACT_FLAG_MASK; } else { flags &= ~COMPACT_FLAG_MASK; } wmem.putByte(FLAGS_BYTE, (byte) flags); } static boolean extractCompactFlag(final Memory mem) { final int flags = mem.getByte(FLAGS_BYTE); return (flags & COMPACT_FLAG_MASK) > 0; } static void insertOooFlag(final WritableMemory wmem, final boolean oooFlag) { int flags = wmem.getByte(FLAGS_BYTE); if (oooFlag) { flags |= OUT_OF_ORDER_FLAG_MASK; } else { flags &= ~OUT_OF_ORDER_FLAG_MASK; } wmem.putByte(FLAGS_BYTE, (byte) flags); } static boolean extractOooFlag(final Memory mem) { final int flags = mem.getByte(FLAGS_BYTE); return (flags & OUT_OF_ORDER_FLAG_MASK) > 0; } static void insertRebuildCurMinNumKxQFlag(final WritableMemory wmem, final boolean rebuild) { int flags = wmem.getByte(FLAGS_BYTE); if (rebuild) { flags |= REBUILD_CURMIN_NUM_KXQ_MASK; } else { flags &= ~REBUILD_CURMIN_NUM_KXQ_MASK; } wmem.putByte(FLAGS_BYTE, (byte) flags); } static boolean extractRebuildCurMinNumKxQFlag(final Memory mem) { final int flags = mem.getByte(FLAGS_BYTE); return (flags & REBUILD_CURMIN_NUM_KXQ_MASK) > 0; } static void insertFlags(final WritableMemory wmem, final int flags) { wmem.putByte(FLAGS_BYTE, (byte) flags); } static int extractFlags(final Memory mem) { return mem.getByte(FLAGS_BYTE) & 0XFF; } //Other static int extractInt(final Memory mem, final long byteOffset) { return mem.getInt(byteOffset); } static void insertInt(final WritableMemory wmem, final long byteOffset, final int value) { wmem.putInt(byteOffset, value); } static int computeLgArr(final Memory mem, final int count, final int lgConfigK) { //value is missing, recompute final CurMode curMode = extractCurMode(mem); if (curMode == CurMode.LIST) { return HllUtil.LG_INIT_LIST_SIZE; } int ceilPwr2 = ceilingIntPowerOf2(count); if ((RESIZE_DENOM * count) > (RESIZE_NUMER * ceilPwr2)) { ceilPwr2 <<= 1; } if (curMode == CurMode.SET) { return Math.max(LG_INIT_SET_SIZE, exactLog2OfLong(ceilPwr2)); } //only used for HLL4 return Math.max(LG_AUX_ARR_INTS[lgConfigK], exactLog2OfLong(ceilPwr2)); } }
2,538
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/package-info.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /** * <h1>The DataSketches&trade; HLL sketch family package</h1> * {@link org.apache.datasketches.hll.HllSketch HllSketch} and {@link org.apache.datasketches.hll.Union Union} * are the public facing classes of this high performance implementation of Phillipe Flajolet's * HyperLogLog algorithm[1] but with significantly improved error behavior and important features that can be * essential for large production systems that must handle massive data. * * <h2>Key Features of the DataSketches&trade; HLL Sketch and its companion Union</h2> * * <h3>Advanced Estimation Algorithms for Optimum Accuracy</h3> * * <h4>Zero error at low cardinalities</h4> * The HLL sketch leverages highly compact arrays and hash tables to keep exact counts until the transition to * dense mode is required for space reasons. The result is perfect accuracy for very low cardinalities. * * <p>Accuracy for very small streams can be important because Big Data is often fragmented into millions of smaller * streams (or segments) that inevitably are power-law distributed in size. If you are sketching all these fragments, * as a general rule, more than 80% of your sketches will be very small, 20% will be much larger, and only a few very * large in cardinality. * * <h4>HIP / Martingale Estimator</h4> * When obtaining a cardinality estimate, the sketch automatically determines if it was the result of the capture of * a single stream, or if was the result of certain qualifying union operations. If this is the case the sketch will * take advantage of Edith Cohen's Historical Inverse Probability (HIP) estimation algorithm[2], which was * also independently developed by Daniel Ting as the Martingale estimation algorithm[3]. * This will result in a 20% improvement in accuracy over the standard Flajolet estimator. * If it is not a single stream or if the specific union operation did not qualify, * the estimator will default to the Composite Estimator. * * <h4>Composite Estimator</h4> * This advanced estimator is a blend of several algorithms including new algorithms developed by Kevin Lang for his * Compressed Probabilistic Counting (CPC) sketch[4]. These algorithms provide near optimal estimation accuracy * for cases that don't qualify for HIP / Martingale estimation. * * <p>As a result of all of this work on accuracy, one will get a very smooth curve of the underlying accuracy of the * sketch once the statistical randomness is removed through multiple trials. This can be observed in the * following graph.</p> * * <p><img src="doc-files/HLL_HIP_K12T20U20.png" width="500" alt="HLL Accuracy">[6]</p> * * <p>The above graph has 7 curves. At y = 0, is the median line that hugs the x-axis so closely that it can't be seen. * The two curves, just above and just below the x-axis, correspond to +/- 1 standard deviation (SD) of error. * The distance between either one of this pair and the x-axis is also known as the Relative Standard Error (RSE). * This type of graph for illustrating sketch error we call a "pitchfork plot".</p> * * <p>The next two curves above and below correspond to +/- 2 SD, and * the top-most and bottom-most curves correspond to +/- 3 SD. * The chart grid lines are set at +/- multiples of Relative Standard Error (RSE) that correspond to +/- 1,2,3 SD. * Below the cardinality of about 512 there is no error at all. This is the point where this particular * sketch transitions from sparse to dense (or estimation) mode.</p> * * <h3>Three HLL Types</h3> * This HLL implementation offers three different types of HLL sketch, each with different * trade-offs with accuracy, space and performance. These types are selected with the * {@link org.apache.datasketches.hll.TgtHllType TgtHllType} parameter. * * <p>In terms of accuracy, all three types, for the same <i>lgConfigK</i>, have the same error * distribution as a function of cardinality.</p> * * <p>The configuration parameter <i>lgConfigK</i> is the log-base-2 of <i>K</i>, * where <i>K</i> is the number of buckets or slots for the sketch. <i>lgConfigK</i> impacts both accuracy and * the size of the sketch in memory and when stored.</p> * * <h4>HLL 8</h4> * This uses an 8-bit byte per HLL bucket. It is generally the * fastest in terms of update time but has the largest storage footprint of about <i>K</i> bytes. * * <h4>HLL 6</h4> * This uses a 6-bit field per HLL bucket. It is the generally the next fastest * in terms of update time with a storage footprint of about <i>3/4 * K</i> bytes. * * <h4>HLL 4</h4> * This uses a 4-bit field per HLL bucket and for large counts may require * the use of a small internal auxiliary array for storing statistical exceptions, which are rare. * For the values of <i>lgConfigK &gt; 13</i> (<i>K</i> = 8192), * this additional array adds about 3% to the overall storage. It is generally the slowest in * terms of update time, but has the smallest storage footprint of about <i>K/2 * 1.03</i> bytes. * * <h3>Off-Heap Operation</h3> * This HLL sketch also offers the capability of operating off-heap. Given a <i>WritableMemory[5]</i> object * created by the user, the sketch will perform all of its updates and internal phase transitions * in that object, which can actually reside either on-heap or off-heap based on how it was * configured. In large systems that must update and union many millions of sketches, having the * sketch operate off-heap avoids the serialization and deserialization costs of moving sketches from heap to * off-heap and back, and reduces the need for garbage collection. * * <h3>Merging sketches with different configured <i>lgConfigK</i></h3> * This enables a user to union a HLL sketch that was configured with, say, <i>lgConfigK = 12</i> * with another loaded HLL sketch that was configured with, say, <i>lgConfigK = 14</i>. * * <p>Why is this important? Suppose you have been building a history of sketches of your customer's * data that go back a full year (or 5 or 10!) that were all configured with <i>lgConfigK = 12</i>. Because sketches * are so much smaller than the raw data it is possible that the raw data was discarded keeping only the sketches. * Even if you have the raw data, it might be very expensive and time consuming to reload and rebuild all your * sketches with a larger more accurate size, say, <i>lgConfigK = 14</i>. * This capability enables you to merge last year's data with this year's data built with larger sketches and still * have meaningful results.</p> * * <p>In other words, you can change your mind about what size sketch you need for your application at any time and * will not lose access to the data contained in your older historical sketches.</p> * * <p>This capability does come with a caveat: The resulting accuracy of the merged sketch will be the accuracy of the * smaller of the two sketches. Without this capability, you would either be stuck with the configuration you first * chose forever, or you would have to rebuild all your sketches from scratch, or worse, not be able to recover your * historical data.</p> * * <h3>Multi-language, multi-platform.</h3> * The binary structures for our sketch serializations are language and platform independent. * This means it is possible to generate an HLL sketch on a C++ Windows platform and it can be used on a * Java or Python Unix platform. * * <p>[1] Philippe Flajolet, et al, <a href="https://algo.inria.fr/flajolet/Publications/FlFuGaMe07.pdf"> <i>HyperLogLog: the analysis of a near-optimal cardinality estimation algorithm.</i></a> * DMTCS proc. <b>AH</b>, 2007, 127-146. * * <p>[2] Edith Cohen, <a href="https://arxiv.org/pdf/1306.3284.pdf"> <i>All-Distances Sketches, Revisited: HIP Estimators for Massive Graphs Analysis.</i></a> * PODS'14, June 22-27, Snowbird, UT, USA. * * <p>[3] Daniel Ting, * <a href="https://research.facebook.com/publications/streamed-approximate-counting-of-distinct-elements"> <i>Streamed Approximate Counting of Distinct Elements, Beating Optimal Batch Methods.</i></a> * KDD'14 August 24, 2014 New York, New York USA. * * <p>[4] Kevin Lang, * <a href="https://arxiv.org/abs/1708.06839"> <i>Back to the Future: an Even More Nearly Optimal Cardinality Estimation Algorithm.</i></a> * arXiv 1708.06839, August 22, 2017, Yahoo Research. * * <p>[5] Memory Component, * <a href="https://datasketches.apache.org/docs/Memory/MemoryComponent.html"> <i>DataSketches Memory Component</i></a> * * <p>[6] MacBook Pro 2.3 GHz 8-Core Intel Core i9 * * @see org.apache.datasketches.cpc.CpcSketch * * @author Lee Rhodes * @author Kevin Lang */ package org.apache.datasketches.hll;
2,539
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hll/DirectHll8Array.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hll; import static org.apache.datasketches.hll.HllUtil.VAL_MASK_6; import static org.apache.datasketches.hll.HllUtil.noWriteAccess; import static org.apache.datasketches.hll.PreambleUtil.HLL_BYTE_ARR_START; import org.apache.datasketches.common.SketchesStateException; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.memory.WritableMemory; /** * Uses 8 bits per slot in a byte array. * @author Lee Rhodes * @author Kevin Lang */ class DirectHll8Array extends DirectHllArray { //Called by HllSketch.writableWrap(), DirectCouponList.promoteListOrSetToHll DirectHll8Array(final int lgConfigK, final WritableMemory wmem) { super(lgConfigK, TgtHllType.HLL_8, wmem); } //Called by HllSketch.wrap(Memory) DirectHll8Array(final int lgConfigK, final Memory mem) { super(lgConfigK, TgtHllType.HLL_8, mem); } @Override HllSketchImpl copy() { return Hll8Array.heapify(mem); } @Override HllSketchImpl couponUpdate(final int coupon) { if (wmem == null) { noWriteAccess(); } final int newValue = HllUtil.getPairValue(coupon); final int configKmask = (1 << getLgConfigK()) - 1; final int slotNo = HllUtil.getPairLow26(coupon) & configKmask; updateSlotWithKxQ(slotNo, newValue); return this; } @Override int getHllByteArrBytes() { return hll8ArrBytes(lgConfigK); } @Override int getNibble(final int slotNo) { throw new SketchesStateException("Improper access."); } @Override final int getSlotValue(final int slotNo) { return mem.getByte(HLL_BYTE_ARR_START + slotNo) & VAL_MASK_6; } @Override PairIterator iterator() { return new DirectHll8Iterator(1 << lgConfigK); } @Override void putNibble(final int slotNo, final int nibValue) { throw new SketchesStateException("Improper access."); } @Override //Used by Union when source is not HLL8 final void updateSlotNoKxQ(final int slotNo, final int newValue) { final int oldValue = getSlotValue(slotNo); if (newValue > oldValue) { wmem.putByte(HLL_BYTE_ARR_START + slotNo, (byte) (newValue & VAL_MASK_6)); } } @Override //Used by this couponUpdate() //updates HipAccum, CurMin, NumAtCurMin, KxQs and checks newValue > oldValue final void updateSlotWithKxQ(final int slotNo, final int newValue) { final int oldValue = getSlotValue(slotNo); if (newValue > oldValue) { wmem.putByte(HLL_BYTE_ARR_START + slotNo, (byte) (newValue & VAL_MASK_6)); hipAndKxQIncrementalUpdate(this, oldValue, newValue); if (oldValue == 0) { decNumAtCurMin(); assert getNumAtCurMin() >= 0; } } } //ITERATOR final class DirectHll8Iterator extends HllPairIterator { DirectHll8Iterator(final int lengthPairs) { super(lengthPairs); } @Override int value() { final int tmp = mem.getByte(HLL_BYTE_ARR_START + index); return tmp & VAL_MASK_6; } } }
2,540
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hash/XxHash.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hash; import org.apache.datasketches.memory.Memory; /** * The XxHash is a fast, non-cryptographic, 64-bit hash function that has * excellent avalanche and 2-way bit independence properties. * * <p>This class wraps the * <a href="https://github.com/apache/datasketches-memory/blob/master/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/XxHash.java">Memory Component XxHash</a> * implementation. * * @author Lee Rhodes */ public class XxHash { /** * Compute the hash of the given Memory object. * @param mem The given Memory object * @param offsetBytes Starting at this offset in bytes * @param lengthBytes Continuing for this number of bytes * @param seed use this seed for the hash function * @return return the resulting 64-bit hash value. */ public static long hash(final Memory mem, final long offsetBytes, final long lengthBytes, final long seed) { return mem.xxHash64(offsetBytes, lengthBytes, seed); } /** * Returns a 64-bit hash. * @param in a long * @param seed A long valued seed. * @return the hash */ public static long hash(final long in, final long seed) { return org.apache.datasketches.memory.XxHash.hashLong(in, seed); } }
2,541
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hash/MurmurHash3.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hash; import java.io.Serializable; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Objects; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.common.Util; import org.apache.datasketches.memory.Memory; /** * <p> * The MurmurHash3 is a fast, non-cryptographic, 128-bit hash function that has * excellent avalanche and 2-way bit independence properties. * </p> * * <p> * Austin Appleby's C++ * <a href="https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp"> * MurmurHash3_x64_128(...), final revision 150</a>, * which is in the Public Domain, was the inspiration for this implementation in Java. * </p> * * <p> * This java implementation pays close attention to the C++ algorithms in order to * maintain bit-wise compatibility, but the design is quite different. This implementation has also * been extended to include processing of arrays of longs, char or ints, which was not part of the * original C++ implementation. This implementation produces the same exact output hash bits as * the above C++ method given the same input.</p> * * <p>In addition, with this implementation, the hash of byte[], char[], int[], or long[] will * produce the same hash result if, and only if, all the arrays have the same exact length in * bytes, and if the contents of the values in the arrays have the same byte endianness and * overall order. There is a unit test for this class that demonstrates this.</p> * * <p> * The structure of this implementation also reflects a separation of code that is dependent on the * input structure (in this case byte[], int[] or long[]) from code that is independent of the input * structure. This also makes the code more readable and suitable for future extensions. * </p> * * <p>Note that even though this hash function produces 128 bits, the entropy of the resulting hash cannot * be greater than the entropy of the input. For example, if the input is only a single long of 64 bits, * the entropy of the resulting 128 bit hash is no greater than 64 bits. * * @author Lee Rhodes */ public final class MurmurHash3 implements Serializable { private static final long serialVersionUID = 0L; private MurmurHash3() {} //--Hash of long--------------------------------------------------------- /** * Hash the given long. * * @param key The input long. * @param seed A long valued seed. * @return a 128-bit hash of the input as a long array of size 2. */ public static long[] hash(final long key, final long seed) { final HashState hashState = new HashState(seed, seed); return hashState.finalMix128(key, 0, Long.BYTES); } //--Hash of long[]------------------------------------------------------- /** * Hash the given long[] array. * * @param key The input long[] array. It must be non-null and non-empty. * @param seed A long valued seed. * @return a 128-bit hash of the input as a long array of size 2. */ public static long[] hash(final long[] key, final long seed) { return hash(key, 0, key.length, seed); } /** * Hash a portion of the given long[] array. * * @param key The input long[] array. It must be non-null and non-empty. * @param offsetLongs the starting offset in longs. * @param lengthLongs the length in longs of the portion of the array to be hashed. * @param seed A long valued seed. * @return a 128-bit hash of the input as a long array of size 2 */ public static long[] hash(final long[] key, final int offsetLongs, final int lengthLongs, final long seed) { Objects.requireNonNull(key); final int arrLen = key.length; checkPositive(arrLen); Util.checkBounds(offsetLongs, lengthLongs, arrLen); final HashState hashState = new HashState(seed, seed); // Number of full 128-bit blocks of 2 longs (the body). // Possible exclusion of a remainder of 1 long. final int nblocks = lengthLongs >>> 1; //longs / 2 // Process the 128-bit blocks (the body) into the hash for (int i = 0; i < nblocks; i++ ) { final long k1 = key[offsetLongs + (i << 1)]; //offsetLongs + 0, 2, 4, ... final long k2 = key[offsetLongs + (i << 1) + 1]; //offsetLongs + 1, 3, 5, ... hashState.blockMix128(k1, k2); } // Get the tail index wrt hashed portion, remainder length final int tail = nblocks << 1; // 2 longs / block final int rem = lengthLongs - tail; // remainder longs: 0,1 // Get the tail final long k1 = rem == 0 ? 0 : key[offsetLongs + tail]; //k2 -> 0 // Mix the tail into the hash and return return hashState.finalMix128(k1, 0, lengthLongs << 3); //convert to bytes } //--Hash of int[]-------------------------------------------------------- /** * Hash the given int[] array. * * @param key The input int[] array. It must be non-null and non-empty. * @param seed A long valued seed. * @return a 128-bit hash of the input as a long array of size 2. */ public static long[] hash(final int[] key, final long seed) { return hash(key, 0, key.length, seed); } /** * Hash a portion of the given int[] array. * * @param key The input int[] array. It must be non-null and non-empty. * @param offsetInts the starting offset in ints. * @param lengthInts the length in ints of the portion of the array to be hashed. * @param seed A long valued seed. * @return a 128-bit hash of the input as a long array of size 2. */ public static long[] hash(final int[] key, final int offsetInts, final int lengthInts, final long seed) { Objects.requireNonNull(key); final int arrLen = key.length; checkPositive(arrLen); Util.checkBounds(offsetInts, lengthInts, arrLen); final HashState hashState = new HashState(seed, seed); // Number of full 128-bit blocks of 4 ints. // Possible exclusion of a remainder of up to 3 ints. final int nblocks = lengthInts >>> 2; //ints / 4 // Process the 128-bit blocks (the body) into the hash for (int i = 0; i < nblocks; i++ ) { //4 ints per block final long k1 = getLong(key, offsetInts + (i << 2), 2); //offsetInts + 0, 4, 8, ... final long k2 = getLong(key, offsetInts + (i << 2) + 2, 2); //offsetInts + 2, 6, 10, ... hashState.blockMix128(k1, k2); } // Get the tail index wrt hashed portion, remainder length final int tail = nblocks << 2; // 4 ints per block final int rem = lengthInts - tail; // remainder ints: 0,1,2,3 // Get the tail final long k1; final long k2; if (rem > 2) { //k1 -> whole; k2 -> partial k1 = getLong(key, offsetInts + tail, 2); k2 = getLong(key, offsetInts + tail + 2, rem - 2); } else { //k1 -> whole(2), partial(1) or 0; k2 == 0 k1 = rem == 0 ? 0 : getLong(key, offsetInts + tail, rem); k2 = 0; } // Mix the tail into the hash and return return hashState.finalMix128(k1, k2, lengthInts << 2); //convert to bytes } //--Hash of char[]------------------------------------------------------- /** * Hash the given char[] array. * * @param key The input char[] array. It must be non-null and non-empty. * @param seed A long valued seed. * @return a 128-bit hash of the input as a long array of size 2 */ public static long[] hash(final char[] key, final long seed) { return hash(key, 0, key.length, seed); } /** * Hash a portion of the given char[] array. * * @param key The input char[] array. It must be non-null and non-empty. * @param offsetChars the starting offset in chars. * @param lengthChars the length in chars of the portion of the array to be hashed. * @param seed A long valued seed. * @return a 128-bit hash of the input as a long array of size 2 */ public static long[] hash(final char[] key, final int offsetChars, final int lengthChars, final long seed) { Objects.requireNonNull(key); final int arrLen = key.length; checkPositive(arrLen); Util.checkBounds(offsetChars, lengthChars, arrLen); final HashState hashState = new HashState(seed, seed); // Number of full 128-bit blocks of 8 chars. // Possible exclusion of a remainder of up to 7 chars. final int nblocks = lengthChars >>> 3; //chars / 8 // Process the 128-bit blocks (the body) into the hash for (int i = 0; i < nblocks; i++ ) { //8 chars per block final long k1 = getLong(key, offsetChars + (i << 3), 4); //offsetChars + 0, 8, 16, ... final long k2 = getLong(key, offsetChars + (i << 3) + 4, 4); //offsetChars + 4, 12, 20, ... hashState.blockMix128(k1, k2); } // Get the tail index wrt hashed portion, remainder length final int tail = nblocks << 3; // 8 chars per block final int rem = lengthChars - tail; // remainder chars: 0,1,2,3,4,5,6,7 // Get the tail final long k1; final long k2; if (rem > 4) { //k1 -> whole; k2 -> partial k1 = getLong(key, offsetChars + tail, 4); k2 = getLong(key, offsetChars + tail + 4, rem - 4); } else { //k1 -> whole, partial or 0; k2 == 0 k1 = rem == 0 ? 0 : getLong(key, offsetChars + tail, rem); k2 = 0; } // Mix the tail into the hash and return return hashState.finalMix128(k1, k2, lengthChars << 1); //convert to bytes } //--Hash of byte[]------------------------------------------------------- /** * Hash the given byte[] array. * * @param key The input byte[] array. It must be non-null and non-empty. * @param seed A long valued seed. * @return a 128-bit hash of the input as a long array of size 2. */ public static long[] hash(final byte[] key, final long seed) { return hash(key, 0, key.length, seed); } /** * Hash a portion of the given byte[] array. * * @param key The input byte[] array. It must be non-null and non-empty. * @param offsetBytes the starting offset in bytes. * @param lengthBytes the length in bytes of the portion of the array to be hashed. * @param seed A long valued seed. * @return a 128-bit hash of the input as a long array of size 2. */ public static long[] hash(final byte[] key, final int offsetBytes, final int lengthBytes, final long seed) { Objects.requireNonNull(key); final int arrLen = key.length; checkPositive(arrLen); Util.checkBounds(offsetBytes, lengthBytes, arrLen); final HashState hashState = new HashState(seed, seed); // Number of full 128-bit blocks of 16 bytes. // Possible exclusion of a remainder of up to 15 bytes. final int nblocks = lengthBytes >>> 4; //bytes / 16 // Process the 128-bit blocks (the body) into the hash for (int i = 0; i < nblocks; i++ ) { //16 bytes per block final long k1 = getLong(key, offsetBytes + (i << 4), 8); //offsetBytes + 0, 16, 32, ... final long k2 = getLong(key, offsetBytes + (i << 4) + 8, 8); //offsetBytes + 8, 24, 40, ... hashState.blockMix128(k1, k2); } // Get the tail index wrt hashed portion, remainder length final int tail = nblocks << 4; //16 bytes per block final int rem = lengthBytes - tail; // remainder bytes: 0,1,...,15 // Get the tail final long k1; final long k2; if (rem > 8) { //k1 -> whole; k2 -> partial k1 = getLong(key, offsetBytes + tail, 8); k2 = getLong(key, offsetBytes + tail + 8, rem - 8); } else { //k1 -> whole, partial or 0; k2 == 0 k1 = rem == 0 ? 0 : getLong(key, offsetBytes + tail, rem); k2 = 0; } // Mix the tail into the hash and return return hashState.finalMix128(k1, k2, lengthBytes); } //--Hash of ByteBuffer--------------------------------------------------- /** * Hash the remaining bytes of the given ByteBuffer starting at position(). * * @param buf The input ByteBuffer. It must be non-null and non-empty. * @param seed A long valued seed. * @return a 128-bit hash of the input as a long array of size 2. */ public static long[] hash(final ByteBuffer buf, final long seed) { Objects.requireNonNull(buf); final int pos = buf.position(); final int rem = buf.remaining(); checkPositive(rem); final Memory mem = Memory.wrap(buf, ByteOrder.LITTLE_ENDIAN).region(pos, rem); return hash(mem, seed); } //--Hash of Memory------------------------------------------------------- /** * Hash the given Memory. * * <p>Note: if you want to hash only a portion of Memory, convert it to the * appropriate Region first with ByteOrder = Little Endian. If it is not * Little Endian a new region view will be created as Little Endian. * This does not change the underlying data. * * @param mem The input Memory. It must be non-null and non-empty. * @param seed A long valued seed. * @return a 128-bit hash of the input as a long array of size 2. */ public static long[] hash(final Memory mem, final long seed) { Objects.requireNonNull(mem); final long lengthBytes = mem.getCapacity(); checkPositive(lengthBytes); final Memory memLE = mem.getTypeByteOrder() == ByteOrder.LITTLE_ENDIAN ? mem : mem.region(0, lengthBytes, ByteOrder.LITTLE_ENDIAN); final HashState hashState = new HashState(seed, seed); // Number of full 128-bit blocks of 16 bytes. // Possible exclusion of a remainder of up to 15 bytes. final long nblocks = lengthBytes >>> 4; //bytes / 16 // Process the 128-bit blocks (the body) into the hash for (long i = 0; i < nblocks; i++ ) { //16 bytes per block final long k1 = memLE.getLong(i << 4); //0, 16, 32, ... final long k2 = memLE.getLong((i << 4) + 8); //8, 24, 40, ... hashState.blockMix128(k1, k2); } // Get the tail index wrt hashed portion, remainder length final long tail = nblocks << 4; //16 bytes per block final int rem = (int)(lengthBytes - tail); // remainder bytes: 0,1,...,15 // Get the tail final long k1; final long k2; if (rem > 8) { //k1 -> whole; k2 -> partial k1 = memLE.getLong(tail); k2 = getLong(memLE, tail + 8, rem - 8); } else { //k1 -> whole, partial or 0; k2 == 0 k1 = rem == 0 ? 0 : getLong(memLE, tail, rem); k2 = 0; } // Mix the tail into the hash and return return hashState.finalMix128(k1, k2, lengthBytes); } //--HashState class------------------------------------------------------ /** * Common processing of the 128-bit hash state independent of input type. */ private static final class HashState { private static final long C1 = 0x87c37b91114253d5L; private static final long C2 = 0x4cf5ad432745937fL; private long h1; private long h2; HashState(final long h1, final long h2) { this.h1 = h1; this.h2 = h2; } /** * Block mix (128-bit block) of input key to internal hash state. * * @param k1 intermediate mix value * @param k2 intermediate mix value */ void blockMix128(final long k1, final long k2) { h1 ^= mixK1(k1); h1 = Long.rotateLeft(h1, 27); h1 += h2; h1 = h1 * 5 + 0x52dce729; h2 ^= mixK2(k2); h2 = Long.rotateLeft(h2, 31); h2 += h1; h2 = h2 * 5 + 0x38495ab5; } long[] finalMix128(final long k1, final long k2, final long inputLengthBytes) { h1 ^= mixK1(k1); h2 ^= mixK2(k2); h1 ^= inputLengthBytes; h2 ^= inputLengthBytes; h1 += h2; h2 += h1; h1 = finalMix64(h1); h2 = finalMix64(h2); h1 += h2; h2 += h1; return new long[] { h1, h2 }; } /** * Final self mix of h*. * * @param h input to final mix * @return mix */ private static long finalMix64(long h) { h ^= h >>> 33; h *= 0xff51afd7ed558ccdL; h ^= h >>> 33; h *= 0xc4ceb9fe1a85ec53L; h ^= h >>> 33; return h; } /** * Self mix of k1 * * @param k1 input argument * @return mix */ private static long mixK1(long k1) { k1 *= C1; k1 = Long.rotateLeft(k1, 31); k1 *= C2; return k1; } /** * Self mix of k2 * * @param k2 input argument * @return mix */ private static long mixK2(long k2) { k2 *= C2; k2 = Long.rotateLeft(k2, 33); k2 *= C1; return k2; } } //--Helper methods------------------------------------------------------- /** * Gets a long from the given int array starting at the given int array index and continuing for * remainder (rem) integers. The integers are extracted in little-endian order. There is no limit * checking. * * @param intArr The given input int array. * @param index Zero-based index from the start of the int array. * @param rem Remainder integers. An integer in the range [1,2]. * @return long */ private static long getLong(final int[] intArr, final int index, final int rem) { long out = 0L; for (int i = rem; i-- > 0;) { //i= 1,0 final int v = intArr[index + i]; out ^= (v & 0xFFFFFFFFL) << i * 32; //equivalent to |= } return out; } /** * Gets a long from the given char array starting at the given char array index and continuing for * remainder (rem) chars. The chars are extracted in little-endian order. There is no limit * checking. * * @param charArr The given input char array. * @param index Zero-based index from the start of the char array. * @param rem Remainder chars. An integer in the range [1,4]. * @return a long */ private static long getLong(final char[] charArr, final int index, final int rem) { long out = 0L; for (int i = rem; i-- > 0;) { //i= 3,2,1,0 final char c = charArr[index + i]; out ^= (c & 0xFFFFL) << i * 16; //equivalent to |= } return out; } /** * Gets a long from the given byte array starting at the given byte array index and continuing for * remainder (rem) bytes. The bytes are extracted in little-endian order. There is no limit * checking. * * @param bArr The given input byte array. * @param index Zero-based index from the start of the byte array. * @param rem Remainder bytes. An integer in the range [1,8]. * @return a long */ private static long getLong(final byte[] bArr, final int index, final int rem) { long out = 0L; for (int i = rem; i-- > 0;) { //i= 7,6,5,4,3,2,1,0 final byte b = bArr[index + i]; out ^= (b & 0xFFL) << i * 8; //equivalent to |= } return out; } /** * Gets a long from the given Memory starting at the given offsetBytes and continuing for * remainder (rem) bytes. The bytes are extracted in little-endian order. There is no limit * checking. * * @param mem The given input Memory. * @param offsetBytes Zero-based offset in bytes from the start of the Memory. * @param rem Remainder bytes. An integer in the range [1,8]. * @return a long */ private static long getLong(final Memory mem, final long offsetBytes, final int rem) { long out = 0L; if (rem == 8) { return mem.getLong(offsetBytes); } for (int i = rem; i-- > 0; ) { //i= 7,6,5,4,3,2,1,0 final byte b = mem.getByte(offsetBytes + i); out ^= (b & 0xFFL) << (i << 3); //equivalent to |= } return out; } private static void checkPositive(final long size) { if (size <= 0) { throw new SketchesArgumentException("Array size must not be negative or zero: " + size); } } }
2,542
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hash/MurmurHash3Adaptor.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.hash; import static java.nio.charset.StandardCharsets.UTF_8; import static org.apache.datasketches.common.Util.ceilingIntPowerOf2; import static org.apache.datasketches.hash.MurmurHash3.hash; import java.nio.ByteBuffer; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.common.SketchesStateException; /** * A general purpose wrapper for the MurmurHash3. * <ul> * <li>Inputs can be long, long[], int[], char[], byte[], double or String.</li> * <li>Returns null if arrays or String is null or empty.</li> * <li>Provides methods for returning the 128-bit result as either an array of 2 longs or as a byte * array of 16 bytes.</li> * <li>Provides modulo, asDouble and asInt functions.</li> * </ul> * * @author Lee Rhodes */ public final class MurmurHash3Adaptor { private static final long BIT62 = 1L << 62; private static final long MAX_LONG = Long.MAX_VALUE; private static final long INT_MASK = 0x7FFFFFFFL; private static final long PRIME = 9219741426499971445L; //from P. L'Ecuyer and R. Simard private MurmurHash3Adaptor() {} /** * Hash a long and long seed. * * @param datum the input long value * @param seed A long valued seed. * @return The 128-bit hash as a byte[16] in Big Endian order from 2 64-bit longs. */ public static byte[] hashToBytes(final long datum, final long seed) { final long[] data = { datum }; return toByteArray(hash(data, seed)); } /** * Hash a long[] and long seed. * * @param data the input long array * @param seed A long valued seed. * @return The 128-bit hash as a byte[16] in Big Endian order from 2 64-bit longs. */ public static byte[] hashToBytes(final long[] data, final long seed) { if ((data == null) || (data.length == 0)) { return null; } return toByteArray(hash(data, seed)); } /** * Hash an int[] and long seed. * * @param data the input int array * @param seed A long valued seed. * @return The 128-bit hash as a byte[16] in Big Endian order from 2 64-bit longs. */ public static byte[] hashToBytes(final int[] data, final long seed) { if ((data == null) || (data.length == 0)) { return null; } return toByteArray(hash(data, seed)); } /** * Hash a char[] and long seed. * * @param data the input char array * @param seed A long valued seed. * @return The 128-bit hash as a byte[16] in Big Endian order from 2 64-bit longs. */ public static byte[] hashToBytes(final char[] data, final long seed) { if ((data == null) || (data.length == 0)) { return null; } return toByteArray(hash(data, seed)); } /** * Hash a byte[] and long seed. * * @param data the input byte array * @param seed A long valued seed. * @return The 128-bit hash as a byte[16] in Big Endian order from 2 64-bit longs. */ public static byte[] hashToBytes(final byte[] data, final long seed) { if ((data == null) || (data.length == 0)) { return null; } return toByteArray(hash(data, seed)); } /** * Hash a double and long seed. * * @param datum the input double * @param seed A long valued seed. * @return The 128-bit hash as a byte[16] in Big Endian order from 2 64-bit longs. */ public static byte[] hashToBytes(final double datum, final long seed) { final double d = (datum == 0.0) ? 0.0 : datum; //canonicalize -0.0, 0.0 final long[] data = { Double.doubleToLongBits(d) }; //canonicalize all NaN forms return toByteArray(hash(data, seed)); } /** * Hash a String and long seed. * * @param datum the input String * @param seed A long valued seed. * @return The 128-bit hash as a byte[16] in Big Endian order from 2 64-bit longs. */ public static byte[] hashToBytes(final String datum, final long seed) { if ((datum == null) || datum.isEmpty()) { return null; } final byte[] data = datum.getBytes(UTF_8); return toByteArray(hash(data, seed)); } /** * Hash a long and long seed. * * @param datum the input long * @param seed A long valued seed. * @return The 128-bit hash as a long[2]. */ public static long[] hashToLongs(final long datum, final long seed) { final long[] data = { datum }; return hash(data, seed); } /** * Hash a long[] and long seed. * * @param data the input long array. * @param seed A long valued seed. * @return The 128-bit hash as a long[2]. */ public static long[] hashToLongs(final long[] data, final long seed) { if ((data == null) || (data.length == 0)) { return null; } return hash(data, seed); } /** * Hash a int[] and long seed. * * @param data the input int array. * @param seed A long valued seed. * @return The 128-bit hash as a long[2]. */ public static long[] hashToLongs(final int[] data, final long seed) { if ((data == null) || (data.length == 0)) { return null; } return hash(data, seed); } /** * Hash a char[] and long seed. * * @param data the input char array. * @param seed A long valued seed. * @return The 128-bit hash as a long[2]. */ public static long[] hashToLongs(final char[] data, final long seed) { if ((data == null) || (data.length == 0)) { return null; } return hash(data, seed); } /** * Hash a byte[] and long seed. * * @param data the input byte array. * @param seed A long valued seed. * @return The 128-bit hash as a long[2]. */ public static long[] hashToLongs(final byte[] data, final long seed) { if ((data == null) || (data.length == 0)) { return null; } return hash(data, seed); } /** * Hash a double and long seed. * * @param datum the input double. * @param seed A long valued seed. * @return The 128-bit hash as a long[2]. */ public static long[] hashToLongs(final double datum, final long seed) { final double d = (datum == 0.0) ? 0.0 : datum; //canonicalize -0.0, 0.0 final long[] data = { Double.doubleToLongBits(d) };//canonicalize all NaN forms return hash(data, seed); } /** * Hash a String and long seed. * * @param datum the input String. * @param seed A long valued seed. * @return The 128-bit hash as a long[2]. */ public static long[] hashToLongs(final String datum, final long seed) { if ((datum == null) || datum.isEmpty()) { return null; } final byte[] data = datum.getBytes(UTF_8); return hash(data, seed); } //As Integer functions /** * Returns a deterministic uniform random integer between zero (inclusive) and * n (exclusive) given the input data. * @param data the input long array. * @param n The upper exclusive bound of the integers produced. Must be &gt; 1. * @return deterministic uniform random integer */ public static int asInt(final long[] data, final int n) { if ((data == null) || (data.length == 0)) { throw new SketchesArgumentException("Input is null or empty."); } return asInteger(data, n); //data is long[] } /** * Returns a deterministic uniform random integer between zero (inclusive) and * n (exclusive) given the input data. * @param data the input int array. * @param n The upper exclusive bound of the integers produced. Must be &gt; 1. * @return deterministic uniform random integer */ public static int asInt(final int[] data, final int n) { if ((data == null) || (data.length == 0)) { throw new SketchesArgumentException("Input is null or empty."); } return asInteger(toLongArray(data), n); //data is int[] } /** * Returns a deterministic uniform random integer between zero (inclusive) and * n (exclusive) given the input data. * @param data the input byte array. * @param n The upper exclusive bound of the integers produced. Must be &gt; 1. * @return deterministic uniform random integer. */ public static int asInt(final byte[] data, final int n) { if ((data == null) || (data.length == 0)) { throw new SketchesArgumentException("Input is null or empty."); } return asInteger(toLongArray(data), n); //data is byte[] } /** * Returns a deterministic uniform random integer between zero (inclusive) and * n (exclusive) given the input datum. * @param datum the input long * @param n The upper exclusive bound of the integers produced. Must be &gt; 1. * @return deterministic uniform random integer */ public static int asInt(final long datum, final int n) { final long[] data = { datum }; return asInteger(data, n); //data is long[] } /** * Returns a deterministic uniform random integer between zero (inclusive) and * n (exclusive) given the input double. * @param datum the given double. * @param n The upper exclusive bound of the integers produced. Must be &gt; 1. * @return deterministic uniform random integer */ public static int asInt(final double datum, final int n) { final double d = (datum == 0.0) ? 0.0 : datum; //canonicalize -0.0, 0.0 final long[] data = { Double.doubleToLongBits(d) };//canonicalize all NaN forms return asInteger(data, n); //data is long[] } /** * Returns a deterministic uniform random integer between zero (inclusive) and * n (exclusive) given the input datum. * @param datum the given String. * @param n The upper exclusive bound of the integers produced. Must be &gt; 1. * @return deterministic uniform random integer */ public static int asInt(final String datum, final int n) { if ((datum == null) || datum.isEmpty()) { throw new SketchesArgumentException("Input is null or empty."); } final byte[] data = datum.getBytes(UTF_8); return asInteger(toLongArray(data), n); //data is byte[] } /** * Returns a deterministic uniform random integer with a minimum inclusive value of zero and a * maximum exclusive value of n given the input data. * * <p>The integer values produced are only as random as the MurmurHash3 algorithm, which may be * adequate for many applications. However, if you are looking for high guarantees of randomness * you should turn to more sophisticated random generators such as Mersenne Twister or Well19937c * algorithms. * * @param data The input data (key) * @param n The upper exclusive bound of the integers produced. Must be &gt; 1. * @return deterministic uniform random integer */ private static int asInteger(final long[] data, final int n) { int t; int cnt = 0; long seed = 0; if (n < 2) { throw new SketchesArgumentException("Given value of n must be &gt; 1."); } if (n > (1 << 30)) { while (++cnt < 10000) { final long[] h = MurmurHash3.hash(data, seed); t = (int) (h[0] & INT_MASK); if (t < n) { return t; } t = (int) ((h[0] >>> 33)); if (t < n) { return t; } t = (int) (h[1] & INT_MASK); if (t < n) { return t; } t = (int) ((h[1] >>> 33)); if (t < n) { return t; } seed += PRIME; } // end while throw new SketchesStateException( "Internal Error: Failed to find integer &lt; n within 10000 iterations."); } final long mask = ceilingIntPowerOf2(n) - 1; while (++cnt < 10000) { final long[] h = MurmurHash3.hash(data, seed); t = (int) (h[0] & mask); if (t < n) { return t; } t = (int) ((h[0] >>> 33) & mask); if (t < n) { return t; } t = (int) (h[1] & mask); if (t < n) { return t; } t = (int) ((h[1] >>> 33) & mask); if (t < n) { return t; } seed += PRIME; } // end while throw new SketchesStateException( "Internal Error: Failed to find integer &lt; n within 10000 iterations."); } /** * Returns a uniform random double with a minimum inclusive value of zero and a maximum exclusive * value of 1.0. * * <p>The double values produced are only as random as the MurmurHash3 algorithm, which may be * adequate for many applications. However, if you are looking for high guarantees of randomness * you should turn to more sophisticated random generators such as Mersenne Twister or Well * algorithms. * * @param hash The output of the MurmurHash3. * @return the uniform random double. */ public static double asDouble(final long[] hash) { return (hash[0] >>> 12) * 0x1.0p-52d; } /** * Returns the remainder from the modulo division of the 128-bit output of the murmurHash3 by the * divisor. * * @param h0 The lower 64-bits of the 128-bit MurmurHash3 hash. * @param h1 The upper 64-bits of the 128-bit MurmurHash3 hash. * @param divisor Must be positive and greater than zero. * @return the modulo result. */ public static int modulo(final long h0, final long h1, final int divisor) { final long d = divisor; final long modH0 = (h0 < 0L) ? addRule(mulRule(BIT62, 2L, d), (h0 & MAX_LONG), d) : h0 % d; final long modH1 = (h1 < 0L) ? addRule(mulRule(BIT62, 2L, d), (h1 & MAX_LONG), d) : h1 % d; final long modTop = mulRule(mulRule(BIT62, 4L, d), modH1, d); return (int) addRule(modTop, modH0, d); } /** * Returns the remainder from the modulo division of the 128-bit output of the murmurHash3 by the * divisor. * * @param hash The size 2 long array from the MurmurHash3. * @param divisor Must be positive and greater than zero. * @return the modulo result */ public static int modulo(final long[] hash, final int divisor) { return modulo(hash[0], hash[1], divisor); } private static long addRule(final long a, final long b, final long d) { return ((a % d) + (b % d)) % d; } private static long mulRule(final long a, final long b, final long d) { return ((a % d) * (b % d)) % d; } private static byte[] toByteArray(final long[] hash) { //Assumes Big Endian final byte[] bArr = new byte[16]; final ByteBuffer bb = ByteBuffer.wrap(bArr); bb.putLong(hash[0]); bb.putLong(hash[1]); return bArr; } private static long[] toLongArray(final byte[] data) { final int dataLen = data.length; final int longLen = (dataLen + 7) / 8; final long[] longArr = new long[longLen]; for (int bi = 0; bi < dataLen; bi++) { final int li = bi / 8; longArr[li] |= (((long)data[bi]) << ((bi * 8) % 64)); } return longArr; } private static long[] toLongArray(final int[] data) { final int dataLen = data.length; final int longLen = (dataLen + 1) / 2; final long[] longArr = new long[longLen]; for (int ii = 0; ii < dataLen; ii++) { final int li = ii / 2; longArr[li] |= (((long)data[ii]) << ((ii * 32) % 64)); } return longArr; } }
2,543
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/hash/package-info.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /** * <p>The hash package contains a high-performing and extended Java implementations * of Austin Appleby's 128-bit MurmurHash3 hash function originally coded in C. * This core MurmurHash3.java class is used throughout many of the sketch classes for consistency * and as long as the user specifies the same seed will result in coordinated hash operations. * This package also contains an adaptor class that extends the basic class with more functions * commonly associated with hashing. * </p> */ package org.apache.datasketches.hash;
2,544
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/Union.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; import static java.lang.Math.min; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.thetacommon.QuickSelect; import org.apache.datasketches.thetacommon.ThetaUtil; /** * Compute the union of two or more generic tuple sketches or generic tuple sketches combined with * theta sketches. A new instance represents an empty set. * @param <S> Type of Summary */ public class Union<S extends Summary> { private final SummarySetOperations<S> summarySetOps_; private QuickSelectSketch<S> qsk_; private long unionThetaLong_; // need to maintain outside of the sketch private boolean empty_; /** * Creates new Union instance with instructions on how to process two summaries that * overlap. This will have the default nominal entries (K). * @param summarySetOps instance of SummarySetOperations */ public Union(final SummarySetOperations<S> summarySetOps) { this(ThetaUtil.DEFAULT_NOMINAL_ENTRIES, summarySetOps); } /** * Creates new Union instance. * @param nomEntries nominal entries (K). Forced to the nearest power of 2 greater than * given value. * @param summarySetOps instance of SummarySetOperations */ public Union(final int nomEntries, final SummarySetOperations<S> summarySetOps) { summarySetOps_ = summarySetOps; qsk_ = new QuickSelectSketch<>(nomEntries, null); unionThetaLong_ = qsk_.getThetaLong(); empty_ = true; } /** * Perform a stateless, pair-wise union operation between two tuple sketches. * The returned sketch will be cut back to the smaller of the two k values if required. * * <p>Nulls and empty sketches are ignored.</p> * * @param tupleSketchA The first argument * @param tupleSketchB The second argument * @return the result ordered CompactSketch on the heap. */ public CompactSketch<S> union(final Sketch<S> tupleSketchA, final Sketch<S> tupleSketchB) { reset(); union(tupleSketchA); union(tupleSketchB); final CompactSketch<S> csk = getResult(true); return csk; } /** * Perform a stateless, pair-wise union operation between a tupleSketch and a thetaSketch. * The returned sketch will be cut back to the smaller of the two k values if required. * * <p>Nulls and empty sketches are ignored.</p> * * @param tupleSketch The first argument * @param thetaSketch The second argument * @param summary the given proxy summary for the theta sketch, which doesn't have one. * This may not be null. * @return the result ordered CompactSketch on the heap. */ public CompactSketch<S> union(final Sketch<S> tupleSketch, final org.apache.datasketches.theta.Sketch thetaSketch, final S summary) { reset(); union(tupleSketch); union(thetaSketch, summary); final CompactSketch<S> csk = getResult(true); return csk; } /** * Performs a stateful union of the internal set with the given tupleSketch. * @param tupleSketch input tuple sketch to merge with the internal set. * * <p>Nulls and empty sketches are ignored.</p> */ public void union(final Sketch<S> tupleSketch) { if (tupleSketch == null || tupleSketch.isEmpty()) { return; } empty_ = false; unionThetaLong_ = min(tupleSketch.thetaLong_, unionThetaLong_); final TupleSketchIterator<S> it = tupleSketch.iterator(); while (it.next()) { qsk_.merge(it.getHash(), it.getSummary(), summarySetOps_); } unionThetaLong_ = min(unionThetaLong_, qsk_.thetaLong_); } /** * Performs a stateful union of the internal set with the given thetaSketch by combining entries * using the hashes from the theta sketch and summary values from the given summary. * @param thetaSketch the given theta sketch input. If null or empty, it is ignored. * @param summary the given proxy summary for the theta sketch, which doesn't have one. This may * not be null. */ public void union(final org.apache.datasketches.theta.Sketch thetaSketch, final S summary) { if (summary == null) { throw new SketchesArgumentException("Summary cannot be null."); } if (thetaSketch == null || thetaSketch.isEmpty()) { return; } empty_ = false; final long thetaIn = thetaSketch.getThetaLong(); unionThetaLong_ = min(thetaIn, unionThetaLong_); final org.apache.datasketches.theta.HashIterator it = thetaSketch.iterator(); while (it.next()) { qsk_.merge(it.get(), summary, summarySetOps_); //copies summary } unionThetaLong_ = min(unionThetaLong_, qsk_.thetaLong_); } /** * Gets the result of a sequence of stateful <i>union</i> operations as an unordered CompactSketch * @return result of the stateful unions so far. The state of this operation is not reset after the * result is returned. */ public CompactSketch<S> getResult() { return getResult(false); } /** * Gets the result of a sequence of stateful <i>union</i> operations as an unordered CompactSketch. * @param reset If <i>true</i>, clears this operator to the empty state after this result is * returned. Set this to <i>false</i> if you wish to obtain an intermediate result. * @return result of the stateful union */ @SuppressWarnings("unchecked") public CompactSketch<S> getResult(final boolean reset) { final CompactSketch<S> result; if (empty_) { result = qsk_.compact(); } else if (unionThetaLong_ >= qsk_.thetaLong_ && qsk_.getRetainedEntries() <= qsk_.getNominalEntries()) { //unionThetaLong_ >= qsk_.thetaLong_ means we can ignore unionThetaLong_. We don't need to rebuild. //qsk_.getRetainedEntries() <= qsk_.getNominalEntries() means we don't need to pull back to k. result = qsk_.compact(); } else { final long tmpThetaLong = min(unionThetaLong_, qsk_.thetaLong_); //count the number of valid hashes in because Alpha can have dirty values int numHashesIn = 0; TupleSketchIterator<S> it = qsk_.iterator(); while (it.next()) { //counts valid hashes if (it.getHash() < tmpThetaLong) { numHashesIn++; } } if (numHashesIn == 0) { //numHashes == 0 && empty == false means Theta < 1.0 //Therefore, this is a degenerate sketch: theta < 1.0, count = 0, empty = false result = new CompactSketch<>(null, null, tmpThetaLong, empty_); } else { //we know: empty == false, count > 0 final int numHashesOut; final long thetaLongOut; if (numHashesIn > qsk_.getNominalEntries()) { //we need to trim hashes and need a new thetaLong final long[] tmpHashArr = new long[numHashesIn]; // temporary, order will be destroyed by quick select it = qsk_.iterator(); int i = 0; while (it.next()) { final long hash = it.getHash(); if (hash < tmpThetaLong) { tmpHashArr[i++] = hash; } } numHashesOut = qsk_.getNominalEntries(); thetaLongOut = QuickSelect.select(tmpHashArr, 0, numHashesIn - 1, numHashesOut); } else { numHashesOut = numHashesIn; thetaLongOut = tmpThetaLong; } //now prepare the output arrays final long[] hashArr = new long[numHashesOut]; final S[] summaries = Util.newSummaryArray(qsk_.getSummaryTable(), numHashesOut); it = qsk_.iterator(); int i = 0; while (it.next()) { //select the qualifying hashes from the gadget synchronized with the summaries final long hash = it.getHash(); if (hash < thetaLongOut) { hashArr[i] = hash; summaries[i] = (S) it.getSummary().copy(); i++; } } result = new CompactSketch<>(hashArr, summaries, thetaLongOut, empty_); } } if (reset) { reset(); } return result; } /** * Resets the internal set to the initial state, which represents an empty set. This is only useful * after sequences of stateful union operations. */ public void reset() { qsk_.reset(); unionThetaLong_ = qsk_.getThetaLong(); empty_ = true; } }
2,545
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/UpdatableSummary.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; /** * Interface for updating user-defined Summary * @param <U> type of update value */ public interface UpdatableSummary<U> extends Summary { /** * This is to provide a method of updating summaries. * This is primarily used internally. * @param value update value * @return this */ UpdatableSummary<U> update(U value); }
2,546
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/SummaryFactory.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; /** * Interface for user-defined SummaryFactory * @param <S> type of Summary */ public interface SummaryFactory<S extends Summary> { /** * @return new instance of Summary */ public S newSummary(); }
2,547
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/Util.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; import static java.nio.charset.StandardCharsets.UTF_8; import static org.apache.datasketches.common.Util.ceilingIntPowerOf2; import static org.apache.datasketches.hash.MurmurHash3.hash; import static org.apache.datasketches.memory.XxHash.hashCharArr; import static org.apache.datasketches.memory.XxHash.hashString; import java.lang.reflect.Array; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.thetacommon.ThetaUtil; /** * Common utility functions for Tuples */ public final class Util { private static final int PRIME = 0x7A3C_CA71; /** * Converts a <i>double</i> to a <i>long[]</i>. * @param value the given double value * @return the long array */ public static final long[] doubleToLongArray(final double value) { final double d = (value == 0.0) ? 0.0 : value; // canonicalize -0.0, 0.0 final long[] array = { Double.doubleToLongBits(d) }; // canonicalize all NaN & +/- infinity forms return array; } /** * Converts a String to a UTF_8 byte array. If the given value is either null or empty this * method returns null. * @param value the given String value * @return the UTF_8 byte array */ public static final byte[] stringToByteArray(final String value) { if ((value == null) || value.isEmpty()) { return null; } return value.getBytes(UTF_8); } /** * Computes and checks the 16-bit seed hash from the given long seed. * The seed hash may not be zero in order to maintain compatibility with older serialized * versions that did not have this concept. * @param seed <a href="{@docRoot}/resources/dictionary.html#seed">See Update Hash Seed</a> * @return the seed hash. */ public static short computeSeedHash(final long seed) { final long[] seedArr = {seed}; final short seedHash = (short)((hash(seedArr, 0L)[0]) & 0xFFFFL); if (seedHash == 0) { throw new SketchesArgumentException( "The given seed: " + seed + " produced a seedHash of zero. " + "You must choose a different seed."); } return seedHash; } /** * Checks the two given seed hashes. If they are not equal, this method throws an Exception. * @param seedHashA given seed hash A * @param seedHashB given seed hash B */ public static final void checkSeedHashes(final short seedHashA, final short seedHashB) { if (seedHashA != seedHashB) { throw new SketchesArgumentException("Incompatible Seed Hashes. " + seedHashA + ", " + seedHashB); } } /** * Gets the starting capacity of a new sketch given the Nominal Entries and the log Resize Factor. * @param nomEntries the given Nominal Entries * @param lgResizeFactor the given log Resize Factor * @return the starting capacity */ public static int getStartingCapacity(final int nomEntries, final int lgResizeFactor) { return 1 << ThetaUtil.startingSubMultiple( // target table size is twice the number of nominal entries Integer.numberOfTrailingZeros(ceilingIntPowerOf2(nomEntries) * 2), lgResizeFactor, ThetaUtil.MIN_LG_ARR_LONGS ); } /** * Concatenate array of Strings to a single String. * @param strArr the given String array * @return the concatenated String */ public static String stringConcat(final String[] strArr) { final int len = strArr.length; final StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { sb.append(strArr[i]); if ((i + 1) < len) { sb.append(','); } } return sb.toString(); } /** * @param s the string to hash * @return the hash of the string */ public static long stringHash(final String s) { return hashString(s, 0, s.length(), PRIME); } /** * @param strArray array of Strings * @return long hash of concatenated strings. */ public static long stringArrHash(final String[] strArray) { final String s = stringConcat(strArray); return hashCharArr(s.toCharArray(), 0, s.length(), PRIME); } /** * Will copy compact summary arrays as well as hashed summary tables (with nulls). * @param <S> type of summary * @param summaryArr the given summary array or table * @return the copy */ @SuppressWarnings("unchecked") public static <S extends Summary> S[] copySummaryArray(final S[] summaryArr) { final int len = summaryArr.length; final S[] tmpSummaryArr = newSummaryArray(summaryArr, len); for (int i = 0; i < len; i++) { final S summary = summaryArr[i]; if (summary == null) { continue; } tmpSummaryArr[i] = (S) summary.copy(); } return tmpSummaryArr; } @SuppressWarnings("unchecked") public static <S extends Summary> S[] newSummaryArray(final S[] summaryArr, final int length) { final Class<S> summaryType = (Class<S>) summaryArr.getClass().getComponentType(); final S[] tmpSummaryArr = (S[]) Array.newInstance(summaryType, length); return tmpSummaryArr; } }
2,548
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/CompactSketch.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; import static org.apache.datasketches.thetacommon.HashOperations.count; import java.lang.reflect.Array; import java.nio.ByteOrder; import org.apache.datasketches.common.ByteArrayUtil; import org.apache.datasketches.common.Family; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.memory.Memory; /** * CompactSketches are never created directly. They are created as a result of * the compact() method of an UpdatableSketch or as a result of the getResult() * method of a set operation like Union, Intersection or AnotB. CompactSketch * consists of a compact list (i.e. no intervening spaces) of hash values, * corresponding list of Summaries, and a value for theta. The lists may or may * not be ordered. CompactSketch is read-only. * * @param <S> type of Summary */ public class CompactSketch<S extends Summary> extends Sketch<S> { private static final byte serialVersionWithSummaryClassNameUID = 1; private static final byte serialVersionUIDLegacy = 2; private static final byte serialVersionUID = 3; private static final short defaultSeedHash = (short) 37836; // for compatibility with C++ private long[] hashArr_; private S[] summaryArr_; private enum FlagsLegacy { IS_BIG_ENDIAN, IS_EMPTY, HAS_ENTRIES, IS_THETA_INCLUDED } private enum Flags { IS_BIG_ENDIAN, IS_READ_ONLY, IS_EMPTY, IS_COMPACT, IS_ORDERED } /** * Create a CompactSketch from correct components * @param hashArr compacted hash array * @param summaryArr compacted summary array * @param thetaLong long value of theta * @param empty empty flag */ CompactSketch(final long[] hashArr, final S[] summaryArr, final long thetaLong, final boolean empty) { hashArr_ = hashArr; summaryArr_ = summaryArr; thetaLong_ = thetaLong; empty_ = empty; } /** * This is to create an instance of a CompactSketch given a serialized form * * @param mem Memory object with serialized CompactSketch * @param deserializer the SummaryDeserializer */ CompactSketch(final Memory mem, final SummaryDeserializer<S> deserializer) { int offset = 0; final byte preambleLongs = mem.getByte(offset++); final byte version = mem.getByte(offset++); final byte familyId = mem.getByte(offset++); SerializerDeserializer.validateFamily(familyId, preambleLongs); if (version > serialVersionUID) { throw new SketchesArgumentException( "Unsupported serial version. Expected: " + serialVersionUID + " or lower, actual: " + version); } SerializerDeserializer .validateType(mem.getByte(offset++), SerializerDeserializer.SketchType.CompactSketch); if (version <= serialVersionUIDLegacy) { // legacy serial format final byte flags = mem.getByte(offset++); final boolean isBigEndian = (flags & 1 << FlagsLegacy.IS_BIG_ENDIAN.ordinal()) > 0; if (isBigEndian ^ ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN)) { throw new SketchesArgumentException("Byte order mismatch"); } empty_ = (flags & 1 << FlagsLegacy.IS_EMPTY.ordinal()) > 0; final boolean isThetaIncluded = (flags & 1 << FlagsLegacy.IS_THETA_INCLUDED.ordinal()) > 0; if (isThetaIncluded) { thetaLong_ = mem.getLong(offset); offset += Long.BYTES; } else { thetaLong_ = Long.MAX_VALUE; } final boolean hasEntries = (flags & 1 << FlagsLegacy.HAS_ENTRIES.ordinal()) > 0; if (hasEntries) { int classNameLength = 0; if (version == serialVersionWithSummaryClassNameUID) { classNameLength = mem.getByte(offset++); } final int count = mem.getInt(offset); offset += Integer.BYTES; if (version == serialVersionWithSummaryClassNameUID) { offset += classNameLength; } hashArr_ = new long[count]; for (int i = 0; i < count; i++) { hashArr_[i] = mem.getLong(offset); offset += Long.BYTES; } for (int i = 0; i < count; i++) { offset += readSummary(mem, offset, i, count, deserializer); } } } else { // current serial format offset++; //skip unused byte final byte flags = mem.getByte(offset++); offset += 2; //skip 2 unused bytes empty_ = (flags & 1 << Flags.IS_EMPTY.ordinal()) > 0; thetaLong_ = Long.MAX_VALUE; int count = 0; if (!empty_) { if (preambleLongs == 1) { count = 1; } else { count = mem.getInt(offset); offset += Integer.BYTES; offset += 4; // unused if (preambleLongs > 2) { thetaLong_ = mem.getLong(offset); offset += Long.BYTES; } } } hashArr_ = new long[count]; for (int i = 0; i < count; i++) { hashArr_[i] = mem.getLong(offset); offset += Long.BYTES; offset += readSummary(mem, offset, i, count, deserializer); } } } @SuppressWarnings({"unchecked"}) private int readSummary(final Memory mem, final int offset, final int i, final int count, final SummaryDeserializer<S> deserializer) { final Memory memRegion = mem.region(offset, mem.getCapacity() - offset); final DeserializeResult<S> result = deserializer.heapifySummary(memRegion); final S summary = result.getObject(); final Class<S> summaryType = (Class<S>) result.getObject().getClass(); if (summaryArr_ == null) { summaryArr_ = (S[]) Array.newInstance(summaryType, count); } summaryArr_[i] = summary; return result.getSize(); } @Override public CompactSketch<S> compact() { return this; } long[] getHashArr() { return hashArr_; } S[] getSummaryArr() { return summaryArr_; } @Override public int getRetainedEntries() { return hashArr_ == null ? 0 : hashArr_.length; } @Override public int getCountLessThanThetaLong(final long thetaLong) { return count(hashArr_, thetaLong); } // Layout of first 8 bytes: // Long || Start Byte Adr: // Adr: // || 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | // 0 || seed hash | Flags | unused | SkType | FamID | SerVer | Preamble_Longs | @Override public byte[] toByteArray() { final int count = getRetainedEntries(); final boolean isSingleItem = count == 1 && !isEstimationMode(); final int preambleLongs = isEmpty() || isSingleItem ? 1 : isEstimationMode() ? 3 : 2; int summariesSizeBytes = 0; final byte[][] summariesBytes = new byte[count][]; if (count > 0) { for (int i = 0; i < count; i++) { summariesBytes[i] = summaryArr_[i].toByteArray(); summariesSizeBytes += summariesBytes[i].length; } } final int sizeBytes = Long.BYTES * preambleLongs + Long.BYTES * count + summariesSizeBytes; final byte[] bytes = new byte[sizeBytes]; int offset = 0; bytes[offset++] = (byte) preambleLongs; bytes[offset++] = serialVersionUID; bytes[offset++] = (byte) Family.TUPLE.getID(); bytes[offset++] = (byte) SerializerDeserializer.SketchType.CompactSketch.ordinal(); offset++; // unused bytes[offset++] = (byte) ( (1 << Flags.IS_COMPACT.ordinal()) | (1 << Flags.IS_READ_ONLY.ordinal()) | (isEmpty() ? 1 << Flags.IS_EMPTY.ordinal() : 0) ); ByteArrayUtil.putShortLE(bytes, offset, defaultSeedHash); offset += Short.BYTES; if (!isEmpty()) { if (!isSingleItem) { ByteArrayUtil.putIntLE(bytes, offset, count); offset += Integer.BYTES; offset += 4; // unused if (isEstimationMode()) { ByteArrayUtil.putLongLE(bytes, offset, thetaLong_); offset += Long.BYTES; } } } for (int i = 0; i < count; i++) { ByteArrayUtil.putLongLE(bytes, offset, hashArr_[i]); offset += Long.BYTES; System.arraycopy(summariesBytes[i], 0, bytes, offset, summariesBytes[i].length); offset += summariesBytes[i].length; } return bytes; } @Override public TupleSketchIterator<S> iterator() { return new TupleSketchIterator<>(hashArr_, summaryArr_); } }
2,549
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/QuickSelectSketch.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; import static org.apache.datasketches.common.Util.ceilingIntPowerOf2; import static org.apache.datasketches.common.Util.checkBounds; import static org.apache.datasketches.common.Util.exactLog2OfLong; import static org.apache.datasketches.thetacommon.HashOperations.count; import java.lang.reflect.Array; import java.nio.ByteOrder; import java.util.Objects; import org.apache.datasketches.common.ByteArrayUtil; import org.apache.datasketches.common.Family; import org.apache.datasketches.common.ResizeFactor; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.thetacommon.HashOperations; import org.apache.datasketches.thetacommon.QuickSelect; import org.apache.datasketches.thetacommon.ThetaUtil; /** * A generic tuple sketch using the QuickSelect algorithm. * * @param <S> type of Summary */ class QuickSelectSketch<S extends Summary> extends Sketch<S> { private static final byte serialVersionUID = 2; private enum Flags { IS_BIG_ENDIAN, IS_IN_SAMPLING_MODE, IS_EMPTY, HAS_ENTRIES, IS_THETA_INCLUDED } static final int DEFAULT_LG_RESIZE_FACTOR = ResizeFactor.X8.lg(); private final int nomEntries_; private int lgCurrentCapacity_; private final int lgResizeFactor_; private int count_; private final float samplingProbability_; private int rebuildThreshold_; private long[] hashTable_; S[] summaryTable_; /** * This is to create an instance of a QuickSelectSketch with default resize factor. * @param nomEntries Nominal number of entries. Forced to the nearest power of 2 greater than * given value. * @param summaryFactory An instance of a SummaryFactory. */ QuickSelectSketch( final int nomEntries, final SummaryFactory<S> summaryFactory) { this(nomEntries, DEFAULT_LG_RESIZE_FACTOR, summaryFactory); } /** * This is to create an instance of a QuickSelectSketch with custom resize factor * @param nomEntries Nominal number of entries. Forced to the nearest power of 2 greater than * given value. * @param lgResizeFactor log2(resizeFactor) - value from 0 to 3: * <pre> * 0 - no resizing (max size allocated), * 1 - double internal hash table each time it reaches a threshold * 2 - grow four times * 3 - grow eight times (default) * </pre> * @param summaryFactory An instance of a SummaryFactory. */ QuickSelectSketch( final int nomEntries, final int lgResizeFactor, final SummaryFactory<S> summaryFactory) { this(nomEntries, lgResizeFactor, 1f, summaryFactory); } /** * This is to create an instance of a QuickSelectSketch with custom resize factor and sampling * probability * @param nomEntries Nominal number of entries. Forced to the nearest power of 2 greater than * or equal to the given value. * @param lgResizeFactor log2(resizeFactor) - value from 0 to 3: * <pre> * 0 - no resizing (max size allocated), * 1 - double internal hash table each time it reaches a threshold * 2 - grow four times * 3 - grow eight times (default) * </pre> * @param samplingProbability the given sampling probability * @param summaryFactory An instance of a SummaryFactory. */ QuickSelectSketch( final int nomEntries, final int lgResizeFactor, final float samplingProbability, final SummaryFactory<S> summaryFactory) { this( nomEntries, lgResizeFactor, samplingProbability, summaryFactory, Util.getStartingCapacity(nomEntries, lgResizeFactor) ); } QuickSelectSketch( final int nomEntries, final int lgResizeFactor, final float samplingProbability, final SummaryFactory<S> summaryFactory, final int startingSize) { nomEntries_ = ceilingIntPowerOf2(nomEntries); lgResizeFactor_ = lgResizeFactor; samplingProbability_ = samplingProbability; summaryFactory_ = summaryFactory; //super thetaLong_ = (long) (Long.MAX_VALUE * (double) samplingProbability); //super lgCurrentCapacity_ = Integer.numberOfTrailingZeros(startingSize); hashTable_ = new long[startingSize]; summaryTable_ = null; // wait for the first summary to call Array.newInstance() setRebuildThreshold(); } /** * Copy constructor * @param sketch the QuickSelectSketch to be copied. */ QuickSelectSketch(final QuickSelectSketch<S> sketch) { nomEntries_ = sketch.nomEntries_; lgCurrentCapacity_ = sketch.lgCurrentCapacity_; lgResizeFactor_ = sketch.lgResizeFactor_; count_ = sketch.count_; samplingProbability_ = sketch.samplingProbability_; rebuildThreshold_ = sketch.rebuildThreshold_; thetaLong_ = sketch.thetaLong_; empty_ = sketch.empty_; summaryFactory_ = sketch.summaryFactory_; hashTable_ = sketch.hashTable_.clone(); summaryTable_ = Util.copySummaryArray(sketch.summaryTable_); } /** * This is to create an instance of a QuickSelectSketch given a serialized form * @param mem Memory object with serialized QuickSelectSketch * @param deserializer the SummaryDeserializer * @param summaryFactory the SummaryFactory * @deprecated As of 3.0.0, heapifying an UpdatableSketch is deprecated. * This capability will be removed in a future release. * Heapifying a CompactSketch is not deprecated. */ @Deprecated QuickSelectSketch( final Memory mem, final SummaryDeserializer<S> deserializer, final SummaryFactory<S> summaryFactory) { Objects.requireNonNull(mem, "SourceMemory must not be null."); Objects.requireNonNull(deserializer, "Deserializer must not be null."); checkBounds(0, 8, mem.getCapacity()); summaryFactory_ = summaryFactory; int offset = 0; final byte preambleLongs = mem.getByte(offset++); //byte 0 PreLongs final byte version = mem.getByte(offset++); //byte 1 SerVer final byte familyId = mem.getByte(offset++); //byte 2 FamID SerializerDeserializer.validateFamily(familyId, preambleLongs); if (version > serialVersionUID) { throw new SketchesArgumentException( "Unsupported serial version. Expected: " + serialVersionUID + " or lower, actual: " + version); } SerializerDeserializer.validateType(mem.getByte(offset++), //byte 3 SerializerDeserializer.SketchType.QuickSelectSketch); final byte flags = mem.getByte(offset++); //byte 4 final boolean isBigEndian = (flags & 1 << Flags.IS_BIG_ENDIAN.ordinal()) > 0; if (isBigEndian ^ ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN)) { throw new SketchesArgumentException("Endian byte order mismatch"); } nomEntries_ = 1 << mem.getByte(offset++); //byte 5 lgCurrentCapacity_ = mem.getByte(offset++); //byte 6 lgResizeFactor_ = mem.getByte(offset++); //byte 7 checkBounds(0, preambleLongs * 8L, mem.getCapacity()); final boolean isInSamplingMode = (flags & 1 << Flags.IS_IN_SAMPLING_MODE.ordinal()) > 0; samplingProbability_ = isInSamplingMode ? mem.getFloat(offset) : 1f; //bytes 8 - 11 if (isInSamplingMode) { offset += Float.BYTES; } final boolean isThetaIncluded = (flags & 1 << Flags.IS_THETA_INCLUDED.ordinal()) > 0; if (isThetaIncluded) { thetaLong_ = mem.getLong(offset); offset += Long.BYTES; } else { thetaLong_ = (long) (Long.MAX_VALUE * (double) samplingProbability_); } int count = 0; final boolean hasEntries = (flags & 1 << Flags.HAS_ENTRIES.ordinal()) > 0; if (hasEntries) { count = mem.getInt(offset); offset += Integer.BYTES; } final int currentCapacity = 1 << lgCurrentCapacity_; hashTable_ = new long[currentCapacity]; for (int i = 0; i < count; i++) { final long hash = mem.getLong(offset); offset += Long.BYTES; final Memory memRegion = mem.region(offset, mem.getCapacity() - offset); final DeserializeResult<S> summaryResult = deserializer.heapifySummary(memRegion); final S summary = summaryResult.getObject(); offset += summaryResult.getSize(); insert(hash, summary); } empty_ = (flags & 1 << Flags.IS_EMPTY.ordinal()) > 0; setRebuildThreshold(); } /** * @return a deep copy of this sketch */ QuickSelectSketch<S> copy() { return new QuickSelectSketch<>(this); } long[] getHashTable() { return hashTable_; } @Override public int getRetainedEntries() { return count_; } @Override public int getCountLessThanThetaLong(final long thetaLong) { return count(hashTable_, thetaLong); } S[] getSummaryTable() { return summaryTable_; } /** * Get configured nominal number of entries * @return nominal number of entries */ public int getNominalEntries() { return nomEntries_; } /** * Get log_base2 of Nominal Entries * @return log_base2 of Nominal Entries */ public int getLgK() { return exactLog2OfLong(nomEntries_); } /** * Get configured sampling probability * @return sampling probability */ public float getSamplingProbability() { return samplingProbability_; } /** * Get current capacity * @return current capacity */ public int getCurrentCapacity() { return 1 << lgCurrentCapacity_; } /** * Get configured resize factor * @return resize factor */ public ResizeFactor getResizeFactor() { return ResizeFactor.getRF(lgResizeFactor_); } /** * Rebuilds reducing the actual number of entries to the nominal number of entries if needed */ public void trim() { if (count_ > nomEntries_) { updateTheta(); resize(hashTable_.length); } } /** * Resets this sketch an empty state. */ public void reset() { empty_ = true; count_ = 0; thetaLong_ = (long) (Long.MAX_VALUE * (double) samplingProbability_); final int startingCapacity = Util.getStartingCapacity(nomEntries_, lgResizeFactor_); lgCurrentCapacity_ = Integer.numberOfTrailingZeros(startingCapacity); hashTable_ = new long[startingCapacity]; summaryTable_ = null; // wait for the first summary to call Array.newInstance() setRebuildThreshold(); } /** * Converts the current state of the sketch into a compact sketch * @return compact sketch */ @Override @SuppressWarnings("unchecked") public CompactSketch<S> compact() { if (getRetainedEntries() == 0) { if (empty_) { return new CompactSketch<>(null, null, Long.MAX_VALUE, true); } return new CompactSketch<>(null, null, thetaLong_, false); } final long[] hashArr = new long[getRetainedEntries()]; final S[] summaryArr = Util.newSummaryArray(summaryTable_, getRetainedEntries()); int i = 0; for (int j = 0; j < hashTable_.length; j++) { if (summaryTable_[j] != null) { hashArr[i] = hashTable_[j]; summaryArr[i] = (S)summaryTable_[j].copy(); i++; } } return new CompactSketch<>(hashArr, summaryArr, thetaLong_, empty_); } // Layout of first 8 bytes: // Long || Start Byte Adr: // Adr: // || 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | // 0 || RF | lgArr | lgNom | Flags | SkType | FamID | SerVer | Preamble_Longs | /** * This serializes an UpdatableSketch (QuickSelectSketch). * @return serialized representation of an UpdatableSketch (QuickSelectSketch). * @deprecated As of 3.0.0, serializing an UpdatableSketch is deprecated. * This capability will be removed in a future release. * Serializing a CompactSketch is not deprecated. */ @Deprecated @Override public byte[] toByteArray() { byte[][] summariesBytes = null; int summariesBytesLength = 0; if (count_ > 0) { summariesBytes = new byte[count_][]; int i = 0; for (int j = 0; j < summaryTable_.length; j++) { if (summaryTable_[j] != null) { summariesBytes[i] = summaryTable_[j].toByteArray(); summariesBytesLength += summariesBytes[i].length; i++; } } } int sizeBytes = Byte.BYTES // preamble longs + Byte.BYTES // serial version + Byte.BYTES // family + Byte.BYTES // sketch type + Byte.BYTES // flags + Byte.BYTES // log2(nomEntries) + Byte.BYTES // log2(currentCapacity) + Byte.BYTES; // log2(resizeFactor) if (isInSamplingMode()) { sizeBytes += Float.BYTES; // samplingProbability } final boolean isThetaIncluded = isInSamplingMode() ? thetaLong_ < samplingProbability_ : thetaLong_ < Long.MAX_VALUE; if (isThetaIncluded) { sizeBytes += Long.BYTES; } if (count_ > 0) { sizeBytes += Integer.BYTES; // count } sizeBytes += Long.BYTES * count_ + summariesBytesLength; final byte[] bytes = new byte[sizeBytes]; int offset = 0; bytes[offset++] = PREAMBLE_LONGS; bytes[offset++] = serialVersionUID; bytes[offset++] = (byte) Family.TUPLE.getID(); bytes[offset++] = (byte) SerializerDeserializer.SketchType.QuickSelectSketch.ordinal(); final boolean isBigEndian = ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN); bytes[offset++] = (byte) ( (isBigEndian ? 1 << Flags.IS_BIG_ENDIAN.ordinal() : 0) | (isInSamplingMode() ? 1 << Flags.IS_IN_SAMPLING_MODE.ordinal() : 0) | (empty_ ? 1 << Flags.IS_EMPTY.ordinal() : 0) | (count_ > 0 ? 1 << Flags.HAS_ENTRIES.ordinal() : 0) | (isThetaIncluded ? 1 << Flags.IS_THETA_INCLUDED.ordinal() : 0) ); bytes[offset++] = (byte) Integer.numberOfTrailingZeros(nomEntries_); bytes[offset++] = (byte) lgCurrentCapacity_; bytes[offset++] = (byte) lgResizeFactor_; if (samplingProbability_ < 1f) { ByteArrayUtil.putFloatLE(bytes, offset, samplingProbability_); offset += Float.BYTES; } if (isThetaIncluded) { ByteArrayUtil.putLongLE(bytes, offset, thetaLong_); offset += Long.BYTES; } if (count_ > 0) { ByteArrayUtil.putIntLE(bytes, offset, count_); offset += Integer.BYTES; } if (count_ > 0) { int i = 0; for (int j = 0; j < hashTable_.length; j++) { if (summaryTable_[j] != null) { ByteArrayUtil.putLongLE(bytes, offset, hashTable_[j]); offset += Long.BYTES; System.arraycopy(summariesBytes[i], 0, bytes, offset, summariesBytes[i].length); offset += summariesBytes[i].length; i++; } } } return bytes; } // non-public methods below // this is a special back door insert for merging // not sufficient by itself without keeping track of theta of another sketch @SuppressWarnings("unchecked") void merge(final long hash, final S summary, final SummarySetOperations<S> summarySetOps) { empty_ = false; if (hash > 0 && hash < thetaLong_) { final int index = findOrInsert(hash); if (index < 0) { insertSummary(~index, (S)summary.copy()); //did not find, so insert } else { insertSummary(index, summarySetOps.union(summaryTable_[index], (S) summary.copy())); } rebuildIfNeeded(); } } boolean isInSamplingMode() { return samplingProbability_ < 1f; } void setThetaLong(final long theta) { thetaLong_ = theta; } void setEmpty(final boolean value) { empty_ = value; } int findOrInsert(final long hash) { final int index = HashOperations.hashSearchOrInsert(hashTable_, lgCurrentCapacity_, hash); if (index < 0) { count_++; } return index; } boolean rebuildIfNeeded() { if (count_ <= rebuildThreshold_) { return false; } if (hashTable_.length > nomEntries_) { updateTheta(); rebuild(); } else { resize(hashTable_.length * (1 << lgResizeFactor_)); } return true; } void rebuild() { resize(hashTable_.length); } void insert(final long hash, final S summary) { final int index = HashOperations.hashInsertOnly(hashTable_, lgCurrentCapacity_, hash); insertSummary(index, summary); count_++; empty_ = false; } private void updateTheta() { final long[] hashArr = new long[count_]; int i = 0; //Because of the association of the hashTable with the summaryTable we cannot destroy the // hashTable structure. So we must copy. May as well compact at the same time. // Might consider a whole table clone and use the selectExcludingZeros method instead. // Not sure if there would be any speed advantage. for (int j = 0; j < hashTable_.length; j++) { if (summaryTable_[j] != null) { hashArr[i++] = hashTable_[j]; } } thetaLong_ = QuickSelect.select(hashArr, 0, count_ - 1, nomEntries_); } private void resize(final int newSize) { final long[] oldHashTable = hashTable_; final S[] oldSummaryTable = summaryTable_; hashTable_ = new long[newSize]; summaryTable_ = Util.newSummaryArray(summaryTable_, newSize); lgCurrentCapacity_ = Integer.numberOfTrailingZeros(newSize); count_ = 0; for (int i = 0; i < oldHashTable.length; i++) { if (oldSummaryTable[i] != null && oldHashTable[i] < thetaLong_) { insert(oldHashTable[i], oldSummaryTable[i]); } } setRebuildThreshold(); } private void setRebuildThreshold() { if (hashTable_.length > nomEntries_) { rebuildThreshold_ = (int) (hashTable_.length * ThetaUtil.REBUILD_THRESHOLD); } else { rebuildThreshold_ = (int) (hashTable_.length * ThetaUtil.RESIZE_THRESHOLD); } } @SuppressWarnings("unchecked") protected void insertSummary(final int index, final S summary) { if (summaryTable_ == null) { summaryTable_ = (S[]) Array.newInstance(summary.getClass(), hashTable_.length); } summaryTable_[index] = summary; } @Override public TupleSketchIterator<S> iterator() { return new TupleSketchIterator<>(hashTable_, summaryTable_); } }
2,550
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/HashTables.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; import static java.lang.Math.ceil; import static java.lang.Math.max; import static java.lang.Math.min; import static org.apache.datasketches.common.Util.ceilingIntPowerOf2; import static org.apache.datasketches.thetacommon.HashOperations.hashInsertOnly; import static org.apache.datasketches.thetacommon.HashOperations.hashSearch; import java.lang.reflect.Array; import org.apache.datasketches.thetacommon.ThetaUtil; @SuppressWarnings("unchecked") class HashTables<S extends Summary> { long[] hashTable = null; S[] summaryTable = null; int lgTableSize = 0; int numKeys = 0; HashTables() { } //must have valid entries void fromSketch(final Sketch<S> sketch) { numKeys = sketch.getRetainedEntries(); lgTableSize = getLgTableSize(numKeys); hashTable = new long[1 << lgTableSize]; final TupleSketchIterator<S> it = sketch.iterator(); while (it.next()) { final long hash = it.getHash(); final int index = hashInsertOnly(hashTable, lgTableSize, hash); final S mySummary = (S)it.getSummary().copy(); if (summaryTable == null) { summaryTable = (S[]) Array.newInstance(mySummary.getClass(), 1 << lgTableSize); } summaryTable[index] = mySummary; } } //must have valid entries void fromSketch(final org.apache.datasketches.theta.Sketch sketch, final S summary) { numKeys = sketch.getRetainedEntries(true); lgTableSize = getLgTableSize(numKeys); hashTable = new long[1 << lgTableSize]; final org.apache.datasketches.theta.HashIterator it = sketch.iterator(); while (it.next()) { final long hash = it.get(); final int index = hashInsertOnly(hashTable, lgTableSize, hash); final S mySummary = (S)summary.copy(); if (summaryTable == null) { summaryTable = (S[]) Array.newInstance(mySummary.getClass(), 1 << lgTableSize); } summaryTable[index] = mySummary; } } private void fromArrays(final long[] hashArr, final S[] summaryArr, final int count) { numKeys = count; lgTableSize = getLgTableSize(count); summaryTable = null; hashTable = new long[1 << lgTableSize]; for (int i = 0; i < count; i++) { final long hash = hashArr[i]; final int index = hashInsertOnly(hashTable, lgTableSize, hash); final S mySummary = summaryArr[i]; if (summaryTable == null) { summaryTable = (S[]) Array.newInstance(mySummary.getClass(), 1 << lgTableSize); } summaryTable[index] = summaryArr[i]; } } //For Tuple Sketches HashTables<S> getIntersectHashTables( final Sketch<S> nextTupleSketch, final long thetaLong, final SummarySetOperations<S> summarySetOps) { //Match nextSketch data with local instance data, filtering by theta final int maxMatchSize = min(numKeys, nextTupleSketch.getRetainedEntries()); final long[] matchHashArr = new long[maxMatchSize]; final S[] matchSummariesArr = Util.newSummaryArray(summaryTable, maxMatchSize); int matchCount = 0; final TupleSketchIterator<S> it = nextTupleSketch.iterator(); while (it.next()) { final long hash = it.getHash(); if (hash >= thetaLong) { continue; } final int index = hashSearch(hashTable, lgTableSize, hash); if (index < 0) { continue; } //Copy the intersecting items from local hashTables_ // sequentially into local matchHashArr_ and matchSummaries_ matchHashArr[matchCount] = hash; matchSummariesArr[matchCount] = summarySetOps.intersection(summaryTable[index], it.getSummary()); matchCount++; } final HashTables<S> resultHT = new HashTables<>(); resultHT.fromArrays(matchHashArr, matchSummariesArr, matchCount); return resultHT; } //For Theta Sketches HashTables<S> getIntersectHashTables( final org.apache.datasketches.theta.Sketch nextThetaSketch, final long thetaLong, final SummarySetOperations<S> summarySetOps, final S summary) { final Class<S> summaryType = (Class<S>) summary.getClass(); //Match nextSketch data with local instance data, filtering by theta final int maxMatchSize = min(numKeys, nextThetaSketch.getRetainedEntries()); final long[] matchHashArr = new long[maxMatchSize]; final S[] matchSummariesArr = (S[]) Array.newInstance(summaryType, maxMatchSize); int matchCount = 0; final org.apache.datasketches.theta.HashIterator it = nextThetaSketch.iterator(); //scan B & search A(hashTable) for match while (it.next()) { final long hash = it.get(); if (hash >= thetaLong) { continue; } final int index = hashSearch(hashTable, lgTableSize, hash); if (index < 0) { continue; } //Copy the intersecting items from local hashTables_ // sequentially into local matchHashArr_ and matchSummaries_ matchHashArr[matchCount] = hash; matchSummariesArr[matchCount] = summarySetOps.intersection(summaryTable[index], summary); matchCount++; } final HashTables<S> resultHT = new HashTables<>(); resultHT.fromArrays(matchHashArr, matchSummariesArr, matchCount); return resultHT; } void clear() { hashTable = null; summaryTable = null; lgTableSize = 0; numKeys = 0; } static int getLgTableSize(final int count) { final int tableSize = max(ceilingIntPowerOf2((int) ceil(count / 0.75)), 1 << ThetaUtil.MIN_LG_NOM_LONGS); return Integer.numberOfTrailingZeros(tableSize); } }
2,551
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/SummaryDeserializer.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; import org.apache.datasketches.memory.Memory; /** * Interface for deserializing user-defined Summary * @param <S> type of Summary */ public interface SummaryDeserializer<S extends Summary> { /** * This is to create an instance of a Summary given a serialized representation. * The user may assume that the start of the given Memory is the correct place to start * deserializing. However, the user must be able to determine the number of bytes required to * deserialize the summary as the capacity of the given Memory may * include multiple such summaries and may be much larger than required for a single summary. * @param mem Memory object with serialized representation of a Summary * @return DeserializedResult object, which contains a Summary object and number of bytes read * from the Memory */ public DeserializeResult<S> heapifySummary(Memory mem); }
2,552
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/JaccardSimilarity.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; import static java.lang.Math.max; import static java.lang.Math.min; import static org.apache.datasketches.common.Util.ceilingIntPowerOf2; import static org.apache.datasketches.thetacommon.BoundsOnRatiosInTupleSketchedSets.getEstimateOfBoverA; import static org.apache.datasketches.thetacommon.BoundsOnRatiosInTupleSketchedSets.getLowerBoundForBoverA; import static org.apache.datasketches.thetacommon.BoundsOnRatiosInTupleSketchedSets.getUpperBoundForBoverA; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.thetacommon.ThetaUtil; /** * Jaccard similarity of two Tuple Sketches, or alternatively, of a Tuple and Theta Sketch. * * <p>Note: only retained hash values are compared, and the Tuple summary values are not accounted for in the * similarity measure.</p> * * @author Lee Rhodes * @author David Cromberge */ public final class JaccardSimilarity { private static final double[] ZEROS = {0.0, 0.0, 0.0}; // LB, Estimate, UB private static final double[] ONES = {1.0, 1.0, 1.0}; /** * Computes the Jaccard similarity index with upper and lower bounds. The Jaccard similarity index * <i>J(A,B) = (A ^ B)/(A U B)</i> is used to measure how similar the two sketches are to each * other. If J = 1.0, the sketches are considered equal. If J = 0, the two sketches are * distinct from each other. A Jaccard of .95 means the overlap between the two * populations is 95% of the union of the two populations. * * <p>Note: For very large pairs of sketches, where the configured nominal entries of the sketches * are 2^25 or 2^26, this method may produce unpredictable results. * * @param sketchA The first argument, a Tuple sketch with summary type <i>S</i> * @param sketchB The second argument, a Tuple sketch with summary type <i>S</i> * @param summarySetOps instance of SummarySetOperations used to unify or intersect summaries. * @param <S> Summary * @return a double array {LowerBound, Estimate, UpperBound} of the Jaccard index. * The Upper and Lower bounds are for a confidence interval of 95.4% or +/- 2 standard deviations. */ public static <S extends Summary> double[] jaccard( final Sketch<S> sketchA, final Sketch<S> sketchB, final SummarySetOperations<S> summarySetOps) { //Corner case checks if (sketchA == null || sketchB == null) { return ZEROS.clone(); } if (sketchA.isEmpty() && sketchB.isEmpty()) { return ONES.clone(); } if (sketchA.isEmpty() || sketchB.isEmpty()) { return ZEROS.clone(); } final int countA = sketchA.getRetainedEntries(); final int countB = sketchB.getRetainedEntries(); //Create the Union final int minK = 1 << ThetaUtil.MIN_LG_NOM_LONGS; final int maxK = 1 << ThetaUtil.MAX_LG_NOM_LONGS; final int newK = max(min(ceilingIntPowerOf2(countA + countB), maxK), minK); final Union<S> union = new Union<>(newK, summarySetOps); union.union(sketchA); union.union(sketchB); final Sketch<S> unionAB = union.getResult(); final long thetaLongUAB = unionAB.getThetaLong(); final long thetaLongA = sketchA.getThetaLong(); final long thetaLongB = sketchB.getThetaLong(); final int countUAB = unionAB.getRetainedEntries(); //Check for identical data if (countUAB == countA && countUAB == countB && thetaLongUAB == thetaLongA && thetaLongUAB == thetaLongB) { return ONES.clone(); } //Create the Intersection final Intersection<S> inter = new Intersection<>(summarySetOps); inter.intersect(sketchA); inter.intersect(sketchB); inter.intersect(unionAB); //ensures that intersection is a subset of the union final Sketch<S> interABU = inter.getResult(); final double lb = getLowerBoundForBoverA(unionAB, interABU); final double est = getEstimateOfBoverA(unionAB, interABU); final double ub = getUpperBoundForBoverA(unionAB, interABU); return new double[] {lb, est, ub}; } /** * Computes the Jaccard similarity index with upper and lower bounds. The Jaccard similarity index * <i>J(A,B) = (A ^ B)/(A U B)</i> is used to measure how similar the two sketches are to each * other. If J = 1.0, the sketches are considered equal. If J = 0, the two sketches are * distinct from each other. A Jaccard of .95 means the overlap between the two * populations is 95% of the union of the two populations. * * <p>Note: For very large pairs of sketches, where the configured nominal entries of the sketches * are 2^25 or 2^26, this method may produce unpredictable results. * * @param sketchA The first argument, a Tuple sketch with summary type <i>S</i> * @param sketchB The second argument, a Theta sketch * @param summary the given proxy summary for the theta sketch, which doesn't have one. * This may not be null. * @param summarySetOps instance of SummarySetOperations used to unify or intersect summaries. * @param <S> Summary * @return a double array {LowerBound, Estimate, UpperBound} of the Jaccard index. * The Upper and Lower bounds are for a confidence interval of 95.4% or +/- 2 standard deviations. */ public static <S extends Summary> double[] jaccard( final Sketch<S> sketchA, final org.apache.datasketches.theta.Sketch sketchB, final S summary, final SummarySetOperations<S> summarySetOps) { // Null case checks if (summary == null) { throw new SketchesArgumentException("Summary cannot be null."); } //Corner case checks if (sketchA == null || sketchB == null) { return ZEROS.clone(); } if (sketchA.isEmpty() && sketchB.isEmpty()) { return ONES.clone(); } if (sketchA.isEmpty() || sketchB.isEmpty()) { return ZEROS.clone(); } final int countA = sketchA.getRetainedEntries(); final int countB = sketchB.getRetainedEntries(true); //Create the Union final int minK = 1 << ThetaUtil.MIN_LG_NOM_LONGS; final int maxK = 1 << ThetaUtil.MAX_LG_NOM_LONGS; final int newK = max(min(ceilingIntPowerOf2(countA + countB), maxK), minK); final Union<S> union = new Union<>(newK, summarySetOps); union.union(sketchA); union.union(sketchB, summary); final Sketch<S> unionAB = union.getResult(); final long thetaLongUAB = unionAB.getThetaLong(); final long thetaLongA = sketchA.getThetaLong(); final long thetaLongB = sketchB.getThetaLong(); final int countUAB = unionAB.getRetainedEntries(); //Check for identical data if (countUAB == countA && countUAB == countB && thetaLongUAB == thetaLongA && thetaLongUAB == thetaLongB) { return ONES.clone(); } //Create the Intersection final Intersection<S> inter = new Intersection<>(summarySetOps); inter.intersect(sketchA); inter.intersect(sketchB, summary); inter.intersect(unionAB); //ensures that intersection is a subset of the union final Sketch<S> interABU = inter.getResult(); final double lb = getLowerBoundForBoverA(unionAB, interABU); final double est = getEstimateOfBoverA(unionAB, interABU); final double ub = getUpperBoundForBoverA(unionAB, interABU); return new double[] {lb, est, ub}; } /** * Returns true if the two given sketches have exactly the same hash values and the same * theta values. Thus, they are equivalent. * @param sketchA The first argument, a Tuple sketch with summary type <i>S</i> * @param sketchB The second argument, a Tuple sketch with summary type <i>S</i> * @param summarySetOps instance of SummarySetOperations used to unify or intersect summaries. * @param <S> Summary * @return true if the two given sketches have exactly the same hash values and the same * theta values. */ public static <S extends Summary> boolean exactlyEqual( final Sketch<S> sketchA, final Sketch<S> sketchB, final SummarySetOperations<S> summarySetOps) { //Corner case checks if (sketchA == null || sketchB == null) { return false; } if (sketchA == sketchB) { return true; } if (sketchA.isEmpty() && sketchB.isEmpty()) { return true; } if (sketchA.isEmpty() || sketchB.isEmpty()) { return false; } final int countA = sketchA.getRetainedEntries(); final int countB = sketchB.getRetainedEntries(); //Create the Union final Union<S> union = new Union<>(ceilingIntPowerOf2(countA + countB), summarySetOps); union.union(sketchA); union.union(sketchB); final Sketch<S> unionAB = union.getResult(); final long thetaLongUAB = unionAB.getThetaLong(); final long thetaLongA = sketchA.getThetaLong(); final long thetaLongB = sketchB.getThetaLong(); final int countUAB = unionAB.getRetainedEntries(); //Check for identical counts and thetas if (countUAB == countA && countUAB == countB && thetaLongUAB == thetaLongA && thetaLongUAB == thetaLongB) { return true; } return false; } /** * Returns true if the two given sketches have exactly the same hash values and the same * theta values. Thus, they are equivalent. * @param sketchA The first argument, a Tuple sketch with summary type <i>S</i> * @param sketchB The second argument, a Theta sketch * @param summary the given proxy summary for the theta sketch, which doesn't have one. * This may not be null. * @param summarySetOps instance of SummarySetOperations used to unify or intersect summaries. * @param <S> Summary * @return true if the two given sketches have exactly the same hash values and the same * theta values. */ public static <S extends Summary> boolean exactlyEqual( final Sketch<S> sketchA, final org.apache.datasketches.theta.Sketch sketchB, final S summary, final SummarySetOperations<S> summarySetOps) { // Null case checks if (summary == null) { throw new SketchesArgumentException("Summary cannot be null."); } //Corner case checks if (sketchA == null || sketchB == null) { return false; } if (sketchA.isEmpty() && sketchB.isEmpty()) { return true; } if (sketchA.isEmpty() || sketchB.isEmpty()) { return false; } final int countA = sketchA.getRetainedEntries(); final int countB = sketchB.getRetainedEntries(true); //Create the Union final Union<S> union = new Union<>(ceilingIntPowerOf2(countA + countB), summarySetOps); union.union(sketchA); union.union(sketchB, summary); final Sketch<S> unionAB = union.getResult(); final long thetaLongUAB = unionAB.getThetaLong(); final long thetaLongA = sketchA.getThetaLong(); final long thetaLongB = sketchB.getThetaLong(); final int countUAB = unionAB.getRetainedEntries(); //Check for identical counts and thetas if (countUAB == countA && countUAB == countB && thetaLongUAB == thetaLongA && thetaLongUAB == thetaLongB) { return true; } return false; } /** * Tests similarity of a measured Sketch against an expected Sketch. * Computes the lower bound of the Jaccard index <i>J<sub>LB</sub></i> of the measured and * expected sketches. * if <i>J<sub>LB</sub> &ge; threshold</i>, then the sketches are considered to be * similar with a confidence of 97.7%. * * @param measured a Tuple sketch with summary type <i>S</i> to be tested * @param expected the reference Tuple sketch with summary type <i>S</i> that is considered to be correct. * @param summarySetOps instance of SummarySetOperations used to unify or intersect summaries. * @param threshold a real value between zero and one. * @param <S> Summary * @return if true, the similarity of the two sketches is greater than the given threshold * with at least 97.7% confidence. */ public static <S extends Summary> boolean similarityTest( final Sketch<S> measured, final Sketch<S> expected, final SummarySetOperations<S> summarySetOps, final double threshold) { //index 0: the lower bound //index 1: the mean estimate //index 2: the upper bound final double jRatioLB = jaccard(measured, expected, summarySetOps)[0]; //choosing the lower bound return jRatioLB >= threshold; } /** * Tests similarity of a measured Sketch against an expected Sketch. * Computes the lower bound of the Jaccard index <i>J<sub>LB</sub></i> of the measured and * expected sketches. * if <i>J<sub>LB</sub> &ge; threshold</i>, then the sketches are considered to be * similar with a confidence of 97.7%. * * @param measured a Tuple sketch with summary type <i>S</i> to be tested * @param expected the reference Theta sketch that is considered to be correct. * @param summary the given proxy summary for the theta sketch, which doesn't have one. * This may not be null. * @param summarySetOps instance of SummarySetOperations used to unify or intersect summaries. * @param threshold a real value between zero and one. * @param <S> Summary * @return if true, the similarity of the two sketches is greater than the given threshold * with at least 97.7% confidence. */ public static <S extends Summary> boolean similarityTest( final Sketch<S> measured, final org.apache.datasketches.theta.Sketch expected, final S summary, final SummarySetOperations<S> summarySetOps, final double threshold) { //index 0: the lower bound //index 1: the mean estimate //index 2: the upper bound final double jRatioLB = jaccard(measured, expected, summary, summarySetOps)[0]; //choosing the lower bound return jRatioLB >= threshold; } /** * Tests dissimilarity of a measured Sketch against an expected Sketch. * Computes the upper bound of the Jaccard index <i>J<sub>UB</sub></i> of the measured and * expected sketches. * if <i>J<sub>UB</sub> &le; threshold</i>, then the sketches are considered to be * dissimilar with a confidence of 97.7%. * * @param measured a Tuple sketch with summary type <i>S</i> to be tested * @param expected the reference Theta sketch that is considered to be correct. * @param summarySetOps instance of SummarySetOperations used to unify or intersect summaries. * @param threshold a real value between zero and one. * @param <S> Summary * @return if true, the dissimilarity of the two sketches is greater than the given threshold * with at least 97.7% confidence. */ public static <S extends Summary> boolean dissimilarityTest( final Sketch<S> measured, final Sketch<S> expected, final SummarySetOperations<S> summarySetOps, final double threshold) { //index 0: the lower bound //index 1: the mean estimate //index 2: the upper bound final double jRatioUB = jaccard(measured, expected, summarySetOps)[2]; //choosing the upper bound return jRatioUB <= threshold; } /** * Tests dissimilarity of a measured Sketch against an expected Sketch. * Computes the upper bound of the Jaccard index <i>J<sub>UB</sub></i> of the measured and * expected sketches. * if <i>J<sub>UB</sub> &le; threshold</i>, then the sketches are considered to be * dissimilar with a confidence of 97.7%. * * @param measured a Tuple sketch with summary type <i>S</i> to be tested * @param expected the reference Theta sketch that is considered to be correct. * @param summary the given proxy summary for the theta sketch, which doesn't have one. * This may not be null. * @param summarySetOps instance of SummarySetOperations used to unify or intersect summaries. * @param threshold a real value between zero and one. * @param <S> Summary * @return if true, the dissimilarity of the two sketches is greater than the given threshold * with at least 97.7% confidence. */ public static <S extends Summary> boolean dissimilarityTest( final Sketch<S> measured, final org.apache.datasketches.theta.Sketch expected, final S summary, final SummarySetOperations<S> summarySetOps, final double threshold) { //index 0: the lower bound //index 1: the mean estimate //index 2: the upper bound final double jRatioUB = jaccard(measured, expected, summary, summarySetOps)[2]; //choosing the upper bound return jRatioUB <= threshold; } }
2,553
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/AnotB.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; import static java.lang.Math.min; import static org.apache.datasketches.common.Util.exactLog2OfLong; import static org.apache.datasketches.thetacommon.HashOperations.convertToHashTable; import static org.apache.datasketches.thetacommon.HashOperations.hashSearch; import java.lang.reflect.Method; import java.util.Arrays; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.common.SketchesStateException; import org.apache.datasketches.common.SuppressFBWarnings; import org.apache.datasketches.thetacommon.SetOperationCornerCases; import org.apache.datasketches.thetacommon.SetOperationCornerCases.AnotbAction; import org.apache.datasketches.thetacommon.SetOperationCornerCases.CornerCase; import org.apache.datasketches.thetacommon.ThetaUtil; /** * Computes a set difference, A-AND-NOT-B, of two generic tuple sketches. * This class includes both stateful and stateless operations. * * <p>The stateful operation is as follows:</p> * <pre><code> * AnotB anotb = new AnotB(); * * anotb.setA(Sketch skA); //The first argument. * anotb.notB(Sketch skB); //The second (subtraction) argument. * anotb.notB(Sketch skC); // ...any number of additional subtractions... * anotb.getResult(false); //Get an interim result. * anotb.notB(Sketch skD); //Additional subtractions. * anotb.getResult(true); //Final result and resets the AnotB operator. * </code></pre> * * <p>The stateless operation is as follows:</p> * <pre><code> * AnotB anotb = new AnotB(); * * CompactSketch csk = anotb.aNotB(Sketch skA, Sketch skB); * </code></pre> * * <p>Calling the <i>setA</i> operation a second time essentially clears the internal state and loads * the new sketch.</p> * * <p>The stateless and stateful operations are independent of each other.</p> * * @param <S> Type of Summary * * @author Lee Rhodes */ @SuppressFBWarnings(value = "DP_DO_INSIDE_DO_PRIVILEGED", justification = "Defer fix") public final class AnotB<S extends Summary> { private boolean empty_ = true; private long thetaLong_ = Long.MAX_VALUE; private long[] hashArr_ = null; //always in compact form, not necessarily sorted private S[] summaryArr_ = null; //always in compact form, not necessarily sorted private int curCount_ = 0; private static final Method GET_CACHE; static { try { GET_CACHE = org.apache.datasketches.theta.Sketch.class.getDeclaredMethod("getCache"); GET_CACHE.setAccessible(true); } catch (final Exception e) { throw new SketchesStateException("Could not reflect getCache(): " + e); } } /** * This is part of a multistep, stateful AnotB operation and sets the given Tuple sketch as the * first argument <i>A</i> of <i>A-AND-NOT-B</i>. This overwrites the internal state of this * AnotB operator with the contents of the given sketch. * This sets the stage for multiple following <i>notB</i> steps. * * <p>An input argument of null will throw an exception.</p> * * <p>Rationale: In mathematics a "null set" is a set with no members, which we call an empty set. * That is distinctly different from the java <i>null</i>, which represents a nonexistent object. * In most cases it is a programming error due to some object that was not properly initialized. * With a null as the first argument, we cannot know what the user's intent is. * Since it is very likely that a <i>null</i> is a programming error, we throw a an exception.</p> * * <p>An empty input argument will set the internal state to empty.</p> * * <p>Rationale: An empty set is a mathematically legal concept. Although it makes any subsequent, * valid argument for B irrelevant, we must allow this and assume the user knows what they are * doing.</p> * * <p>Performing {@link #getResult(boolean)} just after this step will return a compact form of * the given argument.</p> * * @param skA The incoming sketch for the first argument, <i>A</i>. */ public void setA(final Sketch<S> skA) { if (skA == null) { reset(); throw new SketchesArgumentException("The input argument <i>A</i> may not be null"); } empty_ = skA.isEmpty(); thetaLong_ = skA.getThetaLong(); final DataArrays<S> da = getCopyOfDataArraysTuple(skA); summaryArr_ = da.summaryArr; //it may be null hashArr_ = da.hashArr; //it may be null curCount_ = (hashArr_ == null) ? 0 : hashArr_.length; } /** * This is part of a multistep, stateful AnotB operation and sets the given Tuple sketch as the * second (or <i>n+1</i>th) argument <i>B</i> of <i>A-AND-NOT-B</i>. * Performs an <i>AND NOT</i> operation with the existing internal state of this AnotB operator. * * <p>An input argument of null or empty is ignored.</p> * * <p>Rationale: A <i>null</i> for the second or following arguments is more tolerable because * <i>A NOT null</i> is still <i>A</i> even if we don't know exactly what the null represents. It * clearly does not have any content that overlaps with <i>A</i>. Also, because this can be part of * a multistep operation with multiple <i>notB</i> steps. Other following steps can still produce * a valid result.</p> * * <p>Use {@link #getResult(boolean)} to obtain the result.</p> * * @param skB The incoming Tuple sketch for the second (or following) argument <i>B</i>. */ public void notB(final Sketch<S> skB) { if (skB == null) { return; } //ignore final long thetaLongB = skB.getThetaLong(); final int countB = skB.getRetainedEntries(); final boolean emptyB = skB.isEmpty(); final int id = SetOperationCornerCases.createCornerCaseId(thetaLong_, curCount_, empty_, thetaLongB, countB, emptyB); final CornerCase cCase = CornerCase.caseIdToCornerCase(id); final AnotbAction anotbAction = cCase.getAnotbAction(); switch (anotbAction) { case EMPTY_1_0_T: { reset(); break; } case DEGEN_MIN_0_F: { reset(); thetaLong_ = min(thetaLong_, thetaLongB); empty_ = false; break; } case DEGEN_THA_0_F: { empty_ = false; curCount_ = 0; //thetaLong_ is ok break; } case TRIM_A: { thetaLong_ = min(thetaLong_, thetaLongB); final DataArrays<S> da = trimAndCopyDataArrays(hashArr_, summaryArr_, thetaLong_, true); hashArr_ = da.hashArr; curCount_ = (hashArr_ == null) ? 0 : hashArr_.length; summaryArr_ = da.summaryArr; //empty_ = is whatever SkA is, break; } case SKETCH_A: { break; //result is already in A } case FULL_ANOTB: { //both A and B should have valid entries. thetaLong_ = min(thetaLong_, thetaLongB); final DataArrays<S> daR = getCopyOfResultArraysTuple(thetaLong_, curCount_, hashArr_, summaryArr_, skB); hashArr_ = daR.hashArr; curCount_ = (hashArr_ == null) ? 0 : hashArr_.length; summaryArr_ = daR.summaryArr; //empty_ = is whatever SkA is, } //default: not possible } } /** * This is part of a multistep, stateful AnotB operation and sets the given Theta sketch as the * second (or <i>n+1</i>th) argument <i>B</i> of <i>A-AND-NOT-B</i>. * Performs an <i>AND NOT</i> operation with the existing internal state of this AnotB operator. * Calls to this method can be intermingled with calls to * {@link #notB(org.apache.datasketches.theta.Sketch)}. * * <p>An input argument of null or empty is ignored.</p> * * <p>Rationale: A <i>null</i> for the second or following arguments is more tolerable because * <i>A NOT null</i> is still <i>A</i> even if we don't know exactly what the null represents. It * clearly does not have any content that overlaps with <i>A</i>. Also, because this can be part of * a multistep operation with multiple <i>notB</i> steps. Other following steps can still produce * a valid result.</p> * * <p>Use {@link #getResult(boolean)} to obtain the result.</p> * * @param skB The incoming Theta sketch for the second (or following) argument <i>B</i>. */ public void notB(final org.apache.datasketches.theta.Sketch skB) { if (skB == null) { return; } //ignore final long thetaLongB = skB.getThetaLong(); final int countB = skB.getRetainedEntries(); final boolean emptyB = skB.isEmpty(); final int id = SetOperationCornerCases.createCornerCaseId(thetaLong_, curCount_, empty_, thetaLongB, countB, emptyB); final CornerCase cCase = CornerCase.caseIdToCornerCase(id); final AnotbAction anotbAction = cCase.getAnotbAction(); switch (anotbAction) { case EMPTY_1_0_T: { reset(); break; } case DEGEN_MIN_0_F: { reset(); thetaLong_ = min(thetaLong_, thetaLongB); empty_ = false; break; } case DEGEN_THA_0_F: { empty_ = false; curCount_ = 0; //thetaLong_ is ok break; } case TRIM_A: { thetaLong_ = min(thetaLong_, thetaLongB); final DataArrays<S> da = trimAndCopyDataArrays(hashArr_, summaryArr_,thetaLong_, true); hashArr_ = da.hashArr; curCount_ = (hashArr_ == null) ? 0 : hashArr_.length; summaryArr_ = da.summaryArr; break; } case SKETCH_A: { break; //result is already in A } case FULL_ANOTB: { //both A and B should have valid entries. thetaLong_ = min(thetaLong_, thetaLongB); final DataArrays<S> daB = getCopyOfResultArraysTheta(thetaLong_, curCount_, hashArr_, summaryArr_, skB); hashArr_ = daB.hashArr; curCount_ = (hashArr_ == null) ? 0 : hashArr_.length; summaryArr_ = daB.summaryArr; //empty_ = is whatever SkA is, } //default: not possible } } /** * Gets the result of the multistep, stateful operation AnotB that have been executed with calls * to {@link #setA(Sketch)} and ({@link #notB(Sketch)} or * {@link #notB(org.apache.datasketches.theta.Sketch)}). * * @param reset If <i>true</i>, clears this operator to the empty state after this result is * returned. Set this to <i>false</i> if you wish to obtain an intermediate result. * @return the result of this operation as an unordered {@link CompactSketch}. */ public CompactSketch<S> getResult(final boolean reset) { final CompactSketch<S> result; if (curCount_ == 0) { result = new CompactSketch<>(null, null, thetaLong_, thetaLong_ == Long.MAX_VALUE); } else { result = new CompactSketch<>(hashArr_, Util.copySummaryArray(summaryArr_), thetaLong_, false); } if (reset) { reset(); } return result; } /** * Returns the A-and-not-B set operation on the two given Tuple sketches. * * <p>This a stateless operation and has no impact on the internal state of this operator. * Thus, this is not an accumulating update and is independent of the {@link #setA(Sketch)}, * {@link #notB(Sketch)}, {@link #notB(org.apache.datasketches.theta.Sketch)}, and * {@link #getResult(boolean)} methods.</p> * * <p>If either argument is null an exception is thrown.</p> * * <p>Rationale: In mathematics a "null set" is a set with no members, which we call an empty set. * That is distinctly different from the java <i>null</i>, which represents a nonexistent object. * In most cases it is a programming error due to some object that was not properly initialized. * With a null as the first argument, we cannot know what the user's intent is. * With a null as the second argument, we can't ignore it as we must return a result and there is * no following possible viable arguments for the second argument. * Since it is very likely that a <i>null</i> is a programming error, we throw an exception.</p> * * @param skA The incoming Tuple sketch for the first argument * @param skB The incoming Tuple sketch for the second argument * @param <S> Type of Summary * @return the result as an unordered {@link CompactSketch} */ @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "hashArr and summaryArr are guaranteed to be valid due to the switch on CornerCase") public static <S extends Summary> CompactSketch<S> aNotB( final Sketch<S> skA, final Sketch<S> skB) { if (skA == null || skB == null) { throw new SketchesArgumentException("Neither argument may be null for this stateless operation."); } final long thetaLongA = skA.getThetaLong(); final int countA = skA.getRetainedEntries(); final boolean emptyA = skA.isEmpty(); final long thetaLongB = skB.getThetaLong(); final int countB = skB.getRetainedEntries(); final boolean emptyB = skB.isEmpty(); final int id = SetOperationCornerCases.createCornerCaseId(thetaLongA, countA, emptyA, thetaLongB, countB, emptyB); final CornerCase cCase = CornerCase.caseIdToCornerCase(id); final AnotbAction anotbAction = cCase.getAnotbAction(); CompactSketch<S> result = null; switch (anotbAction) { case EMPTY_1_0_T: { result = new CompactSketch<>(null, null, Long.MAX_VALUE, true); break; } case DEGEN_MIN_0_F: { final long thetaLong = min(thetaLongA, thetaLongB); result = new CompactSketch<>(null, null, thetaLong, false); break; } case DEGEN_THA_0_F: { result = new CompactSketch<>(null, null, thetaLongA, false); break; } case TRIM_A: { final DataArrays<S> daA = getCopyOfDataArraysTuple(skA); final long[] hashArrA = daA.hashArr; final S[] summaryArrA = daA.summaryArr; final long minThetaLong = min(thetaLongA, thetaLongB); final DataArrays<S> da = trimAndCopyDataArrays(hashArrA, summaryArrA, minThetaLong, false); result = new CompactSketch<>(da.hashArr, da.summaryArr, minThetaLong, skA.empty_); break; } case SKETCH_A: { final DataArrays<S> daA = getCopyOfDataArraysTuple(skA); result = new CompactSketch<>(daA.hashArr, daA.summaryArr, thetaLongA, skA.empty_); break; } case FULL_ANOTB: { //both A and B should have valid entries. final DataArrays<S> daA = getCopyOfDataArraysTuple(skA); final long minThetaLong = min(thetaLongA, thetaLongB); final DataArrays<S> daR = getCopyOfResultArraysTuple(minThetaLong, daA.hashArr.length, daA.hashArr, daA.summaryArr, skB); final int countR = (daR.hashArr == null) ? 0 : daR.hashArr.length; if (countR == 0) { result = new CompactSketch<>(null, null, minThetaLong, minThetaLong == Long.MAX_VALUE); } else { result = new CompactSketch<>(daR.hashArr, daR.summaryArr, minThetaLong, false); } } //default: not possible } return result; } /** * Returns the A-and-not-B set operation on a Tuple sketch and a Theta sketch. * * <p>This a stateless operation and has no impact on the internal state of this operator. * Thus, this is not an accumulating update and is independent of the {@link #setA(Sketch)}, * {@link #notB(Sketch)}, {@link #notB(org.apache.datasketches.theta.Sketch)}, and * {@link #getResult(boolean)} methods.</p> * * <p>If either argument is null an exception is thrown.</p> * * <p>Rationale: In mathematics a "null set" is a set with no members, which we call an empty set. * That is distinctly different from the java <i>null</i>, which represents a nonexistent object. * In most cases it is a programming error due to some object that was not properly initialized. * With a null as the first argument, we cannot know what the user's intent is. * With a null as the second argument, we can't ignore it as we must return a result and there is * no following possible viable arguments for the second argument. * Since it is very likely that a <i>null</i> is a programming error for either argument * we throw a an exception.</p> * * @param skA The incoming Tuple sketch for the first argument * @param skB The incoming Theta sketch for the second argument * @param <S> Type of Summary * @return the result as an unordered {@link CompactSketch} */ @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "hashArr and summaryArr are guaranteed to be valid due to the switch on CornerCase") public static <S extends Summary> CompactSketch<S> aNotB( final Sketch<S> skA, final org.apache.datasketches.theta.Sketch skB) { if (skA == null || skB == null) { throw new SketchesArgumentException("Neither argument may be null for this stateless operation."); } final long thetaLongA = skA.getThetaLong(); final int countA = skA.getRetainedEntries(); final boolean emptyA = skA.isEmpty(); final long thetaLongB = skB.getThetaLong(); final int countB = skB.getRetainedEntries(); final boolean emptyB = skB.isEmpty(); final int id = SetOperationCornerCases.createCornerCaseId(thetaLongA, countA, emptyA, thetaLongB, countB, emptyB); final CornerCase cCase = CornerCase.caseIdToCornerCase(id); final AnotbAction anotbAction = cCase.getAnotbAction(); CompactSketch<S> result = null; switch (anotbAction) { case EMPTY_1_0_T: { result = new CompactSketch<>(null, null, Long.MAX_VALUE, true); break; } case DEGEN_MIN_0_F: { final long thetaLong = min(thetaLongA, thetaLongB); result = new CompactSketch<>(null, null, thetaLong, false); break; } case DEGEN_THA_0_F: { result = new CompactSketch<>(null, null, thetaLongA, false); break; } case TRIM_A: { final DataArrays<S> daA = getCopyOfDataArraysTuple(skA); final long[] hashArrA = daA.hashArr; final S[] summaryArrA = daA.summaryArr; final long minThetaLong = min(thetaLongA, thetaLongB); final DataArrays<S> da = trimAndCopyDataArrays(hashArrA, summaryArrA, minThetaLong, false); result = new CompactSketch<>(da.hashArr, da.summaryArr, minThetaLong, skA.empty_); break; } case SKETCH_A: { final DataArrays<S> daA = getCopyOfDataArraysTuple(skA); result = new CompactSketch<>(daA.hashArr, daA.summaryArr, thetaLongA, skA.empty_); break; } case FULL_ANOTB: { //both A and B have valid entries. final DataArrays<S> daA = getCopyOfDataArraysTuple(skA); final long minThetaLong = min(thetaLongA, thetaLongB); @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "hashArr and summaryArr are guaranteed to be valid due to the switch on CornerCase") final DataArrays<S> daR = getCopyOfResultArraysTheta(minThetaLong, daA.hashArr.length, daA.hashArr, daA.summaryArr, skB); final int countR = (daR.hashArr == null) ? 0 : daR.hashArr.length; if (countR == 0) { result = new CompactSketch<>(null, null, minThetaLong, minThetaLong == Long.MAX_VALUE); } else { result = new CompactSketch<>(daR.hashArr, daR.summaryArr, minThetaLong, false); } } //default: not possible } return result; } //restricted static class DataArrays<S extends Summary> { DataArrays() {} long[] hashArr; S[] summaryArr; } private static <S extends Summary> DataArrays<S> getCopyOfDataArraysTuple( final Sketch<S> sk) { final CompactSketch<S> csk; final DataArrays<S> da = new DataArrays<>(); if (sk instanceof CompactSketch) { csk = (CompactSketch<S>) sk; } else { csk = ((QuickSelectSketch<S>)sk).compact(); } final int count = csk.getRetainedEntries(); if (count == 0) { da.hashArr = null; da.summaryArr = null; } else { da.hashArr = csk.getHashArr().clone(); //deep copy, may not be sorted da.summaryArr = Util.copySummaryArray(csk.getSummaryArr()); } return da; } @SuppressWarnings("unchecked") //Both skA and skB must have entries (count > 0) private static <S extends Summary> DataArrays<S> getCopyOfResultArraysTuple( final long minThetaLong, final int countA, final long[] hashArrA, final S[] summaryArrA, final Sketch<S> skB) { final DataArrays<S> daR = new DataArrays<>(); //Rebuild/get hashtable of skB final long[] hashTableB; if (skB instanceof CompactSketch) { final CompactSketch<S> cskB = (CompactSketch<S>) skB; final int countB = skB.getRetainedEntries(); hashTableB = convertToHashTable(cskB.getHashArr(), countB, minThetaLong, ThetaUtil.REBUILD_THRESHOLD); } else { final QuickSelectSketch<S> qskB = (QuickSelectSketch<S>) skB; hashTableB = qskB.getHashTable(); } //build temporary arrays of skA final long[] tmpHashArrA = new long[countA]; final S[] tmpSummaryArrA = Util.newSummaryArray(summaryArrA, countA); //search for non matches and build temp arrays final int lgHTBLen = exactLog2OfLong(hashTableB.length); int nonMatches = 0; for (int i = 0; i < countA; i++) { final long hash = hashArrA[i]; if (hash != 0 && hash < minThetaLong) { //skips hashes of A >= minTheta final int index = hashSearch(hashTableB, lgHTBLen, hash); if (index == -1) { tmpHashArrA[nonMatches] = hash; tmpSummaryArrA[nonMatches] = (S) summaryArrA[i].copy(); nonMatches++; } } } daR.hashArr = Arrays.copyOfRange(tmpHashArrA, 0, nonMatches); daR.summaryArr = Arrays.copyOfRange(tmpSummaryArrA, 0, nonMatches); return daR; } @SuppressWarnings("unchecked") private static <S extends Summary> DataArrays<S> getCopyOfResultArraysTheta( final long minThetaLong, final int countA, final long[] hashArrA, final S[] summaryArrA, final org.apache.datasketches.theta.Sketch skB) { final DataArrays<S> daB = new DataArrays<>(); //Rebuild/get hashtable of skB final long[] hashTableB; //read only final long[] hashCacheB; try { hashCacheB = (long[])GET_CACHE.invoke(skB); } catch (final Exception e) { throw new SketchesStateException("Reflection Exception " + e); } if (skB instanceof org.apache.datasketches.theta.CompactSketch) { final int countB = skB.getRetainedEntries(true); hashTableB = convertToHashTable(hashCacheB, countB, minThetaLong, ThetaUtil.REBUILD_THRESHOLD); } else { hashTableB = hashCacheB; } //build temporary result arrays of skA final long[] tmpHashArrA = new long[countA]; final S[] tmpSummaryArrA = Util.newSummaryArray(summaryArrA, countA); //search for non matches and build temp arrays final int lgHTBLen = exactLog2OfLong(hashTableB.length); int nonMatches = 0; for (int i = 0; i < countA; i++) { final long hash = hashArrA[i]; if (hash != 0 && hash < minThetaLong) { //skips hashes of A >= minTheta final int index = hashSearch(hashTableB, lgHTBLen, hash); if (index == -1) { //not found tmpHashArrA[nonMatches] = hash; tmpSummaryArrA[nonMatches] = (S) summaryArrA[i].copy(); nonMatches++; } } } //trim the arrays daB.hashArr = Arrays.copyOfRange(tmpHashArrA, 0, nonMatches); daB.summaryArr = Arrays.copyOfRange(tmpSummaryArrA, 0, nonMatches); return daB; } @SuppressWarnings("unchecked") private static <S extends Summary> DataArrays<S> trimAndCopyDataArrays( final long[] hashArr, final S[] summaryArr, final long minThetaLong, final boolean copy) { //build temporary arrays final int countIn = hashArr.length; final long[] tmpHashArr = new long[countIn]; final S[] tmpSummaryArr = Util.newSummaryArray(summaryArr, countIn); int countResult = 0; for (int i = 0; i < countIn; i++) { final long hash = hashArr[i]; if (hash < minThetaLong) { tmpHashArr[countResult] = hash; tmpSummaryArr[countResult] = (S) (copy ? summaryArr[i].copy() : summaryArr[i]); countResult++; } else { continue; } } //Remove empty slots final DataArrays<S> da = new DataArrays<>(); da.hashArr = Arrays.copyOfRange(tmpHashArr, 0, countResult); da.summaryArr = Arrays.copyOfRange(tmpSummaryArr, 0, countResult); return da; } /** * Resets this operation back to the empty state. */ public void reset() { empty_ = true; thetaLong_ = Long.MAX_VALUE; hashArr_ = null; summaryArr_ = null; curCount_ = 0; } }
2,554
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/Sketches.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; import org.apache.datasketches.memory.Memory; /** * Convenient static methods to instantiate generic tuple sketches. */ @SuppressWarnings("deprecation") public final class Sketches { /** * @param <S> Type of Summary * @return an empty instance of Sketch */ public static <S extends Summary> Sketch<S> createEmptySketch() { return new CompactSketch<>(null, null, Long.MAX_VALUE, true); } /** * Instantiate a Sketch from a given Memory. * @param <S> Type of Summary * @param mem Memory object representing a Sketch * @param deserializer instance of SummaryDeserializer * @return Sketch created from its Memory representation */ public static <S extends Summary> Sketch<S> heapifySketch( final Memory mem, final SummaryDeserializer<S> deserializer) { final SerializerDeserializer.SketchType sketchType = SerializerDeserializer.getSketchType(mem); if (sketchType == SerializerDeserializer.SketchType.QuickSelectSketch) { return new QuickSelectSketch<>(mem, deserializer, null); } return new CompactSketch<>(mem, deserializer); } /** * Instantiate UpdatableSketch from a given Memory * @param <U> Type of update value * @param <S> Type of Summary * @param mem Memory object representing a Sketch * @param deserializer instance of SummaryDeserializer * @param summaryFactory instance of SummaryFactory * @return Sketch created from its Memory representation */ public static <U, S extends UpdatableSummary<U>> UpdatableSketch<U, S> heapifyUpdatableSketch( final Memory mem, final SummaryDeserializer<S> deserializer, final SummaryFactory<S> summaryFactory) { return new UpdatableSketch<>(mem, deserializer, summaryFactory); } }
2,555
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/Sketch.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; import static org.apache.datasketches.common.Util.LS; import org.apache.datasketches.thetacommon.BinomialBoundsN; /** * This is an equivalent to org.apache.datasketches.theta.Sketch with * addition of a user-defined Summary object associated with every unique entry * in the sketch. * @param <S> Type of Summary */ public abstract class Sketch<S extends Summary> { protected static final byte PREAMBLE_LONGS = 1; long thetaLong_; boolean empty_ = true; protected SummaryFactory<S> summaryFactory_; Sketch() {} /** * Converts this sketch to a CompactSketch on the Java heap. * * <p>If this sketch is already in compact form this operation returns <i>this</i>. * * @return this sketch as a CompactSketch on the Java heap. */ public abstract CompactSketch<S> compact(); /** * Estimates the cardinality of the set (number of unique values presented to the sketch) * @return best estimate of the number of unique values */ public double getEstimate() { if (!isEstimationMode()) { return getRetainedEntries(); } return getRetainedEntries() / getTheta(); } /** * Gets the approximate upper error bound given the specified number of Standard Deviations. * This will return getEstimate() if isEmpty() is true. * * @param numStdDev * <a href="{@docRoot}/resources/dictionary.html#numStdDev">See Number of Standard Deviations</a> * @return the upper bound. */ public double getUpperBound(final int numStdDev) { if (!isEstimationMode()) { return getRetainedEntries(); } return BinomialBoundsN.getUpperBound(getRetainedEntries(), getTheta(), numStdDev, empty_); } /** * Gets the approximate lower error bound given the specified number of Standard Deviations. * This will return getEstimate() if isEmpty() is true. * * @param numStdDev * <a href="{@docRoot}/resources/dictionary.html#numStdDev">See Number of Standard Deviations</a> * @return the lower bound. */ public double getLowerBound(final int numStdDev) { if (!isEstimationMode()) { return getRetainedEntries(); } return BinomialBoundsN.getLowerBound(getRetainedEntries(), getTheta(), numStdDev, empty_); } /** * Gets the estimate of the true distinct population of subset tuples represented by the count * of entries in a subset of the total retained entries of the sketch. * @param numSubsetEntries number of entries for a chosen subset of the sketch. * @return the estimate of the true distinct population of subset tuples represented by the count * of entries in a subset of the total retained entries of the sketch. */ public double getEstimate(final int numSubsetEntries) { if (!isEstimationMode()) { return numSubsetEntries; } return numSubsetEntries / getTheta(); } /** * Gets the estimate of the lower bound of the true distinct population represented by the count * of entries in a subset of the total retained entries of the sketch. * @param numStdDev * <a href="{@docRoot}/resources/dictionary.html#numStdDev">See Number of Standard Deviations</a> * @param numSubsetEntries number of entries for a chosen subset of the sketch. * @return the estimate of the lower bound of the true distinct population represented by the count * of entries in a subset of the total retained entries of the sketch. */ public double getLowerBound(final int numStdDev, final int numSubsetEntries) { if (!isEstimationMode()) { return numSubsetEntries; } return BinomialBoundsN.getLowerBound(numSubsetEntries, getTheta(), numStdDev, isEmpty()); } /** * Gets the estimate of the upper bound of the true distinct population represented by the count * of entries in a subset of the total retained entries of the sketch. * @param numStdDev * <a href="{@docRoot}/resources/dictionary.html#numStdDev">See Number of Standard Deviations</a> * @param numSubsetEntries number of entries for a chosen subset of the sketch. * @return the estimate of the upper bound of the true distinct population represented by the count * of entries in a subset of the total retained entries of the sketch. */ public double getUpperBound(final int numStdDev, final int numSubsetEntries) { if (!isEstimationMode()) { return numSubsetEntries; } return BinomialBoundsN.getUpperBound(numSubsetEntries, getTheta(), numStdDev, isEmpty()); } /** * <a href="{@docRoot}/resources/dictionary.html#empty">See Empty</a> * @return true if empty. */ public boolean isEmpty() { return empty_; } /** * Returns true if the sketch is Estimation Mode (as opposed to Exact Mode). * This is true if theta &lt; 1.0 AND isEmpty() is false. * @return true if the sketch is in estimation mode. */ public boolean isEstimationMode() { return thetaLong_ < Long.MAX_VALUE && !isEmpty(); } /** * @return number of retained entries */ public abstract int getRetainedEntries(); /** * Gets the number of hash values less than the given theta expressed as a long. * @param thetaLong the given theta as a long between zero and <i>Long.MAX_VALUE</i>. * @return the number of hash values less than the given thetaLong. */ public abstract int getCountLessThanThetaLong(final long thetaLong); /** * Gets the Summary Factory class of type S * @return the Summary Factory class of type S */ public SummaryFactory<S> getSummaryFactory() { return summaryFactory_; } /** * Gets the value of theta as a double between zero and one * @return the value of theta as a double */ public double getTheta() { return getThetaLong() / (double) Long.MAX_VALUE; } /** * This is to serialize a sketch instance to a byte array. * * <p>As of 3.0.0, serializing an UpdatableSketch is deprecated. * This capability will be removed in a future release. * Serializing a CompactSketch is not deprecated.</p> * @return serialized representation of the sketch */ public abstract byte[] toByteArray(); /** * Returns a SketchIterator * @return a SketchIterator */ public abstract TupleSketchIterator<S> iterator(); /** * Returns Theta as a long * @return Theta as a long */ public long getThetaLong() { return isEmpty() ? Long.MAX_VALUE : thetaLong_; } @Override public String toString() { final StringBuilder sb = new StringBuilder(); sb.append("### ").append(this.getClass().getSimpleName()).append(" SUMMARY: ").append(LS); sb.append(" Estimate : ").append(getEstimate()).append(LS); sb.append(" Upper Bound, 95% conf : ").append(getUpperBound(2)).append(LS); sb.append(" Lower Bound, 95% conf : ").append(getLowerBound(2)).append(LS); sb.append(" Theta (double) : ").append(this.getTheta()).append(LS); sb.append(" Theta (long) : ").append(this.getThetaLong()).append(LS); sb.append(" EstMode? : ").append(isEstimationMode()).append(LS); sb.append(" Empty? : ").append(isEmpty()).append(LS); sb.append(" Retained Entries : ").append(this.getRetainedEntries()).append(LS); if (this instanceof UpdatableSketch) { @SuppressWarnings("rawtypes") final UpdatableSketch updatable = (UpdatableSketch) this; sb.append(" Nominal Entries (k) : ").append(updatable.getNominalEntries()).append(LS); sb.append(" Current Capacity : ").append(updatable.getCurrentCapacity()).append(LS); sb.append(" Resize Factor : ").append(updatable.getResizeFactor().getValue()).append(LS); sb.append(" Sampling Probability (p): ").append(updatable.getSamplingProbability()).append(LS); } sb.append("### END SKETCH SUMMARY").append(LS); return sb.toString(); } }
2,556
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/SummarySetOperations.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; /** * This is to provide methods of producing unions and intersections of two Summary objects. * @param <S> type of Summary */ public interface SummarySetOperations<S extends Summary> { /** * This is called by the union operator when both sketches have the same hash value. * * <p><b>Caution:</b> Do not modify the input Summary objects. Also do not return them directly, * unless they are immutable (most Summary objects are not). For mutable Summary objects, it is * important to create a new Summary object with the correct contents to be returned. Do not * return null summaries. * * @param a Summary from sketch A * @param b Summary from sketch B * @return union of Summary A and Summary B */ public S union(S a, S b); /** * This is called by the intersection operator when both sketches have the same hash value. * * <p><b>Caution:</b> Do not modify the input Summary objects. Also do not return them directly, * unless they are immutable (most Summary objects are not). For mutable Summary objects, it is * important to create a new Summary object with the correct contents to be returned. Do not * return null summaries. * * @param a Summary from sketch A * @param b Summary from sketch B * @return intersection of Summary A and Summary B */ public S intersection(S a, S b); }
2,557
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/UpdatableSketch.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; import org.apache.datasketches.hash.MurmurHash3; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.thetacommon.ThetaUtil; /** * An extension of QuickSelectSketch&lt;S&gt;, which can be updated with many types of keys. * Summary objects are created using a user-defined SummaryFactory class, * which should allow very flexible parameterization if needed. * Keys are presented to a sketch along with values of a user-defined * update type U. When an entry is inserted into a sketch or a duplicate key is * presented to a sketch then summary.update(U value) method will be called. So * any kind of user-defined accumulation is possible. Summaries also must know * how to copy themselves. Also union and intersection of summaries can be * implemented in a sub-class of SummarySetOperations, which will be used in * case Union or Intersection of two instances of Tuple Sketch is needed * @param <U> Type of the value, which is passed to update method of a Summary * @param <S> Type of the UpdatableSummary&lt;U&gt; */ public class UpdatableSketch<U, S extends UpdatableSummary<U>> extends QuickSelectSketch<S> { /** * This is to create a new instance of an UpdatableQuickSelectSketch. * @param nomEntries Nominal number of entries. Forced to the nearest power of 2 greater than * or equal to the given value. * @param lgResizeFactor log2(resizeFactor) - value from 0 to 3: * <pre> * 0 - no resizing (max size allocated), * 1 - double internal hash table each time it reaches a threshold * 2 - grow four times * 3 - grow eight times (default) * </pre> * @param samplingProbability * <a href="{@docRoot}/resources/dictionary.html#p">See Sampling Probability</a> * @param summaryFactory An instance of a SummaryFactory. */ public UpdatableSketch(final int nomEntries, final int lgResizeFactor, final float samplingProbability, final SummaryFactory<S> summaryFactory) { super(nomEntries, lgResizeFactor, samplingProbability, summaryFactory); } /** * This is to create an instance of a sketch given a serialized form * @param srcMem Memory object with data of a serialized UpdatableSketch * @param deserializer instance of SummaryDeserializer * @param summaryFactory instance of SummaryFactory * @deprecated As of 3.0.0, heapifying an UpdatableSketch is deprecated. * This capability will be removed in a future release. * Heapifying a CompactSketch is not deprecated. */ @Deprecated public UpdatableSketch(final Memory srcMem, final SummaryDeserializer<S> deserializer, final SummaryFactory<S> summaryFactory) { super(srcMem, deserializer, summaryFactory); } /** * Copy Constructor * @param sketch the sketch to copy */ public UpdatableSketch(final UpdatableSketch<U, S> sketch) { super(sketch); } /** * @return a deep copy of this sketch */ @Override public UpdatableSketch<U,S> copy() { return new UpdatableSketch<>(this); } /** * Updates this sketch with a long key and U value. * The value is passed to update() method of the Summary object associated with the key * * @param key The given long key * @param value The given U value */ public void update(final long key, final U value) { update(new long[] {key}, value); } /** * Updates this sketch with a double key and U value. * The value is passed to update() method of the Summary object associated with the key * * @param key The given double key * @param value The given U value */ public void update(final double key, final U value) { update(Util.doubleToLongArray(key), value); } /** * Updates this sketch with a String key and U value. * The value is passed to update() method of the Summary object associated with the key * * @param key The given String key * @param value The given U value */ public void update(final String key, final U value) { update(Util.stringToByteArray(key), value); } /** * Updates this sketch with a byte[] key and U value. * The value is passed to update() method of the Summary object associated with the key * * @param key The given byte[] key * @param value The given U value */ public void update(final byte[] key, final U value) { if ((key == null) || (key.length == 0)) { return; } insertOrIgnore(MurmurHash3.hash(key, ThetaUtil.DEFAULT_UPDATE_SEED)[0] >>> 1, value); } /** * Updates this sketch with a int[] key and U value. * The value is passed to update() method of the Summary object associated with the key * * @param key The given int[] key * @param value The given U value */ public void update(final int[] key, final U value) { if ((key == null) || (key.length == 0)) { return; } insertOrIgnore(MurmurHash3.hash(key, ThetaUtil.DEFAULT_UPDATE_SEED)[0] >>> 1, value); } /** * Updates this sketch with a long[] key and U value. * The value is passed to update() method of the Summary object associated with the key * * @param key The given long[] key * @param value The given U value */ public void update(final long[] key, final U value) { if ((key == null) || (key.length == 0)) { return; } insertOrIgnore(MurmurHash3.hash(key, ThetaUtil.DEFAULT_UPDATE_SEED)[0] >>> 1, value); } void insertOrIgnore(final long hash, final U value) { setEmpty(false); if (hash >= getThetaLong()) { return; } int index = findOrInsert(hash); if (index < 0) { index = ~index; insertSummary(index, getSummaryFactory().newSummary()); } summaryTable_[index].update(value); rebuildIfNeeded(); } }
2,558
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/SerializerDeserializer.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; import org.apache.datasketches.common.Family; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.memory.Memory; /** * Multipurpose serializer-deserializer for a collection of sketches defined by the enum. */ public final class SerializerDeserializer { /** * Defines the sketch classes that this SerializerDeserializer can handle. */ public static enum SketchType { QuickSelectSketch, CompactSketch, ArrayOfDoublesQuickSelectSketch, ArrayOfDoublesCompactSketch, ArrayOfDoublesUnion } static final int TYPE_BYTE_OFFSET = 3; /** * Validates the preamble-Longs value given the family ID * @param familyId the given family ID * @param preambleLongs the given preambleLongs value */ public static void validateFamily(final byte familyId, final byte preambleLongs) { final Family family = Family.idToFamily(familyId); if (family.equals(Family.TUPLE)) { if (preambleLongs < Family.TUPLE.getMinPreLongs() || preambleLongs > Family.TUPLE.getMaxPreLongs()) { throw new SketchesArgumentException( "Possible corruption: Invalid PreambleLongs value for family TUPLE: " + preambleLongs); } } else { throw new SketchesArgumentException( "Possible corruption: Invalid Family: " + family.toString()); } } /** * Validates the sketch type byte versus the expected value * @param sketchTypeByte the given sketch type byte * @param expectedType the expected value */ public static void validateType(final byte sketchTypeByte, final SketchType expectedType) { final SketchType sketchType = getSketchType(sketchTypeByte); if (!sketchType.equals(expectedType)) { throw new SketchesArgumentException("Sketch Type mismatch. Expected " + expectedType.name() + ", got " + sketchType.name()); } } /** * Gets the sketch type byte from the given Memory image * @param mem the given Memory image * @return the SketchType */ public static SketchType getSketchType(final Memory mem) { final byte sketchTypeByte = mem.getByte(TYPE_BYTE_OFFSET); return getSketchType(sketchTypeByte); } private static SketchType getSketchType(final byte sketchTypeByte) { if ((sketchTypeByte < 0) || (sketchTypeByte >= SketchType.values().length)) { throw new SketchesArgumentException("Invalid Sketch Type " + sketchTypeByte); } return SketchType.values()[sketchTypeByte]; } }
2,559
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/DeserializeResult.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; /** * Returns an object and its size in bytes as a result of a deserialize operation * @param <T> Type of object */ public class DeserializeResult<T> { private final T object; private final int size; /** * Creates an instance. * @param object Deserialized object. * @param size Deserialized size in bytes. */ public DeserializeResult(final T object, final int size) { this.object = object; this.size = size; } /** * @return Deserialized object */ public T getObject() { return object; } /** * @return Size in bytes occupied by the object in the serialized form */ public int getSize() { return size; } }
2,560
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/Intersection.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; import static java.lang.Math.ceil; import static java.lang.Math.max; import static java.lang.Math.min; import static org.apache.datasketches.common.Util.ceilingIntPowerOf2; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.common.SketchesStateException; import org.apache.datasketches.thetacommon.ThetaUtil; /** * Computes an intersection of two or more generic tuple sketches or generic tuple sketches * combined with theta sketches. * A new instance represents the Universal Set. Because the Universal Set * cannot be realized a <i>getResult()</i> on a new instance will produce an error. * Every update() computes an intersection with the internal state, which will never * grow larger and may be reduced to zero. * * @param <S> Type of Summary */ @SuppressWarnings("unchecked") public class Intersection<S extends Summary> { private final SummarySetOperations<S> summarySetOps_; private boolean empty_; private long thetaLong_; private HashTables<S> hashTables_; private boolean firstCall_; /** * Creates new Intersection instance with instructions on how to process two summaries that * intersect. * @param summarySetOps instance of SummarySetOperations */ public Intersection(final SummarySetOperations<S> summarySetOps) { summarySetOps_ = summarySetOps; empty_ = false; // universal set at the start thetaLong_ = Long.MAX_VALUE; hashTables_ = new HashTables<>(); firstCall_ = true; } /** * Perform a stateless intersect set operation on the two given tuple sketches and returns the * result as an unordered CompactSketch on the heap. * @param tupleSketchA The first sketch argument. It must not be null. * @param tupleSketchB The second sketch argument. It must not be null. * @return an unordered CompactSketch on the heap */ public CompactSketch<S> intersect( final Sketch<S> tupleSketchA, final Sketch<S> tupleSketchB) { reset(); intersect(tupleSketchA); intersect(tupleSketchB); final CompactSketch<S> csk = getResult(); reset(); return csk; } /** * Perform a stateless intersect set operation on a tuple sketch and a theta sketch and returns the * result as an unordered CompactSketch on the heap. * @param tupleSketch The first sketch argument. It must not be null. * @param thetaSketch The second sketch argument. It must not be null. * @param summary the given proxy summary for the theta sketch, which doesn't have one. * This must not be null. * @return an unordered CompactSketch on the heap */ public CompactSketch<S> intersect( final Sketch<S> tupleSketch, final org.apache.datasketches.theta.Sketch thetaSketch, final S summary) { reset(); intersect(tupleSketch); intersect(thetaSketch, summary); final CompactSketch<S> csk = getResult(); reset(); return csk; } /** * Performs a stateful intersection of the internal set with the given tupleSketch. * @param tupleSketch input sketch to intersect with the internal state. It must not be null. */ public void intersect(final Sketch<S> tupleSketch) { if (tupleSketch == null) { throw new SketchesArgumentException("Sketch must not be null"); } final boolean firstCall = firstCall_; firstCall_ = false; // input sketch could be first or next call final boolean emptyIn = tupleSketch.isEmpty(); if (empty_ || emptyIn) { //empty rule //Whatever the current internal state, we make our local empty. resetToEmpty(); return; } final long thetaLongIn = tupleSketch.getThetaLong(); thetaLong_ = min(thetaLong_, thetaLongIn); //Theta rule if (tupleSketch.getRetainedEntries() == 0) { hashTables_.clear(); return; } // input sketch will have valid entries > 0 if (firstCall) { //Copy firstSketch data into local instance hashTables_ hashTables_.fromSketch(tupleSketch); } //Next Call else { if (hashTables_.numKeys == 0) { return; } //process intersect with current hashTables hashTables_ = hashTables_.getIntersectHashTables(tupleSketch, thetaLong_, summarySetOps_); } } /** * Performs a stateful intersection of the internal set with the given thetaSketch by combining entries * using the hashes from the theta sketch and summary values from the given summary and rules * from the summarySetOps defined by the Intersection constructor. * @param thetaSketch input theta sketch to intersect with the internal state. It must not be null. * @param summary the given proxy summary for the theta sketch, which doesn't have one. * It will be copied for each matching index. It must not be null. */ public void intersect(final org.apache.datasketches.theta.Sketch thetaSketch, final S summary) { if (thetaSketch == null) { throw new SketchesArgumentException("Sketch must not be null"); } if (summary == null) { throw new SketchesArgumentException("Summary cannot be null."); } final boolean firstCall = firstCall_; firstCall_ = false; // input sketch is not null, could be first or next call final boolean emptyIn = thetaSketch.isEmpty(); if (empty_ || emptyIn) { //empty rule //Whatever the current internal state, we make our local empty. resetToEmpty(); return; } final long thetaLongIn = thetaSketch.getThetaLong(); thetaLong_ = min(thetaLong_, thetaLongIn); //Theta rule final int countIn = thetaSketch.getRetainedEntries(); if (countIn == 0) { hashTables_.clear(); return; } // input sketch will have valid entries > 0 if (firstCall) { final org.apache.datasketches.theta.Sketch firstSketch = thetaSketch; //Copy firstSketch data into local instance hashTables_ hashTables_.fromSketch(firstSketch, summary); } //Next Call else { if (hashTables_.numKeys == 0) { return; } hashTables_ = hashTables_.getIntersectHashTables(thetaSketch, thetaLongIn, summarySetOps_, summary); } } /** * Gets the internal set as an unordered CompactSketch * @return result of the intersections so far */ public CompactSketch<S> getResult() { if (firstCall_) { throw new SketchesStateException( "getResult() with no intervening intersections is not a legal result."); } final int countIn = hashTables_.numKeys; if (countIn == 0) { return new CompactSketch<>(null, null, thetaLong_, empty_); } final int tableSize = hashTables_.hashTable.length; final long[] hashArr = new long[countIn]; final S[] summaryArr = Util.newSummaryArray(hashTables_.summaryTable, countIn); //compact the arrays int cnt = 0; for (int i = 0; i < tableSize; i++) { final long hash = hashTables_.hashTable[i]; if (hash == 0 || hash > thetaLong_) { continue; } hashArr[cnt] = hash; summaryArr[cnt] = (S) hashTables_.summaryTable[i].copy(); cnt++; } assert cnt == countIn; return new CompactSketch<>(hashArr, summaryArr, thetaLong_, empty_); } /** * Returns true if there is a valid intersection result available * @return true if there is a valid intersection result available */ public boolean hasResult() { return !firstCall_; } /** * Resets the internal set to the initial state, which represents the Universal Set */ public void reset() { hardReset(); } private void hardReset() { empty_ = false; thetaLong_ = Long.MAX_VALUE; hashTables_.clear(); firstCall_ = true; } private void resetToEmpty() { empty_ = true; thetaLong_ = Long.MAX_VALUE; hashTables_.clear(); firstCall_ = false; } static int getLgTableSize(final int count) { final int tableSize = max(ceilingIntPowerOf2((int) ceil(count / 0.75)), 1 << ThetaUtil.MIN_LG_NOM_LONGS); return Integer.numberOfTrailingZeros(tableSize); } }
2,561
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/TupleSketchIterator.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; /** * Iterator over a generic tuple sketch * @param <S> Type of Summary */ public class TupleSketchIterator<S extends Summary> { private final long[] hashArrTbl_; //could be either hashArr or hashTable private final S[] summaryArrTbl_; //could be either summaryArr or summaryTable private int i_; TupleSketchIterator(final long[] hashes, final S[] summaries) { hashArrTbl_ = hashes; summaryArrTbl_ = summaries; i_ = -1; } /** * Advancing the iterator and checking existence of the next entry * is combined here for efficiency. This results in an undefined * state of the iterator before the first call of this method. * @return true if the next element exists */ public boolean next() { if (hashArrTbl_ == null) { return false; } i_++; while (i_ < hashArrTbl_.length) { if (hashArrTbl_[i_] > 0) { return true; } i_++; } return false; } /** * Gets the hash from the current entry in the sketch, which is a hash * of the original key passed to update(). The original keys are not * retained. Don't call this before calling next() for the first time * or after getting false from next(). * @return hash from the current entry */ public long getHash() { return hashArrTbl_[i_]; } /** * Gets a Summary object from the current entry in the sketch. * Don't call this before calling next() for the first time * or after getting false from next(). * @return Summary object for the current entry (this is not a copy!) */ public S getSummary() { return summaryArrTbl_[i_]; } }
2,562
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/package-info.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /** * The tuple package contains a number of sketches based on the same * fundamental algorithms of the Theta Sketch Framework and extend these * concepts for whole new families of sketches. */ package org.apache.datasketches.tuple;
2,563
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/Summary.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; /** * Interface for user-defined Summary, which is associated with every hash in a tuple sketch */ public interface Summary { /** * Deep copy. * * <p><b>Caution:</b> This must implement a deep copy. * * @return deep copy of the Summary */ public Summary copy(); /** * This is to serialize a Summary instance to a byte array. * * <p>The user should encode in the byte array its total size, which is used during * deserialization, especially if the Summary has variable sized elements. * * @return serialized representation of the Summary */ public byte[] toByteArray(); }
2,564
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/Filter.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; import java.util.function.Predicate; import org.apache.datasketches.common.ResizeFactor; /** * Class for filtering entries from a {@link Sketch} given a {@link Summary} * * @param <T> Summary type against which apply the {@link Predicate} */ public class Filter<T extends Summary> { private final Predicate<T> predicate; /** * Filter constructor with a {@link Predicate} * @param predicate Predicate to use in this filter. If the Predicate returns False, the * element is discarded. If the Predicate returns True, then the element is kept in the * {@link Sketch} */ public Filter(final Predicate<T> predicate) { this.predicate = predicate; } /** * Filters elements on the provided {@link Sketch} * * @param sketchIn The sketch against which apply the {@link Predicate} * @return A new Sketch with some of the entries filtered out based on the {@link Predicate} */ @SuppressWarnings("unchecked") public CompactSketch<T> filter(final Sketch<T> sketchIn) { if (sketchIn == null) { return new CompactSketch<>(null, null, Long.MAX_VALUE, true); } final QuickSelectSketch<T> sketch = new QuickSelectSketch<>(sketchIn.getRetainedEntries(), ResizeFactor.X1.lg(), null); final TupleSketchIterator<T> it = sketchIn.iterator(); while (it.next()) { final T summary = it.getSummary(); if (predicate.test(summary)) { sketch.insert(it.getHash(), (T)summary.copy()); } } sketch.setThetaLong(sketchIn.getThetaLong()); if (!sketchIn.isEmpty()) { sketch.setEmpty(false); } return sketch.compact(); } }
2,565
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/UpdatableSketchBuilder.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple; import org.apache.datasketches.common.ResizeFactor; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.thetacommon.ThetaUtil; /** * For building a new generic tuple UpdatableSketch * @param <U> Type of update value * @param <S> Type of Summary */ public class UpdatableSketchBuilder<U, S extends UpdatableSummary<U>> { private int nomEntries_; private ResizeFactor resizeFactor_; private float samplingProbability_; private final SummaryFactory<S> summaryFactory_; private static final float DEFAULT_SAMPLING_PROBABILITY = 1; private static final ResizeFactor DEFAULT_RESIZE_FACTOR = ResizeFactor.X8; /** * Creates an instance of UpdatableSketchBuilder with default parameters * @param summaryFactory An instance of SummaryFactory. */ public UpdatableSketchBuilder(final SummaryFactory<S> summaryFactory) { nomEntries_ = ThetaUtil.DEFAULT_NOMINAL_ENTRIES; resizeFactor_ = DEFAULT_RESIZE_FACTOR; samplingProbability_ = DEFAULT_SAMPLING_PROBABILITY; summaryFactory_ = summaryFactory; } /** * This is to set the nominal number of entries. * @param nomEntries Nominal number of entries. Forced to the nearest power of 2 greater than * or equal to the given value. * @return this UpdatableSketchBuilder */ public UpdatableSketchBuilder<U, S> setNominalEntries(final int nomEntries) { nomEntries_ = 1 << ThetaUtil.checkNomLongs(nomEntries); return this; } /** * This is to set the resize factor. * Value of X1 means that the maximum capacity is allocated from the start. * Default resize factor is X8. * @param resizeFactor value of X1, X2, X4 or X8 * @return this UpdatableSketchBuilder */ public UpdatableSketchBuilder<U, S> setResizeFactor(final ResizeFactor resizeFactor) { resizeFactor_ = resizeFactor; return this; } /** * This is to set sampling probability. * Default probability is 1. * @param samplingProbability sampling probability from 0 to 1 * @return this UpdatableSketchBuilder */ public UpdatableSketchBuilder<U, S> setSamplingProbability(final float samplingProbability) { if ((samplingProbability < 0) || (samplingProbability > 1f)) { throw new SketchesArgumentException("sampling probability must be between 0 and 1"); } samplingProbability_ = samplingProbability; return this; } /** * Returns an UpdatableSketch with the current configuration of this Builder. * @return an UpdatableSketch */ public UpdatableSketch<U, S> build() { return new UpdatableSketch<>(nomEntries_, resizeFactor_.lg(), samplingProbability_, summaryFactory_); } /** * Resets the Nominal Entries, Resize Factor and Sampling Probability to their default values. * The assignment of <i>U</i> and <i>S</i> remain the same. */ public void reset() { nomEntries_ = ThetaUtil.DEFAULT_NOMINAL_ENTRIES; resizeFactor_ = DEFAULT_RESIZE_FACTOR; samplingProbability_ = DEFAULT_SAMPLING_PROBABILITY; } }
2,566
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/adouble/DoubleSummaryDeserializer.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.adouble; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.tuple.DeserializeResult; import org.apache.datasketches.tuple.SummaryDeserializer; /** * @author Lee Rhodes */ public class DoubleSummaryDeserializer implements SummaryDeserializer<DoubleSummary> { @Override public DeserializeResult<DoubleSummary> heapifySummary(final Memory mem) { return DoubleSummary.fromMemory(mem); } }
2,567
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/adouble/DoubleSummaryFactory.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.adouble; import org.apache.datasketches.tuple.SummaryFactory; /** * Factory for DoubleSummary. * * @author Lee Rhodes */ public final class DoubleSummaryFactory implements SummaryFactory<DoubleSummary> { private final DoubleSummary.Mode summaryMode_; /** * Creates an instance of DoubleSummaryFactory with a given mode * @param summaryMode summary mode */ public DoubleSummaryFactory(final DoubleSummary.Mode summaryMode) { summaryMode_ = summaryMode; } @Override public DoubleSummary newSummary() { return new DoubleSummary(summaryMode_); } }
2,568
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/adouble/DoubleSketch.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.adouble; import org.apache.datasketches.common.ResizeFactor; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.tuple.UpdatableSketch; /** * @author Lee Rhodes */ public class DoubleSketch extends UpdatableSketch<Double, DoubleSummary> { /** * Constructs this sketch with given <i>lgK</i>. * @param lgK Log_base2 of <i>Nominal Entries</i>. * <a href="{@docRoot}/resources/dictionary.html#nomEntries">See Nominal Entries</a> * @param mode The DoubleSummary mode to be used */ public DoubleSketch(final int lgK, final DoubleSummary.Mode mode) { this(lgK, ResizeFactor.X8.ordinal(), 1.0F, mode); } /** * Creates this sketch with the following parameters: * @param lgK Log_base2 of <i>Nominal Entries</i>. * @param lgResizeFactor log2(resizeFactor) - value from 0 to 3: * <pre> * 0 - no resizing (max size allocated), * 1 - double internal hash table each time it reaches a threshold * 2 - grow four times * 3 - grow eight times (default) * </pre> * @param samplingProbability * <a href="{@docRoot}/resources/dictionary.html#p">See Sampling Probability</a> * @param mode The DoubleSummary mode to be used */ public DoubleSketch(final int lgK, final int lgResizeFactor, final float samplingProbability, final DoubleSummary.Mode mode) { super(1 << lgK, lgResizeFactor, samplingProbability, new DoubleSummaryFactory(mode)); } /** * Constructs this sketch from a Memory image, which must be from an DoubleSketch, and * usually with data. * @param mem the given Memory * @param mode The DoubleSummary mode to be used * @deprecated As of 3.0.0, heapifying an UpdatableSketch is deprecated. * This capability will be removed in a future release. * Heapifying a CompactSketch is not deprecated. */ @Deprecated public DoubleSketch(final Memory mem, final DoubleSummary.Mode mode) { super(mem, new DoubleSummaryDeserializer(), new DoubleSummaryFactory(mode)); } @Override public void update(final String key, final Double value) { super.update(key, value); } @Override public void update(final long key, final Double value) { super.update(key, value); } }
2,569
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/adouble/DoubleSummarySetOperations.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.adouble; import org.apache.datasketches.tuple.SummarySetOperations; import org.apache.datasketches.tuple.adouble.DoubleSummary.Mode; /** * Methods for defining how unions and intersections of two objects of type DoubleSummary * are performed. */ public final class DoubleSummarySetOperations implements SummarySetOperations<DoubleSummary> { private final Mode unionSummaryMode_; /** * Intersection is not well defined or even meaningful between numeric values. * Nevertheless, this can be defined to be a different type of aggregation for intersecting hashes. */ private final Mode intersectionSummaryMode_; /** * Creates an instance with default mode of <i>sum</i> for both union and intersection. * This exists for backward compatibility. */ public DoubleSummarySetOperations() { unionSummaryMode_ = DoubleSummary.Mode.Sum; intersectionSummaryMode_ = DoubleSummary.Mode.Sum; } /** * Creates an instance given a DoubleSummary update mode where the mode is the same for both * union and intersection. This exists for backward compatibility. * @param summaryMode DoubleSummary update mode. */ public DoubleSummarySetOperations(final Mode summaryMode) { unionSummaryMode_ = summaryMode; intersectionSummaryMode_ = summaryMode; } /** * Creates an instance with two modes. * @param unionSummaryMode for unions * @param intersectionSummaryMode for intersections */ public DoubleSummarySetOperations(final Mode unionSummaryMode, final Mode intersectionSummaryMode) { unionSummaryMode_ = unionSummaryMode; intersectionSummaryMode_ = intersectionSummaryMode; } @Override public DoubleSummary union(final DoubleSummary a, final DoubleSummary b) { final DoubleSummary result = new DoubleSummary(unionSummaryMode_); result.update(a.getValue()); result.update(b.getValue()); return result; } @Override public DoubleSummary intersection(final DoubleSummary a, final DoubleSummary b) { final DoubleSummary result = new DoubleSummary(intersectionSummaryMode_); result.update(a.getValue()); result.update(b.getValue()); return result; } }
2,570
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/adouble/DoubleSummary.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.adouble; import org.apache.datasketches.common.ByteArrayUtil; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.tuple.DeserializeResult; import org.apache.datasketches.tuple.UpdatableSummary; /** * Summary for generic tuple sketches of type Double. * This summary keeps a double value. On update a predefined operation is performed depending on * the mode. * Supported modes: Sum, Min, Max, AlwaysOne, Increment. The default mode is Sum. */ public final class DoubleSummary implements UpdatableSummary<Double> { private double value_; private final Mode mode_; /** * The aggregation modes for this Summary */ public enum Mode { /** * The aggregation mode is the summation function. * <p>New retained value = previous retained value + incoming value</p> */ Sum, /** * The aggregation mode is the minimum function. * <p>New retained value = min(previous retained value, incoming value)</p> */ Min, /** * The aggregation mode is the maximum function. * <p>New retained value = max(previous retained value, incoming value)</p> */ Max, /** * The aggregation mode is always one. * <p>New retained value = 1.0</p> */ AlwaysOne } /** * Creates an instance of DoubleSummary with a given starting value and mode * @param value starting value * @param mode update mode */ private DoubleSummary(final double value, final Mode mode) { value_ = value; mode_ = mode; } /** * Creates an instance of DoubleSummary with a given mode. * @param mode update mode */ public DoubleSummary(final Mode mode) { mode_ = mode; switch (mode) { case Sum: value_ = 0; break; case Min: value_ = Double.POSITIVE_INFINITY; break; case Max: value_ = Double.NEGATIVE_INFINITY; break; case AlwaysOne: value_ = 1.0; break; } } @Override public DoubleSummary update(final Double value) { switch (mode_) { case Sum: value_ += value; break; case Min: if (value < value_) { value_ = value; } break; case Max: if (value > value_) { value_ = value; } break; case AlwaysOne: value_ = 1.0; break; } return this; } @Override public DoubleSummary copy() { return new DoubleSummary(value_, mode_); } /** * @return current value of the DoubleSummary */ public double getValue() { return value_; } private static final int SERIALIZED_SIZE_BYTES = 9; private static final int VALUE_INDEX = 0; private static final int MODE_BYTE_INDEX = 8; @Override public byte[] toByteArray() { final byte[] bytes = new byte[SERIALIZED_SIZE_BYTES]; ByteArrayUtil.putDoubleLE(bytes, VALUE_INDEX, value_); bytes[MODE_BYTE_INDEX] = (byte) mode_.ordinal(); return bytes; } /** * Creates an instance of the DoubleSummary given a serialized representation * @param mem Memory object with serialized DoubleSummary * @return DeserializedResult object, which contains a DoubleSummary object and number of bytes * read from the Memory */ public static DeserializeResult<DoubleSummary> fromMemory(final Memory mem) { return new DeserializeResult<>(new DoubleSummary(mem.getDouble(VALUE_INDEX), Mode.values()[mem.getByte(MODE_BYTE_INDEX)]), SERIALIZED_SIZE_BYTES); } }
2,571
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/adouble/package-info.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /** * This package is for a generic implementation of the Tuple sketch for single Double value. */ package org.apache.datasketches.tuple.adouble;
2,572
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/strings/ArrayOfStringsSummarySetOperations.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.strings; import org.apache.datasketches.tuple.SummarySetOperations; /** * @author Lee Rhodes */ public class ArrayOfStringsSummarySetOperations implements SummarySetOperations<ArrayOfStringsSummary> { @Override public ArrayOfStringsSummary union(final ArrayOfStringsSummary a, final ArrayOfStringsSummary b) { return a.copy(); } @Override public ArrayOfStringsSummary intersection(final ArrayOfStringsSummary a, final ArrayOfStringsSummary b) { return a.copy(); } }
2,573
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/strings/ArrayOfStringsSummaryDeserializer.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.strings; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.tuple.DeserializeResult; import org.apache.datasketches.tuple.SummaryDeserializer; /** * @author Lee Rhodes */ public class ArrayOfStringsSummaryDeserializer implements SummaryDeserializer<ArrayOfStringsSummary> { @Override public DeserializeResult<ArrayOfStringsSummary> heapifySummary(final Memory mem) { return ArrayOfStringsSummaryDeserializer.fromMemory(mem); } /** * Also used in test. * @param mem the given memory * @return the DeserializeResult */ static DeserializeResult<ArrayOfStringsSummary> fromMemory(final Memory mem) { final ArrayOfStringsSummary nsum = new ArrayOfStringsSummary(mem); final int totBytes = mem.getInt(0); return new DeserializeResult<>(nsum, totBytes); } }
2,574
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/strings/ArrayOfStringsSummaryFactory.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.strings; import org.apache.datasketches.tuple.SummaryFactory; /** * @author Lee Rhodes */ public class ArrayOfStringsSummaryFactory implements SummaryFactory<ArrayOfStringsSummary> { @Override public ArrayOfStringsSummary newSummary() { return new ArrayOfStringsSummary(); } }
2,575
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/strings/ArrayOfStringsSketch.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.strings; import static org.apache.datasketches.tuple.Util.stringArrHash; import org.apache.datasketches.common.ResizeFactor; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.tuple.UpdatableSketch; /** * @author Lee Rhodes */ public class ArrayOfStringsSketch extends UpdatableSketch<String[], ArrayOfStringsSummary> { /** * Constructs new sketch with default <i>K</i> = 4096 (<i>lgK</i> = 12), default ResizeFactor=X8, * and default <i>p</i> = 1.0. */ public ArrayOfStringsSketch() { this(12); } /** * Constructs new sketch with default ResizeFactor=X8, default <i>p</i> = 1.0 and given <i>lgK</i>. * @param lgK Log_base2 of <i>Nominal Entries</i>. * <a href="{@docRoot}/resources/dictionary.html#nomEntries">See Nominal Entries</a> */ public ArrayOfStringsSketch(final int lgK) { this(lgK, ResizeFactor.X8, 1.0F); } /** * Constructs new sketch with given ResizeFactor, <i>p</i> and <i>lgK</i>. * @param lgK Log_base2 of <i>Nominal Entries</i>. * <a href="{@docRoot}/resources/dictionary.html#nomEntries">See Nominal Entries</a> * @param rf ResizeFactor * <a href="{@docRoot}/resources/dictionary.html#resizeFactor">See Resize Factor</a> * @param p sampling probability * <a href="{@docRoot}/resources/dictionary.html#p">See Sampling Probability</a> */ public ArrayOfStringsSketch(final int lgK, final ResizeFactor rf, final float p) { super(1 << lgK, rf.lg(), p, new ArrayOfStringsSummaryFactory()); } /** * Constructs this sketch from a Memory image, which must be from an ArrayOfStringsSketch, and * usually with data. * @param mem the given Memory * @deprecated As of 3.0.0, heapifying an UpdatableSketch is deprecated. * This capability will be removed in a future release. * Heapifying a CompactSketch is not deprecated. */ @Deprecated public ArrayOfStringsSketch(final Memory mem) { super(mem, new ArrayOfStringsSummaryDeserializer(), new ArrayOfStringsSummaryFactory()); } /** * Copy Constructor * @param sketch the sketch to copy */ public ArrayOfStringsSketch(final ArrayOfStringsSketch sketch) { super(sketch); } /** * @return a deep copy of this sketch */ @Override public ArrayOfStringsSketch copy() { return new ArrayOfStringsSketch(this); } /** * Updates the sketch with String arrays for both key and value. * @param strArrKey the given String array key * @param strArr the given String array value */ public void update(final String[] strArrKey, final String[] strArr) { super.update(stringArrHash(strArrKey), strArr); } }
2,576
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/strings/ArrayOfStringsSummary.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.strings; import static java.nio.charset.StandardCharsets.UTF_8; import static org.apache.datasketches.tuple.Util.stringArrHash; import static org.apache.datasketches.tuple.Util.stringConcat; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.memory.Buffer; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.memory.WritableBuffer; import org.apache.datasketches.memory.WritableMemory; import org.apache.datasketches.tuple.UpdatableSummary; /** * @author Lee Rhodes */ public class ArrayOfStringsSummary implements UpdatableSummary<String[]> { private String[] nodesArr = null; ArrayOfStringsSummary() { //required for ArrayOfStringsSummaryFactory nodesArr = null; } //Used by copy() and in test ArrayOfStringsSummary(final String[] nodesArr) { this.nodesArr = nodesArr.clone(); checkNumNodes(nodesArr.length); } //used by fromMemory and in test ArrayOfStringsSummary(final Memory mem) { final Buffer buf = mem.asBuffer(); final int totBytes = buf.getInt(); checkInBytes(mem, totBytes); final int nodes = buf.getByte(); checkNumNodes(nodes); final String[] nodesArr = new String[nodes]; for (int i = 0; i < nodes; i++) { final int len = buf.getInt(); final byte[] byteArr = new byte[len]; buf.getByteArray(byteArr, 0, len); nodesArr[i] = new String(byteArr, UTF_8); } this.nodesArr = nodesArr; } @Override public ArrayOfStringsSummary copy() { final ArrayOfStringsSummary nodes = new ArrayOfStringsSummary(nodesArr); return nodes; } @Override public byte[] toByteArray() { final ComputeBytes cb = new ComputeBytes(nodesArr); final int totBytes = cb.totBytes_; final byte[] out = new byte[totBytes]; final WritableMemory wmem = WritableMemory.writableWrap(out); final WritableBuffer wbuf = wmem.asWritableBuffer(); wbuf.putInt(totBytes); wbuf.putByte(cb.numNodes_); for (int i = 0; i < cb.numNodes_; i++) { wbuf.putInt(cb.nodeLengthsArr_[i]); wbuf.putByteArray(cb.nodeBytesArr_[i], 0, cb.nodeLengthsArr_[i]); } assert wbuf.getPosition() == totBytes; return out; } //From UpdatableSummary @Override public ArrayOfStringsSummary update(final String[] value) { if (nodesArr == null) { nodesArr = value.clone(); } return this; } //From Object @Override public int hashCode() { return (int) stringArrHash(nodesArr); } @Override public boolean equals(final Object summary) { if (summary == null || !(summary instanceof ArrayOfStringsSummary)) { return false; } final String thatStr = stringConcat(((ArrayOfStringsSummary) summary).nodesArr); final String thisStr = stringConcat(nodesArr); return thisStr.equals(thatStr); } /** * @return the nodes array for this summary. */ public String[] getValue() { return nodesArr.clone(); } //also used in test static void checkNumNodes(final int numNodes) { if (numNodes > 127) { throw new SketchesArgumentException("Number of nodes cannot exceed 127."); } } //also used in test static void checkInBytes(final Memory mem, final int totBytes) { if (mem.getCapacity() < totBytes) { throw new SketchesArgumentException("Incoming Memory has insufficient capacity."); } } private static class ComputeBytes { final byte numNodes_; final int[] nodeLengthsArr_; final byte[][] nodeBytesArr_; final int totBytes_; ComputeBytes(final String[] nodesArr) { numNodes_ = (byte) nodesArr.length; checkNumNodes(numNodes_); nodeLengthsArr_ = new int[numNodes_]; nodeBytesArr_ = new byte[numNodes_][]; int sumNodeBytes = 0; for (int i = 0; i < numNodes_; i++) { nodeBytesArr_[i] = nodesArr[i].getBytes(UTF_8); nodeLengthsArr_[i] = nodeBytesArr_[i].length; sumNodeBytes += nodeLengthsArr_[i]; } totBytes_ = sumNodeBytes + (numNodes_ + 1) * Integer.BYTES + 1; } } }
2,577
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/strings/package-info.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /** * This package is for a generic implementation of the Tuple sketch for single String value. */ package org.apache.datasketches.tuple.strings;
2,578
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/aninteger/IntegerSummaryDeserializer.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.aninteger; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.tuple.DeserializeResult; import org.apache.datasketches.tuple.SummaryDeserializer; /** * @author Lee Rhodes */ public class IntegerSummaryDeserializer implements SummaryDeserializer<IntegerSummary> { @Override public DeserializeResult<IntegerSummary> heapifySummary(final Memory mem) { return IntegerSummary.fromMemory(mem); } }
2,579
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/aninteger/IntegerSketch.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.aninteger; import org.apache.datasketches.common.ResizeFactor; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.tuple.UpdatableSketch; /** * @author Lee Rhodes */ public class IntegerSketch extends UpdatableSketch<Integer, IntegerSummary> { /** * Constructs this sketch with given <i>lgK</i>. * @param lgK Log_base2 of <i>Nominal Entries</i>. * <a href="{@docRoot}/resources/dictionary.html#nomEntries">See Nominal Entries</a> * @param mode The IntegerSummary mode to be used */ public IntegerSketch(final int lgK, final IntegerSummary.Mode mode) { this(lgK, ResizeFactor.X8.ordinal(), 1.0F, mode); } /** * Creates this sketch with the following parameters: * @param lgK Log_base2 of <i>Nominal Entries</i>. * @param lgResizeFactor log2(resizeFactor) - value from 0 to 3: * <pre> * 0 - no resizing (max size allocated), * 1 - double internal hash table each time it reaches a threshold * 2 - grow four times * 3 - grow eight times (default) * </pre> * @param samplingProbability * <a href="{@docRoot}/resources/dictionary.html#p">See Sampling Probability</a> * @param mode The IntegerSummary mode to be used */ public IntegerSketch(final int lgK, final int lgResizeFactor, final float samplingProbability, final IntegerSummary.Mode mode) { super(1 << lgK, lgResizeFactor, samplingProbability, new IntegerSummaryFactory(mode)); } /** * Constructs this sketch from a Memory image, which must be from an IntegerSketch, and * usually with data. * @param mem the given Memory * @param mode The IntegerSummary mode to be used * @deprecated As of 3.0.0, heapifying an UpdatableSketch is deprecated. * This capability will be removed in a future release. * Heapifying a CompactSketch is not deprecated. */ @Deprecated public IntegerSketch(final Memory mem, final IntegerSummary.Mode mode) { super(mem, new IntegerSummaryDeserializer(), new IntegerSummaryFactory(mode)); } @Override public void update(final String key, final Integer value) { super.update(key, value); } @Override public void update(final long key, final Integer value) { super.update(key, value); } }
2,580
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/aninteger/IntegerSummarySetOperations.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.aninteger; import org.apache.datasketches.tuple.SummarySetOperations; import org.apache.datasketches.tuple.aninteger.IntegerSummary.Mode; /** * Methods for defining how unions and intersections of two objects of type IntegerSummary * are performed. * * @author Lee Rhodes */ public class IntegerSummarySetOperations implements SummarySetOperations<IntegerSummary> { private final Mode unionSummaryMode_; /** * Intersection is not well defined or even meaningful between numeric values. * Nevertheless, this can be defined to be a different type of aggregation for intersecting hashes. */ private final Mode intersectionSummaryMode_; /** * Creates a new instance with two modes * @param unionSummaryMode for unions * @param intersectionSummaryMode for intersections */ public IntegerSummarySetOperations(final Mode unionSummaryMode, final Mode intersectionSummaryMode) { unionSummaryMode_ = unionSummaryMode; intersectionSummaryMode_ = intersectionSummaryMode; } @Override public IntegerSummary union(final IntegerSummary a, final IntegerSummary b) { final IntegerSummary result = new IntegerSummary(unionSummaryMode_); result.update(a.getValue()); result.update(b.getValue()); return result; } @Override public IntegerSummary intersection(final IntegerSummary a, final IntegerSummary b) { final IntegerSummary result = new IntegerSummary(intersectionSummaryMode_); result.update(a.getValue()); result.update(b.getValue()); return result; } }
2,581
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/aninteger/IntegerSummary.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.aninteger; import org.apache.datasketches.common.ByteArrayUtil; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.tuple.DeserializeResult; import org.apache.datasketches.tuple.UpdatableSummary; /** * Summary for generic tuple sketches of type Integer. * This summary keeps an Integer value. On update a predefined operation is performed depending on * the mode. * Supported modes: Sum, Min, Max, AlwaysOne, Increment. The default mode is Sum. */ public class IntegerSummary implements UpdatableSummary<Integer> { private int value_; private final Mode mode_; /** * The aggregation modes for this Summary */ public enum Mode { /** * The aggregation mode is the summation function. * <p>New retained value = previous retained value + incoming value</p> */ Sum, /** * The aggregation mode is the minimum function. * <p>New retained value = min(previous retained value, incoming value)</p> */ Min, /** * The aggregation mode is the maximum function. * <p>New retained value = max(previous retained value, incoming value)</p> */ Max, /** * The aggregation mode is always one. * <p>New retained value = 1</p> */ AlwaysOne } /** * Creates an instance of IntegerSummary with a given starting value and mode. * @param value starting value * @param mode update mode */ private IntegerSummary(final int value, final Mode mode) { value_ = value; mode_ = mode; } /** * Creates an instance of IntegerSummary with a given mode. * @param mode update mode. This should not be called by a user. */ public IntegerSummary(final Mode mode) { mode_ = mode; switch (mode) { case Sum: value_ = 0; break; case Min: value_ = Integer.MAX_VALUE; break; case Max: value_ = Integer.MIN_VALUE; break; case AlwaysOne: value_ = 1; break; } } @Override public IntegerSummary update(final Integer value) { switch (mode_) { case Sum: value_ += value; break; case Min: if (value < value_) { value_ = value; } break; case Max: if (value > value_) { value_ = value; } break; case AlwaysOne: value_ = 1; break; } return this; } @Override public IntegerSummary copy() { return new IntegerSummary(value_, mode_); } /** * @return current value of the IntegerSummary */ public int getValue() { return value_; } private static final int SERIALIZED_SIZE_BYTES = 5; private static final int VALUE_INDEX = 0; private static final int MODE_BYTE_INDEX = 4; @Override public byte[] toByteArray() { final byte[] bytes = new byte[SERIALIZED_SIZE_BYTES]; ByteArrayUtil.putIntLE(bytes, VALUE_INDEX, value_); bytes[MODE_BYTE_INDEX] = (byte) mode_.ordinal(); return bytes; } /** * Creates an instance of the IntegerSummary given a serialized representation * @param mem Memory object with serialized IntegerSummary * @return DeserializedResult object, which contains a IntegerSummary object and number of bytes * read from the Memory */ public static DeserializeResult<IntegerSummary> fromMemory(final Memory mem) { return new DeserializeResult<>(new IntegerSummary(mem.getInt(VALUE_INDEX), Mode.values()[mem.getByte(MODE_BYTE_INDEX)]), SERIALIZED_SIZE_BYTES); } }
2,582
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/aninteger/IntegerSummaryFactory.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.aninteger; import org.apache.datasketches.tuple.SummaryFactory; /** * Factory for IntegerSummary. * * @author Lee Rhodes */ public class IntegerSummaryFactory implements SummaryFactory<IntegerSummary> { private final IntegerSummary.Mode summaryMode_; /** * Creates an instance of IntegerSummaryFactory with a given mode * @param summaryMode summary mode */ public IntegerSummaryFactory(final IntegerSummary.Mode summaryMode) { summaryMode_ = summaryMode; } @Override public IntegerSummary newSummary() { return new IntegerSummary(summaryMode_); } }
2,583
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/aninteger/package-info.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /** * This package is for a generic implementation of the Tuple sketch for single Integer value. */ package org.apache.datasketches.tuple.aninteger;
2,584
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/arrayofdoubles/DirectArrayOfDoublesQuickSelectSketch.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.arrayofdoubles; import java.nio.ByteOrder; import java.util.Arrays; import org.apache.datasketches.common.Family; import org.apache.datasketches.common.ResizeFactor; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.memory.WritableMemory; import org.apache.datasketches.thetacommon.HashOperations; import org.apache.datasketches.tuple.SerializerDeserializer; import org.apache.datasketches.tuple.Util; /** * Direct QuickSelect tuple sketch of type ArrayOfDoubles. * <p>This implementation uses data in a given Memory that is owned and managed by the caller. * This Memory can be off-heap, which if managed properly will greatly reduce the need for * the JVM to perform garbage collection.</p> */ class DirectArrayOfDoublesQuickSelectSketch extends ArrayOfDoublesQuickSelectSketch { // these values exist only on heap, never serialized private WritableMemory mem_; // these can be derived from the mem_ contents, but are kept here for performance private int keysOffset_; private int valuesOffset_; /** * Construct a new sketch using the given Memory as its backing store. * * @param nomEntries Nominal number of entries. Forced to the nearest power of 2 greater than * given value. * @param lgResizeFactor log2(resize factor) - value from 0 to 3: * 0 - no resizing (max size allocated), * 1 - double internal hash table each time it reaches a threshold * 2 - grow four times * 3 - grow eight times (default) * @param samplingProbability * <a href="{@docRoot}/resources/dictionary.html#p">See Sampling Probability</a> * @param numValues Number of double values to keep for each key. * @param seed <a href="{@docRoot}/resources/dictionary.html#seed">See seed</a> * @param dstMem <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a> */ DirectArrayOfDoublesQuickSelectSketch(final int nomEntries, final int lgResizeFactor, final float samplingProbability, final int numValues, final long seed, final WritableMemory dstMem) { super(numValues, seed); mem_ = dstMem; final int startingCapacity = Util.getStartingCapacity(nomEntries, lgResizeFactor); checkIfEnoughMemory(dstMem, startingCapacity, numValues); mem_.putByte(PREAMBLE_LONGS_BYTE, (byte) 1); mem_.putByte(SERIAL_VERSION_BYTE, serialVersionUID); mem_.putByte(FAMILY_ID_BYTE, (byte) Family.TUPLE.getID()); mem_.putByte(SKETCH_TYPE_BYTE, (byte) SerializerDeserializer.SketchType.ArrayOfDoublesQuickSelectSketch.ordinal()); final boolean isBigEndian = ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN); mem_.putByte(FLAGS_BYTE, (byte) ( (isBigEndian ? 1 << Flags.IS_BIG_ENDIAN.ordinal() : 0) | (samplingProbability < 1f ? 1 << Flags.IS_IN_SAMPLING_MODE.ordinal() : 0) | (1 << Flags.IS_EMPTY.ordinal()) )); mem_.putByte(NUM_VALUES_BYTE, (byte) numValues); mem_.putShort(SEED_HASH_SHORT, Util.computeSeedHash(seed)); thetaLong_ = (long) (Long.MAX_VALUE * (double) samplingProbability); mem_.putLong(THETA_LONG, thetaLong_); mem_.putByte(LG_NOM_ENTRIES_BYTE, (byte) Integer.numberOfTrailingZeros(nomEntries)); mem_.putByte(LG_CUR_CAPACITY_BYTE, (byte) Integer.numberOfTrailingZeros(startingCapacity)); mem_.putByte(LG_RESIZE_FACTOR_BYTE, (byte) lgResizeFactor); mem_.putFloat(SAMPLING_P_FLOAT, samplingProbability); mem_.putInt(RETAINED_ENTRIES_INT, 0); keysOffset_ = ENTRIES_START; valuesOffset_ = keysOffset_ + (SIZE_OF_KEY_BYTES * startingCapacity); mem_.clear(keysOffset_, (long) SIZE_OF_KEY_BYTES * startingCapacity); // clear keys only lgCurrentCapacity_ = Integer.numberOfTrailingZeros(startingCapacity); setRebuildThreshold(); } /** * Wraps the given Memory. * @param mem <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a> * @param seed update seed */ DirectArrayOfDoublesQuickSelectSketch(final WritableMemory mem, final long seed) { super(mem.getByte(NUM_VALUES_BYTE), seed); mem_ = mem; SerializerDeserializer.validateFamily(mem.getByte(FAMILY_ID_BYTE), mem.getByte(PREAMBLE_LONGS_BYTE)); SerializerDeserializer.validateType(mem_.getByte(SKETCH_TYPE_BYTE), SerializerDeserializer.SketchType.ArrayOfDoublesQuickSelectSketch); final byte version = mem_.getByte(SERIAL_VERSION_BYTE); if (version != serialVersionUID) { throw new SketchesArgumentException("Serial version mismatch. Expected: " + serialVersionUID + ", actual: " + version); } final boolean isBigEndian = (mem.getByte(FLAGS_BYTE) & (1 << Flags.IS_BIG_ENDIAN.ordinal())) != 0; if (isBigEndian ^ ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN)) { throw new SketchesArgumentException("Byte order mismatch"); } Util.checkSeedHashes(mem.getShort(SEED_HASH_SHORT), Util.computeSeedHash(seed)); keysOffset_ = ENTRIES_START; valuesOffset_ = keysOffset_ + (SIZE_OF_KEY_BYTES * getCurrentCapacity()); // to do: make parent take care of its own parts lgCurrentCapacity_ = Integer.numberOfTrailingZeros(getCurrentCapacity()); thetaLong_ = mem_.getLong(THETA_LONG); isEmpty_ = (mem_.getByte(FLAGS_BYTE) & (1 << Flags.IS_EMPTY.ordinal())) != 0; setRebuildThreshold(); } @Override //converts Memory hashTable of double[] to compacted double[][] public double[][] getValues() { final int count = getRetainedEntries(); final double[][] values = new double[count][]; if (count > 0) { long keyOffset = keysOffset_; long valuesOffset = valuesOffset_; int cnt = 0; for (int j = 0; j < getCurrentCapacity(); j++) { if (mem_.getLong(keyOffset) != 0) { final double[] array = new double[numValues_]; mem_.getDoubleArray(valuesOffset, array, 0, numValues_); values[cnt++] = array; } keyOffset += SIZE_OF_KEY_BYTES; valuesOffset += (long)SIZE_OF_VALUE_BYTES * numValues_; } } return values; } @Override //converts heap hashTable of double[] to compacted double[] double[] getValuesAsOneDimension() { final int count = getRetainedEntries(); final double[] values = new double[count * numValues_]; final int cap = getCurrentCapacity(); if (count > 0) { long keyOffsetBytes = keysOffset_; long valuesOffsetBytes = valuesOffset_; int cnt = 0; for (int j = 0; j < cap; j++) { if (mem_.getLong(keyOffsetBytes) != 0) { mem_.getDoubleArray(valuesOffsetBytes, values, cnt++ * numValues_, numValues_); } keyOffsetBytes += SIZE_OF_KEY_BYTES; valuesOffsetBytes += (long)SIZE_OF_VALUE_BYTES * numValues_; } assert cnt == count; } return values; } @Override //converts heap hashTable of long[] to compacted long[] long[] getKeys() { final int count = getRetainedEntries(); final long[] keys = new long[count]; final int cap = getCurrentCapacity(); if (count > 0) { long keyOffsetBytes = keysOffset_; int cnt = 0; for (int j = 0; j < cap; j++) { final long key; if ((key = mem_.getLong(keyOffsetBytes)) != 0) { keys[cnt++] = key; } keyOffsetBytes += SIZE_OF_KEY_BYTES; } assert cnt == count; } return keys; } @Override public int getRetainedEntries() { return mem_.getInt(RETAINED_ENTRIES_INT); } @Override public int getNominalEntries() { return 1 << mem_.getByte(LG_NOM_ENTRIES_BYTE); } @Override public ResizeFactor getResizeFactor() { return ResizeFactor.getRF(mem_.getByte(LG_RESIZE_FACTOR_BYTE)); } @Override public float getSamplingProbability() { return mem_.getFloat(SAMPLING_P_FLOAT); } @Override public byte[] toByteArray() { final int sizeBytes = getSerializedSizeBytes(); final byte[] byteArray = new byte[sizeBytes]; final WritableMemory mem = WritableMemory.writableWrap(byteArray); serializeInto(mem); return byteArray; } @Override public ArrayOfDoublesSketchIterator iterator() { return new DirectArrayOfDoublesSketchIterator(mem_, keysOffset_, getCurrentCapacity(), numValues_); } @Override public boolean hasMemory() { return true; } @Override WritableMemory getMemory() { return mem_; } @Override int getSerializedSizeBytes() { return valuesOffset_ + (SIZE_OF_VALUE_BYTES * numValues_ * getCurrentCapacity()); } @Override void serializeInto(final WritableMemory mem) { mem_.copyTo(0, mem, 0, mem.getCapacity()); } @Override public void reset() { if (!isEmpty_) { isEmpty_ = true; mem_.setBits(FLAGS_BYTE, (byte) (1 << Flags.IS_EMPTY.ordinal())); } final int lgResizeFactor = mem_.getByte(LG_RESIZE_FACTOR_BYTE); final float samplingProbability = mem_.getFloat(SAMPLING_P_FLOAT); final int startingCapacity = Util.getStartingCapacity(getNominalEntries(), lgResizeFactor); thetaLong_ = (long) (Long.MAX_VALUE * (double) samplingProbability); mem_.putLong(THETA_LONG, thetaLong_); mem_.putByte(LG_CUR_CAPACITY_BYTE, (byte) Integer.numberOfTrailingZeros(startingCapacity)); mem_.putInt(RETAINED_ENTRIES_INT, 0); keysOffset_ = ENTRIES_START; valuesOffset_ = keysOffset_ + (SIZE_OF_KEY_BYTES * startingCapacity); mem_.clear(keysOffset_, (long) SIZE_OF_KEY_BYTES * startingCapacity); // clear keys only lgCurrentCapacity_ = Integer.numberOfTrailingZeros(startingCapacity); setRebuildThreshold(); } @Override protected long getKey(final int index) { return mem_.getLong(keysOffset_ + ((long) SIZE_OF_KEY_BYTES * index)); } @Override protected void incrementCount() { final int count = mem_.getInt(RETAINED_ENTRIES_INT); if (count == 0) { mem_.setBits(FLAGS_BYTE, (byte) (1 << Flags.HAS_ENTRIES.ordinal())); } mem_.putInt(RETAINED_ENTRIES_INT, count + 1); } @Override protected final int getCurrentCapacity() { return 1 << mem_.getByte(LG_CUR_CAPACITY_BYTE); } @Override protected void setThetaLong(final long thetaLong) { thetaLong_ = thetaLong; mem_.putLong(THETA_LONG, thetaLong_); } @Override protected void setValues(final int index, final double[] values) { long offset = valuesOffset_ + ((long) SIZE_OF_VALUE_BYTES * numValues_ * index); for (int i = 0; i < numValues_; i++) { mem_.putDouble(offset, values[i]); offset += SIZE_OF_VALUE_BYTES; } } @Override protected void updateValues(final int index, final double[] values) { long offset = valuesOffset_ + ((long) SIZE_OF_VALUE_BYTES * numValues_ * index); for (int i = 0; i < numValues_; i++) { mem_.putDouble(offset, mem_.getDouble(offset) + values[i]); offset += SIZE_OF_VALUE_BYTES; } } @Override protected void setNotEmpty() { if (isEmpty_) { isEmpty_ = false; mem_.clearBits(FLAGS_BYTE, (byte) (1 << Flags.IS_EMPTY.ordinal())); } } @Override protected boolean isInSamplingMode() { return (mem_.getByte(FLAGS_BYTE) & (1 << Flags.IS_IN_SAMPLING_MODE.ordinal())) != 0; } // rebuild in the same memory @Override protected void rebuild(final int newCapacity) { final int numValues = getNumValues(); checkIfEnoughMemory(mem_, newCapacity, numValues); final int currCapacity = getCurrentCapacity(); final long[] keys = new long[currCapacity]; final double[] values = new double[currCapacity * numValues]; mem_.getLongArray(keysOffset_, keys, 0, currCapacity); mem_.getDoubleArray(valuesOffset_, values, 0, currCapacity * numValues); mem_.clear(keysOffset_, ((long) SIZE_OF_KEY_BYTES * newCapacity) + ((long) SIZE_OF_VALUE_BYTES * newCapacity * numValues)); mem_.putInt(RETAINED_ENTRIES_INT, 0); mem_.putByte(LG_CUR_CAPACITY_BYTE, (byte)Integer.numberOfTrailingZeros(newCapacity)); valuesOffset_ = keysOffset_ + (SIZE_OF_KEY_BYTES * newCapacity); lgCurrentCapacity_ = Integer.numberOfTrailingZeros(newCapacity); for (int i = 0; i < keys.length; i++) { if ((keys[i] != 0) && (keys[i] < thetaLong_)) { insert(keys[i], Arrays.copyOfRange(values, i * numValues, (i + 1) * numValues)); } } setRebuildThreshold(); } @Override protected int insertKey(final long key) { return HashOperations.hashInsertOnlyMemory(mem_, lgCurrentCapacity_, key, ENTRIES_START); } @Override protected int findOrInsertKey(final long key) { return HashOperations.hashSearchOrInsertMemory(mem_, lgCurrentCapacity_, key, ENTRIES_START); } @Override protected double[] find(final long key) { final int index = HashOperations.hashSearchMemory(mem_, lgCurrentCapacity_, key, ENTRIES_START); if (index == -1) { return null; } final double[] array = new double[numValues_]; mem_.getDoubleArray(valuesOffset_ + ((long) SIZE_OF_VALUE_BYTES * numValues_ * index), array, 0, numValues_); return array; } private static void checkIfEnoughMemory(final Memory mem, final int numEntries, final int numValues) { final int sizeNeeded = ENTRIES_START + ((SIZE_OF_KEY_BYTES + (SIZE_OF_VALUE_BYTES * numValues)) * numEntries); if (sizeNeeded > mem.getCapacity()) { throw new SketchesArgumentException("Not enough memory: need " + sizeNeeded + " bytes, got " + mem.getCapacity() + " bytes"); } } }
2,585
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/arrayofdoubles/ArrayOfDoublesCombiner.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.arrayofdoubles; /** * Combines two arrays of double values for use with ArrayOfDoubles tuple sketches */ public interface ArrayOfDoublesCombiner { /** * Method of combining two arrays of double values * @param a Array A. * @param b Array B. * @return Result of combining A and B */ public double[] combine(double[] a, double[] b); }
2,586
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/arrayofdoubles/HeapArrayOfDoublesSketchIterator.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.arrayofdoubles; import java.util.Arrays; /** * Iterator over the on-heap ArrayOfDoublesSketch (compact or hash table) */ final class HeapArrayOfDoublesSketchIterator implements ArrayOfDoublesSketchIterator { private long[] keys_; private double[] values_; private int numValues_; private int i_; HeapArrayOfDoublesSketchIterator(final long[] keys, final double[] values, final int numValues) { keys_ = keys; values_ = values; numValues_ = numValues; i_ = -1; } @Override public boolean next() { if (keys_ == null) { return false; } i_++; while (i_ < keys_.length) { if (keys_[i_] != 0) { return true; } i_++; } return false; } @Override public long getKey() { return keys_[i_]; } @Override public double[] getValues() { if (numValues_ == 1) { return new double[] { values_[i_] }; } return Arrays.copyOfRange(values_, i_ * numValues_, (i_ + 1) * numValues_); } }
2,587
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/arrayofdoubles/ArrayOfDoublesCompactSketch.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.arrayofdoubles; /** * Top level compact tuple sketch of type ArrayOfDoubles. Compact sketches are never created * directly. They are created as a result of the compact() method on a QuickSelectSketch * or the getResult() method of a set operation like Union, Intersection or AnotB. * Compact sketch consists of a compact list (i.e. no intervening spaces) of hash values, * corresponding list of double values, and a value for theta. The lists may or may * not be ordered. A compact sketch is read-only. */ public abstract class ArrayOfDoublesCompactSketch extends ArrayOfDoublesSketch { static final byte serialVersionUID = 1; // Layout of retained entries: // Long || Start Byte Adr: // Adr: // || 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | // 3 ||-----------------------------------|----------Retained Entries------------| static final int EMPTY_SIZE = 16; static final int RETAINED_ENTRIES_INT = 16; // 4 bytes of padding for 8 byte alignment static final int ENTRIES_START = 24; ArrayOfDoublesCompactSketch(final int numValues) { super(numValues); } @Override public int getCurrentBytes() { final int count = getRetainedEntries(); int sizeBytes = EMPTY_SIZE; if (count > 0) { sizeBytes = ENTRIES_START + (SIZE_OF_KEY_BYTES * count) + (SIZE_OF_VALUE_BYTES * numValues_ * count); } return sizeBytes; } @Override public int getMaxBytes() { return getCurrentBytes(); } }
2,588
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/arrayofdoubles/DirectArrayOfDoublesSketchIterator.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.arrayofdoubles; import org.apache.datasketches.memory.Memory; /** * Iterator over the off-heap, Direct tuple sketch of type ArrayOfDoubles (compact or hash table). * <p>This implementation uses data in a given Memory that is owned and managed by the caller. * This Memory can be off-heap, which if managed properly will greatly reduce the need for * the JVM to perform garbage collection.</p> */ final class DirectArrayOfDoublesSketchIterator implements ArrayOfDoublesSketchIterator { private Memory mem_; private int offset_; private int numEntries_; private int numValues_; private int i_; private static final int SIZE_OF_KEY_BYTES = 8; private static final int SIZE_OF_VALUE_BYTES = 8; DirectArrayOfDoublesSketchIterator(final Memory mem, final int offset, final int numEntries, final int numValues) { mem_ = mem; offset_ = offset; numEntries_ = numEntries; numValues_ = numValues; i_ = -1; } @Override public boolean next() { i_++; while (i_ < numEntries_) { if (mem_.getLong(offset_ + ((long) SIZE_OF_KEY_BYTES * i_)) != 0) { return true; } i_++; } return false; } @Override public long getKey() { return mem_.getLong(offset_ + ((long) SIZE_OF_KEY_BYTES * i_)); } @Override public double[] getValues() { if (numValues_ == 1) { return new double[] { mem_.getDouble(offset_ + ((long) SIZE_OF_KEY_BYTES * numEntries_) + ((long) SIZE_OF_VALUE_BYTES * i_)) }; } final double[] array = new double[numValues_]; mem_.getDoubleArray(offset_ + ((long) SIZE_OF_KEY_BYTES * numEntries_) + ((long) SIZE_OF_VALUE_BYTES * i_ * numValues_), array, 0, numValues_); return array; } }
2,589
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/arrayofdoubles/ArrayOfDoublesAnotB.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.arrayofdoubles; import org.apache.datasketches.memory.WritableMemory; /** * Computes a set difference of two tuple sketches of type ArrayOfDoubles */ public abstract class ArrayOfDoublesAnotB { ArrayOfDoublesAnotB() {} /** * Perform A-and-not-B set operation on the two given sketches. * A null sketch is interpreted as an empty sketch. * This is not an accumulating update. Calling update() more than once * without calling getResult() will discard the result of previous update(). * Both input sketches must have the same <i>numValues</i>. * * @param a The incoming sketch for the first argument * @param b The incoming sketch for the second argument */ public abstract void update(ArrayOfDoublesSketch a, ArrayOfDoublesSketch b); /** * Gets the result of this operation in the form of a ArrayOfDoublesCompactSketch * @return compact sketch representing the result of the operation */ public abstract ArrayOfDoublesCompactSketch getResult(); /** * Gets the result of this operation in the form of a ArrayOfDoublesCompactSketch * @param mem memory for the result (can be null) * @return compact sketch representing the result of the operation (off-heap if memory is * provided) */ public abstract ArrayOfDoublesCompactSketch getResult(WritableMemory mem); }
2,590
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/arrayofdoubles/ArrayOfDoublesUnion.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.arrayofdoubles; import static java.lang.Math.min; import org.apache.datasketches.common.Family; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.memory.WritableMemory; import org.apache.datasketches.thetacommon.ThetaUtil; import org.apache.datasketches.tuple.SerializerDeserializer; import org.apache.datasketches.tuple.Util; /** * The base class for unions of tuple sketches of type ArrayOfDoubles. */ public abstract class ArrayOfDoublesUnion { static final byte serialVersionUID = 1; //For layout see toByteArray() static final int PREAMBLE_SIZE_BYTES = 16; static final int PREAMBLE_LONGS_BYTE = 0; // not used, always 1 static final int SERIAL_VERSION_BYTE = 1; static final int FAMILY_ID_BYTE = 2; static final int SKETCH_TYPE_BYTE = 3; static final int FLAGS_BYTE = 4; static final int NUM_VALUES_BYTE = 5; static final int SEED_HASH_SHORT = 6; static final int THETA_LONG = 8; ArrayOfDoublesQuickSelectSketch gadget_; long unionThetaLong_; /** * Constructs this Union initializing it with the given sketch, which can be on-heap or off-heap. * @param sketch the given sketch. */ ArrayOfDoublesUnion(final ArrayOfDoublesQuickSelectSketch sketch) { gadget_ = sketch; unionThetaLong_ = sketch.getThetaLong(); } /** * Heapify the given Memory as an ArrayOfDoublesUnion * @param srcMem the given source Memory * @return an ArrayOfDoublesUnion */ public static ArrayOfDoublesUnion heapify(final Memory srcMem) { return heapify(srcMem, ThetaUtil.DEFAULT_UPDATE_SEED); } /** * Heapify the given Memory and seed as an ArrayOfDoublesUnion * @param srcMem the given source Memory * @param seed the given seed * @return an ArrayOfDoublesUnion */ public static ArrayOfDoublesUnion heapify(final Memory srcMem, final long seed) { return HeapArrayOfDoublesUnion.heapifyUnion(srcMem, seed); } /** * Wrap the given Memory as an ArrayOfDoublesUnion * @param srcMem the given source Memory * @return an ArrayOfDoublesUnion */ public static ArrayOfDoublesUnion wrap(final Memory srcMem) { return wrap(srcMem, ThetaUtil.DEFAULT_UPDATE_SEED); } /** * Wrap the given Memory and seed as an ArrayOfDoublesUnion * @param srcMem the given source Memory * @param seed the given seed * @return an ArrayOfDoublesUnion */ public static ArrayOfDoublesUnion wrap(final Memory srcMem, final long seed) { return DirectArrayOfDoublesUnion.wrapUnion((WritableMemory) srcMem, seed, false); } /** * Wrap the given WritableMemory as an ArrayOfDoublesUnion * @param srcMem the given source Memory * @return an ArrayOfDoublesUnion */ public static ArrayOfDoublesUnion wrap(final WritableMemory srcMem) { return wrap(srcMem, ThetaUtil.DEFAULT_UPDATE_SEED); } /** * Wrap the given WritableMemory and seed as an ArrayOfDoublesUnion * @param srcMem the given source Memory * @param seed the given seed * @return an ArrayOfDoublesUnion */ public static ArrayOfDoublesUnion wrap(final WritableMemory srcMem, final long seed) { return DirectArrayOfDoublesUnion.wrapUnion(srcMem, seed, true); } /** * Updates the union by adding a set of entries from a given sketch, which can be on-heap or off-heap. * Both the given tupleSketch and the internal state of the Union must have the same <i>numValues</i>. * * <p>Nulls and empty sketches are ignored.</p> * * @param tupleSketch sketch to add to the union */ public void union(final ArrayOfDoublesSketch tupleSketch) { if (tupleSketch == null) { return; } Util.checkSeedHashes(gadget_.getSeedHash(), tupleSketch.getSeedHash()); if (gadget_.getNumValues() != tupleSketch.getNumValues()) { throw new SketchesArgumentException("Incompatible sketches: number of values mismatch " + gadget_.getNumValues() + " and " + tupleSketch.getNumValues()); } if (tupleSketch.isEmpty()) { return; } else { gadget_.setNotEmpty(); } setUnionThetaLong(min(min(unionThetaLong_, tupleSketch.getThetaLong()), gadget_.getThetaLong())); if (tupleSketch.getRetainedEntries() == 0) { return; } final ArrayOfDoublesSketchIterator it = tupleSketch.iterator(); while (it.next()) { if (it.getKey() < unionThetaLong_) { gadget_.merge(it.getKey(), it.getValues()); } } // keep the union theta as low as possible for performance if (gadget_.getThetaLong() < unionThetaLong_) { setUnionThetaLong(gadget_.getThetaLong()); } } /** * Returns the resulting union in the form of a compact sketch * @param dstMem memory for the result (can be null) * @return compact sketch representing the union (off-heap if memory is provided) */ public ArrayOfDoublesCompactSketch getResult(final WritableMemory dstMem) { long unionThetaLong = unionThetaLong_; if (gadget_.getRetainedEntries() > gadget_.getNominalEntries()) { unionThetaLong = Math.min(unionThetaLong, gadget_.getNewThetaLong()); } if (dstMem == null) { return new HeapArrayOfDoublesCompactSketch(gadget_, unionThetaLong); } return new DirectArrayOfDoublesCompactSketch(gadget_, unionThetaLong, dstMem); } /** * Returns the resulting union in the form of a compact sketch * @return on-heap compact sketch representing the union */ public ArrayOfDoublesCompactSketch getResult() { return getResult(null); } /** * Resets the union to an empty state */ public void reset() { gadget_.reset(); setUnionThetaLong(gadget_.getThetaLong()); } // Layout of first 16 bytes: // Long || Start Byte Adr: // Adr: // || 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | // 0 || Seed Hash=0 | #Dbls=0|Flags=0 | SkType | FamID | SerVer | Preamble_Longs | // || 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | // 1 ||---------------------------Union Theta Long-----------------------------------------| /** * @return a byte array representation of this object */ public byte[] toByteArray() { final int sizeBytes = PREAMBLE_SIZE_BYTES + gadget_.getSerializedSizeBytes(); final byte[] byteArray = new byte[sizeBytes]; final WritableMemory mem = WritableMemory.writableWrap(byteArray); mem.putByte(PREAMBLE_LONGS_BYTE, (byte) 1); // unused, always 1 mem.putByte(SERIAL_VERSION_BYTE, serialVersionUID); mem.putByte(FAMILY_ID_BYTE, (byte) Family.TUPLE.getID()); mem.putByte(SKETCH_TYPE_BYTE, (byte) SerializerDeserializer.SketchType.ArrayOfDoublesUnion.ordinal()); //byte 4-7 automatically zero mem.putLong(THETA_LONG, unionThetaLong_); gadget_.serializeInto(mem.writableRegion(PREAMBLE_SIZE_BYTES, mem.getCapacity() - PREAMBLE_SIZE_BYTES)); return byteArray; } /** * @param nomEntries Nominal number of entries. Forced to the nearest power of 2 greater than or equal to * given value. * @param numValues Number of double values to keep for each key * @return maximum required storage bytes given nomEntries and numValues */ public static int getMaxBytes(final int nomEntries, final int numValues) { return ArrayOfDoublesQuickSelectSketch.getMaxBytes(nomEntries, numValues) + PREAMBLE_SIZE_BYTES; } void setUnionThetaLong(final long thetaLong) { unionThetaLong_ = thetaLong; } }
2,591
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/arrayofdoubles/ArrayOfDoublesSketchIterator.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.arrayofdoubles; /** * Interface for iterating over tuple sketches of type ArrayOfDoubles */ public interface ArrayOfDoublesSketchIterator { /** * Advancing the iterator and checking existence of the next entry * is combined here for efficiency. This results in an undefined * state of the iterator before the first call of this method. * @return true if the next element exists */ public boolean next(); /** * Gets a key from the current entry in the sketch, which is a hash * of the original key passed to update(). The original keys are not * retained. Don't call this before calling next() for the first time * or after getting false from next(). * @return hash key from the current entry */ public long getKey(); /** * Gets an array of values from the current entry in the sketch. * Don't call this before calling next() for the first time * or after getting false from next(). * @return array of double values for the current entry (may or may not be a copy) */ public double[] getValues(); }
2,592
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/arrayofdoubles/HashTables.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.arrayofdoubles; import static java.lang.Math.ceil; import static java.lang.Math.max; import static java.lang.Math.min; import static org.apache.datasketches.common.Util.ceilingIntPowerOf2; import static org.apache.datasketches.thetacommon.HashOperations.hashInsertOnly; import static org.apache.datasketches.thetacommon.HashOperations.hashSearch; import org.apache.datasketches.thetacommon.ThetaUtil; class HashTables { private long[] hashTable = null; private double[][] valueTable = null; private int numValues = 0; private int lgTableSize = 0; private int numKeys = 0; //Construct from sketch HashTables(final ArrayOfDoublesSketch sketchIn) { numKeys = sketchIn.getRetainedEntries(); numValues = sketchIn.getNumValues(); lgTableSize = getLgTableSize(numKeys); final int tableSize = 1 << lgTableSize; hashTable = new long[tableSize]; valueTable = new double[tableSize][]; final ArrayOfDoublesSketchIterator it = sketchIn.iterator(); while (it.next()) { final long hash = it.getKey(); final int index = hashInsertOnly(hashTable, lgTableSize, hash); valueTable[index] = new double[numValues]; System.arraycopy(it.getValues(), 0, valueTable[index], 0, numValues); } } //Construct: Load the hash and value tables from packed hash and value arrays private HashTables(final long[] hashArr, final double[][] valuesArr, final int numKeys, final int numValues) { this.numValues = numValues; this.numKeys = numKeys; lgTableSize = getLgTableSize(numKeys); final int tableSize = 1 << lgTableSize; hashTable = new long[tableSize]; valueTable = new double[tableSize][]; for (int i = 0; i < numKeys; i++) { final long hash = hashArr[i]; final int index = hashInsertOnly(hashTable, lgTableSize, hash); valueTable[index] = new double[numValues]; System.arraycopy(valuesArr[i], 0, valueTable[index], 0, numValues); } } HashTables getIntersectHashTables( final ArrayOfDoublesSketch nextTupleSketch, final long thetaLong, final ArrayOfDoublesCombiner combiner) { //Match nextSketch data with local instance data, filtering by theta final int maxMatchSize = min(numKeys, nextTupleSketch.getRetainedEntries()); assert numValues == nextTupleSketch.numValues_; final long[] matchHashArr = new long[maxMatchSize]; final double[][] matchValuesArr = new double[maxMatchSize][]; //Copy the intersecting items from local hashTables_ // sequentially into local packed matchHashArr_ and matchValuesArr int matchCount = 0; final ArrayOfDoublesSketchIterator it = nextTupleSketch.iterator(); while (it.next()) { final long hash = it.getKey(); if (hash >= thetaLong) { continue; } final int index = hashSearch(hashTable, lgTableSize, hash); if (index < 0) { continue; } matchHashArr[matchCount] = hash; matchValuesArr[matchCount] = combiner.combine(valueTable[index], it.getValues()); matchCount++; } return new HashTables(matchHashArr, matchValuesArr, matchCount, numValues); } int getNumKeys() { return numKeys; } int getNumValues() { return numValues; } long[] getHashTable() { return hashTable; } double[][] getValueTable() { return valueTable; } void clear() { hashTable = null; valueTable = null; numValues = 0; lgTableSize = 0; numKeys = 0; } static int getLgTableSize(final int numKeys) { final int tableSize = max(ceilingIntPowerOf2((int) ceil(numKeys / 0.75)), 1 << ThetaUtil.MIN_LG_NOM_LONGS); return Integer.numberOfTrailingZeros(tableSize); } }
2,593
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/arrayofdoubles/DirectArrayOfDoublesCompactSketch.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.arrayofdoubles; import java.nio.ByteOrder; import org.apache.datasketches.common.Family; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.memory.WritableMemory; import org.apache.datasketches.tuple.SerializerDeserializer; import org.apache.datasketches.tuple.Util; /** * Direct Compact Sketch of type ArrayOfDoubles. * <p>This implementation uses data in a given Memory that is owned and managed by the caller. * This Memory can be off-heap, which if managed properly will greatly reduce the need for * the JVM to perform garbage collection.</p> */ final class DirectArrayOfDoublesCompactSketch extends ArrayOfDoublesCompactSketch { // this value exists only on heap, never serialized private Memory mem_; /** * Converts the given UpdatableArrayOfDoublesSketch to this compact form. * @param sketch the given UpdatableArrayOfDoublesSketch * @param dstMem the given destination Memory. */ DirectArrayOfDoublesCompactSketch(final ArrayOfDoublesUpdatableSketch sketch, final WritableMemory dstMem) { this(sketch, sketch.getThetaLong(), dstMem); } /** * Converts the given UpdatableArrayOfDoublesSketch to this compact form * trimming if necessary according to given theta * @param sketch the given UpdatableArrayOfDoublesSketch * @param thetaLong new value of thetaLong * @param dstMem the given destination Memory. */ DirectArrayOfDoublesCompactSketch(final ArrayOfDoublesUpdatableSketch sketch, final long thetaLong, final WritableMemory dstMem) { super(sketch.getNumValues()); checkIfEnoughMemory(dstMem, sketch.getRetainedEntries(), sketch.getNumValues()); mem_ = dstMem; dstMem.putByte(PREAMBLE_LONGS_BYTE, (byte) 1); dstMem.putByte(SERIAL_VERSION_BYTE, serialVersionUID); dstMem.putByte(FAMILY_ID_BYTE, (byte) Family.TUPLE.getID()); dstMem.putByte(SKETCH_TYPE_BYTE, (byte) SerializerDeserializer.SketchType.ArrayOfDoublesCompactSketch.ordinal()); final boolean isBigEndian = ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN); isEmpty_ = sketch.isEmpty(); final int count = sketch.getRetainedEntries(); dstMem.putByte(FLAGS_BYTE, (byte) ( (isBigEndian ? 1 << Flags.IS_BIG_ENDIAN.ordinal() : 0) | (isEmpty_ ? 1 << Flags.IS_EMPTY.ordinal() : 0) | (count > 0 ? 1 << Flags.HAS_ENTRIES.ordinal() : 0) )); dstMem.putByte(NUM_VALUES_BYTE, (byte) numValues_); dstMem.putShort(SEED_HASH_SHORT, Util.computeSeedHash(sketch.getSeed())); thetaLong_ = Math.min(sketch.getThetaLong(), thetaLong); dstMem.putLong(THETA_LONG, thetaLong_); if (count > 0) { int keyOffset = ENTRIES_START; int valuesOffset = keyOffset + (SIZE_OF_KEY_BYTES * sketch.getRetainedEntries()); final ArrayOfDoublesSketchIterator it = sketch.iterator(); int actualCount = 0; while (it.next()) { if (it.getKey() < thetaLong_) { dstMem.putLong(keyOffset, it.getKey()); dstMem.putDoubleArray(valuesOffset, it.getValues(), 0, numValues_); keyOffset += SIZE_OF_KEY_BYTES; valuesOffset += SIZE_OF_VALUE_BYTES * numValues_; actualCount++; } } dstMem.putInt(RETAINED_ENTRIES_INT, actualCount); } } /* * Creates an instance from components */ DirectArrayOfDoublesCompactSketch(final long[] keys, final double[] values, final long thetaLong, final boolean isEmpty, final int numValues, final short seedHash, final WritableMemory dstMem) { super(numValues); checkIfEnoughMemory(dstMem, values.length, numValues); mem_ = dstMem; dstMem.putByte(PREAMBLE_LONGS_BYTE, (byte) 1); dstMem.putByte(SERIAL_VERSION_BYTE, serialVersionUID); dstMem.putByte(FAMILY_ID_BYTE, (byte) Family.TUPLE.getID()); dstMem.putByte(SKETCH_TYPE_BYTE, (byte) SerializerDeserializer.SketchType.ArrayOfDoublesCompactSketch.ordinal()); final boolean isBigEndian = ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN); isEmpty_ = isEmpty; final int count = keys.length; dstMem.putByte(FLAGS_BYTE, (byte) ( (isBigEndian ? 1 << Flags.IS_BIG_ENDIAN.ordinal() : 0) | (isEmpty_ ? 1 << Flags.IS_EMPTY.ordinal() : 0) | (count > 0 ? 1 << Flags.HAS_ENTRIES.ordinal() : 0) )); dstMem.putByte(NUM_VALUES_BYTE, (byte) numValues_); dstMem.putShort(SEED_HASH_SHORT, seedHash); thetaLong_ = thetaLong; dstMem.putLong(THETA_LONG, thetaLong_); if (count > 0) { dstMem.putInt(RETAINED_ENTRIES_INT, count); dstMem.putLongArray(ENTRIES_START, keys, 0, count); dstMem.putDoubleArray( ENTRIES_START + ((long) SIZE_OF_KEY_BYTES * count), values, 0, values.length); } } /** * Wraps the given Memory. * @param mem <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a> */ DirectArrayOfDoublesCompactSketch(final Memory mem) { super(mem.getByte(NUM_VALUES_BYTE)); mem_ = mem; SerializerDeserializer.validateFamily(mem.getByte(FAMILY_ID_BYTE), mem.getByte(PREAMBLE_LONGS_BYTE)); SerializerDeserializer.validateType(mem_.getByte(SKETCH_TYPE_BYTE), SerializerDeserializer.SketchType.ArrayOfDoublesCompactSketch); final byte version = mem_.getByte(SERIAL_VERSION_BYTE); if (version != serialVersionUID) { throw new SketchesArgumentException("Serial version mismatch. Expected: " + serialVersionUID + ", actual: " + version); } final boolean isBigEndian = (mem.getByte(FLAGS_BYTE) & (1 << Flags.IS_BIG_ENDIAN.ordinal())) != 0; if (isBigEndian ^ ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN)) { throw new SketchesArgumentException("Byte order mismatch"); } isEmpty_ = (mem_.getByte(FLAGS_BYTE) & (1 << Flags.IS_EMPTY.ordinal())) != 0; thetaLong_ = mem_.getLong(THETA_LONG); } /** * Wraps the given Memory. * @param mem <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a> * @param seed <a href="{@docRoot}/resources/dictionary.html#seed">See seed</a> */ DirectArrayOfDoublesCompactSketch(final Memory mem, final long seed) { super(mem.getByte(NUM_VALUES_BYTE)); mem_ = mem; SerializerDeserializer.validateFamily(mem.getByte(FAMILY_ID_BYTE), mem.getByte(PREAMBLE_LONGS_BYTE)); SerializerDeserializer.validateType(mem_.getByte(SKETCH_TYPE_BYTE), SerializerDeserializer.SketchType.ArrayOfDoublesCompactSketch); final byte version = mem_.getByte(SERIAL_VERSION_BYTE); if (version != serialVersionUID) { throw new SketchesArgumentException("Serial version mismatch. Expected: " + serialVersionUID + ", actual: " + version); } final boolean isBigEndian = (mem.getByte(FLAGS_BYTE) & (1 << Flags.IS_BIG_ENDIAN.ordinal())) != 0; if (isBigEndian ^ ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN)) { throw new SketchesArgumentException("Byte order mismatch"); } Util.checkSeedHashes(mem.getShort(SEED_HASH_SHORT), Util.computeSeedHash(seed)); isEmpty_ = (mem_.getByte(FLAGS_BYTE) & (1 << Flags.IS_EMPTY.ordinal())) != 0; thetaLong_ = mem_.getLong(THETA_LONG); } @Override public ArrayOfDoublesCompactSketch compact(final WritableMemory dstMem) { if (dstMem == null) { return new HeapArrayOfDoublesCompactSketch(getKeys(), getValuesAsOneDimension(), thetaLong_, isEmpty_, numValues_, getSeedHash()); } else { mem_.copyTo(0, dstMem, 0, mem_.getCapacity()); return new DirectArrayOfDoublesCompactSketch(dstMem); } } @Override public int getRetainedEntries() { final boolean hasEntries = (mem_.getByte(FLAGS_BYTE) & (1 << Flags.HAS_ENTRIES.ordinal())) != 0; return (hasEntries ? mem_.getInt(RETAINED_ENTRIES_INT) : 0); } @Override //converts compact Memory array of double[] to compact double[][] public double[][] getValues() { final int count = getRetainedEntries(); final double[][] values = new double[count][]; if (count > 0) { int valuesOffset = ENTRIES_START + (SIZE_OF_KEY_BYTES * count); for (int i = 0; i < count; i++) { final double[] array = new double[numValues_]; mem_.getDoubleArray(valuesOffset, array, 0, numValues_); values[i] = array; valuesOffset += SIZE_OF_VALUE_BYTES * numValues_; } } return values; } @Override //converts compact Memory array of double[] to compact double[] double[] getValuesAsOneDimension() { final int count = getRetainedEntries(); final int numDoubles = count * numValues_; final double[] values = new double[numDoubles]; if (count > 0) { final int valuesOffset = ENTRIES_START + (SIZE_OF_KEY_BYTES * count); mem_.getDoubleArray(valuesOffset, values, 0, numDoubles); } return values; } @Override //converts compact Memory array of long[] to compact long[] long[] getKeys() { final int count = getRetainedEntries(); final long[] keys = new long[count]; if (count > 0) { for (int i = 0; i < count; i++) { mem_.getLongArray(ENTRIES_START, keys, 0, count); } } return keys; } @Override public byte[] toByteArray() { final int sizeBytes = getCurrentBytes(); final byte[] byteArray = new byte[sizeBytes]; final WritableMemory mem = WritableMemory.writableWrap(byteArray); mem_.copyTo(0, mem, 0, sizeBytes); return byteArray; } @Override public ArrayOfDoublesSketchIterator iterator() { return new DirectArrayOfDoublesSketchIterator( mem_, ENTRIES_START, getRetainedEntries(), numValues_); } @Override short getSeedHash() { return mem_.getShort(SEED_HASH_SHORT); } @Override public boolean hasMemory() { return true; } @Override Memory getMemory() { return mem_; } private static void checkIfEnoughMemory(final Memory mem, final int numEntries, final int numValues) { final int sizeNeeded = ENTRIES_START + ((SIZE_OF_KEY_BYTES + (SIZE_OF_VALUE_BYTES * numValues)) * numEntries); if (sizeNeeded > mem.getCapacity()) { throw new SketchesArgumentException("Not enough memory: need " + sizeNeeded + " bytes, got " + mem.getCapacity() + " bytes"); } } }
2,594
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/arrayofdoubles/HeapArrayOfDoublesUnion.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.arrayofdoubles; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.tuple.SerializerDeserializer; /** * The on-heap implementation of the Union set operation for tuple sketches of type * ArrayOfDoubles. */ final class HeapArrayOfDoublesUnion extends ArrayOfDoublesUnion { /** * Creates an instance of HeapArrayOfDoublesUnion with a custom seed * @param nomEntries Nominal number of entries. Forced to the nearest power of 2 greater than * given value. * @param numValues Number of double values to keep for each key. * @param seed <a href="{@docRoot}/resources/dictionary.html#seed">See seed</a> */ HeapArrayOfDoublesUnion(final int nomEntries, final int numValues, final long seed) { super(new HeapArrayOfDoublesQuickSelectSketch(nomEntries, 3, 1f, numValues, seed)); } HeapArrayOfDoublesUnion(final ArrayOfDoublesQuickSelectSketch gadget, final long unionThetaLong) { super(gadget); unionThetaLong_ = unionThetaLong; } /** * This is to create an instance given a serialized form and a custom seed * @param mem <a href="{@docRoot}/resources/dictionary.html#mem">See Memory</a> * @param seed <a href="{@docRoot}/resources/dictionary.html#seed">See seed</a> * @return a ArrayOfDoublesUnion on the Java heap */ static ArrayOfDoublesUnion heapifyUnion(final Memory mem, final long seed) { final byte version = mem.getByte(SERIAL_VERSION_BYTE); if (version != serialVersionUID) { throw new SketchesArgumentException("Serial version mismatch. Expected: " + serialVersionUID + ", actual: " + version); } SerializerDeserializer.validateFamily(mem.getByte(FAMILY_ID_BYTE), mem.getByte(PREAMBLE_LONGS_BYTE)); SerializerDeserializer.validateType(mem.getByte(SKETCH_TYPE_BYTE), SerializerDeserializer.SketchType.ArrayOfDoublesUnion); final Memory sketchMem = mem.region(PREAMBLE_SIZE_BYTES, mem.getCapacity() - PREAMBLE_SIZE_BYTES); final ArrayOfDoublesQuickSelectSketch sketch = new HeapArrayOfDoublesQuickSelectSketch(sketchMem, seed); return new HeapArrayOfDoublesUnion(sketch, mem.getLong(THETA_LONG)); } }
2,595
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/arrayofdoubles/ArrayOfDoublesSetOperationBuilder.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.arrayofdoubles; import org.apache.datasketches.memory.WritableMemory; import org.apache.datasketches.thetacommon.ThetaUtil; /** * Builds set operations object for tuple sketches of type ArrayOfDoubles. */ public class ArrayOfDoublesSetOperationBuilder { private int nomEntries_; private int numValues_; private long seed_; /** * Default Nominal Entries (a.k.a. K) */ public static final int DEFAULT_NOMINAL_ENTRIES = 4096; /** * Default number of values */ public static final int DEFAULT_NUMBER_OF_VALUES = 1; /** * Creates an instance of the builder with default parameters */ public ArrayOfDoublesSetOperationBuilder() { nomEntries_ = DEFAULT_NOMINAL_ENTRIES; numValues_ = DEFAULT_NUMBER_OF_VALUES; seed_ = ThetaUtil.DEFAULT_UPDATE_SEED; } /** * This is to set the nominal number of entries. * @param nomEntries Nominal number of entries. Forced to the nearest power of 2 greater than * given value. * @return this builder */ public ArrayOfDoublesSetOperationBuilder setNominalEntries(final int nomEntries) { nomEntries_ = nomEntries; return this; } /** * This is to set the number of double values associated with each key * @param numValues number of double values * @return this builder */ public ArrayOfDoublesSetOperationBuilder setNumberOfValues(final int numValues) { numValues_ = numValues; return this; } /** * Sets the long seed value that is required by the hashing function. * @param seed <a href="{@docRoot}/resources/dictionary.html#seed">See seed</a> * @return this builder */ public ArrayOfDoublesSetOperationBuilder setSeed(final long seed) { seed_ = seed; return this; } /** * Creates an instance of ArrayOfDoublesUnion based on the current configuration of the builder. * The new instance is allocated on the heap if the memory is not provided. * @return an instance of ArrayOfDoublesUnion */ public ArrayOfDoublesUnion buildUnion() { return new HeapArrayOfDoublesUnion(nomEntries_, numValues_, seed_); } /** * Creates an instance of ArrayOfDoublesUnion based on the current configuration of the builder * and the given destination memory. * @param dstMem destination memory to be used by the sketch * @return an instance of ArrayOfDoublesUnion */ public ArrayOfDoublesUnion buildUnion(final WritableMemory dstMem) { return new DirectArrayOfDoublesUnion(nomEntries_, numValues_, seed_, dstMem); } /** * Creates an instance of ArrayOfDoublesIntersection based on the current configuration of the * builder. * The new instance is allocated on the heap if the memory is not provided. * The number of nominal entries is not relevant to this, so it is ignored. * @return an instance of ArrayOfDoublesIntersection */ public ArrayOfDoublesIntersection buildIntersection() { return new HeapArrayOfDoublesIntersection(numValues_, seed_); } /** * Creates an instance of ArrayOfDoublesIntersection based on the current configuration of the * builder. * The new instance is allocated on the heap if the memory is not provided. * The number of nominal entries is not relevant to this, so it is ignored. * @param dstMem destination memory to be used by the sketch * @return an instance of ArrayOfDoublesIntersection */ public ArrayOfDoublesIntersection buildIntersection(final WritableMemory dstMem) { return new DirectArrayOfDoublesIntersection(numValues_, seed_, dstMem); } /** * Creates an instance of ArrayOfDoublesAnotB based on the current configuration of the builder. * The memory is not relevant to this, so it is ignored if set. * The number of nominal entries is not relevant to this, so it is ignored. * @return an instance of ArrayOfDoublesAnotB */ public ArrayOfDoublesAnotB buildAnotB() { return new ArrayOfDoublesAnotBImpl(numValues_, seed_); } }
2,596
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/arrayofdoubles/ArrayOfDoublesUpdatableSketchBuilder.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.arrayofdoubles; import org.apache.datasketches.common.ResizeFactor; import org.apache.datasketches.common.SketchesArgumentException; import org.apache.datasketches.memory.WritableMemory; import org.apache.datasketches.thetacommon.ThetaUtil; /** * For building a new ArrayOfDoublesUpdatableSketch */ public class ArrayOfDoublesUpdatableSketchBuilder { private int nomEntries_; private ResizeFactor resizeFactor_; private int numValues_; private float samplingProbability_; private long seed_; private static final int DEFAULT_NUMBER_OF_VALUES = 1; private static final float DEFAULT_SAMPLING_PROBABILITY = 1; private static final ResizeFactor DEFAULT_RESIZE_FACTOR = ResizeFactor.X8; /** * Creates an instance of builder with default parameters */ public ArrayOfDoublesUpdatableSketchBuilder() { nomEntries_ = ThetaUtil.DEFAULT_NOMINAL_ENTRIES; resizeFactor_ = DEFAULT_RESIZE_FACTOR; numValues_ = DEFAULT_NUMBER_OF_VALUES; samplingProbability_ = DEFAULT_SAMPLING_PROBABILITY; seed_ = ThetaUtil.DEFAULT_UPDATE_SEED; } /** * This is to set the nominal number of entries. * @param nomEntries Nominal number of entries. Forced to the nearest power of 2 greater than * or equal to given value. * @return this builder */ public ArrayOfDoublesUpdatableSketchBuilder setNominalEntries(final int nomEntries) { nomEntries_ = 1 << ThetaUtil.checkNomLongs(nomEntries); return this; } /** * This is to set the resize factor. * Value of X1 means that the maximum capacity is allocated from the start. * Default resize factor is X8. * @param resizeFactor value of X1, X2, X4 or X8 * @return this UpdatableSketchBuilder */ public ArrayOfDoublesUpdatableSketchBuilder setResizeFactor(final ResizeFactor resizeFactor) { resizeFactor_ = resizeFactor; return this; } /** * This is to set sampling probability. * Default probability is 1. * @param samplingProbability sampling probability from 0 to 1 * @return this builder */ public ArrayOfDoublesUpdatableSketchBuilder setSamplingProbability(final float samplingProbability) { if ((samplingProbability < 0) || (samplingProbability > 1f)) { throw new SketchesArgumentException("sampling probability must be between 0 and 1"); } samplingProbability_ = samplingProbability; return this; } /** * This is to set the number of double values associated with each key * @param numValues number of double values * @return this builder */ public ArrayOfDoublesUpdatableSketchBuilder setNumberOfValues(final int numValues) { numValues_ = numValues; return this; } /** * Sets the long seed value that is required by the hashing function. * @param seed <a href="{@docRoot}/resources/dictionary.html#seed">See seed</a> * @return this builder */ public ArrayOfDoublesUpdatableSketchBuilder setSeed(final long seed) { seed_ = seed; return this; } /** * Returns an ArrayOfDoublesUpdatableSketch with the current configuration of this Builder. * @return an ArrayOfDoublesUpdatableSketch */ public ArrayOfDoublesUpdatableSketch build() { return new HeapArrayOfDoublesQuickSelectSketch(nomEntries_, resizeFactor_.lg(), samplingProbability_, numValues_, seed_); } /** * Returns an ArrayOfDoublesUpdatableSketch with the current configuration of this Builder. * @param dstMem instance of Memory to be used by the sketch * @return an ArrayOfDoublesUpdatableSketch */ public ArrayOfDoublesUpdatableSketch build(final WritableMemory dstMem) { return new DirectArrayOfDoublesQuickSelectSketch(nomEntries_, resizeFactor_.lg(), samplingProbability_, numValues_, seed_, dstMem); } }
2,597
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/arrayofdoubles/ArrayOfDoublesSketches.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.arrayofdoubles; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.memory.WritableMemory; import org.apache.datasketches.thetacommon.ThetaUtil; /** * Convenient static methods to instantiate tuple sketches of type ArrayOfDoubles. */ public final class ArrayOfDoublesSketches { /** * Heapify the given Memory as an ArrayOfDoublesSketch * @param srcMem the given source Memory * @return an ArrayOfDoublesSketch */ public static ArrayOfDoublesSketch heapifySketch(final Memory srcMem) { return heapifySketch(srcMem, ThetaUtil.DEFAULT_UPDATE_SEED); } /** * Heapify the given Memory and seed as a ArrayOfDoublesSketch * @param srcMem the given source Memory * @param seed the given seed * @return an ArrayOfDoublesSketch */ public static ArrayOfDoublesSketch heapifySketch(final Memory srcMem, final long seed) { return ArrayOfDoublesSketch.heapify(srcMem, seed); } /** * Heapify the given Memory as an ArrayOfDoublesUpdatableSketch * @param srcMem the given source Memory * @return an ArrayOfDoublesUpdatableSketch */ public static ArrayOfDoublesUpdatableSketch heapifyUpdatableSketch(final Memory srcMem) { return heapifyUpdatableSketch(srcMem, ThetaUtil.DEFAULT_UPDATE_SEED); } /** * Heapify the given Memory and seed as a ArrayOfDoublesUpdatableSketch * @param srcMem the given source Memory * @param seed the given seed * @return an ArrayOfDoublesUpdatableSketch */ public static ArrayOfDoublesUpdatableSketch heapifyUpdatableSketch(final Memory srcMem, final long seed) { return ArrayOfDoublesUpdatableSketch.heapify(srcMem, seed); } /** * Wrap the given Memory as an ArrayOfDoublesSketch * @param srcMem the given source Memory * @return an ArrayOfDoublesSketch */ public static ArrayOfDoublesSketch wrapSketch(final Memory srcMem) { return wrapSketch(srcMem, ThetaUtil.DEFAULT_UPDATE_SEED); } /** * Wrap the given Memory and seed as a ArrayOfDoublesSketch * @param srcMem the given source Memory * @param seed the given seed * @return an ArrayOfDoublesSketch */ public static ArrayOfDoublesSketch wrapSketch(final Memory srcMem, final long seed) { return ArrayOfDoublesSketch.wrap(srcMem, seed); } /** * Wrap the given WritableMemory as an ArrayOfDoublesUpdatableSketch * @param srcMem the given source Memory * @return an ArrayOfDoublesUpdatableSketch */ public static ArrayOfDoublesUpdatableSketch wrapUpdatableSketch(final WritableMemory srcMem) { return wrapUpdatableSketch(srcMem, ThetaUtil.DEFAULT_UPDATE_SEED); } /** * Wrap the given WritableMemory and seed as a ArrayOfDoublesUpdatableSketch * @param srcMem the given source Memory * @param seed the given seed * @return an ArrayOfDoublesUpdatableSketch */ public static ArrayOfDoublesUpdatableSketch wrapUpdatableSketch(final WritableMemory srcMem, final long seed) { return ArrayOfDoublesUpdatableSketch.wrap(srcMem, seed); } /** * Heapify the given Memory as an ArrayOfDoublesUnion * @param srcMem the given source Memory * @return an ArrayOfDoublesUnion */ public static ArrayOfDoublesUnion heapifyUnion(final Memory srcMem) { return heapifyUnion(srcMem, ThetaUtil.DEFAULT_UPDATE_SEED); } /** * Heapify the given Memory and seed as an ArrayOfDoublesUnion * @param srcMem the given source Memory * @param seed the given seed * @return an ArrayOfDoublesUnion */ public static ArrayOfDoublesUnion heapifyUnion(final Memory srcMem, final long seed) { return ArrayOfDoublesUnion.heapify(srcMem, seed); } /** * Wrap the given Memory as an ArrayOfDoublesUnion * @param srcMem the given source Memory * @return an ArrayOfDoublesUnion */ public static ArrayOfDoublesUnion wrapUnion(final Memory srcMem) { return wrapUnion(srcMem, ThetaUtil.DEFAULT_UPDATE_SEED); } /** * Wrap the given Memory and seed as an ArrayOfDoublesUnion * @param srcMem the given source Memory * @param seed the given seed * @return an ArrayOfDoublesUnion */ public static ArrayOfDoublesUnion wrapUnion(final Memory srcMem, final long seed) { return ArrayOfDoublesUnion.wrap(srcMem, seed); } /** * Wrap the given Memory as an ArrayOfDoublesUnion * @param srcMem the given source Memory * @return an ArrayOfDoublesUnion */ public static ArrayOfDoublesUnion wrapUnion(final WritableMemory srcMem) { return wrapUnion(srcMem, ThetaUtil.DEFAULT_UPDATE_SEED); } /** * Wrap the given Memory and seed as an ArrayOfDoublesUnion * @param srcMem the given source Memory * @param seed the given seed * @return an ArrayOfDoublesUnion */ public static ArrayOfDoublesUnion wrapUnion(final WritableMemory srcMem, final long seed) { return ArrayOfDoublesUnion.wrap(srcMem, seed); } }
2,598
0
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple
Create_ds/datasketches-java/src/main/java/org/apache/datasketches/tuple/arrayofdoubles/ArrayOfDoublesUpdatableSketch.java
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.datasketches.tuple.arrayofdoubles; import org.apache.datasketches.common.ResizeFactor; import org.apache.datasketches.hash.MurmurHash3; import org.apache.datasketches.memory.Memory; import org.apache.datasketches.memory.WritableMemory; import org.apache.datasketches.thetacommon.ThetaUtil; import org.apache.datasketches.tuple.Util; /** * The top level for updatable tuple sketches of type ArrayOfDoubles. */ public abstract class ArrayOfDoublesUpdatableSketch extends ArrayOfDoublesSketch { final long seed_; ArrayOfDoublesUpdatableSketch(final int numValues, final long seed) { super(numValues); seed_ = seed; } /** * Heapify the given Memory as an ArrayOfDoublesUpdatableSketch * @param mem the given Memory * @return an ArrayOfDoublesUpdatableSketch */ public static ArrayOfDoublesUpdatableSketch heapify(final Memory mem) { return heapify(mem, ThetaUtil.DEFAULT_UPDATE_SEED); } /** * Heapify the given Memory and seed as a ArrayOfDoublesUpdatableSketch * @param mem the given Memory * @param seed the given seed * @return an ArrayOfDoublesUpdatableSketch */ public static ArrayOfDoublesUpdatableSketch heapify(final Memory mem, final long seed) { return new HeapArrayOfDoublesQuickSelectSketch(mem, seed); } /** * Wrap the given WritableMemory as an ArrayOfDoublesUpdatableSketch * @param mem the given Memory * @return an ArrayOfDoublesUpdatableSketch */ public static ArrayOfDoublesUpdatableSketch wrap(final WritableMemory mem) { return wrap(mem, ThetaUtil.DEFAULT_UPDATE_SEED); } /** * Wrap the given WritableMemory and seed as a ArrayOfDoublesUpdatableSketch * @param mem the given Memory * @param seed the given seed * @return an ArrayOfDoublesUpdatableSketch */ public static ArrayOfDoublesUpdatableSketch wrap(final WritableMemory mem, final long seed) { return new DirectArrayOfDoublesQuickSelectSketch(mem, seed); } /** * Updates this sketch with a long key and double values. * The values will be stored or added to the ones associated with the key * * @param key The given long key * @param values The given values */ public void update(final long key, final double[] values) { update(new long[] {key}, values); } /** * Updates this sketch with a double key and double values. * The values will be stored or added to the ones associated with the key * * @param key The given double key * @param values The given values */ public void update(final double key, final double[] values) { update(Util.doubleToLongArray(key), values); } /** * Updates this sketch with a String key and double values. * The values will be stored or added to the ones associated with the key * * @param key The given String key * @param values The given values */ public void update(final String key, final double[] values) { update(Util.stringToByteArray(key), values); } /** * Updates this sketch with a byte[] key and double values. * The values will be stored or added to the ones associated with the key * * @param key The given byte[] key * @param values The given values */ public void update(final byte[] key, final double[] values) { if (key == null || key.length == 0) { return; } insertOrIgnore(MurmurHash3.hash(key, seed_)[0] >>> 1, values); } /** * Updates this sketch with a int[] key and double values. * The values will be stored or added to the ones associated with the key * * @param key The given int[] key * @param values The given values */ public void update(final int[] key, final double[] values) { if (key == null || key.length == 0) { return; } insertOrIgnore(MurmurHash3.hash(key, seed_)[0] >>> 1, values); } /** * Updates this sketch with a long[] key and double values. * The values will be stored or added to the ones associated with the key * * @param key The given long[] key * @param values The given values */ public void update(final long[] key, final double[] values) { if (key == null || key.length == 0) { return; } insertOrIgnore(MurmurHash3.hash(key, seed_)[0] >>> 1, values); } /** * Gets the configured nominal number of entries * @return nominal number of entries */ public abstract int getNominalEntries(); /** * Gets the configured resize factor * @return resize factor */ public abstract ResizeFactor getResizeFactor(); /** * Gets the configured sampling probability * @return sampling probability */ public abstract float getSamplingProbability(); /** * Rebuilds reducing the actual number of entries to the nominal number of entries if needed */ public abstract void trim(); /** * Resets this sketch an empty state. */ public abstract void reset(); /** * Gets an on-heap compact representation of the sketch * @return compact sketch */ @Override public ArrayOfDoublesCompactSketch compact() { return compact(null); } /** * Gets an off-heap compact representation of the sketch using the given memory * @param dstMem memory for the compact sketch (can be null) * @return compact sketch (off-heap if memory is provided) */ @Override public ArrayOfDoublesCompactSketch compact(final WritableMemory dstMem) { if (dstMem == null) { return new HeapArrayOfDoublesCompactSketch(this); } return new DirectArrayOfDoublesCompactSketch(this, dstMem); } abstract int getCurrentCapacity(); long getSeed() { return seed_; } @Override short getSeedHash() { return Util.computeSeedHash(seed_); } /** * Insert if key is less than thetaLong and not a duplicate, otherwise ignore. * @param key the hash value of the input value * @param values array of values to update the summary */ abstract void insertOrIgnore(long key, double[] values); }
2,599