fork(1) download
  1.  
  2. #include <locale.h>
  3. #include <Windows.h>
  4. #include <iostream>
  5. #include <limits>
  6.  
  7. template<class _Ty>
  8. class Timer
  9. {
  10. public:
  11. LARGE_INTEGER str, fre, fin;
  12.  
  13. inline void start()
  14. {
  15. QueryPerformanceFrequency(&fre);
  16. QueryPerformanceCounter(&str);
  17. }
  18.  
  19. inline void finish()
  20. {
  21. QueryPerformanceCounter(&fin);
  22. }
  23.  
  24. inline _Ty operator*()
  25. {
  26. return (_Ty)(_Ty(fin.QuadPart - str.QuadPart) * 10e9 / _Ty(fre.QuadPart));
  27. }
  28.  
  29. inline void out()
  30. {
  31. std::cout.precision(std::numeric_limits<long double>::digits10);
  32. std::cout << **this << "ns" << std::endl;
  33. }
  34.  
  35. inline void outms()
  36. {
  37. std::cout.precision(std::numeric_limits<long double>::digits10);
  38. std::cout << (_Ty)(_Ty(fin.QuadPart - str.QuadPart) * 10e3 / _Ty(fre.QuadPart)) << "ms" << std::endl;
  39. }
  40.  
  41. inline void outs()
  42. {
  43. std::cout.precision(std::numeric_limits<long double>::digits10);
  44. std::cout << (_Ty)(_Ty(fin.QuadPart - str.QuadPart) / _Ty(fre.QuadPart)) << "ms" << std::endl;
  45. }
  46.  
  47. };
  48.  
  49. class WString final
  50. {
  51. wchar_t *m_ptr;
  52. wchar_t *m_last;
  53. size_t m_length;
  54.  
  55. bool srp = false;
  56.  
  57. public:
  58.  
  59. static const size_t error = -1;
  60.  
  61. WString()
  62. : m_length(0)
  63. , m_ptr(nullptr)
  64. , m_last(m_ptr)
  65. {
  66. }
  67.  
  68. WString(const wchar_t *ptr)
  69. {
  70. InitString(ptr);
  71. }
  72. void InitString(const wchar_t *str)
  73. {
  74. m_length = wcslen(str);
  75. m_ptr = new wchar_t[m_length + 1];
  76. m_last = m_ptr + m_length - 1;
  77. wcsncpy_s(m_ptr, m_length + 1, str, m_length);
  78. }
  79.  
  80. typedef unsigned long long i64u;
  81.  
  82. static inline i64u has_zero_wide(const i64u n)
  83. {
  84. const i64u finder = 0x0001000100010001ULL;
  85. const i64u masker = 0x8000800080008000ULL;
  86. return (n - finder) & (~n & masker);
  87. }
  88.  
  89. static inline i64u has_some_wide(const wchar_t* s, const i64u cs)
  90. {
  91. return has_zero_wide(*(i64u *)s ^ cs);
  92. }
  93.  
  94. static wchar_t* wcsrnchr(const wchar_t* haystack, size_t len, wchar_t needle)
  95. {
  96. const i64u cs = ((i64u)needle << 48) | ((i64u)needle << 32) | ((i64u)needle << 16) | needle;
  97.  
  98. if (has_some_wide(haystack + len - 4, cs))
  99. {
  100. if (haystack[len - 1] == needle) return (wchar_t*)haystack + len - 1;
  101. if (haystack[len - 2] == needle) return (wchar_t*)haystack + len - 2;
  102. if (haystack[len - 3] == needle) return (wchar_t*)haystack + len - 3;
  103. return (wchar_t*)haystack + len - 4;
  104. }
  105.  
  106. wchar_t* hay;
  107.  
  108. for (hay = (wchar_t*)((i64u)(haystack + len - 1) & -8LL); hay >= haystack; hay -= 4)
  109. if (has_some_wide(hay, cs))
  110. {
  111. if (hay[0] == needle) return hay;
  112. if (hay[1] == needle) return hay + 1;
  113. if (hay[2] == needle) return (wchar_t*)hay + 2;
  114. return hay + 3;
  115. }
  116.  
  117. for (hay += 3; hay >= haystack; --hay)
  118. if (*hay == needle) return hay;
  119.  
  120. return NULL;
  121. }
  122.  
  123. size_t FindLastHelper(const wchar_t *str, size_t ends, size_t len) const
  124. {
  125. if (ends >= m_length)
  126. throw(new StringException(StringErrorCode::ComparasionSizeException));
  127.  
  128. size_t searchLen = m_length - ends;
  129. wchar_t frontch = str[len - 1];
  130. wchar_t *last = m_last - ends;
  131. wchar_t *ptr = m_ptr; /* const */
  132.  
  133. do
  134. {
  135. wchar_t backup = *last;
  136. *last = 0;
  137.  
  138. if ( (ptr = wcsrnchr(m_ptr, searchLen, frontch)) == NULL )
  139. //if ( (ptr = wcsrchr(m_ptr, frontch)) == NULL )
  140. break;
  141. else if ( !wcscmp(ptr - len, str) )
  142. {
  143. *last = backup;
  144. return ptr - m_ptr - 1;
  145. }
  146.  
  147. *last = backup;
  148. last = ptr;
  149. } while (true);
  150.  
  151. return error;
  152. }
  153.  
  154. size_t FindLast(const wchar_t *str, size_t ends) const
  155. {
  156. return FindLastHelper(str, ends, wcslen(str));
  157. }
  158.  
  159. size_t FindLast(const WString& refer, size_t ends) const
  160. {
  161. return FindLastHelper(refer.m_ptr, ends, refer.m_length);
  162. }
  163.  
  164. size_t FindLast(const wchar_t *str) const
  165. {
  166. return FindLastHelper(str, 0, wcslen(str));
  167. }
  168.  
  169. size_t FindLast(const WString& refer) const
  170. {
  171. return FindLastHelper(refer.m_ptr, 0, refer.m_length);
  172. }
  173. };
  174.  
  175. void testfind()
  176. {
  177. #define L1(x) x x x x
  178. #define L2(x) L1(x) L1(x) L1(x) L1(x)
  179. #define L3(x) L2(x) L2(x) L2(x) L2(x)
  180. #define L4(x) L3(x) L3(x) L3(x) L3(x)
  181. #define L5(x) L4(x) L4(x) L4(x) L4(x)
  182. #define L6(x) L5(x) L5(x) L5(x) L5(x)
  183. #define L7(x) L6(x) L6(x) L6(x) L6(x)
  184. WString testTarget(L"rokomo" L6(L"koromo"));
  185. WString findWhat(L"rokomo");
  186.  
  187. Timer<long double> timer;
  188. timer.start();
  189. size_t pos = testTarget.FindLast(findWhat);
  190. timer.finish();
  191.  
  192. std::cout << pos << std::endl;
  193. timer.outms();
  194. }
  195.  
  196.  
  197. int _tmain()
  198. {
  199. _wsetlocale(LC_ALL, L"korean");
  200.  
  201. std::locale::global(std::locale("kor"));
  202. std::wcout.imbue(std::locale("kor"));
  203. std::wcin.imbue(std::locale("kor"));
  204.  
  205. testfind();
  206.  
  207. return 0;
  208. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:10:2: error: 'LARGE_INTEGER' does not name a type
  LARGE_INTEGER str, fre, fin;
  ^
prog.cpp: In member function 'void Timer<_Ty>::start()':
prog.cpp:14:30: error: 'fre' was not declared in this scope
   QueryPerformanceFrequency(&fre);
                              ^
prog.cpp:14:33: error: there are no arguments to 'QueryPerformanceFrequency' that depend on a template parameter, so a declaration of 'QueryPerformanceFrequency' must be available [-fpermissive]
   QueryPerformanceFrequency(&fre);
                                 ^
prog.cpp:14:33: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
prog.cpp:15:28: error: 'str' was not declared in this scope
   QueryPerformanceCounter(&str);
                            ^
prog.cpp:15:31: error: there are no arguments to 'QueryPerformanceCounter' that depend on a template parameter, so a declaration of 'QueryPerformanceCounter' must be available [-fpermissive]
   QueryPerformanceCounter(&str);
                               ^
prog.cpp: In member function 'void Timer<_Ty>::finish()':
prog.cpp:20:28: error: 'fin' was not declared in this scope
   QueryPerformanceCounter(&fin);
                            ^
prog.cpp:20:31: error: there are no arguments to 'QueryPerformanceCounter' that depend on a template parameter, so a declaration of 'QueryPerformanceCounter' must be available [-fpermissive]
   QueryPerformanceCounter(&fin);
                               ^
prog.cpp: In member function '_Ty Timer<_Ty>::operator*()':
prog.cpp:25:20: error: 'fin' was not declared in this scope
   return (_Ty)(_Ty(fin.QuadPart - str.QuadPart) * 10e9 / _Ty(fre.QuadPart));
                    ^
prog.cpp:25:35: error: 'str' was not declared in this scope
   return (_Ty)(_Ty(fin.QuadPart - str.QuadPart) * 10e9 / _Ty(fre.QuadPart));
                                   ^
prog.cpp:25:62: error: 'fre' was not declared in this scope
   return (_Ty)(_Ty(fin.QuadPart - str.QuadPart) * 10e9 / _Ty(fre.QuadPart));
                                                              ^
prog.cpp: In member function 'void Timer<_Ty>::outms()':
prog.cpp:37:26: error: 'fin' was not declared in this scope
   std::cout << (_Ty)(_Ty(fin.QuadPart - str.QuadPart) * 10e3 / _Ty(fre.QuadPart)) << "ms" << std::endl;
                          ^
prog.cpp:37:41: error: 'str' was not declared in this scope
   std::cout << (_Ty)(_Ty(fin.QuadPart - str.QuadPart) * 10e3 / _Ty(fre.QuadPart)) << "ms" << std::endl;
                                         ^
prog.cpp:37:68: error: 'fre' was not declared in this scope
   std::cout << (_Ty)(_Ty(fin.QuadPart - str.QuadPart) * 10e3 / _Ty(fre.QuadPart)) << "ms" << std::endl;
                                                                    ^
prog.cpp: In member function 'void Timer<_Ty>::outs()':
prog.cpp:43:26: error: 'fin' was not declared in this scope
   std::cout << (_Ty)(_Ty(fin.QuadPart - str.QuadPart) / _Ty(fre.QuadPart)) << "ms" << std::endl;
                          ^
prog.cpp:43:41: error: 'str' was not declared in this scope
   std::cout << (_Ty)(_Ty(fin.QuadPart - str.QuadPart) / _Ty(fre.QuadPart)) << "ms" << std::endl;
                                         ^
prog.cpp:43:61: error: 'fre' was not declared in this scope
   std::cout << (_Ty)(_Ty(fin.QuadPart - str.QuadPart) / _Ty(fre.QuadPart)) << "ms" << std::endl;
                                                             ^
prog.cpp: In member function 'void WString::InitString(const wchar_t*)':
prog.cpp:76:48: error: 'wcsncpy_s' was not declared in this scope
    wcsncpy_s(m_ptr, m_length + 1, str, m_length);
                                                ^
prog.cpp: In member function 'size_t WString::FindLastHelper(const wchar_t*, size_t, size_t) const':
prog.cpp:125:15: error: expected type-specifier before 'StringException'
     throw(new StringException(StringErrorCode::ComparasionSizeException));
               ^
prog.cpp:125:15: error: expected ')' before 'StringException'
prog.cpp: In function 'int _tmain()':
prog.cpp:198:31: error: '_wsetlocale' was not declared in this scope
  _wsetlocale(LC_ALL, L"korean");
                               ^
prog.cpp: In instantiation of 'void Timer<_Ty>::start() [with _Ty = long double]':
prog.cpp:187:14:   required from here
prog.cpp:14:28: error: 'QueryPerformanceFrequency' was not declared in this scope
   QueryPerformanceFrequency(&fre);
                            ^
prog.cpp:15:26: error: 'QueryPerformanceCounter' was not declared in this scope
   QueryPerformanceCounter(&str);
                          ^
prog.cpp: In instantiation of 'void Timer<_Ty>::finish() [with _Ty = long double]':
prog.cpp:189:15:   required from here
prog.cpp:20:26: error: 'QueryPerformanceCounter' was not declared in this scope
   QueryPerformanceCounter(&fin);
                          ^
stdout
Standard output is empty