2
0

sampling.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*-------------------------------------------------------------------------
  2. *
  3. * sampling.h
  4. * definitions for sampling functions
  5. *
  6. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  7. * Portions Copyright (c) 1994, Regents of the University of California
  8. *
  9. * src/include/utils/sampling.h
  10. *
  11. *-------------------------------------------------------------------------
  12. */
  13. #ifndef SAMPLING_H
  14. #define SAMPLING_H
  15. #include "common/pg_prng.h"
  16. #include "storage/block.h" /* for typedef BlockNumber */
  17. /* Random generator for sampling code */
  18. extern void sampler_random_init_state(uint32 seed,
  19. pg_prng_state *randstate);
  20. extern double sampler_random_fract(pg_prng_state *randstate);
  21. /* Block sampling methods */
  22. /* Data structure for Algorithm S from Knuth 3.4.2 */
  23. typedef struct
  24. {
  25. BlockNumber N; /* number of blocks, known in advance */
  26. int n; /* desired sample size */
  27. BlockNumber t; /* current block number */
  28. int m; /* blocks selected so far */
  29. pg_prng_state randstate; /* random generator state */
  30. } BlockSamplerData;
  31. typedef BlockSamplerData *BlockSampler;
  32. extern BlockNumber BlockSampler_Init(BlockSampler bs, BlockNumber nblocks,
  33. int samplesize, uint32 randseed);
  34. extern bool BlockSampler_HasMore(BlockSampler bs);
  35. extern BlockNumber BlockSampler_Next(BlockSampler bs);
  36. /* Reservoir sampling methods */
  37. typedef struct
  38. {
  39. double W;
  40. pg_prng_state randstate; /* random generator state */
  41. } ReservoirStateData;
  42. typedef ReservoirStateData *ReservoirState;
  43. extern void reservoir_init_selection_state(ReservoirState rs, int n);
  44. extern double reservoir_get_next_S(ReservoirState rs, double t, int n);
  45. /* Old API, still in use by assorted FDWs */
  46. /* For backwards compatibility, these declarations are duplicated in vacuum.h */
  47. extern double anl_random_fract(void);
  48. extern double anl_init_selection_state(int n);
  49. extern double anl_get_next_S(double t, int n, double *stateptr);
  50. #endif /* SAMPLING_H */