Improve array KVO performance a bit

This commit is contained in:
Thomas Goyne 2015-09-02 11:04:01 -07:00 committed by Ari Lazier
parent e65ad57e00
commit 8350e55a0c
3 changed files with 11 additions and 8 deletions

View File

@ -91,6 +91,15 @@ void IndexSet::insert_at(size_t index)
do_add(pos, 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 size_t IndexSet::iterator::operator*() const
{ {
return m_data->first + m_offset; return m_data->first + m_offset;

View File

@ -48,6 +48,7 @@ public:
void set(size_t len); void set(size_t len);
// Insert an index at the given position, shifting existing indexes back // Insert an index at the given position, shifting existing indexes back
void insert_at(size_t index); void insert_at(size_t index);
void add_shifted(size_t index);
private: private:
using Range = std::pair<size_t, size_t>; using Range = std::pair<size_t, size_t>;

View File

@ -193,14 +193,7 @@ public:
} }
else if (o->kind == kind) { else if (o->kind == kind) {
if (kind == ColumnInfo::Kind::Remove) { if (kind == ColumnInfo::Kind::Remove) {
// Shift the index to compensate for already-removed indices o->indices.add_shifted(index);
for (auto i : o->indices) {
if (i <= index)
++index;
else
break;
}
o->indices.add(index);
} }
else if (kind == ColumnInfo::Kind::Insert) { else if (kind == ColumnInfo::Kind::Insert) {
o->indices.insert_at(index); o->indices.insert_at(index);