sampling.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*-------------------------------------------------------------------------
  2. *
  3. * sampling.h
  4. * definitions for sampling functions
  5. *
  6. * Portions Copyright (c) 1996-2016, 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 "storage/block.h" /* for typedef BlockNumber */
  16. /* Random generator for sampling code */
  17. typedef unsigned short SamplerRandomState[3];
  18. extern void sampler_random_init_state(long seed,
  19. SamplerRandomState randstate);
  20. extern double sampler_random_fract(SamplerRandomState 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. SamplerRandomState randstate; /* random generator state */
  30. } BlockSamplerData;
  31. typedef BlockSamplerData *BlockSampler;
  32. extern void BlockSampler_Init(BlockSampler bs, BlockNumber nblocks,
  33. int samplesize, long 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. SamplerRandomState 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 */