From 139ef0e750330ab71286e83a26fffd24829680a0 Mon Sep 17 00:00:00 2001 From: Niels Date: Sun, 12 Apr 2015 14:14:00 +0200 Subject: [PATCH] implemented front() and back() --- src/json.hpp | 32 ++++++++++++++++- src/json.hpp.re2c | 28 +++++++++++++++ test/unit.cpp | 91 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 1 deletion(-) diff --git a/src/json.hpp b/src/json.hpp index af8e490d..593a9f20 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -587,7 +587,7 @@ class basic_json alloc.construct(m_value.array, count, other); } - // construct a JSON container given an iterator range + /// construct a JSON container given an iterator range template ::value or @@ -603,8 +603,10 @@ class basic_json throw std::runtime_error("iterators are not compatible"); } + // set the type m_type = first.m_object->m_type; + // check if iterator range is complete for non-compound values switch (m_type) { case value_t::number_integer: @@ -1261,6 +1263,34 @@ class basic_json return m_value.object->operator[](key); } + /// access the first element + inline reference front() + { + return *begin(); + } + + /// access the first element + inline const_reference front() const + { + return *cbegin(); + } + + /// access the last element + inline reference back() + { + auto tmp = end(); + --tmp; + return *tmp; + } + + /// access the last element + inline const_reference back() const + { + auto tmp = cend(); + --tmp; + return *tmp; + } + /// remove element given an iterator template operator[](key); } + /// access the first element + inline reference front() + { + return *begin(); + } + + /// access the first element + inline const_reference front() const + { + return *cbegin(); + } + + /// access the last element + inline reference back() + { + auto tmp = end(); + --tmp; + return *tmp; + } + + /// access the last element + inline const_reference back() const + { + auto tmp = cend(); + --tmp; + return *tmp; + } + /// remove element given an iterator template