Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <cstdio>
  5. #include <cinttypes>
  6.  
  7. #pragma GCC optimize("O3")
  8.  
  9. constexpr int INT_CHAR = 8;
  10.  
  11. class Account
  12. {
  13. public:
  14.     static constexpr int DATA_SIZE = 4;
  15.  
  16.     Account()
  17.     {
  18.         memset(data, 0, sizeof(data));
  19.     }
  20.     Account(const uint32_t *data_)
  21.     {
  22.         memcpy(data, data_, sizeof(data));
  23.     }
  24.     Account(const Account &a) :Account(a.data) {}
  25.     Account(const std::string &s);
  26.  
  27.     explicit operator bool() const;
  28.     std::string to_string() const;
  29.     const uint32_t *cdata() const
  30.     {
  31.         return data;
  32.     }
  33.  
  34.     friend bool operator<(const Account &a, const Account &b);
  35.     friend bool operator>(const Account &a, const Account &b);
  36.     friend bool operator==(const Account &a, const Account &b);
  37.     friend bool operator!=(const Account &a, const Account &b);
  38.     friend bool operator<=(const Account &a, const Account &b);
  39.     friend bool operator>=(const Account &a, const Account &b);
  40.     friend struct std::hash<Account>;
  41.  
  42. private:
  43.     uint32_t data[DATA_SIZE];
  44.  
  45.     int hex_to_int(char a) const
  46.     {
  47.         constexpr int HEX_A = 10;
  48.         if (a <= '9') {
  49.             return a - '0';
  50.         } else if (a <= 'Z') {
  51.             return HEX_A + a - 'A';
  52.         } else {
  53.             return HEX_A + a - 'a';
  54.         }
  55.     }
  56.  
  57.     int cmp(const Account &other) const
  58.     {
  59.         for (int i = DATA_SIZE-1; i >= 0; i--) {
  60.             if (data[i] > other.data[i]) {
  61.                 return 1;
  62.             } else if (data[i] < other.data[i]) {
  63.                 return -1;
  64.             }
  65.         }
  66.         return 0;
  67.     }
  68.  
  69. };
  70.  
  71. bool operator<(const Account &a, const Account &b)
  72. {
  73.     return a.cmp(b) < 0;
  74. }
  75. bool operator>(const Account &a, const Account &b)
  76. {
  77.     return a.cmp(b) > 0;
  78. }
  79. bool operator==(const Account &a, const Account &b)
  80. {
  81.     return a.cmp(b) == 0;
  82. }
  83. bool operator!=(const Account &a, const Account &b)
  84. {
  85.     return a.cmp(b) != 0;
  86. }
  87. bool operator<=(const Account &a, const Account &b)
  88. {
  89.     return a.cmp(b) <= 0;
  90. }
  91. bool operator>=(const Account &a, const Account &b)
  92. {
  93.     return a.cmp(b) >= 0;
  94. }
  95.  
  96. Account::Account(const std::string &s)
  97. {
  98.     constexpr int HEX_START = 2;
  99.     constexpr int HALF_BYTE = 4;
  100.  
  101.     memset(data, 0, sizeof(data));
  102.     int hex_len = s.length();
  103.     int i = 0;
  104.     int j = hex_len;
  105.     while(i < DATA_SIZE && j >= HEX_START) {
  106.         for (int k = std::max(HEX_START, j - INT_CHAR); k < j; k++) {
  107.             data[i] <<= HALF_BYTE;
  108.             data[i] |= hex_to_int(s[k]);
  109.         }
  110.  
  111.         i += 1;
  112.         j -= INT_CHAR;
  113.     }
  114. }
  115.  
  116. Account::operator bool() const
  117. {
  118.     for (int i = 0; i < DATA_SIZE; i++) {
  119.         if (data[i]) {
  120.             return true;
  121.         }
  122.     }
  123.     return false;
  124. }
  125.  
  126. std::string Account::to_string() const
  127. {
  128.     constexpr int STR_SIZE = 32 + 2 + 1;
  129.     char str[STR_SIZE] = "0x";
  130.     if (!operator bool()) {
  131.         str[1] = 0;
  132.         return str;
  133.     } else {
  134.         int i = DATA_SIZE - 1;
  135.         int j = 0;
  136.         while (i >= 0) {
  137.             sprintf(str+2+INT_CHAR*j, "%08x", data[i]);
  138.             i -= 1;
  139.             j += 1;
  140.         }
  141.         return str;
  142.     }
  143. }
  144.  
  145. namespace std
  146. {
  147.     template<>
  148.     struct hash<Account>
  149.     {
  150.         size_t operator()(const Account &a) const
  151.         {
  152.             uint32_t res = 0;
  153.             for (int i = 0; i < Account::DATA_SIZE; i++) {
  154.                 res ^= a.data[i];
  155.             }
  156.             return res;
  157.         }
  158.     };
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement