💩 first try on #1045

This commit is contained in:
Niels Lohmann 2018-04-10 08:29:07 +02:00
parent acf10d9af7
commit 8d8f890771
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
3 changed files with 55 additions and 0 deletions

View file

@ -2,6 +2,7 @@
#include <cstddef> // size_t
#include <string> // string, to_string
#include <iterator> // input_iterator_tag
#include <nlohmann/detail/value_t.hpp>
@ -16,6 +17,13 @@ template<typename IteratorType> class iteration_proxy
/// helper class for iteration
class iteration_proxy_internal
{
public:
using difference_type = std::ptrdiff_t;
using value_type = iteration_proxy_internal;
using pointer = iteration_proxy_internal*;
using reference = iteration_proxy_internal&;
using iterator_category = std::input_iterator_tag;
private:
/// the iterator
IteratorType anchor;
@ -25,6 +33,9 @@ template<typename IteratorType> class iteration_proxy
public:
explicit iteration_proxy_internal(IteratorType it) noexcept : anchor(it) {}
iteration_proxy_internal(const iteration_proxy_internal&) = default;
iteration_proxy_internal& operator=(const iteration_proxy_internal&) = default;
/// dereference operator (needed for range-based for)
iteration_proxy_internal& operator*()
{
@ -40,6 +51,12 @@ template<typename IteratorType> class iteration_proxy
return *this;
}
/// equality operator (needed for InputIterator)
bool operator==(const iteration_proxy_internal& o) const noexcept
{
return anchor == o.anchor;
}
/// inequality operator (needed for range-based for)
bool operator!=(const iteration_proxy_internal& o) const noexcept
{

View file

@ -4491,6 +4491,7 @@ class iter_impl
#include <cstddef> // size_t
#include <string> // string, to_string
#include <iterator> // input_iterator_tag
// #include <nlohmann/detail/value_t.hpp>
@ -4506,6 +4507,13 @@ template<typename IteratorType> class iteration_proxy
/// helper class for iteration
class iteration_proxy_internal
{
public:
using difference_type = std::ptrdiff_t;
using value_type = iteration_proxy_internal;
using pointer = iteration_proxy_internal*;
using reference = iteration_proxy_internal&;
using iterator_category = std::input_iterator_tag;
private:
/// the iterator
IteratorType anchor;
@ -4515,6 +4523,9 @@ template<typename IteratorType> class iteration_proxy
public:
explicit iteration_proxy_internal(IteratorType it) noexcept : anchor(it) {}
iteration_proxy_internal(const iteration_proxy_internal&) = default;
iteration_proxy_internal& operator=(const iteration_proxy_internal&) = default;
/// dereference operator (needed for range-based for)
iteration_proxy_internal& operator*()
{
@ -4530,6 +4541,12 @@ template<typename IteratorType> class iteration_proxy
return *this;
}
/// equality operator (needed for InputIterator)
bool operator==(const iteration_proxy_internal& o) const noexcept
{
return anchor == o.anchor;
}
/// inequality operator (needed for range-based for)
bool operator!=(const iteration_proxy_internal& o) const noexcept
{

View file

@ -1615,4 +1615,25 @@ TEST_CASE("regression tests")
float_json j2 = {1000.0, 2000.0, 3000.0};
CHECK(float_json::from_ubjson(float_json::to_ubjson(j2, true, true)) == j2);
}
SECTION("issue #1045 - Using STL algorithms with JSON containers with expected results?")
{
json diffs = nlohmann::json::array();
json m1{{"key1", 42}};
json m2{{"key2", 42}};
auto p1 = m1.items();
auto p2 = m2.items();
using it_type = decltype(p1.begin());
std::set_difference(
p1.begin(), p1.end(),
p2.begin(), p2.end(),
std::inserter(diffs, diffs.end()), [&](const it_type & e1, const it_type & e2) -> bool
{
return (e1.key() < e2.key()) and (e1.value() < e2.value());
});
CHECK(diffs.size() == 2);
}
}