Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. using namespace std;
  2.  
  3. template<typename T>
  4. class CIter {
  5. private:
  6. const T& container;
  7. typename T::const_iterator cit;
  8. typename T::value_type::const_iterator valcit;
  9.  
  10. public:
  11. CIter(const T& container, typename T::const_iterator cit,
  12. typename T::value_type::const_iterator valcit)
  13. : container(container), cit(cit), valcit(valcit) {
  14. while (cit != container.end() && cit->empty()) {
  15. ++cit;
  16. }
  17. if (container.empty() == false) {
  18. if (cit == container.end()) {
  19. valcit = container.back().end();
  20. } else {
  21. valcit = cit->begin();
  22. }
  23. }
  24. }
  25.  
  26. const typename T::value_type::value_type& operator*() const {
  27. return *valcit;
  28. }
  29.  
  30.  
  31. bool operator==(const CIter& other) const {
  32. if (cit == other.cit) {
  33. if (valcit == other.valcit) {
  34. if (&container == &other.container)
  35. return true;
  36. }
  37. }
  38. return false;
  39. }
  40.  
  41. bool operator!=(const CIter &other) const {
  42. if (*this == other) {
  43. return false;
  44. }
  45. return true;
  46. }
  47.  
  48. CIter<T>& operator++() {
  49. if (valcit == typename T::value_type::const_iterator() || cit->empty() ||
  50. ++valcit == cit->end()) {
  51. ++cit;
  52. while (cit != container.end() && cit->empty()) {
  53. ++cit;
  54. }
  55. if (container.empty() == false) {
  56. if (cit == container.end()) {
  57. valcit = container.back().end();
  58. } else {
  59. valcit = cit->begin();
  60. }
  61. }
  62. }
  63. return *this;
  64. }
  65. };
  66.  
  67. template<typename T>
  68. class Iterator {
  69. private:
  70. const T& container;
  71.  
  72. public:
  73. Iterator(const T& temp) : container(temp) {}
  74. CIter<T> begin() {
  75. if (container.empty() == true) {
  76. CIter<T> it(container, container.begin(), typename T::value_type::const_iterator());
  77. return it;
  78. } else {
  79. CIter<T> it(container, container.begin(), container.front().begin());
  80. return it;
  81. }
  82. }
  83. CIter<T> end() {
  84. if (container.empty() == true) {
  85. CIter<T> it(container, container.end(), typename T::value_type::const_iterator());
  86. return it;
  87. } else {
  88. CIter<T> it(container, container.end(), container.back().end());
  89. return it;
  90. }
  91. }
  92. };
  93.  
  94. template<typename OuterCollection>
  95. Iterator<OuterCollection> MakeRange(const OuterCollection &container) {
  96. return Iterator<OuterCollection>(container);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement