Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
json_property_set.h
1#ifndef SOURCEMETA_CORE_JSON_PROPERTY_SET_H_
2#define SOURCEMETA_CORE_JSON_PROPERTY_SET_H_
3
4#ifndef SOURCEMETA_CORE_JSON_EXPORT
5#include <sourcemeta/core/json_export.h>
6#endif
7
8#include <sourcemeta/core/json_hash.h>
9#include <sourcemeta/core/json_value.h>
10
11#include <cassert> // assert
12#include <optional> // std::optional
13#include <utility> // std::pair
14#include <vector> // std::vector
15
16namespace sourcemeta::core {
17
18// Exporting symbols that depends on the standard C++ library is considered
19// safe.
20// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
21#if defined(_MSC_VER)
22#pragma warning(push)
23#pragma warning(disable : 4251 4275)
24#endif
25
45class SOURCEMETA_CORE_JSON_EXPORT JSONPropertySet {
46public:
47 JSONPropertySet() = default;
48
52 using hash_type = JSON::Object::hash_type;
54 using value_type = std::pair<string_type, hash_type>;
56 using underlying_type = std::vector<value_type>;
57 using size_type = underlying_type::size_type;
58 using difference_type = underlying_type::difference_type;
59 using const_iterator = underlying_type::const_iterator;
60
63 [[nodiscard]] auto contains(const string_type &value,
64 const hash_type hash) const -> bool {
65 assert(HASHER(value) == hash);
66 if (HASHER.is_perfect(hash)) {
67 // A perfect hash captures the property name bytes but not its length, so
68 // two names that differ only by trailing NUL bytes hash equal. Comparing
69 // sizes disambiguates them without the cost of a full string comparison
70 for (const auto &entry : this->data_) {
71 if (entry.second == hash && entry.first.size() == value.size()) {
72 return true;
73 }
74 }
75 } else {
76 for (const auto &entry : this->data_) {
77 if (entry.second == hash && entry.first == value) {
78 return true;
79 }
80 }
81 }
82
83 return false;
84 }
85
87 [[nodiscard]] auto contains(const string_type &value) const -> bool {
88 return this->contains(value, HASHER(value));
89 }
90
94 auto insert(const string_type &value, const hash_type hash) -> bool;
95
99 auto insert(string_type &&value, const hash_type hash) -> bool;
100
104 auto insert(const string_type &value) -> bool;
105
109 auto insert(string_type &&value) -> bool;
110
112 [[nodiscard]] auto at(const size_type index) const noexcept
113 -> const value_type & {
114 assert(index < this->data_.size());
115 return this->data_[index];
116 }
117
119 [[nodiscard]] auto empty() const noexcept -> bool {
120 return this->data_.empty();
121 }
122
124 [[nodiscard]] auto size() const noexcept -> size_type {
125 return this->data_.size();
126 }
127
129 [[nodiscard]] auto begin() const noexcept -> const_iterator {
130 return this->data_.begin();
131 }
132
134 [[nodiscard]] auto end() const noexcept -> const_iterator {
135 return this->data_.end();
136 }
137
139 [[nodiscard]] auto cbegin() const noexcept -> const_iterator {
140 return this->data_.cbegin();
141 }
142
144 [[nodiscard]] auto cend() const noexcept -> const_iterator {
145 return this->data_.cend();
146 }
147
149 [[nodiscard]] auto to_json() const -> JSON;
150
153 static auto from_json(const JSON &value) -> std::optional<JSONPropertySet>;
154
155private:
156 static constexpr PropertyHashJSON<string_type> HASHER{};
157 underlying_type data_;
158};
159
160#if defined(_MSC_VER)
161#pragma warning(pop)
162#endif
163
164} // namespace sourcemeta::core
165
166#endif
auto size() const noexcept -> size_type
Get the number of property names in the set.
Definition json_property_set.h:124
auto empty() const noexcept -> bool
Check whether the set is empty.
Definition json_property_set.h:119
std::basic_string< Char, CharTraits, Allocator< Char > > String
The string type used by the JSON document.
Definition json_value.h:52
static auto from_json(const JSON &value) -> std::optional< JSONPropertySet >
auto insert(string_type &&value) -> bool
auto contains(const string_type &value) const -> bool
Check whether the set contains a property name.
Definition json_property_set.h:87
auto contains(const string_type &value, const hash_type hash) const -> bool
Definition json_property_set.h:63
auto insert(const string_type &value, const hash_type hash) -> bool
auto to_json() const -> JSON
Serialise the set as a JSON array of property names.
auto insert(const string_type &value) -> bool
auto insert(string_type &&value, const hash_type hash) -> bool
auto at(const size_type index) const noexcept -> const value_type &
Get a property name and its hash by index.
Definition json_property_set.h:112
JSON::String string_type
The type of the property names held by the set.
Definition json_property_set.h:50
Definition json_value.h:39
Definition json_hash.h:37