Skip to content

Commit ab11110

Browse files
committed
Add code examples from docs to examples folder
1 parent 16817ea commit ab11110

17 files changed

+124
-135
lines changed

c++/itertools/enumerate.hpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,7 @@ namespace itertools {
143143
* iterable lazy object (a itertools::enumerated range), which iterates over tuples consisting of the index and the
144144
* value of the dereferenced iterator of the original range:
145145
*
146-
* @code{.cpp}
147-
* std::vector<char> vec { 'a', 'b', 'c' };
148-
*
149-
* for (auto [idx, val] : enumerate(vec)) {
150-
* std::cout << "(" << idx << ", " << val << ")\n";
151-
* }
152-
* @endcode
146+
* @include doc_enumerate.cpp
153147
*
154148
* Output:
155149
*

c++/itertools/product.hpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,7 @@ namespace itertools {
214214
*
215215
* This function returns an iterable lazy object, which can be used in range-based for loops:
216216
*
217-
* @code{.cpp}
218-
* std::vector<int> v1 { 1, 2, 3 };
219-
* std::vector<char> v2 { 'a', 'b' };
220-
*
221-
* for (auto [i, c] : product(v1, v2)) {
222-
* std::cout << "(" << i << ", " << c << ")\n";
223-
* }
224-
* @endcode
217+
* @include doc_product.cpp
225218
*
226219
* Output:
227220
*

c++/itertools/range.hpp

Lines changed: 7 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,7 @@ namespace itertools {
4949
*
5050
* This function returns an iterable lazy object, which can be used in range-based for loops:
5151
*
52-
* @code{.cpp}
53-
* for (auto i : range(5)) {
54-
* std::cout << i << " ";
55-
* }
56-
* std::cout << "\n";
57-
*
58-
* for (auto i : range(-2, 1)) {
59-
* std::cout << i << " ";
60-
* }
61-
* std::cout << "\n";
62-
*
63-
* for (auto i : range(10, 3, -2)) {
64-
* std::cout << i << " ";
65-
* }
66-
* std::cout << "\n";
67-
*
68-
* for (auto i : range(0, 10, -1)) {
69-
* std::cout << i << " "; // empty
70-
* }
71-
* @endcode
52+
* @include doc_range.cpp
7253
*
7354
* Output:
7455
*
@@ -106,7 +87,8 @@ namespace itertools {
10687

10788
/**
10889
* @brief Default constructor.
109-
* @deprecated Use range::range(long, long) or range::range(long, long, long) instead.
90+
* @deprecated Use range::range(std::integral auto, std::integral auto) or range::range(std::integral auto,
91+
* std::integral auto, std::integral auto) instead.
11092
*/
11193
[[deprecated("range default construction deprecated. Use range::all for full range in slicing operation")]] range() = default;
11294

@@ -370,11 +352,7 @@ namespace itertools {
370352
* @details The given integers specify the excluded last values of the individual itertools::range objects. Each range
371353
* starts at 0 and has a step size of 1.
372354
*
373-
* @code{.cpp}
374-
* for (auto [i1, i2] : product_range(2, 3)) {
375-
* std::cout << "(" << i1 << ", " << i2 << ")\n";
376-
* }
377-
* @endcode
355+
* @include doc_product_range.cpp
378356
*
379357
* Output:
380358
*
@@ -407,25 +385,7 @@ namespace itertools {
407385
/**
408386
* @brief Create a cartesian product range of integer ranges from a tuple of integers.
409387
*
410-
* @details The integers in the given tuple specify the excluded last values of the individual itertools::range
411-
* objects. Each range starts at 0 and has a step size of 1.
412-
*
413-
* @code{.cpp}
414-
* for (auto [i1, i2] : product_range(std::make_tuple(2, 3))) {
415-
* std::cout << "(" << i1 << ", " << i2 << ")\n";
416-
* }
417-
* @endcode
418-
*
419-
* Output:
420-
*
421-
* ```
422-
* (0, 0)
423-
* (0, 1)
424-
* (0, 2)
425-
* (1, 0)
426-
* (1, 1)
427-
* (1, 2)
428-
* ```
388+
* @details It simply forwards the integers in the given tuple to itertools::product_range.
429389
*
430390
* @tparam Is Integer types.
431391
* @param idx_tpl Tuple containing the excluded last values of the integer ranges.
@@ -439,25 +399,7 @@ namespace itertools {
439399
/**
440400
* @brief Create a cartesian product range of integer ranges from an array of integers.
441401
*
442-
* @details The integers in the given array specify the excluded last values of the individual itertools::range
443-
* objects. Each range starts at 0 and has a step size of 1.
444-
*
445-
* @code{.cpp}
446-
* for (auto [i1, i2] : product_range(std::array{2, 3})) {
447-
* std::cout << "(" << i1 << ", " << i2 << ")\n";
448-
* }
449-
* @endcode
450-
*
451-
* Output:
452-
*
453-
* ```
454-
* (0, 0)
455-
* (0, 1)
456-
* (0, 2)
457-
* (1, 0)
458-
* (1, 1)
459-
* (1, 2)
460-
* ```
402+
* @details It simply forwards the integers in the given array to itertools::product_range.
461403
*
462404
* @tparam I Integer type.
463405
* @tparam N Number of elements in the array.
@@ -472,12 +414,7 @@ namespace itertools {
472414
/**
473415
* @brief Apply a function to every element of an integer itertools::range.
474416
*
475-
* @code{.cpp}
476-
* // print out the first 10 squares
477-
* itertools::foreach(itertools::range(1, 11), [](int i) {
478-
* std::cout << i * i << " ";
479-
* });
480-
* @endcode
417+
* @include doc_for_each.cpp
481418
*
482419
* Output:
483420
*

c++/itertools/slice.hpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,7 @@ namespace itertools {
105105
*
106106
* This function returns an iterable lazy object, which can be used in range-based for loops:
107107
*
108-
* @code{.cpp}
109-
* std::array<int, 5> arr { 1, 2, 3, 4, 5 };
110-
*
111-
* for (auto i : slice(arr, 1, 3)) {
112-
* std::cout << i << " ";
113-
* }
114-
* std::cout << "\n";
115-
*
116-
* for (auto i : slice(arr, 3, 7)) {
117-
* std::cout << i << " ";
118-
* }
119-
* std::cout << "\n";
120-
*
121-
* for (auto i : slice(arr, 4, 3)) {
122-
* std::cout << i << " "; // empty slice
123-
* }
124-
* @endcode
108+
* @include doc_slice.cpp
125109
*
126110
* Output:
127111
*

c++/itertools/stride.hpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,7 @@ namespace itertools {
148148
*
149149
* This function returns an iterable lazy object, which can be used in range-based for loops:
150150
*
151-
* @code{.cpp}
152-
* std::vector<int> vec { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
153-
*
154-
* for (auto i : stride(vec, 3)) {
155-
* std::cout << i << " ";
156-
* }
157-
* std::cout << "\n";
158-
*
159-
* for (auto i : stride(vec, 10)) {
160-
* std::cout << i << " ";
161-
* }
162-
* @endcode
151+
* @include doc_stride.cpp
163152
*
164153
* Output:
165154
*

c++/itertools/transform.hpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,7 @@ namespace itertools {
164164
* This function returns an iterable lazy object (a itertools::transformed range), which can be used in range-based
165165
* for loops:
166166
*
167-
* @code{.cpp}
168-
* std::list<int> list { 1, 2, 3, 4, 5 };
169-
*
170-
* for (auto i : itertools::transform(list, [](int i) { return i * i; })) {
171-
* std::cout << i << " ";
172-
* }
173-
* @endcode
167+
* @include doc_transform.cpp
174168
*
175169
* Output:
176170
*

c++/itertools/zip.hpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,7 @@ namespace itertools {
189189
*
190190
* This function returns an iterable lazy object, which can be used in range-based for loops:
191191
*
192-
* @code{.cpp}
193-
* std::vector<int> v1 { 1, 2, 3 };
194-
* std::vector<char> v2 { 'a', 'b', 'c', 'd', 'e' };
195-
*
196-
* for (auto [i1, i2] : zip(v1, v1)) {
197-
* std::cout << "(" << i1 << ", " << i2 << ") ";
198-
* }
199-
*
200-
* for (auto [i1, i2, c3] : zip(v1, v1, v2)) {
201-
* std::cout << "(" << i1 << ", " << i2 << ", " << c3 << ") ";
202-
* }
203-
* @endcode
192+
* @include doc_zip.cpp
204193
*
205194
* Output:
206195
*

doc/examples/CMakeLists.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
# We need GNU and c++23 for some std::ranges
1+
# we need GNU and c++23 for some std::ranges in the example ex1.cpp
22
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
33
add_executable(ex1 ex1.cpp)
44
target_link_libraries(ex1 itertools::itertools_c)
55
target_compile_features(ex1 PRIVATE cxx_std_23)
6-
endif()
6+
endif()
7+
8+
# all other example codes
9+
file(GLOB_RECURSE all_examples RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
10+
list(FILTER all_examples EXCLUDE REGEX "ex1.cpp")
11+
12+
foreach(ex_src ${all_examples})
13+
get_filename_component(ex_name ${ex_src} NAME_WE)
14+
add_executable(${ex_name} ${ex_src})
15+
target_link_libraries(${ex_name} itertools::itertools_c)
16+
endforeach()

doc/examples/doc_enumerate.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <itertools/itertools.hpp>
2+
#include <iostream>
3+
#include <vector>
4+
5+
int main() {
6+
std::vector<char> vec{'a', 'b', 'c'};
7+
for (auto [idx, val] : itertools::enumerate(vec)) std::cout << "(" << idx << ", " << val << ")\n";
8+
}

doc/examples/doc_for_each.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <itertools/itertools.hpp>
2+
#include <iostream>
3+
4+
int main() {
5+
// print out the first 10 squares
6+
itertools::foreach(itertools::range(1, 11), [](int i) { std::cout << i * i << " "; });
7+
std::cout << "\n";
8+
}

0 commit comments

Comments
 (0)