Skip to content

Commit 104a009

Browse files
committed
get_key, get_value
1 parent 1731d4b commit 104a009

3 files changed

Lines changed: 48 additions & 23 deletions

File tree

include/jsoncons/key_value.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,30 @@ struct make_key_value
292292
}
293293
};
294294

295+
template <typename T1,typename T2>
296+
const T1& get_key(const std::pair<T1,T2>& p)
297+
{
298+
return p.first;
299+
}
300+
301+
template <typename T1,typename T2>
302+
const T1& get_key(const key_value<T1,T2>& p)
303+
{
304+
return p.key();
305+
}
306+
307+
template <typename T1,typename T2>
308+
const T2& get_value(const std::pair<T1,T2>& p)
309+
{
310+
return p.second;
311+
}
312+
313+
template <typename T1,typename T2>
314+
const T2& get_value(const key_value<T1,T2>& p)
315+
{
316+
return p.value();
317+
}
318+
295319
} // namespace jsoncons
296320

297321
#endif // JSONCONS_KEY_VALUE_HPP

include/jsoncons/ordered_json_object.hpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,16 @@ namespace jsoncons {
112112
std::memset(bloom, 0, bloom_bytes);
113113
for (auto it = first; it != last; ++it)
114114
{
115-
auto kv = make_key_value<KeyT,Json>()(*it);
116-
uint32_t h = a5hash32(kv.key().data(), kv.key().size(), 0);
115+
const auto& key = get_key(*it);
116+
uint32_t h = a5hash32(key.data(), key.size(), 0);
117117
if (JSONCONS_LIKELY(!bloom_may_contain(bloom, h)))
118118
{
119-
data_.emplace_back(std::move(kv));
119+
data_.emplace_back(key_type(key.begin(), key.end(),get_allocator()), get_value(*it));
120120
bloom_set(bloom, h);
121121
}
122122
else
123123
{
124-
try_emplace(kv.key(), kv.value());
124+
try_emplace(key_type(key.begin(), key.end(),get_allocator()), get_value(*it));
125125
}
126126
}
127127

@@ -130,8 +130,8 @@ namespace jsoncons {
130130
{
131131
for (auto it = first; it != last; ++it)
132132
{
133-
auto kv = make_key_value<KeyT,Json>()(*it);
134-
try_emplace(kv.key(), kv.value());
133+
const auto& key = get_key(*it);
134+
try_emplace(key_type(key.begin(), key.end(),get_allocator()), get_value(*it));
135135
}
136136
}
137137
}
@@ -152,16 +152,16 @@ namespace jsoncons {
152152
std::memset(bloom, 0, bloom_bytes);
153153
for (auto it = first; it != last; ++it)
154154
{
155-
auto kv = make_key_value<KeyT,Json>()(*it);
156-
uint32_t h = a5hash32(kv.key().data(), kv.key().size(), 0);
155+
const auto& key = get_key(*it);
156+
uint32_t h = a5hash32(key.data(), key.size(), 0);
157157
if (JSONCONS_LIKELY(!bloom_may_contain(bloom, h)))
158158
{
159-
data_.emplace_back(std::move(kv));
159+
data_.emplace_back(key_type(key.begin(), key.end(),get_allocator()), get_value(*it));
160160
bloom_set(bloom, h);
161161
}
162162
else
163163
{
164-
try_emplace(kv.key(), kv.value());
164+
try_emplace(key_type(key.begin(), key.end(),get_allocator()), get_value(*it));
165165
}
166166
}
167167

@@ -170,8 +170,8 @@ namespace jsoncons {
170170
{
171171
for (auto it = first; it != last; ++it)
172172
{
173-
auto kv = make_key_value<KeyT,Json>()(*it);
174-
try_emplace(kv.key(), kv.value());
173+
const auto& key = get_key(*it);
174+
try_emplace(key_type(key.begin(), key.end(),get_allocator()), get_value(*it));
175175
}
176176
}
177177
}
@@ -405,6 +405,7 @@ namespace jsoncons {
405405
if (count > 0)
406406
{
407407
const size_type old_size = size();
408+
data_.reserve(count);
408409
data_.reserve(old_size+count);
409410
if (old_size+count <= bloom_bytes)
410411
{
@@ -417,25 +418,26 @@ namespace jsoncons {
417418
}
418419
for (auto it = first; it != last; ++it)
419420
{
420-
auto kv = make_key_value<KeyT,Json>()(*it);
421-
uint32_t h = a5hash32(kv.key().data(), kv.key().size(), 0);
421+
const auto& key = get_key(*it);
422+
uint32_t h = a5hash32(key.data(), key.size(), 0);
422423
if (JSONCONS_LIKELY(!bloom_may_contain(bloom, h)))
423424
{
424-
data_.emplace_back(std::move(kv));
425+
data_.emplace_back(key_type(key.begin(), key.end(),get_allocator()), get_value(*it));
425426
bloom_set(bloom, h);
426427
}
427428
else
428429
{
429-
try_emplace(kv.key(), kv.value());
430+
try_emplace(key_type(key.begin(), key.end(),get_allocator()), get_value(*it));
430431
}
431432
}
433+
432434
}
433435
else
434436
{
435437
for (auto it = first; it != last; ++it)
436438
{
437-
auto kv = make_key_value<KeyT,Json>()(*it);
438-
try_emplace(kv.key(), kv.value());
439+
const auto& key = get_key(*it);
440+
try_emplace(key_type(key.begin(), key.end(),get_allocator()), get_value(*it));
439441
}
440442
}
441443
}
@@ -446,7 +448,8 @@ namespace jsoncons {
446448
{
447449
for (auto it = first; it != last; ++it)
448450
{
449-
data_.emplace_back(make_key_value<KeyT,Json>()(*it));
451+
const auto& key = get_key(*it);
452+
data_.emplace_back(key_type(key.begin(), key.end(),get_allocator()), get_value(*it));
450453
}
451454
}
452455

include/jsoncons/sorted_json_object.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ namespace jsoncons {
108108
data_.reserve(count);
109109
for (auto it = first; it != last; ++it)
110110
{
111-
auto kv = make_key_value<KeyT,Json>()(*it);
112-
data_.emplace_back(std::move(kv));
111+
data_.emplace_back(make_key_value<KeyT,Json>()(*it));
113112
}
114113
std::stable_sort(data_.begin(),data_.end(),
115114
[](const key_value_type& a, const key_value_type& b) -> bool {return a.key().compare(b.key()) < 0;});
@@ -127,8 +126,7 @@ namespace jsoncons {
127126
data_.reserve(count);
128127
for (auto it = first; it != last; ++it)
129128
{
130-
auto kv = make_key_value<KeyT,Json>()(*it, alloc);
131-
data_.emplace_back(std::move(kv));
129+
data_.emplace_back(make_key_value<KeyT,Json>()(*it, alloc));
132130
}
133131
std::stable_sort(data_.begin(), data_.end(),
134132
[](const key_value_type& a, const key_value_type& b) -> bool {return a.key().compare(b.key()) < 0;});

0 commit comments

Comments
 (0)