utils: Add reversed template helper for container iteration
this permits to use range-based for loops to iterate on reversed containers. Should work on any container implementing rbegin and rend.
This commit is contained in:
parent
3f51feaaef
commit
f58cbf0b98
18
src/utils.hh
18
src/utils.hh
|
@ -13,6 +13,24 @@ struct LineAndColumn
|
||||||
: line(line), column(column) {}
|
: line(line), column(column) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename Container>
|
||||||
|
struct ReversedContainer
|
||||||
|
{
|
||||||
|
ReversedContainer(Container& container) : container(container) {}
|
||||||
|
Container& container;
|
||||||
|
|
||||||
|
decltype(container.rbegin()) begin() { return container.rbegin(); }
|
||||||
|
decltype(container.rend()) end() { return container.rend(); }
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Container>
|
||||||
|
ReversedContainer<Container> reversed(Container& container)
|
||||||
|
{
|
||||||
|
return ReversedContainer<Container>(container);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // utils_hh_INCLUDED
|
#endif // utils_hh_INCLUDED
|
||||||
|
|
Loading…
Reference in New Issue
Block a user