codekingpro/portable-devtools
115k
1/*2 * hyperloglog.h3 *4 * A simple HyperLogLog cardinality estimator implementation5 *6 * Portions Copyright (c) 2014-2023, PostgreSQL Global Development Group7 *8 * Based on Hideaki Ohno's C++ implementation. The copyright terms of Ohno's9 * original version (the MIT license) follow.10 *11 * src/include/lib/hyperloglog.h12 */13 14/*15 * Copyright (c) 2013 Hideaki Ohno <hide.o.j55{at}gmail.com>16 *17 * Permission is hereby granted, free of charge, to any person obtaining a copy18 * of this software and associated documentation files (the 'Software'), to19 * deal in the Software without restriction, including without limitation the20 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or21 * sell copies of the Software, and to permit persons to whom the Software is22 * furnished to do so, subject to the following conditions:23 *24 * The above copyright notice and this permission notice shall be included in25 * all copies or substantial portions of the Software.26 *27 * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE30 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER31 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING32 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS33 * IN THE SOFTWARE.34 */35 36#ifndef HYPERLOGLOG_H37#define HYPERLOGLOG_H38 39/*40 * HyperLogLog is an approximate technique for computing the number of distinct41 * entries in a set. Importantly, it does this by using a fixed amount of42 * memory. See the 2007 paper "HyperLogLog: the analysis of a near-optimal43 * cardinality estimation algorithm" for more.44 *45 * hyperLogLogState46 *47 * registerWidth register width, in bits ("k")48 * nRegisters number of registers49 * alphaMM alpha * m ^ 2 (see initHyperLogLog())50 * hashesArr array of hashes51 * arrSize size of hashesArr52 */53typedef struct hyperLogLogState54{55 uint8 registerWidth;56 Size nRegisters;57 double alphaMM;58 uint8 *hashesArr;59 Size arrSize;60} hyperLogLogState;61 62extern void initHyperLogLog(hyperLogLogState *cState, uint8 bwidth);63extern void initHyperLogLogError(hyperLogLogState *cState, double error);64extern void addHyperLogLog(hyperLogLogState *cState, uint32 hash);65extern double estimateHyperLogLog(hyperLogLogState *cState);66extern void freeHyperLogLog(hyperLogLogState *cState);67 68#endif /* HYPERLOGLOG_H */69 