summaryrefslogtreecommitdiffstats
path: root/src/2geom/utils.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/2geom/utils.h')
-rw-r--r--src/2geom/utils.h24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/2geom/utils.h b/src/2geom/utils.h
index bc0ad74b8..3e0dd4717 100644
--- a/src/2geom/utils.h
+++ b/src/2geom/utils.h
@@ -71,6 +71,30 @@ struct NullIterator
void operator=(T const &v) {}
};
+/** @brief Get the next iterator in the container with wrap-around.
+ * If the iterator would become the end iterator after incrementing,
+ * return the begin iterator instead. */
+template <typename Iter, typename Container>
+Iter cyclic_next(Iter i, Container &c) {
+ ++i;
+ if (i == c.end()) {
+ i = c.begin();
+ }
+ return i;
+}
+
+/** @brief Get the previous iterator in the container with wrap-around.
+ * If the passed iterator is the begin iterator, return the iterator
+ * just before the end iterator instead. */
+template <typename Iter, typename Container>
+Iter cyclic_prior(Iter i, Container &c) {
+ if (i == c.begin()) {
+ i = c.end();
+ }
+ --i;
+ return i;
+}
+
} // end namespace Geom
#endif // LIB2GEOM_SEEN_UTILS_H