fork(1) download
  1. #include <iostream>
  2. #include <cstdint>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. namespace prism
  7. {
  8. struct token_t
  9. {
  10. uint64_t m_value;
  11.  
  12. token_t();
  13. token_t(const token_t& rhs);
  14. token_t(const char *str);
  15.  
  16. std::string to_string() const;
  17. token_t& from_string(const char *str);
  18. token_t& operator=(const char *str) { return from_string(str); }
  19. };
  20.  
  21. struct _ulldiv_t
  22. {
  23. uint64_t quot;
  24. uint64_t rem;
  25.  
  26. _ulldiv_t() : quot(0ull), rem(0ull) { }
  27. };
  28.  
  29. _ulldiv_t _div(uint64_t num, uint64_t divider)
  30. {
  31. _ulldiv_t result;
  32. result.rem = num%divider;
  33. result.quot = (uint64_t)num / divider;
  34. return result;
  35. }
  36.  
  37. uint64_t powull(uint64_t base, uint32_t num)
  38. {
  39. uint64_t result = 1;
  40. for (uint32_t i = 0; num > i; ++i)
  41. result = result * base;
  42. return result;
  43. }
  44.  
  45. const uint32_t g_num_letters = 38;
  46. const char g_letters[g_num_letters] =
  47. { '\0', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
  48. 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  49. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '_'
  50. };
  51.  
  52. uint32_t get_id_char(char letter)
  53. {
  54. for (uint32_t i = 0; i < g_num_letters; ++i)
  55. if (letter == g_letters[i])
  56. return i;
  57. return 0;
  58. }
  59.  
  60. std::string token_to_string(prism::token_t token)
  61. {
  62. char output[16] = { 0 };
  63. _ulldiv_t result;
  64. int i = 0;
  65. for (; token.m_value != 0; ++i)
  66. {
  67. result = _div(token.m_value, g_num_letters);
  68. token.m_value = result.quot;
  69. output[i] = g_letters[result.rem];
  70. }
  71. output[i] = '\0';
  72. return std::string(output);
  73. }
  74.  
  75. token_t string_to_token(const char *str)
  76. {
  77. token_t result;
  78. uint32_t len = strlen(str);
  79. for (uint32_t i = 0; i < len; ++i)
  80. result.m_value += powull(g_num_letters, i) * get_id_char(tolower(str[i]));
  81. return result;
  82. }
  83.  
  84. token_t::token_t() : m_value(0) {}
  85. token_t::token_t(const token_t& rhs) : m_value(rhs.m_value) {}
  86. token_t::token_t(const char *str) { m_value = string_to_token(str).m_value; }
  87. std::string token_t::to_string() const { return token_to_string((*this)); }
  88. token_t& token_t::from_string(const char *str) { m_value = string_to_token(str).m_value; return (*this); }
  89. }
  90.  
  91. int main()
  92. {
  93. std::string line;
  94. while(std::getline(std::cin, line))
  95. {
  96. prism::token_t token(line.c_str());
  97. cout << line << " = " << token.m_value << " " << token.to_string() << endl;
  98. }
  99. return 0;
  100. }
Success #stdin #stdout 0s 3420KB
stdin
model
driver
default
stdout
model = 46717261 model
driver = 2251646162 driver
default = 92136531712 default