Skip to content

Commit 5756b79

Browse files
committed
📚 Document counting_iterator
Problem: - `counting_iterator` is undiscoverable because it is not documented. Solution: - Add documentation for `counting_iterator`.
1 parent 45bb83c commit 5756b79

4 files changed

Lines changed: 41 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ Compiler support:
1919

2020
| Branch | GCC versions | Clang versions |
2121
| --- | --- | --- |
22-
| [main](https://github.com/intel/cpp-std-extensions/tree/main) | 12 thru 14 | 18 thru 21 |
22+
| [main](https://github.com/intel/cpp-std-extensions/tree/main) | 12 thru 14 | 18 thru 22 |
2323
| [cpp20](https://github.com/intel/cpp-std-extensions/tree/cpp20) | 12 thru 14 | 14 thru 21 |
2424

docs/intro.adoc

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,15 @@
1616

1717
The following compilers are supported:
1818

19-
* clang 14
20-
* clang 15
21-
* clang 16
22-
* clang 17
2319
* clang 18
2420
* clang 19
2521
* clang 20
26-
* clang 21
22+
* clang 22
2723
* gcc 12
2824
* gcc 13
2925
* gcc 14
3026

31-
In general, `stdx` supports the C\\++17 standard; however some functionality is
32-
available only in C++20 and later.
27+
In general, `stdx` targets the C++23 standard.
3328

3429
=== Dependencies
3530

docs/iterator.adoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,27 @@ constexpr auto c = stdx::ct_capacity(a); // std::size_t{4}
2323
* `stdx::cx_vector`
2424

2525
`ct_capacity_v` is a corresponding variable template of type `std::size_t`.
26+
27+
=== `counting_iterator`
28+
29+
A `counting_iterator` is a random access iterator that counts up indefinitely
30+
from some integral starting value. It is useful for enumerating sequences:
31+
32+
[source,cpp]
33+
----
34+
stdx::for_each(
35+
std::cbegin(input), std::cend(input),
36+
[](auto const &elem, auto idx) {
37+
// do something with element and index
38+
},
39+
stdx::counting_iterator{});
40+
----
41+
42+
By default, it produces `int` values starting from `0`.
43+
It also can be given a starting value, and it will respect the type:
44+
45+
[source,cpp]
46+
----
47+
auto i1 = stdx::counting_iterator{17}; // 17, 18, 19...
48+
auto i2 = stdx::counting_iterator{'A'}; // A, B, C...
49+
----

test/algorithm.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <stdx/algorithm.hpp>
2+
#include <stdx/iterator.hpp>
23
#include <stdx/tuple_algorithms.hpp>
34
#include <stdx/tuple_destructure.hpp>
45

@@ -61,6 +62,19 @@ TEST_CASE("n-ary for_each", "[algorithm]") {
6162
CHECK(output == std::array{3, 6, 9, 12});
6263
}
6364

65+
TEST_CASE("for_each with counting_iterator", "[algorithm]") {
66+
auto const input = std::array{1, 2, 3, 4};
67+
auto output = decltype(input){};
68+
auto [op, i] = stdx::for_each(
69+
std::cbegin(input), std::cend(input),
70+
[it = std::begin(output)](auto... ns) mutable {
71+
*it++ = (0 + ... + ns);
72+
},
73+
stdx::counting_iterator{1});
74+
CHECK(i == stdx::counting_iterator{5});
75+
CHECK(output == std::array{2, 4, 6, 8});
76+
}
77+
6478
TEST_CASE("unary for_each_n", "[algorithm]") {
6579
auto const input = std::array{1, 2, 3, 4};
6680
auto sum = 0;

0 commit comments

Comments
 (0)