From 7d9cfb1b32d0f35033f20dfb0d98ceb745d41060 Mon Sep 17 00:00:00 2001 From: Niels Date: Sun, 28 Jun 2015 15:49:40 +0200 Subject: [PATCH] added documentation for erase functions --- doc/examples/erase__IteratorType.cpp | 30 +++++ doc/examples/erase__IteratorType.output | 6 + .../erase__IteratorType_IteratorType.cpp | 30 +++++ .../erase__IteratorType_IteratorType.output | 6 + doc/examples/erase__key_type.cpp | 17 +++ doc/examples/erase__key_type.output | 2 + doc/examples/erase__size_type.cpp | 15 +++ doc/examples/erase__size_type.output | 1 + src/json.hpp | 127 +++++++++++++++--- src/json.hpp.re2c | 127 +++++++++++++++--- 10 files changed, 327 insertions(+), 34 deletions(-) create mode 100644 doc/examples/erase__IteratorType.cpp create mode 100644 doc/examples/erase__IteratorType.output create mode 100644 doc/examples/erase__IteratorType_IteratorType.cpp create mode 100644 doc/examples/erase__IteratorType_IteratorType.output create mode 100644 doc/examples/erase__key_type.cpp create mode 100644 doc/examples/erase__key_type.output create mode 100644 doc/examples/erase__size_type.cpp create mode 100644 doc/examples/erase__size_type.output diff --git a/doc/examples/erase__IteratorType.cpp b/doc/examples/erase__IteratorType.cpp new file mode 100644 index 00000000..3c02bbff --- /dev/null +++ b/doc/examples/erase__IteratorType.cpp @@ -0,0 +1,30 @@ +#include + +using namespace nlohmann; + +int main() +{ + // create JSON values + json j_boolean = true; + json j_number_integer = 17; + json j_number_float = 23.42; + json j_object = {{"one", 1}, {"two", 2}}; + json j_array = {1, 2, 4, 8, 16}; + json j_string = "Hello, world"; + + // call erase + j_boolean.erase(j_boolean.begin()); + j_number_integer.erase(j_number_integer.begin()); + j_number_float.erase(j_number_float.begin()); + j_object.erase(j_object.find("two")); + j_array.erase(j_array.begin() + 2); + j_string.erase(j_string.begin()); + + // print values + std::cout << j_boolean << '\n'; + std::cout << j_number_integer << '\n'; + std::cout << j_number_float << '\n'; + std::cout << j_object << '\n'; + std::cout << j_array << '\n'; + std::cout << j_string << '\n'; +} diff --git a/doc/examples/erase__IteratorType.output b/doc/examples/erase__IteratorType.output new file mode 100644 index 00000000..e392f8ea --- /dev/null +++ b/doc/examples/erase__IteratorType.output @@ -0,0 +1,6 @@ +null +null +null +{"one":1} +[1,2,8,16] +null diff --git a/doc/examples/erase__IteratorType_IteratorType.cpp b/doc/examples/erase__IteratorType_IteratorType.cpp new file mode 100644 index 00000000..a5bde58b --- /dev/null +++ b/doc/examples/erase__IteratorType_IteratorType.cpp @@ -0,0 +1,30 @@ +#include + +using namespace nlohmann; + +int main() +{ + // create JSON values + json j_boolean = true; + json j_number_integer = 17; + json j_number_float = 23.42; + json j_object = {{"one", 1}, {"two", 2}}; + json j_array = {1, 2, 4, 8, 16}; + json j_string = "Hello, world"; + + // call erase + j_boolean.erase(j_boolean.begin(), j_boolean.end()); + j_number_integer.erase(j_number_integer.begin(), j_number_integer.end()); + j_number_float.erase(j_number_float.begin(), j_number_float.end()); + j_object.erase(j_object.find("two"), j_object.end()); + j_array.erase(j_array.begin() + 1, j_array.begin() + 3); + j_string.erase(j_string.begin(), j_string.end()); + + // print values + std::cout << j_boolean << '\n'; + std::cout << j_number_integer << '\n'; + std::cout << j_number_float << '\n'; + std::cout << j_object << '\n'; + std::cout << j_array << '\n'; + std::cout << j_string << '\n'; +} diff --git a/doc/examples/erase__IteratorType_IteratorType.output b/doc/examples/erase__IteratorType_IteratorType.output new file mode 100644 index 00000000..5d01f008 --- /dev/null +++ b/doc/examples/erase__IteratorType_IteratorType.output @@ -0,0 +1,6 @@ +null +null +null +{"one":1} +[1,8,16] +null diff --git a/doc/examples/erase__key_type.cpp b/doc/examples/erase__key_type.cpp new file mode 100644 index 00000000..a1d1e44d --- /dev/null +++ b/doc/examples/erase__key_type.cpp @@ -0,0 +1,17 @@ +#include + +using namespace nlohmann; + +int main() +{ + // create a JSON object + json j_object = {{"one", 1}, {"two", 2}}; + + // call erase + auto count_one = j_object.erase("one"); + auto count_three = j_object.erase("three"); + + // print values + std::cout << j_object << '\n'; + std::cout << count_one << " " << count_three << '\n'; +} diff --git a/doc/examples/erase__key_type.output b/doc/examples/erase__key_type.output new file mode 100644 index 00000000..28d79391 --- /dev/null +++ b/doc/examples/erase__key_type.output @@ -0,0 +1,2 @@ +{"two":2} +1 0 diff --git a/doc/examples/erase__size_type.cpp b/doc/examples/erase__size_type.cpp new file mode 100644 index 00000000..8578660e --- /dev/null +++ b/doc/examples/erase__size_type.cpp @@ -0,0 +1,15 @@ +#include + +using namespace nlohmann; + +int main() +{ + // create a JSON array + json j_array = {0, 1, 2, 3, 4, 5}; + + // call erase + j_array.erase(2); + + // print values + std::cout << j_array << '\n'; +} diff --git a/doc/examples/erase__size_type.output b/doc/examples/erase__size_type.output new file mode 100644 index 00000000..4ad74061 --- /dev/null +++ b/doc/examples/erase__size_type.output @@ -0,0 +1 @@ +[0,1,3,4,5] diff --git a/src/json.hpp b/src/json.hpp index bf5dccfa..67678d04 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -2456,22 +2456,54 @@ class basic_json return *tmp; } - /// remove element given an iterator - template ::value or - std::is_same::value + std::is_same::value or + std::is_same::value , int>::type = 0> - T erase(T pos) + InteratorType erase(InteratorType pos) { // make sure iterator fits the current value - if (this != pos.m_object or m_type != pos.m_object->m_type) + if (this != pos.m_object) { throw std::domain_error("iterator does not fit current value"); } - T result = end(); + InteratorType result = end(); switch (m_type) { @@ -2516,23 +2548,55 @@ class basic_json return result; } - /// remove elements given an iterator range - template ::value or - std::is_same::value + std::is_same::value or + std::is_same::value , int>::type = 0> - T erase(T first, T last) + InteratorType erase(InteratorType first, InteratorType last) { // make sure iterator fits the current value - if (this != first.m_object or this != last.m_object or - m_type != first.m_object->m_type or m_type != last.m_object->m_type) + if (this != first.m_object or this != last.m_object) { throw std::domain_error("iterators do not fit current value"); } - T result = end(); + InteratorType result = end(); switch (m_type) { @@ -2579,7 +2643,23 @@ class basic_json return result; } - /// remove element from an object given a key + /*! + @brief remove element from a JSON object given a key + + Removes elements from a JSON object with the key value @a key. + + @param[in] key value of the elements to remove + + @return Number of elements removed. If ObjectType is the default `std::map` + type, the return value will always be `0` (@a key was not found) or `1` (@a + key was found). + + @throw std::domain_error when called on a type other than JSON object + + @complexity `log(size()) + count(key)` + + @liveexample{The example shows the effect of erase.,erase__key_type} + */ size_type erase(const typename object_t::key_type& key) { // this erase only works for objects @@ -2591,7 +2671,20 @@ class basic_json return m_value.object->erase(key); } - /// remove element from an array given an index + /*! + @brief remove element from a JSON array given an index + + Removes element from a JSON array at the index @a idx. + + @param[in] idx index of the element to remove + + @throw std::domain_error when called on a type other than JSON array + @throw std::out_of_range when `idx >= size()` + + @complexity Linear in distance between @a idx and the end of the container. + + @liveexample{The example shows the effect of erase.,erase__size_type} + */ void erase(const size_type idx) { // this erase only works for arrays diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 891cb3b4..107260ac 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -2456,22 +2456,54 @@ class basic_json return *tmp; } - /// remove element given an iterator - template ::value or - std::is_same::value + std::is_same::value or + std::is_same::value , int>::type = 0> - T erase(T pos) + InteratorType erase(InteratorType pos) { // make sure iterator fits the current value - if (this != pos.m_object or m_type != pos.m_object->m_type) + if (this != pos.m_object) { throw std::domain_error("iterator does not fit current value"); } - T result = end(); + InteratorType result = end(); switch (m_type) { @@ -2516,23 +2548,55 @@ class basic_json return result; } - /// remove elements given an iterator range - template ::value or - std::is_same::value + std::is_same::value or + std::is_same::value , int>::type = 0> - T erase(T first, T last) + InteratorType erase(InteratorType first, InteratorType last) { // make sure iterator fits the current value - if (this != first.m_object or this != last.m_object or - m_type != first.m_object->m_type or m_type != last.m_object->m_type) + if (this != first.m_object or this != last.m_object) { throw std::domain_error("iterators do not fit current value"); } - T result = end(); + InteratorType result = end(); switch (m_type) { @@ -2579,7 +2643,23 @@ class basic_json return result; } - /// remove element from an object given a key + /*! + @brief remove element from a JSON object given a key + + Removes elements from a JSON object with the key value @a key. + + @param[in] key value of the elements to remove + + @return Number of elements removed. If ObjectType is the default `std::map` + type, the return value will always be `0` (@a key was not found) or `1` (@a + key was found). + + @throw std::domain_error when called on a type other than JSON object + + @complexity `log(size()) + count(key)` + + @liveexample{The example shows the effect of erase.,erase__key_type} + */ size_type erase(const typename object_t::key_type& key) { // this erase only works for objects @@ -2591,7 +2671,20 @@ class basic_json return m_value.object->erase(key); } - /// remove element from an array given an index + /*! + @brief remove element from a JSON array given an index + + Removes element from a JSON array at the index @a idx. + + @param[in] idx index of the element to remove + + @throw std::domain_error when called on a type other than JSON array + @throw std::out_of_range when `idx >= size()` + + @complexity Linear in distance between @a idx and the end of the container. + + @liveexample{The example shows the effect of erase.,erase__size_type} + */ void erase(const size_type idx) { // this erase only works for arrays