Skip to content

Commit 231688f

Browse files
authored
Merge pull request #31 from neatudarius/docs_tweaks
Add direct iterator_interface examples in root docs
2 parents 7bae844 + a9455b6 commit 231688f

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,42 @@ Full runable examples can be found in `examples/` - please check [./examples/REA
3232
The next code snippet shows iterator interface support added in [`std::iterator_interface` (P2727R)](https://wg21.link/P2727R4): define a random access iterator that iterates over a sequence of characters repeated indefinitely.
3333

3434
```cpp
35+
#include <beman/iterator_interface/iterator_interface.hpp>
36+
37+
// repeated_chars_iterator uses iterator_interface to define a random access iterator
38+
// that iterates over a sequence of characters repeated indefinitely.
39+
class repeated_chars_iterator
40+
: public beman::iterator_interface::ext_iterator_interface_compat<repeated_chars_iterator,
41+
std::random_access_iterator_tag,
42+
char,
43+
char> {
44+
public:
45+
// Default constructor creates an end-of-range iterator.
46+
constexpr repeated_chars_iterator() : m_it_begin(nullptr), m_fixed_size(0), m_pos(0) {}
47+
48+
// Constructor for the beginning of the sequence.
49+
constexpr repeated_chars_iterator(const char* it_begin, difference_type size, difference_type n)
50+
: m_it_begin(it_begin), m_fixed_size(size), m_pos(n) {}
51+
52+
// Random access iterator requirements:
53+
constexpr auto operator*() const { return m_it_begin[m_pos % m_fixed_size]; }
54+
constexpr repeated_chars_iterator& operator+=(std::ptrdiff_t i) {
55+
m_pos += i;
56+
return *this;
57+
}
58+
constexpr auto operator-(repeated_chars_iterator other) const { return m_pos - other.m_pos; }
59+
60+
private:
61+
// Start of the sequence of characters.
62+
const char* m_it_begin;
63+
64+
// Number of characters in the sequence.
65+
difference_type m_fixed_size;
66+
67+
// Current position in the sequence.
68+
difference_type m_pos;
69+
};
70+
3571
// Create a repeated_chars_iterator that iterates over the sequence "foo" repeated indefinitely:
3672
// "foofoofoofoofoofoo...". Will actually extract a prefix of the sequence and insert it into a std::string.
3773
constexpr const std::string_view target = "foo";
@@ -52,6 +88,18 @@ std::cout << extracted_result << "\n"; // Expected
5288
The next code snippet shows iterator interface support added in [`std::iterator_interface` (P2727R4)](https://wg21.link/P2727R4): define a forward iterator that iterates over a sequence of integers, skipping those that do not satisfy a predicate.
5389
5490
```cpp
91+
#include <beman/iterator_interface/iterator_interface.hpp>
92+
93+
// filtered_int_iterator uses iterator_interface to define a forward iterator
94+
// that iterates over a sequence of integers, skipping those that do not satisfy a predicate.
95+
template <typename Pred>
96+
struct filtered_int_iterator
97+
: beman::iterator_interface::ext_iterator_interface_compat<filtered_int_iterator<Pred>,
98+
std::forward_iterator_tag,
99+
int> {
100+
// ...
101+
};
102+
55103
// Create a filtered_int_iterator that iterates over the sequence {1, 2, 3, 4, 10, 11, 101, 200, 0},
56104
// skipping odd numbers. 0 is not skipped, so it will be the last element in the sequence.
57105
std::array a = {1, 2, 3, 4, 10, 11, 101, 200, 0};

examples/filter_int_iterator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <array>
1111
#include <iostream>
1212

13-
// filtered_int_iterator uses std::iterator_interface to define a forward iterator
13+
// filtered_int_iterator uses iterator_interface to define a forward iterator
1414
// that iterates over a sequence of integers, skipping those that do not satisfy a predicate.
1515
template <typename Pred>
1616
struct filtered_int_iterator

examples/repeated_chars_iterator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <iostream>
1212
#include <string_view>
1313

14-
// repeated_chars_iterator uses std::iterator_interface to define a random access iterator
14+
// repeated_chars_iterator uses iterator_interface to define a random access iterator
1515
// that iterates over a sequence of characters repeated indefinitely.
1616
class repeated_chars_iterator
1717
: public beman::iterator_interface::ext_iterator_interface_compat<repeated_chars_iterator,

0 commit comments

Comments
 (0)