Skip to content

Commit f9da4ed

Browse files
committed
Fix: Composing STL collections
1 parent ff23c3d commit f9da4ed

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

include/stringzilla/stringzilla.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,7 +1971,7 @@ class basic_string_slice {
19711971
#pragma endregion
19721972

19731973
/** @brief Hashes the string, equivalent to `std::hash<string_view>{}(str)`. */
1974-
size_type hash(std::uint64_t seed = 42) const noexcept {
1974+
size_type hash(std::uint64_t seed = 0) const noexcept {
19751975
return static_cast<size_type>(sz_hash(start_, length_, static_cast<sz_u64_t>(seed)));
19761976
}
19771977

@@ -3759,10 +3759,10 @@ bool basic_string<char_type_, allocator_>::try_preparing_replacement( //
37593759
* @see Similar to `std::less<std::string_view>`: https://en.cppreference.com/w/cpp/utility/functional/less
37603760
*
37613761
* Unlike the STL analog, doesn't require C++14 or including the heavy `<functional>` header.
3762-
* Can be used to combine STL classes with StringZilla logic, like: `std::map<std::string, int, sz::string_view_less>`.
3762+
* Can be used to combine STL classes with StringZilla logic, like: `std::map<std::string, int, sz::less>`.
37633763
*/
3764-
struct string_view_less {
3765-
bool operator()(string_view a, string_view b) const noexcept { return a < b; }
3764+
struct less {
3765+
inline bool operator()(string_view a, string_view b) const noexcept { return a < b; }
37663766
};
37673767

37683768
/**
@@ -3771,10 +3771,10 @@ struct string_view_less {
37713771
*
37723772
* Unlike the STL analog, doesn't require C++14 or including the heavy `<functional>` header.
37733773
* Can be used to combine STL classes with StringZilla logic, like:
3774-
* `std::unordered_map<std::string, int, sz::string_view_hash, sz::string_view_equal_to>`.
3774+
* `std::unordered_map<std::string, int, sz::hash, sz::equal_to>`.
37753775
*/
3776-
struct string_view_equal_to {
3777-
bool operator()(string_view a, string_view b) const noexcept { return a == b; }
3776+
struct equal_to {
3777+
inline bool operator()(string_view a, string_view b) const noexcept { return a == b; }
37783778
};
37793779

37803780
/**
@@ -3783,10 +3783,10 @@ struct string_view_equal_to {
37833783
*
37843784
* Unlike the STL analog, doesn't require C++14 or including the heavy `<functional>` header.
37853785
* Can be used to combine STL classes with StringZilla logic, like:
3786-
* `std::unordered_map<std::string, int, sz::string_view_hash, sz::string_view_equal_to>`.
3786+
* `std::unordered_map<std::string, int, sz::hash, sz::equal_to>`.
37873787
*/
3788-
struct string_view_hash {
3789-
std::size_t operator()(string_view str) const noexcept { return str.hash(); }
3788+
struct hash {
3789+
inline std::size_t operator()(string_view str) const noexcept { return str.hash(); }
37903790
};
37913791

37923792
/** @brief SFINAE-type used to infer the resulting type of concatenating multiple string together. */

scripts/bench_container.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,23 @@ void bench_tokens(strings_type const &strings) {
5151
auto const &s = strings;
5252

5353
// StringZilla structures
54-
bench<std::map<sz::string, int>>("map<sz::string>", s);
55-
bench<std::map<sz::string_view, int>>("map<sz::string_view>", s);
56-
bench<std::unordered_map<sz::string, int>>("unordered_map<sz::string>", s);
57-
bench<std::unordered_map<sz::string_view, int>>("unordered_map<sz::string_view>", s);
54+
bench<std::map<sz::string, int>>("std::map<sz::string>", s);
55+
bench<std::map<sz::string_view, int>>("std::map<sz::string_view>", s);
56+
bench<std::unordered_map<sz::string, int>>("std::umap<sz::string>", s);
57+
bench<std::unordered_map<sz::string_view, int>>("std::umap<sz::string_view>", s);
5858

5959
// Pure STL
60-
bench<std::map<std::string, int>>("map<std::string>", s);
61-
bench<std::map<std::string_view, int>>("map<std::string_view>", s);
62-
bench<std::unordered_map<std::string, int>>("unordered_map<std::string>", s);
63-
bench<std::unordered_map<std::string_view, int>>("unordered_map<std::string_view>", s);
60+
bench<std::map<std::string, int>>("std::map<std::string>", s);
61+
bench<std::map<std::string_view, int>>("std::map<std::string_view>", s);
62+
bench<std::unordered_map<std::string, int>>("std::umap<std::string>", s);
63+
bench<std::unordered_map<std::string_view, int>>("std::umap<std::string_view>", s);
6464

6565
// STL structures with StringZilla operations
66-
// bench<std::map<std::string, int, sz::less>>("map<std::string>", s);
67-
// bench<std::map<std::string_view, int, sz::less>>("map<std::string_view>", s);
68-
// bench<std::unordered_map<std::string, int, sz::hash, sz::equal_to>>("unordered_map<std::string>", s);
69-
// bench<std::unordered_map<std::string_view, int, sz::hash, sz::equal_to>>("unordered_map<std::string_view>", s);
66+
bench<std::map<std::string, int, sz::less>>("std::map<std::string, sz::less>", s);
67+
bench<std::map<std::string_view, int, sz::less>>("std::map<std::string_view, sz::less>", s);
68+
bench<std::unordered_map<std::string, int, sz::hash, sz::equal_to>>("std::umap<std::string, sz::hash>", s);
69+
bench<std::unordered_map<std::string_view, int, sz::hash, sz::equal_to>>("std::umap<std::string_view, sz::hash>",
70+
s);
7071
}
7172

7273
int main(int argc, char const **argv) {
@@ -77,6 +78,8 @@ int main(int argc, char const **argv) {
7778
// Baseline benchmarks for real words, coming in all lengths
7879
std::printf("Benchmarking on real words:\n");
7980
bench_tokens(dataset.tokens);
81+
std::printf("Benchmarking on real lines:\n");
82+
bench_tokens(dataset.lines);
8083

8184
// Run benchmarks on tokens of different length
8285
for (std::size_t token_length : {1, 2, 3, 4, 5, 6, 7, 8, 16, 32}) {

scripts/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,8 +1915,8 @@ static void test_stl_containers() {
19151915
assert(sorted_words_sz.empty());
19161916
assert(words_sz.empty());
19171917

1918-
std::map<std::string, int, sz::string_view_less> sorted_words_stl;
1919-
std::unordered_map<std::string, int, sz::string_view_hash, sz::string_view_equal_to> words_stl;
1918+
std::map<std::string, int, sz::less> sorted_words_stl;
1919+
std::unordered_map<std::string, int, sz::hash, sz::equal_to> words_stl;
19201920
assert(sorted_words_stl.empty());
19211921
assert(words_stl.empty());
19221922
}

0 commit comments

Comments
 (0)