fork download
  1. // Created by Sergey on 12.09.14.
  2. // Copyright (c) 2014 Sergey. All rights reserved.
  3. //
  4.  
  5. #include <iostream>
  6. #include <chrono>
  7. #include <map>
  8. #include <string>
  9.  
  10. template <class T, class ValueType>
  11. class StrongType
  12. {
  13. public:
  14. inline explicit operator ValueType() const { return _value;}
  15. inline bool operator == (const StrongType &other) const
  16. {
  17. return _value == other._value;
  18. }
  19. inline bool operator != (const StrongType &other) const
  20. {
  21. return _value != other._value;
  22. }
  23. inline bool operator < (const StrongType &other) const
  24. {
  25. return _value < other._value;;
  26. }
  27. inline bool operator > (const StrongType &other) const
  28. {
  29. return _value > other._value;
  30. }
  31. inline bool operator <= (const StrongType &other) const
  32. {
  33. return _value <= other._value;
  34. }
  35. inline bool operator >= (const StrongType &other) const
  36. {
  37. return _value >= other._value;
  38. }
  39.  
  40. protected:
  41. explicit StrongType(ValueType value):_value(value) {}
  42.  
  43. private:
  44. ValueType _value;
  45. };
  46.  
  47. template <class T, class ValueType = int>
  48. class StringCache
  49. {
  50. public:
  51. static_assert(std::is_integral<ValueType>::value, "not integral type");
  52.  
  53. class Type: public StrongType<T,ValueType>
  54. {
  55. friend class StringCache;
  56. private:
  57. explicit Type(ValueType value):StrongType<T,ValueType>(value){}
  58. };
  59.  
  60. static Type get(const std::string &value)
  61. {
  62. return Type(_values.insert(std::make_pair(value, _values.size() + 1)).first->second);
  63. }
  64.  
  65. static Type find(const std::string &value)
  66. {
  67. std::map<std::string,int>::const_iterator it =_values.find(value);
  68. if(it == _values.end())
  69. return Type(0);
  70. else
  71. return Type(it->second);
  72. }
  73.  
  74. static const std::string& to_string(const Type &type)
  75. {
  76. static const std::string empty;
  77. if(static_cast<ValueType>(type)>=_values.size())
  78. return empty;
  79. for(const auto &it:_values)
  80. if(it.second == static_cast<ValueType>(type))
  81. return it.first;
  82. return empty;
  83. }
  84.  
  85. private:
  86. static std::map<std::string,int> _values;
  87. };
  88.  
  89. template <class T, class ValueType>
  90. std::map<std::string,int> StringCache<T,ValueType>::_values;
  91.  
  92. class ArmorType:public StringCache<ArmorType>{};
  93. class WeaponType:public StringCache<WeaponType>{};
  94.  
  95. typedef int ArmorDescription;
  96.  
  97. std::string armorId0("armor0");
  98. std::string armorId1("armor1");
  99. std::string armorId2("armor2");
  100. std::string armorId3("armor3");
  101. std::string armorId4("armor4");
  102. std::string armorId5("armor5");
  103. std::string armorId6("armor6");
  104. std::string armorId7("armor7");
  105. std::string armorId8("armor8");
  106. std::string armorId9("armor9");
  107.  
  108. int test1(const std::map<ArmorType::Type,ArmorDescription> &armor)
  109. {
  110. static const ArmorType::Type value(ArmorType::get(armorId1));
  111. return armor.find(value)->second;
  112. }
  113.  
  114. int test2(const std::map<std::string,ArmorDescription> &armor)
  115. {
  116. static const std::string value(armorId1);
  117. return armor.find(value)->second;
  118. }
  119.  
  120. int main(int argc, const char * argv[])
  121. {
  122. std::map<ArmorType::Type,ArmorDescription> armorHash;
  123. std::map<std::string,ArmorDescription> armorString;
  124.  
  125. armorHash.insert(std::make_pair(ArmorType::get(armorId0), ArmorDescription(0)));
  126. armorHash.insert(std::make_pair(ArmorType::get(armorId1), ArmorDescription(1)));
  127. armorHash.insert(std::make_pair(ArmorType::get(armorId2), ArmorDescription(2)));
  128. armorHash.insert(std::make_pair(ArmorType::get(armorId3), ArmorDescription(3)));
  129. armorHash.insert(std::make_pair(ArmorType::get(armorId4), ArmorDescription(4)));
  130. armorHash.insert(std::make_pair(ArmorType::get(armorId5), ArmorDescription(5)));
  131. armorHash.insert(std::make_pair(ArmorType::get(armorId6), ArmorDescription(6)));
  132. armorHash.insert(std::make_pair(ArmorType::get(armorId7), ArmorDescription(7)));
  133. armorHash.insert(std::make_pair(ArmorType::get(armorId8), ArmorDescription(8)));
  134. armorHash.insert(std::make_pair(ArmorType::get(armorId9), ArmorDescription(9)));
  135.  
  136. armorString.insert(std::make_pair(armorId0, ArmorDescription(0)));
  137. armorString.insert(std::make_pair(armorId1, ArmorDescription(1)));
  138. armorString.insert(std::make_pair(armorId2, ArmorDescription(2)));
  139. armorString.insert(std::make_pair(armorId3, ArmorDescription(3)));
  140. armorString.insert(std::make_pair(armorId4, ArmorDescription(4)));
  141. armorString.insert(std::make_pair(armorId5, ArmorDescription(5)));
  142. armorString.insert(std::make_pair(armorId6, ArmorDescription(6)));
  143. armorString.insert(std::make_pair(armorId7, ArmorDescription(7)));
  144. armorString.insert(std::make_pair(armorId8, ArmorDescription(8)));
  145. armorString.insert(std::make_pair(armorId9, ArmorDescription(9)));
  146.  
  147. auto armor1It = armorHash.find(ArmorType::find(armorId1));
  148. auto armor2It = armorHash.find(ArmorType::find(armorId2));
  149. auto armor10It = armorHash.find(ArmorType::find("armor10"));
  150.  
  151. std::cout << ArmorType::to_string(armor1It->first) << ":" << static_cast<int>(armor1It->first) << std::endl;
  152. std::cout << ArmorType::to_string(armor2It->first) << ":" << static_cast<int>(armor2It->first) << std::endl;
  153. std::cout << ArmorType::to_string(armor10It->first) << ":" << static_cast<int>(armor10It->first) << std::endl;
  154.  
  155. int ret1 = 0;
  156. int ret2 = 0;
  157.  
  158. std::chrono::high_resolution_clock::time_point t = std::chrono::high_resolution_clock::now();
  159. for(int i=0;i<1000000;++i)
  160. ret1 += test1(armorHash);
  161. auto duration = std::chrono::high_resolution_clock::now() - t;
  162. std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() << std::endl;
  163.  
  164. t = std::chrono::high_resolution_clock::now();
  165. for(int i=0;i<1000000;++i)
  166. ret2 += test2(armorString);
  167. duration = std::chrono::high_resolution_clock::now() - t;
  168. std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() << std::endl;
  169.  
  170. return 0;
  171. }
Success #stdin #stdout 0.07s 3436KB
stdin
Standard input is empty
stdout
armor1:2
armor2:3
:10
9
59