Skip to content

Commit 8d8f890

Browse files
committed
💩 first try on #1045
1 parent acf10d9 commit 8d8f890

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

include/nlohmann/detail/iterators/iteration_proxy.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <cstddef> // size_t
44
#include <string> // string, to_string
5+
#include <iterator> // input_iterator_tag
56

67
#include <nlohmann/detail/value_t.hpp>
78

@@ -16,6 +17,13 @@ template<typename IteratorType> class iteration_proxy
1617
/// helper class for iteration
1718
class iteration_proxy_internal
1819
{
20+
public:
21+
using difference_type = std::ptrdiff_t;
22+
using value_type = iteration_proxy_internal;
23+
using pointer = iteration_proxy_internal*;
24+
using reference = iteration_proxy_internal&;
25+
using iterator_category = std::input_iterator_tag;
26+
1927
private:
2028
/// the iterator
2129
IteratorType anchor;
@@ -25,6 +33,9 @@ template<typename IteratorType> class iteration_proxy
2533
public:
2634
explicit iteration_proxy_internal(IteratorType it) noexcept : anchor(it) {}
2735

36+
iteration_proxy_internal(const iteration_proxy_internal&) = default;
37+
iteration_proxy_internal& operator=(const iteration_proxy_internal&) = default;
38+
2839
/// dereference operator (needed for range-based for)
2940
iteration_proxy_internal& operator*()
3041
{
@@ -40,6 +51,12 @@ template<typename IteratorType> class iteration_proxy
4051
return *this;
4152
}
4253

54+
/// equality operator (needed for InputIterator)
55+
bool operator==(const iteration_proxy_internal& o) const noexcept
56+
{
57+
return anchor == o.anchor;
58+
}
59+
4360
/// inequality operator (needed for range-based for)
4461
bool operator!=(const iteration_proxy_internal& o) const noexcept
4562
{

single_include/nlohmann/json.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4491,6 +4491,7 @@ class iter_impl
44914491

44924492
#include <cstddef> // size_t
44934493
#include <string> // string, to_string
4494+
#include <iterator> // input_iterator_tag
44944495

44954496
// #include <nlohmann/detail/value_t.hpp>
44964497

@@ -4506,6 +4507,13 @@ template<typename IteratorType> class iteration_proxy
45064507
/// helper class for iteration
45074508
class iteration_proxy_internal
45084509
{
4510+
public:
4511+
using difference_type = std::ptrdiff_t;
4512+
using value_type = iteration_proxy_internal;
4513+
using pointer = iteration_proxy_internal*;
4514+
using reference = iteration_proxy_internal&;
4515+
using iterator_category = std::input_iterator_tag;
4516+
45094517
private:
45104518
/// the iterator
45114519
IteratorType anchor;
@@ -4515,6 +4523,9 @@ template<typename IteratorType> class iteration_proxy
45154523
public:
45164524
explicit iteration_proxy_internal(IteratorType it) noexcept : anchor(it) {}
45174525

4526+
iteration_proxy_internal(const iteration_proxy_internal&) = default;
4527+
iteration_proxy_internal& operator=(const iteration_proxy_internal&) = default;
4528+
45184529
/// dereference operator (needed for range-based for)
45194530
iteration_proxy_internal& operator*()
45204531
{
@@ -4530,6 +4541,12 @@ template<typename IteratorType> class iteration_proxy
45304541
return *this;
45314542
}
45324543

4544+
/// equality operator (needed for InputIterator)
4545+
bool operator==(const iteration_proxy_internal& o) const noexcept
4546+
{
4547+
return anchor == o.anchor;
4548+
}
4549+
45334550
/// inequality operator (needed for range-based for)
45344551
bool operator!=(const iteration_proxy_internal& o) const noexcept
45354552
{

test/src/unit-regression.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,4 +1615,25 @@ TEST_CASE("regression tests")
16151615
float_json j2 = {1000.0, 2000.0, 3000.0};
16161616
CHECK(float_json::from_ubjson(float_json::to_ubjson(j2, true, true)) == j2);
16171617
}
1618+
1619+
SECTION("issue #1045 - Using STL algorithms with JSON containers with expected results?")
1620+
{
1621+
json diffs = nlohmann::json::array();
1622+
json m1{{"key1", 42}};
1623+
json m2{{"key2", 42}};
1624+
auto p1 = m1.items();
1625+
auto p2 = m2.items();
1626+
1627+
using it_type = decltype(p1.begin());
1628+
1629+
std::set_difference(
1630+
p1.begin(), p1.end(),
1631+
p2.begin(), p2.end(),
1632+
std::inserter(diffs, diffs.end()), [&](const it_type & e1, const it_type & e2) -> bool
1633+
{
1634+
return (e1.key() < e2.key()) and (e1.value() < e2.value());
1635+
});
1636+
1637+
CHECK(diffs.size() == 2);
1638+
}
16181639
}

0 commit comments

Comments
 (0)