Created
September 24, 2024 17:26
-
-
Save flomnes/5ca35a28b8037e9e0ed7f84900cd7b8d to your computer and use it in GitHub Desktop.
hashable factorization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <utility> | |
class Hashable | |
{ | |
public: | |
Hashable(const std::string& s1, const std::string& s2): | |
s1(s1), | |
s2(s2) | |
{ | |
} | |
bool operator==(const Hashable& other) const | |
{ | |
return false; | |
} | |
public: | |
const std::string& s1; | |
const std::string& s2; | |
}; | |
template<int N> | |
class Derived: public Hashable | |
{ | |
public: | |
Derived(std::string name, const std::string field): | |
name(name), | |
field(field), | |
Hashable(name, field) | |
{ | |
} | |
private: | |
std::string name, field; | |
}; | |
struct Hash | |
{ | |
std::size_t operator()(const Hashable& n) const | |
{ | |
return n.s1.size() ^ 0xff121 + n.s2.size(); | |
} | |
}; | |
#include <cassert> | |
#include <unordered_map> | |
template<int N> | |
bool f() | |
{ | |
using MapT = std::unordered_map<Derived<N>, int, Hash>; | |
MapT my_map; | |
my_map[{"port", "field"}] = 11; | |
return my_map.at(Derived<N>("port", "field")) == 1; | |
} | |
int main() | |
{ | |
assert(f<1>() && f<2>()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment