Advertisement
Evyatar12

amit noob.cpp

Nov 26th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #include "idManager.h"
  2. #include <random>
  3. #include <iostream>
  4. #include <chrono>
  5. #include "utils.h"
  6.  
  7. #define ID_BYTES    4
  8. #define ID_LENGTH   (ID_BYTES) * 8
  9.  
  10. using namespace std;
  11. using namespace std::chrono;
  12.  
  13. unsigned long long flipOrder(unsigned long long l) {
  14.     unsigned long long res;
  15.  
  16.     int numOfBits = sizeof(long long) * 8;
  17.  
  18.     while (numOfBits-- > 0) {
  19.         res <<= 1;
  20.         res ^= l & 1;
  21.         l >>= 1;
  22.     }
  23.  
  24.     return res;
  25. }
  26.  
  27. class RandomIdGenerator {
  28. private:
  29.     minstd_rand _engine;
  30.     uniform_int_distribution<char> _distribution; // a byte distribution
  31.  
  32. public:
  33.  
  34.     RandomIdGenerator() : _distribution(0, 255) {
  35.         // use current microseconds as a seed for the random generator
  36.         chrono::microseconds ms = duration_cast<chrono::microseconds>(system_clock::now().time_since_epoch());
  37.  
  38.         unsigned long long seed = flipOrder(ms.count());
  39.         _engine.seed(seed);
  40.     }
  41.  
  42.     string randomId() {
  43.         string output;
  44.  
  45.         int bytesRemaining = ID_BYTES;
  46.  
  47.         while (bytesRemaining-- > 0)
  48.             output += _distribution(_engine);
  49.  
  50.         return output;
  51.     }
  52. };
  53.  
  54. RandomIdGenerator idGen;
  55. map<string, ID*> ids = { };
  56.  
  57. bool idExists(const string& id) {
  58.     // ids.find(id) returns ids.end() iff ids doesn't contain id
  59.     return ids.find(id) != ids.end();
  60. }
  61.  
  62. ID* getId(const string& id) {
  63.     // this function gets a pointer to a given existing id
  64.     return ids[id];
  65. }
  66.  
  67. void registerId(const string& id) {
  68.     // this function registers an id as a used id
  69.     if (idExists(id)) {
  70.         throw "Id already exists!";
  71.     }
  72.  
  73.  
  74.     ids[id] = nullptr;
  75. }
  76.  
  77. void setId(const string& id, ID* object) {
  78.     // this function adds an id object to the map
  79.     ids[id] = object;
  80. }
  81.  
  82. string nextId(const string& prefix) {
  83.     // this function generates a new id
  84.     string id;
  85.     bool keep;
  86.  
  87.     do {
  88.         id = prefix + idGen.randomId();
  89.         keep = idExists(id);
  90.     } while (idExists(id));
  91.  
  92.     return id;
  93. }
  94.  
  95. BaseID::BaseID() : _id(nextId(string(""))) {
  96.     setId(_id, this);
  97. }
  98.  
  99. BaseID::BaseID(const type_info& type) : _id(nextId(type.name())) {
  100.     setId(_id, this);
  101. }
  102.  
  103. BaseID::BaseID(string& id) : _id(id) {
  104.     setId(_id, this);
  105. }
  106.  
  107. string BaseID::getID() {
  108.     return _id;
  109. }
  110.  
  111. BaseID::~BaseID() { /* empty destructor */ }
  112.  
  113. ostream& operator<<(ostream& out, ID& id) {
  114.     printHexValue(out, id.getID());
  115.     return out;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement