Advertisement
Nickpips

pipipipipipipip

Mar 24th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <iostream>
  6. #include <fstream>
  7. #include <bitset>
  8. #include <vector>
  9.  
  10. using namespace std;
  11.  
  12. static unsigned int g_seed;
  13.  
  14. // Used to seed the generator.
  15. inline void fast_srand(int seed) {
  16.     g_seed = seed;
  17. }
  18.  
  19. // Compute a pseudorandom integer.
  20. // Output value in range [0, 32767]
  21. inline int fast_rand(void) {
  22.     g_seed = (214013*g_seed+2531011);
  23.     return (g_seed>>16)&0x7FFF;
  24. }
  25.  
  26. int main() {
  27.     fast_srand(0);
  28.    
  29.     for( int numHashes = 1 << 15; numHashes <= (1 << 20); numHashes += 1 << 15 ) {
  30.         constexpr int hashSize = 32;
  31.         int blockSize = 20;
  32.        
  33.         int numBuckets = (1 << blockSize);
  34.         bitset<hashSize> hashMask = bitset<hashSize>(((1 << blockSize) - 1));
  35.        
  36.         vector<bitset<hashSize>> hashes(numHashes);
  37.         vector<int> buckets(numBuckets, 0);
  38.        
  39.         for (size_t i = 0; i < numHashes; i++) {
  40.             hashes.at(i) = bitset<hashSize>((fast_rand() << 16) + fast_rand());
  41.         }
  42.         for( int i = 0; i < numHashes; i++ ) {
  43.             int bucket = (hashes[i] & hashMask).to_ulong();
  44.             buckets.at(bucket)++;
  45.         }
  46.         vector<int> cnt(1000);
  47.         for (size_t i = 0; i < cnt.size(); i++) {
  48.             cnt.at(i) = 0;
  49.         }
  50.         for (size_t i = 0; i < numBuckets; i++) {
  51.             cnt.at(buckets.at(i))++;
  52.         }
  53.         int nextNumHashes = 0;
  54.         for (size_t i = 0; i < cnt.size(); i++) {
  55.             nextNumHashes += cnt.at(i) * i * (i-1) / 2;
  56.         }
  57.         cout << "(" << numHashes << ", " << nextNumHashes << ")" << endl;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement