TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ erase() [3/4]

template<template< typename U, typename V, typename... Args > class ObjectType = std::map, template< typename U, typename... Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer, class BinaryType = std::vector<std::uint8_t>>
template<class IteratorType , typename std::enable_if< std::is_same< IteratorType, typename basic_json_t::iterator >::value||std::is_same< IteratorType, typename basic_json_t::const_iterator >::value, int >::type = 0>
IteratorType nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::erase ( IteratorType  first,
IteratorType  last 
)
inline

remove elements given an iterator range

Removes the element specified by the range [first; last). The iterator first does not need to be dereferenceable if first == last: erasing an empty range is a no-op.

If called on a primitive type other than null, the resulting JSON value will be null.

Parameters
[in]firstiterator to the beginning of the range to remove
[in]lastiterator past the end of the range to remove
Returns
Iterator following the last removed element. If the iterator second refers to the last element, the end() iterator is returned.
Template Parameters
IteratorTypean iterator or const_iterator
Postcondition
Invalidates iterators and references at or after the point of the erase, including the end() iterator.
Exceptions
type_error.307if called on a null value; example: "cannot use erase() with null"
invalid_iterator.203if called on iterators which does not belong to the current JSON value; example: "iterators do not fit current value"
invalid_iterator.204if called on a primitive type with invalid iterators (i.e., if first != begin() and last != end()); example: "iterators out of range"

@complexity The complexity depends on the type:

  • objects: log(size()) + std::distance(first, last)
  • arrays: linear in the distance between first and last, plus linear in the distance between last and end of the container
  • strings and binary: linear in the length of the member
  • other types: constant

@liveexample{The example shows the result of erase() for different JSON types.,erase__IteratorType_IteratorType}

See also
see erase(IteratorType) – removes the element at a given position
see erase(const typename object_t::key_type&) – removes the element from an object at the given key
see erase(const size_type) – removes the element from an array at the given index
Since
version 1.0.0

Definition at line 22036 of file json.hpp.

22037 {
22038 // make sure iterator fits the current value
22039 if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object))
22040 {
22041 JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", *this));
22042 }
22043
22044 IteratorType result = end();
22045
22046 switch (m_type)
22047 {
22048 case value_t::boolean:
22052 case value_t::string:
22053 case value_t::binary:
22054 {
22055 if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin()
22056 || !last.m_it.primitive_iterator.is_end()))
22057 {
22058 JSON_THROW(invalid_iterator::create(204, "iterators out of range", *this));
22059 }
22060
22061 if (is_string())
22062 {
22063 AllocatorType<string_t> alloc;
22064 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
22065 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
22066 m_value.string = nullptr;
22067 }
22068
22069 else if (is_binary())
22070 {
22071 AllocatorType<binary_t> alloc;
22072 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.binary);
22073 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.binary, 1);
22074 m_value.binary = nullptr;
22075 }
22076
22077 m_type = value_t::null;
22079 break;
22080 }
22081
22082 case value_t::object:
22083 {
22084 result.m_it.object_iterator = m_value.object->erase(first.m_it.object_iterator,
22085 last.m_it.object_iterator);
22086 break;
22087 }
22088
22089 case value_t::array:
22090 {
22091 result.m_it.array_iterator = m_value.array->erase(first.m_it.array_iterator,
22092 last.m_it.array_iterator);
22093 break;
22094 }
22095
22096 case value_t::null:
22097 case value_t::discarded:
22098 default:
22099 JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this));
22100 }
22101
22102 return result;
22103 }
void assert_invariant(bool check_parents=true) const noexcept
checks the class invariants
Definition: json.hpp:18935
json_value m_value
the value of the current element
Definition: json.hpp:24928
iterator end() noexcept
returns an iterator to one past the last element
Definition: json.hpp:22448
constexpr bool is_binary() const noexcept
return whether value is a binary array
Definition: json.hpp:20476
constexpr bool is_string() const noexcept
return whether value is a string
Definition: json.hpp:20454
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:24883
@ number_integer
number value (signed integer)
@ discarded
discarded by the parser callback function
@ binary
binary array (ordered collection of bytes)
@ object
object (unordered set of name/value pairs)
@ number_float
number value (floating-point)
@ number_unsigned
number value (unsigned integer)
@ array
array (ordered collection of values)