All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
collection_starts_from_last_change.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2013 Piotr Wygocki
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
15 #ifndef PAAL_COLLECTION_STARTS_FROM_LAST_CHANGE_HPP
16 #define PAAL_COLLECTION_STARTS_FROM_LAST_CHANGE_HPP
17 
18 #include <boost/range/join.hpp>
19 
20 #include <unordered_map>
21 
22 namespace paal {
23 namespace data_structures {
24 
33 template <typename Iterator,
34  typename hash =
35  std::hash<typename std::iterator_traits<Iterator>::value_type>>
37  typedef typename std::iterator_traits<Iterator>::value_type Element;
38  typedef std::unordered_map<Element, Iterator, hash> ElemToIter;
39  typedef std::pair<Iterator, Iterator> Range;
40  typedef boost::joined_range<Range, Range> JoinedRange;
41  typedef typename boost::range_iterator<JoinedRange>::type JoinedIterator;
42 
43  public:
44  typedef JoinedIterator ResultIterator;
45 
47 
55  : m_begin(begin), m_end(end), m_new_begin(m_begin) {
56  assert(m_begin != m_end);
57  for (auto i = m_begin; i != m_end; ++i) {
58  bool b = m_elem_to_iter.emplace(*i, i).second;
59  assert(b);
60  }
61  }
62 
69  void set_last_change(const Element &el) {
70  auto i = m_elem_to_iter.find(el);
71  assert(i != m_elem_to_iter.end());
72  m_new_begin = i->second;
73  }
74 
80  JoinedIterator begin() { return std::begin(get_range()); }
81 
87  JoinedIterator end() { return std::end(get_range()); }
88 
89  private:
95  JoinedRange get_range() {
96  Range r1 = std::make_pair(m_new_begin, m_end);
97  Range r2 = std::make_pair(m_begin, m_new_begin);
98  return boost::join(r1, r2);
99  }
100 
101  Iterator m_begin;
102  Iterator m_end;
103  Iterator m_new_begin;
104  ElemToIter m_elem_to_iter;
105 };
106 }
107 }
108 
109 #endif // PAAL_COLLECTION_STARTS_FROM_LAST_CHANGE_HPP
this collection stores some range and expose set_last_change function each time begin and end is call...
void set_last_change(const Element &el)
one can set the place of the last change (future start position of the range)