aboutsummaryrefslogtreecommitdiff
path: root/snippets/group.h
diff options
context:
space:
mode:
Diffstat (limited to 'snippets/group.h')
-rw-r--r--snippets/group.h16
1 files changed, 16 insertions, 0 deletions
diff --git a/snippets/group.h b/snippets/group.h
new file mode 100644
index 0000000..bd183e0
--- /dev/null
+++ b/snippets/group.h
@@ -0,0 +1,16 @@
+#pragma once
+
+#include <functional>
+#include <ranges>
+
+template <std::ranges::random_access_range Range>
+void group(const Range &v, const std::function<void(int, int)> &fn) {
+ int n = v.size();
+ for (int i = 0; i < n;) {
+ auto i0 = i;
+ while (i < n && v[i0] == v[i]) {
+ i++;
+ }
+ fn(i0, i);
+ }
+}