public static CountMinSketchImpl readFrom(InputStream in) throws IOException { CountMinSketchImpl sketch = new CountMinSketchImpl(); sketch.readFrom0(in); return sketch; }
@Override public void add(Object item) { add(item, 1); }
@Override public void addBinary(byte[] item) { addBinary(item, 1); }
@Override public long estimateCount(Object item) { if (item instanceof String) { return estimateCountForStringItem((String) item); } else if (item instanceof byte[]) { return estimateCountForBinaryItem((byte[]) item); } else { return estimateCountForLongItem(Utils.integralToLong(item)); } }
@Override public void addBinary(byte[] item, long count) { if (count < 0) { throw new IllegalArgumentException("Negative increments not implemented"); } int[] buckets = getHashBuckets(item, depth, width); for (int i = 0; i < depth; ++i) { table[i][buckets[i]] += count; } totalCount += count; }
private long estimateCountForLongItem(long item) { long res = Long.MAX_VALUE; for (int i = 0; i < depth; ++i) { res = Math.min(res, table[i][hash(item, i)]); } return res; }
CountMinSketchImpl(int depth, int width, int seed) { if (depth <= 0 || width <= 0) { throw new IllegalArgumentException("Depth and width must be both positive"); } this.depth = depth; this.width = width; this.eps = 2.0 / width; this.confidence = 1 - 1 / Math.pow(2, depth); initTablesWith(depth, width, seed); }
/** * Creates a {@link CountMinSketch} with given {@code depth}, {@code width}, and random * {@code seed}. * * @param depth depth of the Count-min Sketch, must be positive * @param width width of the Count-min Sketch, must be positive * @param seed random seed */ public static CountMinSketch create(int depth, int width, int seed) { return new CountMinSketchImpl(depth, width, seed); }
@Override public long estimateCount(Object item) { if (item instanceof String) { return estimateCountForStringItem((String) item); } else { return estimateCountForLongItem(Utils.integralToLong(item)); } }
@Override public void addString(String item) { addString(item, 1); }
@Override public void addLong(long item) { addLong(item, 1); }
@Override public long estimateCount(Object item) { if (item instanceof String) { return estimateCountForStringItem((String) item); } else if (item instanceof byte[]) { return estimateCountForBinaryItem((byte[]) item); } else { return estimateCountForLongItem(Utils.integralToLong(item)); } }
@Override public void addBinary(byte[] item, long count) { if (count < 0) { throw new IllegalArgumentException("Negative increments not implemented"); } int[] buckets = getHashBuckets(item, depth, width); for (int i = 0; i < depth; ++i) { table[i][buckets[i]] += count; } totalCount += count; }
@Override public void addLong(long item, long count) { if (count < 0) { throw new IllegalArgumentException("Negative increments not implemented"); } for (int i = 0; i < depth; ++i) { table[i][hash(item, i)] += count; } totalCount += count; }
CountMinSketchImpl(int depth, int width, int seed) { if (depth <= 0 || width <= 0) { throw new IllegalArgumentException("Depth and width must be both positive"); } this.depth = depth; this.width = width; this.eps = 2.0 / width; this.confidence = 1 - 1 / Math.pow(2, depth); initTablesWith(depth, width, seed); }
/** * Creates a {@link CountMinSketch} with given {@code depth}, {@code width}, and random * {@code seed}. * * @param depth depth of the Count-min Sketch, must be positive * @param width width of the Count-min Sketch, must be positive * @param seed random seed */ public static CountMinSketch create(int depth, int width, int seed) { return new CountMinSketchImpl(depth, width, seed); }
@Override public void addString(String item) { addString(item, 1); }