#include #include using std::cout; using std::ostream; template class Sampler { T * buf; int s; int current; public: Sampler() { s=50; current=0; buf=new T[s]; } Sampler(int n) { s=n; current=0; buf=new T[s]; } private: Sampler(const Sampler&); Sampler & operator=(const Sampler&); public: T& operator[](int i) { return buf[i]; } const T& operator[](int i)const { return buf[i]; } int size()const { if (current<=s) return current; else return s; } int maxSize()const { return s; } bool get(const T& t) { current++; if (current<=s) { buf[current-1]=t; return true; } float r=(float)rand()/RAND_MAX; if (r<(float)s/current) { //with probability s/current int r=rand()%s; //choose a spot uniformly and replace for (int i=r; i ostream& operator<<(ostream& os, const Sampler& sampler) { for (int i=0; i