diff --git a/index_set.cpp b/index_set.cpp index d3f70047..3fe27a46 100644 --- a/index_set.cpp +++ b/index_set.cpp @@ -91,6 +91,15 @@ void IndexSet::insert_at(size_t index) do_add(pos, index); } +void IndexSet::add_shifted(size_t index) +{ + auto it = m_ranges.begin(); + for (auto end = m_ranges.end(); it != end && it->first <= index; ++it) { + index += it->second - it->first; + } + do_add(it, index); +} + size_t IndexSet::iterator::operator*() const { return m_data->first + m_offset; diff --git a/index_set.hpp b/index_set.hpp index 4e55c40f..03d036fe 100644 --- a/index_set.hpp +++ b/index_set.hpp @@ -48,6 +48,7 @@ public: void set(size_t len); // Insert an index at the given position, shifting existing indexes back void insert_at(size_t index); + void add_shifted(size_t index); private: using Range = std::pair; diff --git a/shared_realm.cpp b/shared_realm.cpp index 6acd7593..8f3e416b 100644 --- a/shared_realm.cpp +++ b/shared_realm.cpp @@ -193,14 +193,7 @@ public: } else if (o->kind == kind) { if (kind == ColumnInfo::Kind::Remove) { - // Shift the index to compensate for already-removed indices - for (auto i : o->indices) { - if (i <= index) - ++index; - else - break; - } - o->indices.add(index); + o->indices.add_shifted(index); } else if (kind == ColumnInfo::Kind::Insert) { o->indices.insert_at(index);