hyperloglog.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * hyperloglog.h
  3. *
  4. * A simple HyperLogLog cardinality estimator implementation
  5. *
  6. * Portions Copyright (c) 2014-2022, PostgreSQL Global Development Group
  7. *
  8. * Based on Hideaki Ohno's C++ implementation. The copyright terms of Ohno's
  9. * original version (the MIT license) follow.
  10. *
  11. * src/include/lib/hyperloglog.h
  12. */
  13. /*
  14. * Copyright (c) 2013 Hideaki Ohno <hide.o.j55{at}gmail.com>
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a copy
  17. * of this software and associated documentation files (the 'Software'), to
  18. * deal in the Software without restriction, including without limitation the
  19. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  20. * sell copies of the Software, and to permit persons to whom the Software is
  21. * furnished to do so, subject to the following conditions:
  22. *
  23. * The above copyright notice and this permission notice shall be included in
  24. * all copies or substantial portions of the Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  31. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  32. * IN THE SOFTWARE.
  33. */
  34. #ifndef HYPERLOGLOG_H
  35. #define HYPERLOGLOG_H
  36. /*
  37. * HyperLogLog is an approximate technique for computing the number of distinct
  38. * entries in a set. Importantly, it does this by using a fixed amount of
  39. * memory. See the 2007 paper "HyperLogLog: the analysis of a near-optimal
  40. * cardinality estimation algorithm" for more.
  41. *
  42. * hyperLogLogState
  43. *
  44. * registerWidth register width, in bits ("k")
  45. * nRegisters number of registers
  46. * alphaMM alpha * m ^ 2 (see initHyperLogLog())
  47. * hashesArr array of hashes
  48. * arrSize size of hashesArr
  49. */
  50. typedef struct hyperLogLogState
  51. {
  52. uint8 registerWidth;
  53. Size nRegisters;
  54. double alphaMM;
  55. uint8 *hashesArr;
  56. Size arrSize;
  57. } hyperLogLogState;
  58. extern void initHyperLogLog(hyperLogLogState *cState, uint8 bwidth);
  59. extern void initHyperLogLogError(hyperLogLogState *cState, double error);
  60. extern void addHyperLogLog(hyperLogLogState *cState, uint32 hash);
  61. extern double estimateHyperLogLog(hyperLogLogState *cState);
  62. extern void freeHyperLogLog(hyperLogLogState *cState);
  63. #endif /* HYPERLOGLOG_H */