/* _______________ ,----------------------------------------------------------------. //`````````````\\ \ \ //~~~~~~~~~~~~~~~\\ \ by @ohsqueezy & @sleepin \ //=================\\ \ [ohsqueezy.itch.io] [sleepin.itch.io] \ // \\ \ \ // \\ \ code released under the zlib license [git.nugget.fun/pudding] \ // ☆ GUNKISS ☆ \\ \ \ //_________________________\\ `---------------------------------------------------------------*/ #ifndef CAROUSEL_H_ #define CAROUSEL_H_ #include #include #include "utility.hpp" class Carousel { private: std::uint32_t offset = 0; public: template auto current(Container& container) const { auto location = container.begin(); if (location != container.end()) { std::advance(location, offset); return location; } else { throw std::out_of_range("Container is empty"); } } template void next(const Container& container) { offset = ++offset % container.size(); } template void previous(const Container& container) { offset = sb::mod(--offset, container.size()); } template void increment(const Container& container, int amount) { offset = sb::mod(offset + amount, container.size()); } void beginning() { offset = 0; } template void end(const Container& container) { offset = container.size() - 1; } /* Return true if the carousel currently points to the location at the beginning of the container. */ bool at_beginning() const { return offset == 0; } /* Return true if the carousel currently points to the location at the end of the container. */ template bool at_end(const Container& container) { return offset >= container.size() - 1; } }; #endif