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 is unique for the property name it was computed from,
68 // so matching it settles the comparison on its own
69 for (const auto &entry : this->data_) {
70 if (entry.second == hash) {
71 return true;
72 }
73 }
74 } else {
75 for (const auto &entry : this->data_) {
76 if (entry.second == hash && entry.first == value) {
77 return true;
78 }
79 }
80 }
81
82 return false;
83 }
84
86 [[nodiscard]] auto contains(const string_type &value) const -> bool {
87 return this->contains(value, HASHER(value));
88 }
89
93 auto insert(const string_type &value, const hash_type &hash) -> bool;
94
98 auto insert(string_type &&value, const hash_type &hash) -> bool;
99
103 auto insert(const string_type &value) -> bool;
104
108 auto insert(string_type &&value) -> bool;
109
111 [[nodiscard]] auto at(const size_type index) const noexcept
112 -> const value_type & {
113 assert(index < this->data_.size());
114 return this->data_[index];
115 }
116
118 [[nodiscard]] auto empty() const noexcept -> bool {
119 return this->data_.empty();
120 }
121
123 [[nodiscard]] auto size() const noexcept -> size_type {
124 return this->data_.size();
125 }
126
128 [[nodiscard]] auto begin() const noexcept -> const_iterator {
129 return this->data_.begin();
130 }
131
133 [[nodiscard]] auto end() const noexcept -> const_iterator {
134 return this->data_.end();
135 }
136
138 [[nodiscard]] auto cbegin() const noexcept -> const_iterator {
139 return this->data_.cbegin();
140 }
141
143 [[nodiscard]] auto cend() const noexcept -> const_iterator {
144 return this->data_.cend();
145 }
146
148 [[nodiscard]] auto to_json() const -> JSON;
149
152 static auto from_json(const JSON &value) -> std::optional<JSONPropertySet>;
153
154private:
155 static constexpr PropertyHashJSON<string_type> HASHER{};
156 underlying_type data_;
157};
158
159#if defined(_MSC_VER)
160#pragma warning(pop)
161#endif
162
163} // namespace sourcemeta::core
164
165#endif
auto size() const noexcept -> size_type
Get the number of property names in the set.
Definition json_property_set.h:123
auto insert(const string_type &value, const hash_type &hash) -> bool
auto insert(string_type &&value, const hash_type &hash) -> bool
auto empty() const noexcept -> bool
Check whether the set is empty.
Definition json_property_set.h:118
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:86
auto contains(const string_type &value, const hash_type hash) const -> bool
Definition json_property_set.h:63
auto to_json() const -> JSON
Serialise the set as a JSON array of property names.
auto insert(const string_type &value) -> 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:111
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:38