git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: use std::map for records
commit16ad8b0a0563915cb842324b8a1467307f556ac5 authorAndy Arvanitis <andyarvanitis@gmail.com> dateThu 2 Feb 2017, 14:43 (10 years ago) parent2f17513
2 files changed+35−5
pcc/runtime/purescript.cc+5−1 pcc/runtime/purescript.hh+30−4
pcc/runtime/purescript.cc+5−1
@@ -129,7 +129,11 @@ auto any::contains(const Private::Symbol key) const -> bool {
129129 return false;
130130}
131131
132−auto any::at(const std::string& key) const -> const any& {
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+
136+auto any::at(const char * key) const -> const any& {
133137 return static_cast<const record&>(*this).at(key);
134138}
135139
pcc/runtime/purescript.hh+30−4
@@ -36,7 +36,7 @@
3636#include <string>
3737#include <array>
3838#include <deque>
39−#include <unordered_map>
39+#include <map>
4040#include <utility>
4141#include <stdexcept>
4242#include <iso646.h> // mostly for MS Visual Studio compiler
@@ -119,7 +119,6 @@ class any {
119119 static constexpr as_thunk unthunk = as_thunk{};
120120
121121 using dict_pair = std::pair<const Private::Symbol, const any>;
122−
123122 template <size_t N>
124123 using dict = std::array<const dict_pair, N>;
125124
@@ -127,7 +126,14 @@ class any {
127126 using data = std::array<const any, N>;
128127
129128 using array = std::deque<any WITH_ALLOCATOR(any)>;
130− using record = std::unordered_map<std::string, any WITH_ALLOCATOR_PAIR(std::string, 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
136+ WITH_ALLOCATOR_PAIR(const char *, any)>;
131137
132138 using fn = auto (*)(const any&) -> any;
133139 using eff_fn = auto (*)() -> any;
@@ -332,7 +338,7 @@ class any {
332338 operator const record&() const;
333339
334340 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&;
336342 auto size() const -> size_t;
337343 auto empty() const -> bool;
338344 auto contains(const Private::Symbol) const -> bool;
@@ -411,6 +417,16 @@ namespace Private {
411417 template <typename T, typename U=void>
412418 struct TagHelper {};
413419
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+
414430 template <size_t N>
415431 struct TagHelper<any::dict<N>> {
416432 static constexpr any::Tag tag = any::Tag::Dictionary;
@@ -421,6 +437,16 @@ namespace Private {
421437 static constexpr any::Tag tag = any::Tag::Data;
422438 };
423439
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+
424450 template <typename T>
425451 struct TagHelper<T> {
426452 static constexpr any::Tag tag = any::Tag::Pointer;