template<typename Iterator, typename KeyGetter>
class ActsExamples::GroupBy< Iterator, KeyGetter >
Proxy for iterating over groups of elements within a container.
- Note
- Each group will contain at least one element.
Consecutive elements with the same key (as defined by the KeyGetter) are placed in one group. The proxy should always be used as part of a range-based for loop. In combination with structured bindings to reduce the boilerplate, the group iteration can be written as
for (auto&& [key, elements] : GroupBy<...>(...)) {
// do something with just the key
...
// iterate over the group elements
for (const auto& element : elements) {
...
}
}
Definition at line 39 of file GroupBy.h.
template<typename Iterator , typename KeyGetter >
Iterator type representing the end of the groups.
The end iterator will not be dereferenced in C++17 range-based loops. It can thus be a simpler type without the overhead of the full group iterator below.
Definition at line 50 of file GroupBy.h.
template<typename Iterator , typename KeyGetter >
Find the end of the group that starts at the given position.
This uses a linear search from the start position and thus has linear complexity in the group size. It does not assume any ordering of the underlying container and is a cache-friendly access pattern.
Definition at line 121 of file GroupBy.h.
121 {
122
123 if (start == m_end) {
124 return start;
125 }
126
127 return std::find_if_not(std::next(start), m_end,
128 [this, start](const auto& x) {
129 return m_keyGetter(x) == m_keyGetter(*start);
130 });
131 }
Referenced by ActsExamples::GroupBy< Iterator, KeyGetter >::GroupIterator::operator++().