git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: use std::map for records
16ad8b0Andy Arvanitis
pcc/runtime/purescript.cc+5−1
@@ -129,7 +129,11 @@ auto any::contains(const Private::Symbol key) const -> bool {
129 return false;
129 return false;
130}
130}
131
131
132+auto any::cstr_cmp::operator ()(const char * a, const char * b) const -> bool {
133+ return a == b ? false : std::strcmp(a, b) < 0;
134+}
135+
132−auto any::at(const std::string& key) const -> const any& {
136+auto any::at(const char * key) const -> const any& {
133 return static_cast<const record&>(*this).at(key);
137 return static_cast<const record&>(*this).at(key);
134}
138}
135
139
pcc/runtime/purescript.hh+30−4
@@ -36,7 +36,7 @@
36#include <string>
36#include <string>
37#include <array>
37#include <array>
38#include <deque>
38#include <deque>
39−#include <unordered_map>
39+#include <map>
40#include <utility>
40#include <utility>
41#include <stdexcept>
41#include <stdexcept>
42#include <iso646.h> // mostly for MS Visual Studio compiler
42#include <iso646.h> // mostly for MS Visual Studio compiler
@@ -119,7 +119,6 @@ class any {
119 static constexpr as_thunk unthunk = as_thunk{};
119 static constexpr as_thunk unthunk = as_thunk{};
120
120
121 using dict_pair = std::pair<const Private::Symbol, const any>;
121 using dict_pair = std::pair<const Private::Symbol, const any>;
122−
123 template <size_t N>
122 template <size_t N>
124 using dict = std::array<const dict_pair, N>;
123 using dict = std::array<const dict_pair, N>;
125
124
@@ -127,7 +126,14 @@ class any {
127 using data = std::array<const any, N>;
126 using data = std::array<const any, N>;
128
127
129 using array = std::deque<any WITH_ALLOCATOR(any)>;
128 using array = std::deque<any WITH_ALLOCATOR(any)>;
129+
130+ struct cstr_cmp {
131+ auto operator ()(const char *, const char *) const -> bool;
132+ };
133+ using record = std::map<const char *,
134+ any,
135+ cstr_cmp
130− using record = std::unordered_map<std::string, any WITH_ALLOCATOR_PAIR(std::string, any)>;
136+ WITH_ALLOCATOR_PAIR(const char *, any)>;
131
137
132 using fn = auto (*)(const any&) -> any;
138 using fn = auto (*)(const any&) -> any;
133 using eff_fn = auto (*)() -> any;
139 using eff_fn = auto (*)() -> any;
@@ -332,7 +338,7 @@ class any {
332 operator const record&() const;
338 operator const record&() const;
333
339
334 auto operator[](const size_t) const -> const any&;
340 auto operator[](const size_t) const -> const any&;
335− auto at(const std::string&) const -> const any&;
341+ auto at(const char *) const -> const any&;
336 auto size() const -> size_t;
342 auto size() const -> size_t;
337 auto empty() const -> bool;
343 auto empty() const -> bool;
338 auto contains(const Private::Symbol) const -> bool;
344 auto contains(const Private::Symbol) const -> bool;
@@ -411,6 +417,16 @@ namespace Private {
411 template <typename T, typename U=void>
417 template <typename T, typename U=void>
412 struct TagHelper {};
418 struct TagHelper {};
413
419
420+ template <>
421+ struct TagHelper<string> {
422+ static constexpr any::Tag tag = any::Tag::String;
423+ };
424+
425+ template <>
426+ struct TagHelper<const char *> {
427+ static constexpr any::Tag tag = any::Tag::String;
428+ };
429+
414 template <size_t N>
430 template <size_t N>
415 struct TagHelper<any::dict<N>> {
431 struct TagHelper<any::dict<N>> {
416 static constexpr any::Tag tag = any::Tag::Dictionary;
432 static constexpr any::Tag tag = any::Tag::Dictionary;
@@ -421,6 +437,16 @@ namespace Private {
421 static constexpr any::Tag tag = any::Tag::Data;
437 static constexpr any::Tag tag = any::Tag::Data;
422 };
438 };
423
439
440+ template <>
441+ struct TagHelper<any::array> {
442+ static constexpr any::Tag tag = any::Tag::Array;
443+ };
444+
445+ template <>
446+ struct TagHelper<any::record> {
447+ static constexpr any::Tag tag = any::Tag::Record;
448+ };
449+
424 template <typename T>
450 template <typename T>
425 struct TagHelper<T> {
451 struct TagHelper<T> {
426 static constexpr any::Tag tag = any::Tag::Pointer;
452 static constexpr any::Tag tag = any::Tag::Pointer;