git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: add proper hash support for map_key_t
commit24f73baf1b7b117a0ed32ea02118b70225d5d004 authorAndy Arvanitis <andyarvanitis@gmail.com> dateMon 14 Dec 2015, 21:22 (11 years ago) parent5c6b90f
4 files changed+11−27
pcc/include/map_key.hh+9−20 src/Language/PureScript/CodeGen/Cpp.hs+2−2 src/Language/PureScript/CodeGen/Cpp/AST.hs−4 src/Language/PureScript/Pretty/Cpp.hs−1
pcc/include/map_key.hh+9−20
@@ -15,42 +15,31 @@
1515#ifndef PS_MapKey_HH
1616#define PS_MapKey_HH
1717
18+#include <utility>
1819#include "hash.hh"
1920
2021namespace PureScript {
2122
22− struct map_key_t {
23−
24− const uint32_t hash;
25−
26− explicit constexpr map_key_t(const uint32_t hash) : hash(hash) {}
23+ class map_key_t : public std::pair<uint32_t, std::string> {
24+ public:
25+ using std::pair<uint32_t, std::string>::pair;
2726
2827 class hasher {
2928 public:
30− constexpr auto operator() (const map_key_t key) const -> size_t {
31− return key.hash;
29+ auto operator() (const map_key_t& key) const -> size_t {
30+ return key.first;
3231 }
3332 };
3433
3534 class equal {
3635 public:
37− constexpr auto operator() (const map_key_t key1, const map_key_t key2) const -> bool {
38− return key1.hash == key2.hash;
36+ auto operator() (const map_key_t& key1, const map_key_t& key2) const -> bool {
37+ return key1.second == key2.second;
3938 }
4039 };
4140 };
4241
43− // Compile-time map keys (from strings)
44− //
45− template <uint32_t H>
46− struct _compile_time_key_ {
47− static constexpr map_key_t value = map_key_t(H);
48− };
49−
50− template <uint32_t H>
51− constexpr map_key_t _compile_time_key_<H>::value;
52−
53− #define KEY(s) _compile_time_key_<djb2(s)>::value
42+ #define KEY(s) map_key_t(HASH(s), s)
5443
5544} // namespace PureScript
5645
src/Language/PureScript/CodeGen/Cpp.hs+2−2
@@ -148,7 +148,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
148148 | (f' : fs') <- flds = [CppReturn $ CppLambda [CppCaptureAll] (farg <$> [f'])
149149 (Just $ CppAny [])
150150 (CppBlock $ fieldLambdas fs')]
151− | otherwise = [CppReturn (CppObjectLiteral ((ctorKey, CppStringLiteralHash name) : zip fields' (CppVar <$> fields')))]
151+ | otherwise = [CppReturn (CppObjectLiteral ((ctorKey, CppStringLiteral name) : zip fields' (CppVar <$> fields')))]
152152
153153 declToCpp _ ident val = do
154154 val' <- valueToCpp val
@@ -406,7 +406,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
406406 ProductType -> cpps
407407 SumType ->
408408 [ CppIfElse (CppBinary Equal (CppIndexer (CppStringLiteral ctorKey) (CppVar varName))
409− (CppStringLiteralHash ctor'))
409+ (CppStringLiteral ctor'))
410410 (CppBlock cpps)
411411 Nothing ]
412412 where
src/Language/PureScript/CodeGen/Cpp/AST.hs−4
@@ -249,10 +249,6 @@ data Cpp
249249 --
250250 | CppContinue
251251 -- |
252− -- A string literal compile-time hash
253− --
254− | CppStringLiteralHash String
255− -- |
256252 -- Empty statement/expression
257253 --
258254 | CppNoOp
src/Language/PureScript/Pretty/Cpp.hs−1
@@ -55,7 +55,6 @@ literals = mkPattern' match
5555 match (CppNumericLiteral n) = return $ either show show n
5656 match (CppStringLiteral s) | all isAscii s = return $ string s
5757 match (CppStringLiteral s) = return $ "u8" ++ string s
58− match (CppStringLiteralHash s) = return $ "HASH" ++ parens (string s)
5958 match (CppCharLiteral c) = return $ show c
6059 match (CppBooleanLiteral True) = return "true"
6160 match (CppBooleanLiteral False) = return "false"