Skip to content

Commit c3e940c

Browse files
committed
algorithm/search_n : C++26波カッコ初期化に対応 #1311
1 parent f6a62c4 commit c3e940c

File tree

2 files changed

+191
-13
lines changed

2 files changed

+191
-13
lines changed

reference/algorithm/ranges_search_n.md

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* cpp20[meta cpp]
66

77
```cpp
8-
namespace std::ranges {]
8+
namespace std::ranges {
99
template <forward_iterator I,
1010
sentinel_for<I> S,
1111
class T,
@@ -19,6 +19,19 @@ namespace std::ranges {]
1919
const T& value,
2020
Pred pred = {},
2121
Proj proj = {}); // (1) C++20
22+
template <forward_iterator I,
23+
sentinel_for<I> S,
24+
class Pred = ranges::equal_to,
25+
class Proj = identity,
26+
class T = projected_value_t<I, Proj>>
27+
requires indirectly_comparable<I, const T*, Pred, Proj>
28+
constexpr subrange<I>
29+
search_n(I first,
30+
S last,
31+
iter_difference_t<I> count,
32+
const T& value,
33+
Pred pred = {},
34+
Proj proj = {}); // (1) C++26
2235

2336
template <forward_range R,
2437
class T,
@@ -31,6 +44,17 @@ namespace std::ranges {]
3144
const T& value,
3245
Pred pred = {},
3346
Proj proj = {}); // (2) C++20
47+
template <forward_range R,
48+
class Pred = ranges::equal_to,
49+
class Proj = identity,
50+
class T = projected_value_t<iterator_t<R>, Proj>>
51+
requires indirectly_comparable<iterator_t<R>, const T*, Pred, Proj>
52+
constexpr borrowed_subrange_t<R>
53+
search_n(R&& r,
54+
range_difference_t<R> count,
55+
const T& value,
56+
Pred pred = {},
57+
Proj proj = {}); // (2) C++26
3458
}
3559
```
3660
* forward_iterator[link /reference/iterator/forward_iterator.md]
@@ -61,7 +85,17 @@ namespace std::ranges {]
6185
最大で `last - first` 回の対応する比較もしくは述語が適用される。
6286
6387
88+
## 備考
89+
- (1), (2) :
90+
- C++26 : パラメータ`value`として波カッコ初期化`{}`を受け付ける
91+
```cpp
92+
std::vector<T> v;
93+
auto sr = std::ranges::search_n(v, 3, {a, b});
94+
```
95+
96+
6497
## 例
98+
### 基本的な使い方
6599
```cpp example
66100
#include <algorithm>
67101
#include <iostream>
@@ -90,14 +124,49 @@ int main() {
90124
}
91125
```
92126
* std::ranges::search_n[color ff0000]
93-
* v.begin()[link /reference/vector/vector/begin.md]
94127

95-
### 出力
128+
#### 出力
96129
```
97130
found: index==5
98131
found: index==0
99132
```
100133

134+
### 波カッコ初期化を入力として使用する (C++26)
135+
```cpp example
136+
#include <algorithm>
137+
#include <iostream>
138+
#include <vector>
139+
140+
struct Point {
141+
int x;
142+
int y;
143+
144+
bool operator==(const Point& other) const = default;
145+
};
146+
147+
int main() {
148+
std::vector<Point> v = {
149+
{1, 2},
150+
{3, 4},
151+
{3, 4},
152+
{5, 6},
153+
};
154+
155+
// 値が {3, 4} が2回連続している場所を探す
156+
std::ranges::subrange sr = std::ranges::search_n(v, 2, {3, 4});
157+
if (!sr.empty()) {
158+
int index = std::distance(v.begin(), sr.begin());
159+
std::cout << "found: index==" << index << std::endl;
160+
}
161+
}
162+
```
163+
* std::ranges::search_n[color ff0000]
164+
165+
#### 出力
166+
```
167+
found: index==1
168+
```
169+
101170
102171
## 実装例
103172
```cpp
@@ -163,3 +232,5 @@ inline constexpr search_n_impl search_n;
163232

164233
## 参照
165234
- [N4861 25 Algorithms library](https://timsong-cpp.github.io/cppwp/n4861/algorithms)
235+
- [P2248R8 Enabling list-initialization for algorithms](https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2248r8.html)
236+
- C++26で波カッコ初期化 (リスト初期化) に対応した

reference/algorithm/search_n.md

Lines changed: 117 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,87 @@
55

66
```cpp
77
namespace std {
8-
template<class ForwardIterator, class Size, class T>
8+
template <class ForwardIterator,
9+
class Size,
10+
class T>
911
ForwardIterator
1012
search_n(ForwardIterator first,
1113
ForwardIterator last,
1214
Size count,
1315
const T& value); // (1) C++03
14-
15-
template<class ForwardIterator, class Size, class T>
16+
template <class ForwardIterator,
17+
class Size,
18+
class T>
1619
constexpr ForwardIterator
1720
search_n(ForwardIterator first,
1821
ForwardIterator last,
1922
Size count,
2023
const T& value); // (1) C++20
24+
template <class ForwardIterator,
25+
class Size,
26+
class T = typename iterator_traits<ForwardIterator>::value_type>
27+
constexpr ForwardIterator
28+
search_n(ForwardIterator first,
29+
ForwardIterator last,
30+
Size count,
31+
const T& value); // (1) C++26
2132

22-
template<class ForwardIterator, class Size, class T, class BinaryPredicate>
33+
template <class ForwardIterator,
34+
class Size,
35+
class T,
36+
class BinaryPredicate>
2337
ForwardIterator
2438
search_n(ForwardIterator first,
2539
ForwardIterator last,
2640
Size count,
2741
const T& value,
2842
BinaryPredicate pred); // (2) C++03
29-
30-
template<class ForwardIterator, class Size, class T, class BinaryPredicate>
43+
template <class ForwardIterator,
44+
class Size,
45+
class T,
46+
class BinaryPredicate>
3147
constexpr ForwardIterator
3248
search_n(ForwardIterator first,
3349
ForwardIterator last,
3450
Size count,
3551
const T& value,
3652
BinaryPredicate pred); // (2) C++20
53+
template <class ForwardIterator,
54+
class Size,
55+
class T = typename iterator_traits<ForwardIterator>::value_type,
56+
class BinaryPredicate>
57+
constexpr ForwardIterator
58+
search_n(ForwardIterator first,
59+
ForwardIterator last,
60+
Size count,
61+
const T& value,
62+
BinaryPredicate pred); // (2) C++26
3763

38-
template <class ExecutionPolicy, class ForwardIterator, class Size, class T>
64+
template <class ExecutionPolicy,
65+
class ForwardIterator,
66+
class Size,
67+
class T>
3968
ForwardIterator
4069
search_n(ExecutionPolicy&& exec,
4170
ForwardIterator first,
4271
ForwardIterator last,
4372
Size count,
4473
const T& value); // (3) C++17
74+
template <class ExecutionPolicy,
75+
class ForwardIterator,
76+
class Size,
77+
class T = typename iterator_traits<ForwardIterator>::value_type>
78+
ForwardIterator
79+
search_n(ExecutionPolicy&& exec,
80+
ForwardIterator first,
81+
ForwardIterator last,
82+
Size count,
83+
const T& value); // (3) C++26
4584

46-
template <class ExecutionPolicy, class ForwardIterator, class Size, class T,
85+
template <class ExecutionPolicy,
86+
class ForwardIterator,
87+
class Size,
88+
class T,
4789
class BinaryPredicate>
4890
ForwardIterator
4991
search_n(ExecutionPolicy&& exec,
@@ -52,14 +94,31 @@ namespace std {
5294
Size count,
5395
const T& value,
5496
BinaryPredicate pred); // (4) C++17
97+
template <class ExecutionPolicy,
98+
class ForwardIterator,
99+
class Size,
100+
class T = typename iterator_traits<ForwardIterator>::value_type,
101+
class BinaryPredicate>
102+
ForwardIterator
103+
search_n(ExecutionPolicy&& exec,
104+
ForwardIterator first,
105+
ForwardIterator last,
106+
Size count,
107+
const T& value,
108+
BinaryPredicate pred); // (4) C++26
55109
}
56110
```
57111
58112
## 概要
59113
あるシーケンスの中から、特定のサブシーケンスを探す。
60114
115+
- (1): イテレータ範囲`[first, last)`から、値`value`が`count`回連続しているサブシーケンスを検索する
116+
- (2): イテレータ範囲`[first, last)`から、`pred(*it, value) == true`となる要素が`count`回連続しているサブシーケンスを検索する
117+
- (3): 実行ポリシーにしたがって、イテレータ範囲`[first, last)`から、値`value`が`count`回連続しているサブシーケンスを検索する
118+
- (4): 実行ポリシーにしたがって、イテレータ範囲`[first, last)`から、`pred(*it, value) == true`となる要素が`count`回連続しているサブシーケンスを検索する
61119
62-
## 要件
120+
121+
## 適格要件
63122
`Size`は整数型に変換できる型である必要がある。
64123
65124
@@ -73,7 +132,17 @@ namespace std {
73132
最大で `last - first` 回の対応する比較もしくは述語が適用される。
74133
75134
135+
## 備考
136+
- (1), (2), (3), (4) :
137+
- C++26 : パラメータ`value`として波カッコ初期化`{}`を受け付ける
138+
```cpp
139+
std::vector<T> v;
140+
auto it = std::search_n(v.begin(), v.begin(), 3, {a, b});
141+
```
142+
143+
76144
## 例
145+
### 基本的な使い方
77146
```cpp example
78147
#include <algorithm>
79148
#include <iostream>
@@ -105,12 +174,48 @@ int main() {
105174
* v.cbegin()[link /reference/vector/vector/cbegin.md]
106175
* v.cend()[link /reference/vector/vector/cend.md]
107176

108-
### 出力
177+
#### 出力
109178
```
110179
found: index==5
111180
found: index==0
112181
```
113182

183+
### 波カッコ初期化を入力として使用する (C++26)
184+
```cpp example
185+
#include <algorithm>
186+
#include <iostream>
187+
#include <vector>
188+
189+
struct Point {
190+
int x;
191+
int y;
192+
193+
bool operator==(const Point& other) const = default;
194+
};
195+
196+
int main() {
197+
std::vector<Point> v = {
198+
{1, 2},
199+
{3, 4},
200+
{3, 4},
201+
{5, 6},
202+
};
203+
204+
// 値が {3, 4} が2回連続している場所を探す
205+
auto it = std::search_n(v.begin(), v.end(), 2, {3, 4});
206+
if (it != v.end()) {
207+
int index = std::distance(v.begin(), it);
208+
std::cout << "found: index==" << index << std::endl;
209+
}
210+
}
211+
```
212+
* std::search_n[color ff0000]
213+
214+
#### 出力
215+
```
216+
found: index==1
217+
```
218+
114219
115220
## 実装例
116221
```cpp
@@ -171,3 +276,5 @@ ForwardIterator search_n(ForwardIterator first, ForwardIterator last,
171276
- [LWG Issue 714. `search_n` complexity is too lax](http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#714)
172277
- [LWG Issue 2150. Unclear specification of `find_end`](http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2150)
173278
- [P0202R3 Add Constexpr Modifiers to Functions in `<algorithm>` and `<utility>` Headers](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0202r3.html)
279+
- [P2248R8 Enabling list-initialization for algorithms](https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2248r8.html)
280+
- C++26で波カッコ初期化 (リスト初期化) に対応した

0 commit comments

Comments
 (0)