1
0
mirror of https://github.com/nlohmann/json synced 2024-11-24 21:00:13 +00:00

reverse iterators

This commit is contained in:
Niels 2015-02-14 16:47:14 +01:00
parent d87e770767
commit cc4a8319a1
4 changed files with 1216 additions and 314 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
.deps
utf8_test
json_unit

File diff suppressed because it is too large Load Diff

View File

@ -93,7 +93,10 @@ class basic_json
using iterator = basic_json::iterator;
/// a const iterator for a basic_json container
using const_iterator = basic_json::const_iterator;
/// a reverse iterator for a basic_json container
using reverse_iterator = std::reverse_iterator<iterator>;
/// a const reverse iterator for a basic_json container
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
///////////////////////////
// JSON value data types //
@ -264,6 +267,8 @@ class basic_json
std::enable_if<
not std::is_same<V, basic_json::iterator>::value and
not std::is_same<V, basic_json::const_iterator>::value and
not std::is_same<V, basic_json::reverse_iterator>::value and
not std::is_same<V, basic_json::const_reverse_iterator>::value and
not std::is_same<V, typename array_t::iterator>::value and
not std::is_same<V, typename array_t::const_iterator>::value and
std::is_constructible<basic_json, typename V::value_type>::value, int>::type
@ -822,6 +827,30 @@ class basic_json
return result;
}
/// returns a reverse iterator to the beginning
inline reverse_iterator rbegin() noexcept
{
return reverse_iterator(end());
}
/// returns a reverse iterator to the end
inline reverse_iterator rend() noexcept
{
return reverse_iterator(begin());
}
/// returns a reverse iterator to the beginning
inline const_reverse_iterator crbegin() const noexcept
{
return const_reverse_iterator(cend());
}
/// returns a reverse iterator to the end
inline const_reverse_iterator crend() const noexcept
{
return const_reverse_iterator(cbegin());
}
//////////////
// capacity //

View File

@ -2420,6 +2420,81 @@ TEST_CASE("iterators")
CHECK(it != j_const.cend());
CHECK(*it == j_const);
}
SECTION("json + rbegin/rend")
{
json::reverse_iterator it = j.rbegin();
CHECK(it != j.rend());
CHECK(*it == j);
it++;
CHECK(it != j.rbegin());
CHECK(it == j.rend());
it--;
CHECK(it == j.rbegin());
CHECK(it != j.rend());
CHECK(*it == j);
++it;
CHECK(it != j.rbegin());
CHECK(it == j.rend());
--it;
CHECK(it == j.rbegin());
CHECK(it != j.rend());
CHECK(*it == j);
}
SECTION("json + crbegin/crend")
{
json::const_reverse_iterator it = j.crbegin();
CHECK(it != j.crend());
CHECK(*it == j);
it++;
CHECK(it != j.crbegin());
CHECK(it == j.crend());
it--;
CHECK(it == j.crbegin());
CHECK(it != j.crend());
CHECK(*it == j);
++it;
CHECK(it != j.crbegin());
CHECK(it == j.crend());
--it;
CHECK(it == j.crbegin());
CHECK(it != j.crend());
CHECK(*it == j);
}
SECTION("const json + crbegin/crend")
{
json::const_reverse_iterator it = j_const.crbegin();
CHECK(it != j_const.crend());
CHECK(*it == j_const);
it++;
CHECK(it != j_const.crbegin());
CHECK(it == j_const.crend());
it--;
CHECK(it == j_const.crbegin());
CHECK(it != j_const.crend());
CHECK(*it == j_const);
++it;
CHECK(it != j_const.crbegin());
CHECK(it == j_const.crend());
--it;
CHECK(it == j_const.crbegin());
CHECK(it != j_const.crend());
CHECK(*it == j_const);
}
}
SECTION("string")
@ -2526,6 +2601,81 @@ TEST_CASE("iterators")
CHECK(it != j_const.cend());
CHECK(*it == j_const);
}
SECTION("json + rbegin/rend")
{
json::reverse_iterator it = j.rbegin();
CHECK(it != j.rend());
CHECK(*it == j);
it++;
CHECK(it != j.rbegin());
CHECK(it == j.rend());
it--;
CHECK(it == j.rbegin());
CHECK(it != j.rend());
CHECK(*it == j);
++it;
CHECK(it != j.rbegin());
CHECK(it == j.rend());
--it;
CHECK(it == j.rbegin());
CHECK(it != j.rend());
CHECK(*it == j);
}
SECTION("json + crbegin/crend")
{
json::const_reverse_iterator it = j.crbegin();
CHECK(it != j.crend());
CHECK(*it == j);
it++;
CHECK(it != j.crbegin());
CHECK(it == j.crend());
it--;
CHECK(it == j.crbegin());
CHECK(it != j.crend());
CHECK(*it == j);
++it;
CHECK(it != j.crbegin());
CHECK(it == j.crend());
--it;
CHECK(it == j.crbegin());
CHECK(it != j.crend());
CHECK(*it == j);
}
SECTION("const json + crbegin/crend")
{
json::const_reverse_iterator it = j_const.crbegin();
CHECK(it != j_const.crend());
CHECK(*it == j_const);
it++;
CHECK(it != j_const.crbegin());
CHECK(it == j_const.crend());
it--;
CHECK(it == j_const.crbegin());
CHECK(it != j_const.crend());
CHECK(*it == j_const);
++it;
CHECK(it != j_const.crbegin());
CHECK(it == j_const.crend());
--it;
CHECK(it == j_const.crbegin());
CHECK(it != j_const.crend());
CHECK(*it == j_const);
}
}
SECTION("array")
@ -2540,14 +2690,17 @@ TEST_CASE("iterators")
auto it = it_begin;
CHECK(it != it_end);
CHECK(*it == j[0]);
it++;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j[1]);
++it;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j[2]);
++it;
CHECK(it != it_begin);
@ -2561,14 +2714,17 @@ TEST_CASE("iterators")
auto it = it_begin;
CHECK(it != it_end);
CHECK(*it == j_const[0]);
it++;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j_const[1]);
++it;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j_const[2]);
++it;
CHECK(it != it_begin);
@ -2582,14 +2738,17 @@ TEST_CASE("iterators")
auto it = it_begin;
CHECK(it != it_end);
CHECK(*it == j[0]);
it++;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j[1]);
++it;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j[2]);
++it;
CHECK(it != it_begin);
@ -2598,19 +2757,94 @@ TEST_CASE("iterators")
SECTION("const json + cbegin/cend")
{
json::const_iterator it_begin = j.cbegin();
json::const_iterator it_end = j.cend();
json::const_iterator it_begin = j_const.cbegin();
json::const_iterator it_end = j_const.cend();
auto it = it_begin;
CHECK(it != it_end);
CHECK(*it == j[0]);
it++;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j[1]);
++it;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j[2]);
++it;
CHECK(it != it_begin);
CHECK(it == it_end);
}
SECTION("json + rbegin/rend")
{
json::reverse_iterator it_begin = j.rbegin();
json::reverse_iterator it_end = j.rend();
auto it = it_begin;
CHECK(it != it_end);
CHECK(*it == j[2]);
it++;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j[1]);
++it;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j[0]);
++it;
CHECK(it != it_begin);
CHECK(it == it_end);
}
SECTION("json + crbegin/crend")
{
json::const_reverse_iterator it_begin = j.crbegin();
json::const_reverse_iterator it_end = j.crend();
auto it = it_begin;
CHECK(it != it_end);
CHECK(*it == j[2]);
it++;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j[1]);
++it;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j[0]);
++it;
CHECK(it != it_begin);
CHECK(it == it_end);
}
SECTION("const json + crbegin/crend")
{
json::const_reverse_iterator it_begin = j_const.crbegin();
json::const_reverse_iterator it_end = j_const.crend();
auto it = it_begin;
CHECK(it != it_end);
CHECK(*it == j[2]);
it++;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j[1]);
++it;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j[0]);
++it;
CHECK(it != it_begin);
@ -2620,7 +2854,7 @@ TEST_CASE("iterators")
SECTION("object")
{
json j = {{"one", 1}, {"two", 2}, {"three", 3}};
json j = {{"A", 1}, {"B", 2}, {"C", 3}};
json j_const(j);
SECTION("json + begin/end")
@ -2630,14 +2864,17 @@ TEST_CASE("iterators")
auto it = it_begin;
CHECK(it != it_end);
CHECK(*it == j["A"]);
it++;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j["B"]);
++it;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j["C"]);
++it;
CHECK(it != it_begin);
@ -2651,14 +2888,17 @@ TEST_CASE("iterators")
auto it = it_begin;
CHECK(it != it_end);
CHECK(*it == j_const["A"]);
it++;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j_const["B"]);
++it;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j_const["C"]);
++it;
CHECK(it != it_begin);
@ -2672,14 +2912,17 @@ TEST_CASE("iterators")
auto it = it_begin;
CHECK(it != it_end);
CHECK(*it == j["A"]);
it++;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j["B"]);
++it;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j["C"]);
++it;
CHECK(it != it_begin);
@ -2688,19 +2931,94 @@ TEST_CASE("iterators")
SECTION("const json + cbegin/cend")
{
json::const_iterator it_begin = j.cbegin();
json::const_iterator it_end = j.cend();
json::const_iterator it_begin = j_const.cbegin();
json::const_iterator it_end = j_const.cend();
auto it = it_begin;
CHECK(it != it_end);
CHECK(*it == j_const["A"]);
it++;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j_const["B"]);
++it;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j_const["C"]);
++it;
CHECK(it != it_begin);
CHECK(it == it_end);
}
SECTION("json + rbegin/rend")
{
json::reverse_iterator it_begin = j.rbegin();
json::reverse_iterator it_end = j.rend();
auto it = it_begin;
CHECK(it != it_end);
CHECK(*it == j["C"]);
it++;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j["B"]);
++it;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j["A"]);
++it;
CHECK(it != it_begin);
CHECK(it == it_end);
}
SECTION("json + crbegin/crend")
{
json::const_reverse_iterator it_begin = j.crbegin();
json::const_reverse_iterator it_end = j.crend();
auto it = it_begin;
CHECK(it != it_end);
CHECK(*it == j["C"]);
it++;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j["B"]);
++it;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j["A"]);
++it;
CHECK(it != it_begin);
CHECK(it == it_end);
}
SECTION("const json + crbegin/crend")
{
json::const_reverse_iterator it_begin = j_const.crbegin();
json::const_reverse_iterator it_end = j_const.crend();
auto it = it_begin;
CHECK(it != it_end);
CHECK(*it == j["C"]);
it++;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j["B"]);
++it;
CHECK(it != it_begin);
CHECK(it != it_end);
CHECK(*it == j["A"]);
++it;
CHECK(it != it_begin);
@ -2812,6 +3130,81 @@ TEST_CASE("iterators")
CHECK(it != j_const.cend());
CHECK(*it == j_const);
}
SECTION("json + rbegin/rend")
{
json::reverse_iterator it = j.rbegin();
CHECK(it != j.rend());
CHECK(*it == j);
it++;
CHECK(it != j.rbegin());
CHECK(it == j.rend());
it--;
CHECK(it == j.rbegin());
CHECK(it != j.rend());
CHECK(*it == j);
++it;
CHECK(it != j.rbegin());
CHECK(it == j.rend());
--it;
CHECK(it == j.rbegin());
CHECK(it != j.rend());
CHECK(*it == j);
}
SECTION("json + crbegin/crend")
{
json::const_reverse_iterator it = j.crbegin();
CHECK(it != j.crend());
CHECK(*it == j);
it++;
CHECK(it != j.crbegin());
CHECK(it == j.crend());
it--;
CHECK(it == j.crbegin());
CHECK(it != j.crend());
CHECK(*it == j);
++it;
CHECK(it != j.crbegin());
CHECK(it == j.crend());
--it;
CHECK(it == j.crbegin());
CHECK(it != j.crend());
CHECK(*it == j);
}
SECTION("const json + crbegin/crend")
{
json::const_reverse_iterator it = j_const.crbegin();
CHECK(it != j_const.crend());
CHECK(*it == j_const);
it++;
CHECK(it != j_const.crbegin());
CHECK(it == j_const.crend());
it--;
CHECK(it == j_const.crbegin());
CHECK(it != j_const.crend());
CHECK(*it == j_const);
++it;
CHECK(it != j_const.crbegin());
CHECK(it == j_const.crend());
--it;
CHECK(it == j_const.crbegin());
CHECK(it != j_const.crend());
CHECK(*it == j_const);
}
}
SECTION("number (float)")
@ -2918,6 +3311,81 @@ TEST_CASE("iterators")
CHECK(it != j_const.cend());
CHECK(*it == j_const);
}
SECTION("json + rbegin/rend")
{
json::reverse_iterator it = j.rbegin();
CHECK(it != j.rend());
CHECK(*it == j);
it++;
CHECK(it != j.rbegin());
CHECK(it == j.rend());
it--;
CHECK(it == j.rbegin());
CHECK(it != j.rend());
CHECK(*it == j);
++it;
CHECK(it != j.rbegin());
CHECK(it == j.rend());
--it;
CHECK(it == j.rbegin());
CHECK(it != j.rend());
CHECK(*it == j);
}
SECTION("json + crbegin/crend")
{
json::const_reverse_iterator it = j.crbegin();
CHECK(it != j.crend());
CHECK(*it == j);
it++;
CHECK(it != j.crbegin());
CHECK(it == j.crend());
it--;
CHECK(it == j.crbegin());
CHECK(it != j.crend());
CHECK(*it == j);
++it;
CHECK(it != j.crbegin());
CHECK(it == j.crend());
--it;
CHECK(it == j.crbegin());
CHECK(it != j.crend());
CHECK(*it == j);
}
SECTION("const json + crbegin/crend")
{
json::const_reverse_iterator it = j_const.crbegin();
CHECK(it != j_const.crend());
CHECK(*it == j_const);
it++;
CHECK(it != j_const.crbegin());
CHECK(it == j_const.crend());
it--;
CHECK(it == j_const.crbegin());
CHECK(it != j_const.crend());
CHECK(*it == j_const);
++it;
CHECK(it != j_const.crbegin());
CHECK(it == j_const.crend());
--it;
CHECK(it == j_const.crbegin());
CHECK(it != j_const.crend());
CHECK(*it == j_const);
}
}
SECTION("null")
@ -2951,6 +3419,24 @@ TEST_CASE("iterators")
json::const_iterator it_end = j_const.cend();
CHECK(it_begin == it_end);
}
SECTION("json + rbegin/rend")
{
json::reverse_iterator it = j.rbegin();
CHECK(it == j.rend());
}
SECTION("json + crbegin/crend")
{
json::const_reverse_iterator it = j.crbegin();
CHECK(it == j.crend());
}
SECTION("const json + crbegin/crend")
{
json::const_reverse_iterator it = j_const.crbegin();
CHECK(it == j_const.crend());
}
}
}