Skip to content

Commit

Permalink
Refactor object::erase
Browse files Browse the repository at this point in the history
  • Loading branch information
Guðmundur F. Aðalsteinsson committed Sep 16, 2022
1 parent e108860 commit 47f2ad5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 41 deletions.
85 changes: 46 additions & 39 deletions include/boost/json/impl/object.ipp
Expand Up @@ -508,30 +508,17 @@ object::
erase(const_iterator pos) noexcept ->
iterator
{
auto p = begin() + (pos - begin());
if(t_->is_small())
{
p->~value_type();
--t_->size;
auto const pb = end();
if(p != end())
{
return do_erase(pos,
[this](iterator p) {
// the casts silence warnings
std::memcpy(
static_cast<void*>(p),
static_cast<void const*>(pb),
static_cast<void const*>(end()),
sizeof(*p));
}
return p;
}
remove(t_->bucket(p->key()), *p);
p->~value_type();
--t_->size;
if(p != end())
{
reindex_relocate(end(), p);
}
return p;
},
[this](iterator p) {
reindex_relocate(end(), p);
});
}

auto
Expand All @@ -551,30 +538,20 @@ object::
stable_erase(const_iterator pos) noexcept ->
iterator
{
auto p = begin() + (pos - begin());
if(t_->is_small())
{
p->~value_type();
--t_->size;
if(p != end())
{
return do_erase(pos,
[this](iterator p) {
// the casts silence warnings
std::memmove(
static_cast<void*>(p),
static_cast<void const*>(p + 1),
sizeof(*p) * (end() - p));
}
return p;
}
remove(t_->bucket(p->key()), *p);
p->~value_type();
--t_->size;
auto pret = p;
for (; p != end(); ++p)
{
reindex_relocate(p + 1, p);
}
return pret;
},
[this](iterator p) {
for (; p != end(); ++p)
{
reindex_relocate(p + 1, p);
}
});
}

auto
Expand Down Expand Up @@ -863,6 +840,36 @@ destroy(
(--last)->~key_value_pair();
}

template<class FS, class FB>
auto
object::
do_erase(
const_iterator pos,
FS small_reloc,
FB big_reloc) noexcept
-> iterator
{
auto p = begin() + (pos - begin());
if(t_->is_small())
{
p->~value_type();
--t_->size;
if(p != end())
{
small_reloc(p);
}
return p;
}
remove(t_->bucket(p->key()), *p);
p->~value_type();
--t_->size;
if(p != end())
{
big_reloc(p);
}
return p;
}

void
object::
reindex_relocate(
Expand Down
12 changes: 10 additions & 2 deletions include/boost/json/object.hpp
Expand Up @@ -1176,7 +1176,7 @@ class object
References and iterators from `pos` to `end()`, both
included, are invalidated. Other iterators and references
are not invalidated.
The relative order of non-erased elements is preserved.
The relative order of remaining elements is preserved.
@note
Expand All @@ -1202,7 +1202,7 @@ class object
Remove the element which matches `key`, if it exists.
All references and iterators are invalidated.
The relative order of non-erased elements is preserved.
The relative order of remaining elements is preserved.
@par Complexity
Linear in @ref size().
Expand Down Expand Up @@ -1621,6 +1621,14 @@ class object
key_value_pair* first,
key_value_pair* last) noexcept;

template<class FS, class FB>
auto
do_erase(
const_iterator pos,
FS small_reloc,
FB big_reloc) noexcept
-> iterator;

inline
void
reindex_relocate(
Expand Down

0 comments on commit 47f2ad5

Please sign in to comment.