Skip to content

Commit 4711c4c

Browse files
author
Andrew Naplavkov
committedApr 14, 2023
clean up
1 parent 90b8335 commit 4711c4c

File tree

10 files changed

+49
-41
lines changed

10 files changed

+49
-41
lines changed
 

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Step20 is a C++20, header-only library of STL-like algorithms and data structure
2626
* [longest repeated substring](https://en.wikipedia.org/wiki/Longest_repeated_substring_problem):
2727
[snippet](https://github.com/storm-ptr/step20/blob/main/test/main.cpp#L366-L368)
2828
* [suffix array](https://en.wikipedia.org/wiki/Suffix_array):
29-
[snippet](https://github.com/storm-ptr/step20/blob/main/test/main.cpp#L443-L444)
29+
[snippet](https://github.com/storm-ptr/step20/blob/main/test/main.cpp#L447-L448)
3030
* [suffix tree](https://en.wikipedia.org/wiki/Suffix_tree):
31-
[snippet](https://github.com/storm-ptr/step20/blob/main/test/main.cpp#L472-L473),
31+
[snippet](https://github.com/storm-ptr/step20/blob/main/test/main.cpp#L476-L477),
3232
[example](https://github.com/storm-ptr/step20/blob/main/example/suffix_tree_viz/suffix_tree_viz.hpp#L16-L42)

‎detail/utility.hpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,28 @@ auto try_reverse(std::ranges::borrowed_range auto&& range)
5252
}
5353

5454
template <class Container>
55-
class insert_iterator {
55+
class emplace_iterator {
5656
Container* c_;
5757

5858
public:
5959
using iterator_category = std::output_iterator_tag;
6060
using difference_type = std::ptrdiff_t;
6161
using value_type = void;
6262
using reference = void;
63-
explicit constexpr insert_iterator(Container& c) : c_(std::addressof(c)) {}
64-
constexpr insert_iterator& operator*() { return *this; }
65-
constexpr insert_iterator& operator++() { return *this; }
66-
constexpr insert_iterator operator++(int) { return *this; }
63+
explicit constexpr emplace_iterator(Container& c) : c_(std::addressof(c)) {}
64+
constexpr emplace_iterator& operator*() { return *this; }
65+
constexpr emplace_iterator& operator++() { return *this; }
66+
constexpr emplace_iterator operator++(int) { return *this; }
6767

68-
constexpr insert_iterator& operator=(
69-
const typename Container::value_type& value)
68+
template <class T>
69+
constexpr emplace_iterator& operator=(T&& val)
7070
{
71-
if constexpr (requires { c_->insert(value); })
72-
c_->insert(value);
73-
else if constexpr (requires { c_->push(value); })
74-
c_->push(value);
75-
else if constexpr (requires { c_->push_back(value); })
76-
c_->push_back(value);
71+
if constexpr (requires { c_->emplace(std::forward<T>(val)); })
72+
c_->emplace(std::forward<T>(val));
73+
else if constexpr (requires { c_->emplace_back(std::forward<T>(val)); })
74+
c_->emplace_back(std::forward<T>(val));
75+
else if constexpr (requires { c_->push_back(std::forward<T>(val)); })
76+
c_->push_back(std::forward<T>(val)); //< std::basic_string
7777
else
7878
lazy_static_assert<false>();
7979
return *this;

‎example/diff/gcc.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
g++ -I ../../.. -std=c++20 -Wall -O2 -fmax-errors=1 main.cpp
2-
./a.out ./file1.txt ./file2.txt
3-
rm ./a.out
1+
${1:-g++} -I ../../.. -std=c++20 -Wall -O2 -o result main.cpp
2+
./result ./file1.txt ./file2.txt
3+
rm ./result

‎example/suffix_tree_viz/gcc.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
g++ -I ../../.. -std=c++20 -Wall -O2 -fmax-errors=1 main.cpp
2-
./a.out banana$
3-
rm ./a.out
1+
${1:-g++} -I ../../.. -std=c++20 -Wall -O2 -o result main.cpp
2+
./result banana$
3+
rm ./result

‎substring_search.hpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ namespace step20::substring_search {
1212

1313
/// Time complexity O(M*log(N)), where:
1414
/// M - substring length, N - text length.
15-
template <class... Ts, std::ranges::forward_range R>
16-
auto find_any(const suffix_array<Ts...>& arr, R&& str)
15+
template <class... Ts>
16+
auto find_any(const suffix_array<Ts...>& arr,
17+
std::ranges::forward_range auto&& str)
1718
-> std::optional<typename suffix_array<Ts...>::size_type>
1819
{
1920
if (std::ranges::empty(str))
@@ -24,8 +25,9 @@ auto find_any(const suffix_array<Ts...>& arr, R&& str)
2425
}
2526

2627
/// Find all occurrences of the substring.
27-
template <class... Ts, std::ranges::forward_range R>
28-
auto find_all(const suffix_array<Ts...>& arr, R&& str)
28+
template <class... Ts>
29+
auto find_all(const suffix_array<Ts...>& arr,
30+
std::basic_string<typename suffix_array<Ts...>::value_type> str)
2931
-> generator<typename suffix_array<Ts...>::size_type>
3032
{
3133
if (std::ranges::empty(str))
@@ -37,8 +39,9 @@ auto find_all(const suffix_array<Ts...>& arr, R&& str)
3739
/// Find offset of the first occurrence of the substring.
3840

3941
/// Time complexity O(M), where: M - substring length.
40-
template <class... Ts, std::ranges::forward_range R>
41-
auto find_first(const suffix_tree<Ts...>& tree, R&& str)
42+
template <class... Ts>
43+
auto find_first(const suffix_tree<Ts...>& tree,
44+
std::ranges::forward_range auto&& str)
4245
-> std::optional<typename suffix_tree<Ts...>::size_type>
4346
{
4447
if (auto edge = tree.find(str))
@@ -51,8 +54,9 @@ auto find_first(const suffix_tree<Ts...>& tree, R&& str)
5154
/// Suffix tree must be explicit - padded with a terminal symbol.
5255
/// Space complexity asymptotically close to O(log(N)), O(N) at worst,
5356
/// where: N - text length.
54-
template <class... Ts, std::ranges::forward_range R>
55-
auto find_all(const suffix_tree<Ts...>& tree, R&& str)
57+
template <class... Ts>
58+
auto find_all(const suffix_tree<Ts...>& tree,
59+
std::basic_string<typename suffix_tree<Ts...>::value_type> str)
5660
-> generator<typename suffix_tree<Ts...>::size_type>
5761
{
5862
if (std::ranges::empty(str))

‎suffix_array.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ class suffix_array {
4747
std::ranges::transform(sufs, pos_.begin(), &suffix::pos);
4848
}
4949

50-
explicit suffix_array(std::ranges::input_range auto&& r,
51-
const Compare& comp = {})
52-
: suffix_array(to<std::basic_string>(r), comp)
50+
template <std::ranges::input_range R>
51+
explicit suffix_array(R&& r, const Compare& comp = {})
52+
: suffix_array(to<std::basic_string>(std::forward<R>(r)), comp)
5353
{
5454
}
5555

‎suffix_tree.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class suffix_tree {
119119

120120
/// Space complexity asymptotically close to O(log(N)), O(N) at worst,
121121
/// where: N - text length.
122-
generator<edge_type> depth_first_search(const edge_type& start) const
122+
generator<edge_type> depth_first_search(edge_type start) const
123123
{
124124
for (auto stack = std::stack<edge_type>{{start}}; !stack.empty();) {
125125
auto edge = stack.top();
@@ -134,7 +134,7 @@ class suffix_tree {
134134
item.second,
135135
edge.labels_len + label(item.second).length()};
136136
}),
137-
insert_iterator(stack));
137+
emplace_iterator(stack));
138138
}
139139
}
140140

‎test/gcc.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
g++ -I ../.. -std=c++20 -Wall -O2 -fmax-errors=1 main.cpp
2-
./a.out
3-
rm ./a.out
1+
${1:-g++} -I ../.. -std=c++20 -Wall -O2 -o result main.cpp
2+
./result
3+
rm ./result

‎test/main.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,9 @@ void test_substring_search()
423423
else
424424
check(std::ranges::empty(expect));
425425
check(std::ranges::is_permutation(
426-
expect, to<std::vector>(substring_search::find_all(arr, substr))));
426+
expect,
427+
to<std::vector>(
428+
substring_search::find_all(arr, std::string{substr}))));
427429
auto tree = to<suffix_tree>(str);
428430
check(substring_search::find_first(tree, str) == 0);
429431
check(substring_search::find_first(tree, ""sv) == 0);
@@ -433,7 +435,9 @@ void test_substring_search()
433435
else
434436
check(std::ranges::empty(expect));
435437
check(std::ranges::is_permutation(
436-
expect, to<std::vector>(substring_search::find_all(tree, substr))));
438+
expect,
439+
to<std::vector>(
440+
substring_search::find_all(tree, std::string{substr}))));
437441
}
438442
}
439443

‎to.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ To to(From&& from)
1515
auto result = To{};
1616
if constexpr (requires { result.reserve(std::ranges::size(from)); })
1717
result.reserve(std::ranges::size(from));
18-
std::ranges::copy(from, insert_iterator(result));
18+
std::ranges::copy(from, emplace_iterator(result));
1919
return result;
2020
}
2121

2222
template <template <class...> class To, std::ranges::input_range From>
2323
auto to(From&& from)
2424
{
25-
return to<To<std::ranges::range_value_t<From>>>(from);
25+
return to<To<std::ranges::range_value_t<From>>>(std::forward<From>(from));
2626
}
2727

2828
} // namespace step20

0 commit comments

Comments
 (0)
Please sign in to comment.