diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-02-07 23:50:50 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2024-02-12 20:27:16 +0000 |
commit | ca276ca5471a4e7a137f68a81feb150282eae62f (patch) | |
tree | 6ae3825a7cb5ce3deffe42468bbf9682c0a70e99 /lib/collections.h | |
parent | Merge branch 'glcontainer-fix' (diff) | |
download | ilt-ca276ca5471a4e7a137f68a81feb150282eae62f.tar.bz2 ilt-ca276ca5471a4e7a137f68a81feb150282eae62f.tar.xz ilt-ca276ca5471a4e7a137f68a81feb150282eae62f.zip |
Add stripiter
A generic iterator wrapper returning a tuple of 3 references to the original values, as
processed in the fashion of an OpenGL triangle strip.
Diffstat (limited to 'lib/collections.h')
-rw-r--r-- | lib/collections.h | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/collections.h b/lib/collections.h index 943b986..6cee10c 100644 --- a/lib/collections.h +++ b/lib/collections.h @@ -2,6 +2,7 @@ #include <algorithm> #include <array> +#include <cstdint> #include <span> #include <utility> #include <vector> @@ -160,3 +161,62 @@ template<typename T> struct pair_range { }; template<typename T> pair_range(std::pair<T, T>) -> pair_range<T>; + +template<typename iter> struct stripiter { + [[nodiscard]] constexpr bool + operator!=(const stripiter & other) const + { + return current != other.current; + } + + [[nodiscard]] constexpr bool + operator==(const stripiter & other) const + { + return current == other.current; + } + + constexpr stripiter & + operator++() + { + ++current; + off = 1 - off; + return *this; + } + + constexpr stripiter & + operator--() + { + --current; + off = 1 - off; + return *this; + } + + constexpr auto + operator-(const stripiter & other) const + { + return current - other.current; + } + + constexpr auto + operator*() const + { + return std::tie(*(current - (2 - off)), *(current - off - 1), *current); + } + + iter current; + uint8_t off {}; +}; + +template<typename T> struct std::iterator_traits<stripiter<T>> : std::iterator_traits<T> { }; + +constexpr auto +strip_begin(IterableCollection auto & cont) +{ + return stripiter {cont.begin() + 2}; +} + +constexpr auto +strip_end(IterableCollection auto & cont) +{ + return stripiter {cont.end()}; +} |