fork download
  1. #include <iostream>
  2. #include <cstdint>
  3. #include <chrono>
  4. #include <thread>
  5. #include <future>
  6. #include <functional>
  7.  
  8. class StopWatch {
  9. std::chrono::high_resolution_clock::time_point S;
  10. std::chrono::high_resolution_clock::time_point E;
  11. public:
  12.  
  13. typedef std::chrono::milliseconds TimeType;
  14.  
  15. StopWatch() { Start(); };
  16.  
  17. bool Start() {
  18. S = E = std::chrono::high_resolution_clock::now();
  19. return true;
  20. }
  21. bool ReStart() {
  22. return Start();
  23. }
  24. bool Stop() {
  25. E = std::chrono::high_resolution_clock::now();
  26. return true;
  27. }
  28.  
  29. bool Reset() {
  30. S = E = std::chrono::high_resolution_clock::now();
  31. return true;
  32. }
  33. template<class T = TimeType>
  34. T Ellipse() {
  35. return std::chrono::duration_cast<T>(std::chrono::high_resolution_clock::now() - S);
  36. }
  37. template<class T = TimeType>
  38. T Result() {
  39. return std::chrono::duration_cast<T>(E - S);
  40. }
  41.  
  42. };
  43.  
  44. template<class INT>
  45. class CycleTimer {
  46. public:
  47. CycleTimer() {}
  48. CycleTimer(const INT& MOD) :M(MOD) {}
  49.  
  50. bool IsZero() {
  51. return (V % M)==0;
  52. }
  53.  
  54. bool IsTail() {
  55. return (V % M) == (M-1);//take care to -1 method.
  56. }
  57.  
  58. INT Value() {
  59. return V % M;
  60. }
  61.  
  62. INT Count() { return V; }
  63. INT MOD() { return M; }
  64.  
  65. bool SetMOD(const INT& In) {
  66. M = In;
  67. return true;
  68.  
  69. }
  70.  
  71. bool Reset() {
  72. V = 0;
  73. return true;
  74. }
  75.  
  76. bool Update() {
  77. V++;
  78. return true;
  79. }
  80.  
  81. protected:
  82. INT V = 0;
  83. INT M = 7;
  84. };
  85.  
  86. template<class INT>
  87. class TimeTimer :public CycleTimer<INT> {
  88. public:
  89. TimeTimer(const INT& Mod, std::chrono::nanoseconds NS) :CycleTimer<INT>(Mod), Wait(NS) {}
  90.  
  91.  
  92. bool Start() {
  93. auto Fun = [this]() { while (IsRunning_) { this->Update(); std::this_thread::sleep_for(Wait); }};
  94. IsRunning_ = true;
  95. auto A = std::async(std::launch::async,Fun);
  96.  
  97. return true;
  98. }
  99. bool Stop() {
  100. IsRunning_=false;
  101. return true;
  102. }
  103.  
  104. virtual ~TimeTimer() {
  105. Stop();
  106. std::this_thread::sleep_for(std::chrono::seconds(3));
  107. }
  108.  
  109. bool IsRunning() {
  110. return IsRunning_;
  111. }
  112.  
  113. protected:
  114. bool IsRunning_ = false;
  115. std::chrono::nanoseconds Wait = { 0 };
  116. };
  117.  
  118. int main() {
  119.  
  120. TimeTimer<std::uintmax_t> TT(7, std::chrono::seconds(1));
  121.  
  122. TT.Start();
  123.  
  124. while (TT.IsRunning()){
  125. std::cout << TT.Count() << std::endl;
  126.  
  127. if (TT.IsTail()) {
  128. TT.Stop();
  129. }
  130. }
  131.  
  132. return 0;
  133.  
  134. }
  135. /** /
  136. int main() {
  137. CycleTimer<int> CT(16);
  138.  
  139. //CT.Update();
  140.  
  141. do{
  142. std::cout << CT.Count() << std::endl;
  143. CT.Update();
  144. } while (!CT.IsTail());
  145.  
  146. return 0;
  147. }
  148. /**/
Time limit exceeded #stdin #stdout 5s 5508KB
stdin
Standard input is empty
stdout
Standard output is empty