fork(2) download
  1. #include <iostream>
  2. #include <type_traits>
  3. #include <utility>
  4. #include <chrono>
  5. #include <set>
  6. #include <map>
  7. #include <vector>
  8. #include <iterator>
  9. #include <utility>
  10.  
  11.  
  12.  
  13.  
  14. template<typename Type>
  15. struct RangeTypes {
  16. using RangeType = std::pair<Type,Type> ;
  17.  
  18. class Iterator {
  19. public:
  20.  
  21. Iterator(): m_rangePtr(nullptr), m_currVal(0) {};
  22. ~Iterator() {};
  23. Iterator(RangeType & range, bool atEnd=false): m_rangePtr(&range) {
  24. if(atEnd) {
  25. m_currVal = m_rangePtr->second;
  26. } else {
  27. m_currVal = m_rangePtr->first;
  28. }
  29. };
  30.  
  31. /** pre-increment ++it
  32.   * Allow to iterate over the end of the sequence
  33.   */
  34. Iterator & operator++() {
  35. if(m_rangePtr) {
  36. ++m_currVal;
  37. }
  38. return *this;
  39. }
  40.  
  41. /** post-increment it++ */
  42. Iterator operator++(int) {
  43. Iterator it(*this);
  44. operator++();
  45. return it;
  46. }
  47.  
  48. bool operator==(const Iterator &rhs) {
  49. if(m_rangePtr) {
  50. if(m_rangePtr == rhs.m_rangePtr ) { // if sequences are the same
  51. return m_currVal == rhs.m_currVal;
  52. }
  53. }
  54. return false;
  55. }
  56.  
  57. // Return false if the same!
  58. bool operator!=(const Iterator &rhs) {
  59. return !(*this==rhs);
  60. }
  61.  
  62. // get current value;
  63. Type operator*() {
  64. return m_currVal;
  65. }
  66.  
  67. private:
  68. RangeType * m_rangePtr;
  69. Type m_currVal;
  70. };
  71.  
  72. class ContainerType : public RangeType {
  73. public:
  74.  
  75. typedef Iterator iterator;
  76.  
  77. ContainerType(RangeType & range ): RangeType(range) {
  78. // if range wrong, set to no range!
  79. if(this->first > this->second) {
  80. this->first = 0;
  81. this->second = 0;
  82. }
  83. }
  84. iterator begin() {
  85. return iterator(*this);
  86. };
  87. iterator end() {
  88. return iterator(*this,true);
  89. };
  90. };
  91. };
  92.  
  93.  
  94. #define INIT_TIMER auto start = std::chrono::high_resolution_clock::now();
  95. #define START_TIMER start = std::chrono::high_resolution_clock::now();
  96. #define STOP_TIMER(name) \
  97.   double count = std::chrono::duration<double,std::milli>(std::chrono::high_resolution_clock::now()-start ).count(); \
  98.   std::cout << "RUNTIME of " << name << ": " << count << " ms " << std::endl;
  99.  
  100.  
  101. int main(){
  102.  
  103.  
  104. typedef unsigned int Type;
  105. unsigned int max = 10000;
  106. std::pair<Type, Type> a(0,max);
  107.  
  108.  
  109. int loops = 100;
  110. int n = 0;
  111.  
  112. RangeTypes<Type>::ContainerType range(a);
  113. INIT_TIMER
  114. START_TIMER
  115. for(int i=1; i<loops; i++) {
  116. for(auto it=range.begin(); it != range.end(); it++) {
  117. n += *it;
  118. //std::cout << i << std::endl;
  119. }
  120. }
  121. STOP_TIMER("Range: ")
  122.  
  123. std::cout<<"Finished";
  124.  
  125. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
RUNTIME of Range: : 0.000174 ms 
Finished