Skip to content

Commit 00aa8c4

Browse files
committed
define more comparisons
1 parent c61422b commit 00aa8c4

2 files changed

Lines changed: 12 additions & 19 deletions

File tree

src/stringmap.hpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,14 @@ struct string_hash {
2626

2727
struct string_eq {
2828
using is_transparent = void;
29-
// NOTE: The reason why std::string& comes first is because the key type of
30-
// the map is std::string. (I couldn't figure out where this is specified,
31-
// but looking at the implementation of std::unordered_map this seems to be
32-
// the case.)
33-
bool operator()(const std::string& a, const std::string& b) const {
34-
return a == b;
35-
}
36-
bool operator()(const std::string& a, std::string_view b) const {
37-
return a == b;
38-
}
39-
bool operator()(const std::string& a, ObjString* b) const {
40-
return a == b->value;
29+
30+
static std::string_view to_view(const std::string& s) { return s; }
31+
static std::string_view to_view(std::string_view s) { return s; }
32+
static std::string_view to_view(ObjString* obj) { return obj->value; }
33+
34+
template <typename T, typename U>
35+
bool operator()(const T& t, const U& u) const {
36+
return to_view(t) == to_view(u);
4137
}
4238
};
4339

test.lox

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
fun bench() {
2-
var result = 0;
3-
for (var i = 0; i < 10000000; i = i + 1) {
4-
result = result + 1;
5-
}
6-
return result;
1+
var result = 0;
2+
for (var i = 0; i < 10000000; i = i + 1) {
3+
result = result + 1;
74
}
8-
print bench();
5+
print result;

0 commit comments

Comments
 (0)