Allow using string view to find JSON value. (#8332)
- Allow comparison between string and string view. - Fix compiler warnings.
This commit is contained in:
@@ -187,13 +187,17 @@ using I32Array = JsonTypedArray<int32_t, Value::ValueKind::kI32Array>;
|
||||
using I64Array = JsonTypedArray<int64_t, Value::ValueKind::kI64Array>;
|
||||
|
||||
class JsonObject : public Value {
|
||||
std::map<std::string, Json> object_;
|
||||
public:
|
||||
using Map = std::map<std::string, Json, std::less<>>;
|
||||
|
||||
private:
|
||||
Map object_;
|
||||
|
||||
public:
|
||||
JsonObject() : Value(ValueKind::kObject) {}
|
||||
JsonObject(std::map<std::string, Json>&& object) noexcept; // NOLINT
|
||||
JsonObject(Map&& object) noexcept; // NOLINT
|
||||
JsonObject(JsonObject const& that) = delete;
|
||||
JsonObject(JsonObject && that) noexcept;
|
||||
JsonObject(JsonObject&& that) noexcept;
|
||||
|
||||
void Save(JsonWriter* writer) const override;
|
||||
|
||||
@@ -201,15 +205,13 @@ class JsonObject : public Value {
|
||||
Json& operator[](int ind) override { return Value::operator[](ind); }
|
||||
Json& operator[](std::string const& key) override { return object_[key]; }
|
||||
|
||||
std::map<std::string, Json> const& GetObject() && { return object_; }
|
||||
std::map<std::string, Json> const& GetObject() const & { return object_; }
|
||||
std::map<std::string, Json> & GetObject() & { return object_; }
|
||||
Map const& GetObject() && { return object_; }
|
||||
Map const& GetObject() const& { return object_; }
|
||||
Map& GetObject() & { return object_; }
|
||||
|
||||
bool operator==(Value const& rhs) const override;
|
||||
|
||||
static bool IsClassOf(Value const* value) {
|
||||
return value->Type() == ValueKind::kObject;
|
||||
}
|
||||
static bool IsClassOf(Value const* value) { return value->Type() == ValueKind::kObject; }
|
||||
~JsonObject() override = default;
|
||||
};
|
||||
|
||||
@@ -559,16 +561,13 @@ std::vector<T> const& GetImpl(JsonTypedArray<T, kind> const& val) {
|
||||
}
|
||||
|
||||
// Object
|
||||
template <typename T,
|
||||
typename std::enable_if<
|
||||
std::is_same<T, JsonObject>::value>::type* = nullptr>
|
||||
std::map<std::string, Json>& GetImpl(T& val) { // NOLINT
|
||||
template <typename T, typename std::enable_if<std::is_same<T, JsonObject>::value>::type* = nullptr>
|
||||
JsonObject::Map& GetImpl(T& val) { // NOLINT
|
||||
return val.GetObject();
|
||||
}
|
||||
template <typename T,
|
||||
typename std::enable_if<
|
||||
std::is_same<T, JsonObject const>::value>::type* = nullptr>
|
||||
std::map<std::string, Json> const& GetImpl(T& val) { // NOLINT
|
||||
typename std::enable_if<std::is_same<T, JsonObject const>::value>::type* = nullptr>
|
||||
JsonObject::Map const& GetImpl(T& val) { // NOLINT
|
||||
return val.GetObject();
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#ifndef XGBOOST_STRING_VIEW_H_
|
||||
#define XGBOOST_STRING_VIEW_H_
|
||||
#include <xgboost/logging.h>
|
||||
#include <xgboost/span.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
@@ -19,6 +20,7 @@ struct StringView {
|
||||
size_t size_{0};
|
||||
|
||||
public:
|
||||
using value_type = CharT; // NOLINT
|
||||
using iterator = const CharT*; // NOLINT
|
||||
using const_iterator = iterator; // NOLINT
|
||||
using reverse_iterator = std::reverse_iterator<const_iterator>; // NOLINT
|
||||
@@ -77,5 +79,14 @@ inline bool operator==(StringView l, StringView r) {
|
||||
}
|
||||
|
||||
inline bool operator!=(StringView l, StringView r) { return !(l == r); }
|
||||
|
||||
inline bool operator<(StringView l, StringView r) {
|
||||
return common::Span<StringView::value_type const>{l.c_str(), l.size()} <
|
||||
common::Span<StringView::value_type const>{r.c_str(), r.size()};
|
||||
}
|
||||
|
||||
inline bool operator<(std::string const& l, StringView r) { return StringView{l} < r; }
|
||||
|
||||
inline bool operator<(StringView l, std::string const& r) { return l < StringView{r}; }
|
||||
} // namespace xgboost
|
||||
#endif // XGBOOST_STRING_VIEW_H_
|
||||
|
||||
Reference in New Issue
Block a user