Guest User

Untitled

a guest
Jan 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. #include <exception>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <list>
  5. #include <sstream>
  6. #include <vector>
  7.  
  8. template<typename ForwardIterator, typename IndexIterator>
  9. class indirection_iterator {
  10. public:
  11. indirection_iterator(ForwardIterator element_begin,
  12. ForwardIterator element_end,
  13. IndexIterator index_begin,
  14. IndexIterator index_end)
  15. :
  16. m_element_begin{element_begin},
  17. m_element_end{element_end},
  18. m_index_begin{index_begin},
  19. m_index_end{index_end},
  20. m_index_current{index_begin},
  21. m_elements{std::distance(element_begin, element_end)}
  22. {}
  23.  
  24. indirection_iterator begin()
  25. {
  26. return indirection_iterator{m_element_begin,
  27. m_element_end,
  28. m_index_begin,
  29. m_index_end,
  30. m_index_begin};
  31. }
  32.  
  33. indirection_iterator end()
  34. {
  35. return indirection_iterator{m_element_begin,
  36. m_element_end,
  37. m_index_begin,
  38. m_index_end,
  39. m_index_end};
  40. }
  41.  
  42. auto operator*()
  43. {
  44. auto index = *m_index_current;
  45. check_index(index, m_elements);
  46. auto element_iterator = m_element_begin;
  47. std::advance(element_iterator, index);
  48. return *element_iterator;
  49. }
  50.  
  51. void operator++()
  52. {
  53. std::advance(m_index_current, 1);
  54. }
  55.  
  56. bool operator!=(indirection_iterator const& other)
  57. {
  58. return m_index_current != other.m_index_current;
  59. }
  60.  
  61. private:
  62.  
  63. indirection_iterator(ForwardIterator element_begin,
  64. ForwardIterator element_end,
  65. IndexIterator index_begin,
  66. IndexIterator index_end,
  67. IndexIterator index_current)
  68. :
  69. m_element_begin{element_begin},
  70. m_element_end{element_end},
  71. m_index_begin{index_begin},
  72. m_index_end{index_end},
  73. m_index_current{index_current},
  74. m_elements{std::distance(element_begin, element_end)}
  75. {}
  76.  
  77. template<typename IndexIter>
  78. using index_type = typename std::iterator_traits<IndexIter>::value_type;
  79.  
  80. ForwardIterator m_element_begin;
  81. ForwardIterator m_element_end;
  82. IndexIterator m_index_begin;
  83. IndexIterator m_index_end;
  84. IndexIterator m_index_current;
  85. typename std::iterator_traits<ForwardIterator>::difference_type m_elements;
  86.  
  87. template<typename index_type>
  88. void check_index(index_type index, size_t elements)
  89. {
  90. if (index < 0)
  91. {
  92. std::stringstream ss;
  93. ss << "index(" << index << ") < 0";
  94. throw std::runtime_error{ss.str()};
  95. }
  96.  
  97. if (index >= elements)
  98. {
  99. std::stringstream ss;
  100. ss << "index(" << index << ") >= elements(" << elements << ")";
  101. throw std::runtime_error{ss.str()};
  102. }
  103. }
  104. };
  105.  
  106.  
  107. static std::vector<char> get_alphabet() {
  108. std::vector<char> alphabet;
  109.  
  110. for (char ch = 'a'; ch <= 'z'; ch++)
  111. {
  112. alphabet.push_back(ch);
  113. }
  114.  
  115. for (char ch = 'A'; ch <= 'Z'; ch++)
  116. {
  117. alphabet.push_back(ch);
  118. }
  119.  
  120. alphabet.push_back(',');
  121. alphabet.push_back(' ');
  122. alphabet.push_back('!');
  123. alphabet.push_back('n');
  124.  
  125. return alphabet;
  126. }
  127.  
  128. int main(int argc, const char * argv[]) {
  129. std::vector<char> alphabet = get_alphabet();
  130. std::list<int> char_indices =
  131. { 33, 4, 11, 11, 14, 52, 53, 48, 14, 17, 11, 3, 54, 55 };
  132.  
  133. indirection_iterator<decltype(alphabet.cbegin()),
  134. decltype(char_indices.cbegin())> indirection_iter {
  135. alphabet.cbegin(),
  136. alphabet.cend(),
  137. char_indices.cbegin(),
  138. char_indices.cend()
  139. };
  140.  
  141. std::copy(indirection_iter.begin(),
  142. indirection_iter.end(),
  143. std::ostream_iterator<char>{std::cout});
  144. return 0;
  145. }
Add Comment
Please, Sign In to add comment