Use std::string_view for key type in json::Object (#7062)
This commit is contained in:
parent
48e8382785
commit
3614af7f64
@ -25,6 +25,7 @@
|
||||
- NodeJS:
|
||||
- CHANGED: Use node-api instead of NAN. [#6452](https://github.com/Project-OSRM/osrm-backend/pull/6452)
|
||||
- Misc:
|
||||
- CHANGED: Use std::string_view for key type in json::Object. [#7062](https://github.com/Project-OSRM/osrm-backend/pull/7062)
|
||||
- CHANGED: Use thread_local instead of boost::thread_specific_ptr. [#6991](https://github.com/Project-OSRM/osrm-backend/pull/6991)
|
||||
- CHANGED: Bump flatbuffers to v24.3.25 version. [#6988](https://github.com/Project-OSRM/osrm-backend/pull/6988)
|
||||
- CHANGED: Add .reserve(...) to assembleGeometry function. [#6983](https://github.com/Project-OSRM/osrm-backend/pull/6983)
|
||||
|
@ -30,7 +30,7 @@ struct V8Renderer
|
||||
{
|
||||
Napi::Value child;
|
||||
std::visit(V8Renderer(env, child), keyValue.second);
|
||||
obj.Set(keyValue.first, child);
|
||||
obj.Set(keyValue.first.data(), child);
|
||||
}
|
||||
out = obj;
|
||||
}
|
||||
|
@ -104,7 +104,7 @@ using Value = std::variant<String, Number, Object, Array, True, False, Null>;
|
||||
*/
|
||||
struct Object
|
||||
{
|
||||
std::unordered_map<std::string, Value> values;
|
||||
std::unordered_map<std::string_view, Value> values;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -44,13 +44,13 @@ struct Comparator
|
||||
|
||||
bool operator()(const Object &lhs, const Object &rhs) const
|
||||
{
|
||||
std::set<std::string> lhs_keys;
|
||||
std::set<std::string_view> lhs_keys;
|
||||
for (const auto &key_value : lhs.values)
|
||||
{
|
||||
lhs_keys.insert(key_value.first);
|
||||
}
|
||||
|
||||
std::set<std::string> rhs_keys;
|
||||
std::set<std::string_view> rhs_keys;
|
||||
for (const auto &key_value : rhs.values)
|
||||
{
|
||||
rhs_keys.insert(key_value.first);
|
||||
@ -60,7 +60,7 @@ struct Comparator
|
||||
{
|
||||
if (rhs_keys.find(key) == rhs_keys.end())
|
||||
{
|
||||
reason = rhs_path + " doesn't have key \"" + key + "\"";
|
||||
reason = rhs_path + " doesn't have key \"" + std::string(key) + "\"";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -69,7 +69,7 @@ struct Comparator
|
||||
{
|
||||
if (lhs_keys.find(key) == lhs_keys.end())
|
||||
{
|
||||
reason = lhs_path + " doesn't have key \"" + key + "\"";
|
||||
reason = lhs_path + " doesn't have key \"" + std::string(key) + "\"";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -81,10 +81,11 @@ struct Comparator
|
||||
|
||||
const auto &rhs_child = rhs.values.find(key)->second;
|
||||
const auto &lhs_child = lhs.values.find(key)->second;
|
||||
auto is_same =
|
||||
std::visit(Comparator(reason, lhs_path + "." + key, rhs_path + "." + key),
|
||||
lhs_child,
|
||||
rhs_child);
|
||||
auto is_same = std::visit(Comparator(reason,
|
||||
lhs_path + "." + std::string(key),
|
||||
rhs_path + "." + std::string(key)),
|
||||
lhs_child,
|
||||
rhs_child);
|
||||
if (!is_same)
|
||||
{
|
||||
return false;
|
||||
|
@ -97,7 +97,7 @@ template <typename Out> struct Renderer
|
||||
void operator()(const Null &) { write<>("null"); }
|
||||
|
||||
private:
|
||||
void write(const std::string &str);
|
||||
void write(std::string_view str);
|
||||
void write(const char *str, size_t size);
|
||||
void write(char ch);
|
||||
|
||||
@ -110,7 +110,7 @@ template <typename Out> struct Renderer
|
||||
Out &out;
|
||||
};
|
||||
|
||||
template <> void Renderer<std::vector<char>>::write(const std::string &str)
|
||||
template <> void Renderer<std::vector<char>>::write(std::string_view str)
|
||||
{
|
||||
out.insert(out.end(), str.begin(), str.end());
|
||||
}
|
||||
@ -122,7 +122,7 @@ template <> void Renderer<std::vector<char>>::write(const char *str, size_t size
|
||||
|
||||
template <> void Renderer<std::vector<char>>::write(char ch) { out.push_back(ch); }
|
||||
|
||||
template <> void Renderer<std::ostream>::write(const std::string &str) { out << str; }
|
||||
template <> void Renderer<std::ostream>::write(std::string_view str) { out << str; }
|
||||
|
||||
template <> void Renderer<std::ostream>::write(const char *str, size_t size)
|
||||
{
|
||||
@ -131,7 +131,7 @@ template <> void Renderer<std::ostream>::write(const char *str, size_t size)
|
||||
|
||||
template <> void Renderer<std::ostream>::write(char ch) { out << ch; }
|
||||
|
||||
template <> void Renderer<std::string>::write(const std::string &str) { out += str; }
|
||||
template <> void Renderer<std::string>::write(std::string_view str) { out += str; }
|
||||
|
||||
template <> void Renderer<std::string>::write(const char *str, size_t size)
|
||||
{
|
||||
|
@ -93,7 +93,7 @@ function run_benchmarks_for_folder {
|
||||
echo "Took: ${DIFF}s"
|
||||
done
|
||||
done
|
||||
|
||||
|
||||
for ALGORITHM in ch mld; do
|
||||
for BENCH in nearest table trip route match; do
|
||||
echo "Running random $BENCH $ALGORITHM"
|
||||
|
@ -9,12 +9,17 @@
|
||||
#include <rapidjson/document.h>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <unordered_set>
|
||||
|
||||
using namespace osrm;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
// we use std::string_view as a key in the object, so since here we have dynamic keys we have to
|
||||
// "hold" them somewhere okay for tests...
|
||||
static std::unordered_set<std::string> gKeysHolder;
|
||||
|
||||
void convert(const rapidjson::Value &value, json::Value &result)
|
||||
{
|
||||
if (value.IsString())
|
||||
@ -32,7 +37,8 @@ void convert(const rapidjson::Value &value, json::Value &result)
|
||||
{
|
||||
json::Value member;
|
||||
convert(itr->value, member);
|
||||
object.values.emplace(itr->name.GetString(), std::move(member));
|
||||
auto keyItr = gKeysHolder.emplace(itr->name.GetString()).first;
|
||||
object.values.emplace(*keyItr, std::move(member));
|
||||
}
|
||||
result = std::move(object);
|
||||
}
|
||||
@ -122,6 +128,7 @@ int main(int argc, char **argv)
|
||||
|
||||
if (std::string{out_vec.begin(), out_vec.end()} != out_str || out_str != out_ss_str)
|
||||
{
|
||||
std::cerr << "Vector/string results are not equal\n";
|
||||
throw std::logic_error("Vector/stringstream/string results are not equal");
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
|
Loading…
Reference in New Issue
Block a user