Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8fcbda3

Browse files
committed
New issue from Hewill: "boyer_moore_searcher and boyer_moore_horspool_searcher should be constexpr-friendly"
1 parent 3b16057 commit 8fcbda3

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

xml/issue4365.xml

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?xml version='1.0' encoding='utf-8' standalone='no'?>
2+
<!DOCTYPE issue SYSTEM "lwg-issue.dtd">
3+
4+
<issue num="4365" status="New">
5+
<title>`boyer_moore_searcher` and `boyer_moore_horspool_searcher` should be `constexpr`-friendly</title>
6+
<section>
7+
<sref ref="[func.search.bm]"/><sref ref="[func.search.bmh]"/>
8+
</section>
9+
<submitter>Hewill Kang</submitter>
10+
<date>03 Sep 2025</date>
11+
<priority>99</priority>
12+
13+
<discussion>
14+
<p>
15+
Currently, `boyer_moore_searcher` and `boyer_moore_horspool_searcher` are not
16+
`constexpr`-friendly because their underlying implementation needs to precompute
17+
the shift table, which usually requires a `vector` or `unordered_map` to store.
18+
<p/>
19+
Thanks to <paper num="P3372R3"/>, unordered containers are now `constexpr`-friendly.
20+
Although `std::hash` still lacks `constexpr` support, users can provide their own
21+
hash functions to use unordered containers at compile time.
22+
<p/>
23+
Given that both `boyer_moore_searcher` and `boyer_moore_horspool_searcher` can
24+
take a custom hash, it makes perfect sense that they could be `constexpr`-friendly.
25+
<p/>
26+
Not to mention that library implementations usually simply use arrays instead of
27+
hash tables for the common string case because characters only have 256 values,
28+
so `unordered_map` is not actually used.
29+
</p>
30+
</discussion>
31+
32+
<resolution>
33+
<p>
34+
This wording is relative to <paper num="N5014"/>.
35+
</p>
36+
37+
<ol>
38+
39+
<li><p>Modify <sref ref="[func.search.bm]"/> as indicated:</p>
40+
41+
<blockquote>
42+
<blockquote>
43+
<pre>
44+
namespace std {
45+
template&lt;class RandomAccessIterator1,
46+
class Hash = hash&lt;typename iterator_traits&lt;RandomAccessIterator1&gt;::value_type&gt;,
47+
class BinaryPredicate = equal_to&lt;&gt;&gt;
48+
class boyer_moore_searcher {
49+
public:
50+
<ins>constexpr</ins> boyer_moore_searcher(RandomAccessIterator1 pat_first,
51+
RandomAccessIterator1 pat_last,
52+
Hash hf = Hash(),
53+
BinaryPredicate pred = BinaryPredicate());
54+
55+
template&lt;class RandomAccessIterator2&gt;
56+
<ins>constexpr</ins> pair&lt;RandomAccessIterator2, RandomAccessIterator2&gt;
57+
operator()(RandomAccessIterator2 first, RandomAccessIterator2 last) const;
58+
59+
private:
60+
RandomAccessIterator1 pat_first_; // <i>exposition only</i>
61+
RandomAccessIterator1 pat_last_; // <i>exposition only</i>
62+
Hash hash_; // <i>exposition only</i>
63+
BinaryPredicate pred_; // <i>exposition only</i>
64+
};
65+
}
66+
</pre>
67+
</blockquote>
68+
<pre>
69+
<ins>constexpr</ins> boyer_moore_searcher(RandomAccessIterator1 pat_first,
70+
RandomAccessIterator1 pat_last,
71+
Hash hf = Hash(),
72+
BinaryPredicate pred = BinaryPredicate());
73+
</pre>
74+
<blockquote>
75+
<p>
76+
-1- <i>Preconditions</i>: The value type of <code>RandomAccessIterator1</code> meets the
77+
<i>Cpp17DefaultConstructible</i>, <i>Cpp17CopyConstructible</i>, and <i>Cpp17CopyAssignable</i>
78+
requirements.
79+
</p>
80+
</blockquote>
81+
<blockquote>
82+
[&hellip;]
83+
</blockquote>
84+
<pre>
85+
template&lt;class RandomAccessIterator2&gt;
86+
<ins>constexpr</ins> pair&lt;RandomAccessIterator2, RandomAccessIterator2&gt;
87+
operator()(RandomAccessIterator2 first, RandomAccessIterator2 last) const;
88+
</pre>
89+
<blockquote>
90+
<p>
91+
-5- <i>Mandates</i>: <code>RandomAccessIterator1</code> and <code>RandomAccessIterator2</code>
92+
have the same value type.
93+
</p>
94+
</blockquote>
95+
</blockquote>
96+
97+
</li>
98+
99+
<li><p>Modify <sref ref="[func.search.bmh]"/> as indicated:</p>
100+
101+
<blockquote>
102+
<blockquote>
103+
<pre>
104+
namespace std {
105+
template&lt;class RandomAccessIterator1,
106+
class Hash = hash&lt;typename iterator_traits&lt;RandomAccessIterator1&gt;::value_type&gt;,
107+
class BinaryPredicate = equal_to&lt;&gt;&gt;
108+
class boyer_moore_horspool_searcher {
109+
public:
110+
<ins>constexpr</ins> boyer_moore_horspool_searcher(RandomAccessIterator1 pat_first,
111+
RandomAccessIterator1 pat_last,
112+
Hash hf = Hash(),
113+
BinaryPredicate pred = BinaryPredicate());
114+
115+
template&lt;class RandomAccessIterator2&gt;
116+
<ins>constexpr</ins> pair&lt;RandomAccessIterator2, RandomAccessIterator2&gt;
117+
operator()(RandomAccessIterator2 first, RandomAccessIterator2 last) const;
118+
119+
private:
120+
RandomAccessIterator1 pat_first_; // <i>exposition only</i>
121+
RandomAccessIterator1 pat_last_; // <i>exposition only</i>
122+
Hash hash_; // <i>exposition only</i>
123+
BinaryPredicate pred_; // <i>exposition only</i>
124+
};
125+
}
126+
</pre>
127+
</blockquote>
128+
<pre>
129+
<ins>constexpr</ins> boyer_moore_horspool_searcher(RandomAccessIterator1 pat_first,
130+
RandomAccessIterator1 pat_last,
131+
Hash hf = Hash(),
132+
BinaryPredicate pred = BinaryPredicate());
133+
</pre>
134+
<blockquote>
135+
<p>
136+
-1- <i>Preconditions</i>: The value type of <code>RandomAccessIterator1</code> meets the
137+
<i>Cpp17DefaultConstructible</i>, <i>Cpp17CopyConstructible</i>, and <i>Cpp17CopyAssignable</i>
138+
requirements.
139+
</p>
140+
</blockquote>
141+
<blockquote>
142+
[&hellip;]
143+
</blockquote>
144+
<pre>
145+
template&lt;class RandomAccessIterator2&gt;
146+
<ins>constexpr</ins> pair&lt;RandomAccessIterator2, RandomAccessIterator2&gt;
147+
operator()(RandomAccessIterator2 first, RandomAccessIterator2 last) const;
148+
</pre>
149+
<blockquote>
150+
<p>
151+
-5- <i>Mandates</i>: <code>RandomAccessIterator1</code> and <code>RandomAccessIterator2</code>
152+
have the same value type.
153+
</p>
154+
</blockquote>
155+
</blockquote>
156+
157+
</li>
158+
159+
</ol></resolution>
160+
161+
</issue>

0 commit comments

Comments
 (0)