git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: simplify maps
commit55df68c1f2cd2f280e676b02a69b6d687a99bdb8 authorAndy Arvanitis <andyarvanitis@gmail.com> dateSun 20 Dec 2015, 15:26 (11 years ago) parent24f73ba
6 files changed+11−100
pcc/Make.hs−2 pcc/include/hash.hh−42 pcc/include/map_key.hh−46 pcc/include/purescript.cc+1−1 pcc/include/purescript.hh+5−5 src/Language/PureScript/Pretty/Cpp.hs+5−4
pcc/Make.hs−2
@@ -122,8 +122,6 @@ buildMakeActions outputDir filePathMap usePrefix =
122122 writeTextFile (outputDir </> "CMakeLists.txt") (fromString cmakeListsTxt)
123123 writeTextFile (supportDir </> "PureScript.hh") $ B.unpack $(embedFile "pcc/include/purescript.hh")
124124 writeTextFile (supportDir </> "PureScript.cc") $ B.unpack $(embedFile "pcc/include/purescript.cc")
125− writeTextFile (supportDir </> "map_key.hh") $ B.unpack $(embedFile "pcc/include/map_key.hh")
126− writeTextFile (supportDir </> "hash.hh") $ B.unpack $(embedFile "pcc/include/hash.hh")
127125
128126 let inputPath = dropExtension $ getInputFile mn
129127 hfile = addExtension inputPath "hh"
pcc/include/hash.hhdeleted−42
@@ -1,42 +0,0 @@
1−///////////////////////////////////////////////////////////////////////////////
2−//
3−// Module : hash.hh
4−// Copyright : (c) Andy Arvanitis 2015
5−// License : MIT
6−//
7−// Maintainer : Andy Arvanitis <andy.arvanitis@gmail.com>
8−// Stability : experimental
9−// Portability :
10−//
11−// Provides compile-time hashes for string literals.
12−//
13−///////////////////////////////////////////////////////////////////////////////
14−//
15−#ifndef PS_Hash_HH
16−#define PS_Hash_HH
17−
18−#include <cstdint>
19−
20−namespace PureScript {
21−
22− // Hash function which uses an xor version of the djb2 algorithm
23− //
24− constexpr auto djb2(const char s[], const uint32_t hash = 5381) -> uint32_t {
25− return !s[0] ? hash : djb2(s + 1, 33 * hash ^ s[0]);
26− }
27−
28− // Compile-time string literal hash
29− //
30− template <uint32_t H>
31− struct _compile_time_hash_ {
32− static constexpr long value = H;
33− };
34−
35− template <uint32_t H>
36− constexpr long _compile_time_hash_<H>::value;
37−
38− #define HASH(s) _compile_time_hash_<djb2(s)>::value
39−
40−} // namespace PureScript
41−
42−#endif // PS_Hash_HH
pcc/include/map_key.hhdeleted−46
@@ -1,46 +0,0 @@
1−///////////////////////////////////////////////////////////////////////////////
2−//
3−// Module : map_key.hh
4−// Copyright : (c) Andy Arvanitis 2015
5−// License : MIT
6−//
7−// Maintainer : Andy Arvanitis <andy.arvanitis@gmail.com>
8−// Stability : experimental
9−// Portability :
10−//
11−// Support for maps using keys with compile-time hashes.
12−//
13−///////////////////////////////////////////////////////////////////////////////
14−//
15−#ifndef PS_MapKey_HH
16−#define PS_MapKey_HH
17−
18−#include <utility>
19−#include "hash.hh"
20−
21−namespace PureScript {
22−
23− class map_key_t : public std::pair<uint32_t, std::string> {
24− public:
25− using std::pair<uint32_t, std::string>::pair;
26−
27− class hasher {
28− public:
29− auto operator() (const map_key_t& key) const -> size_t {
30− return key.first;
31− }
32− };
33−
34− class equal {
35− public:
36− auto operator() (const map_key_t& key1, const map_key_t& key2) const -> bool {
37− return key1.second == key2.second;
38− }
39− };
40− };
41−
42− #define KEY(s) map_key_t(HASH(s), s)
43−
44−} // namespace PureScript
45−
46−#endif // PS_MapKey_HH
pcc/include/purescript.cc+1−1
@@ -215,7 +215,7 @@ any::~any() {
215215 } while (true);
216216 }
217217
218− auto any::operator[](const map_key_t rhs) const -> const any& {
218+ auto any::operator[](const char rhs[]) const -> const any& {
219219 RETURN_VALUE(Type::Map, m->at(rhs),)
220220 }
221221
pcc/include/purescript.hh+5−5
@@ -32,9 +32,6 @@
3232#include <unordered_map>
3333#include <stdexcept>
3434
35−#include "map_key.hh"
36−#include "hash.hh"
37−
3835namespace PureScript {
3936
4037using string = std::string;
@@ -43,6 +40,9 @@ using nullptr_t = std::nullptr_t;
4340
4441const bool undefined = false;
4542
43+// TODO: deprecated -- remove
44+#define KEY(k) k
45+
4646// A variant data class designed to provide some features of dynamic typing.
4747//
4848class any {
@@ -69,7 +69,7 @@ class any {
6969 };
7070 static constexpr as_thunk unthunk = as_thunk{};
7171
72− using map = std::unordered_map<map_key_t, const any, map_key_t::hasher, map_key_t::equal>;
72+ using map = std::unordered_map<std::string, const any>;
7373 using vector = std::vector<any>;
7474 using fn = std::function<any(const any&)>;
7575 using eff_fn = std::function<any()>;
@@ -256,7 +256,7 @@ class any {
256256 operator const map&() const;
257257 operator const vector&() const;
258258
259− auto operator[](const map_key_t) const -> const any&;
259+ auto operator[](const char []) const -> const any&;
260260 auto operator[](const vector::size_type) const -> const any&;
261261 auto operator[](const any&) const -> const any&;
262262
src/Language/PureScript/Pretty/Cpp.hs+5−4
@@ -76,7 +76,8 @@ literals = mkPattern' match
7676 , withIndent $ do
7777 cpps <- forM ps $ \(key, value) -> do
7878 val <- prettyPrintCpp' value
79− return $ "{ KEY" ++ parens(show key) ++ ", " ++ val ++ " }"
79+ k <- prettyPrintCpp' (CppStringLiteral key)
80+ return $ "{ " ++ k ++ ", " ++ val ++ " }"
8081 indentString <- currentIndent
8182 return $ intercalate ", \n" $ map (indentString ++) cpps
8283 , return "\n"
@@ -257,7 +258,7 @@ string s = '"' : concatMap encodeChar (decodeSurrogates s) ++ "\""
257258 where
258259 decodeSurrogates :: String -> String
259260 decodeSurrogates [c] = [c]
260− decodeSurrogates s = catMaybes $ zipWith decodePair s (tail s ++ ['\0'])
261+ decodeSurrogates s' = catMaybes $ zipWith decodePair s' (tail s' ++ ['\0'])
261262 where
262263 decodePair :: Char -> Char -> Maybe Char
263264 decodePair a b
@@ -295,8 +296,8 @@ accessor = mkPattern match
295296indexer :: Pattern PrinterState Cpp (String, Cpp)
296297indexer = mkPattern' match
297298 where
298− match (CppIndexer index@(CppNumericLiteral {}) val) = (,) <$> prettyPrintCpp' index <*> pure val
299− match (CppIndexer (CppStringLiteral index) val) = return ("KEY" ++ parens (show index), val)
299+ match (CppIndexer (CppNumericLiteral (Left index)) val) = return (show index ++ "UL", val)
300+ match (CppIndexer index val) = (,) <$> prettyPrintCpp' index <*> pure val
300301 match _ = mzero
301302
302303lam :: Pattern PrinterState Cpp ((String, [(String, Maybe CppType)], Maybe CppType), Cpp)