Skip to content

Commit

Permalink
Shuffle stuff around and clean some things up
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoyne committed Sep 9, 2015
1 parent eeb2ddd commit b129ebe
Show file tree
Hide file tree
Showing 6 changed files with 441 additions and 354 deletions.
40 changes: 3 additions & 37 deletions index_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,7 @@

using namespace realm;

size_t IndexSet::size() const
{
size_t size = 0;
for (auto const& range : m_ranges) {
size += range.second - range.first;
}
return size;
}

std::vector<IndexSet::Range>::iterator IndexSet::find(size_t index)
IndexSet::iterator IndexSet::find(size_t index)
{
for (auto it = m_ranges.begin(), end = m_ranges.end(); it != end; ++it) {
if (it->second > index)
Expand All @@ -43,14 +34,14 @@ void IndexSet::add(size_t index)
do_add(find(index), index);
}

void IndexSet::do_add(std::vector<Range>::iterator it, size_t index)
void IndexSet::do_add(iterator it, size_t index)
{
bool more_before = it != m_ranges.begin(), valid = it != m_ranges.end();
if (valid && it->first <= index && it->second > index) {
// index is already in set
}
else if (more_before && (it - 1)->second == index) {
// index is immediate after an existing range
// index is immediately after an existing range
++(it - 1)->second;
}
else if (more_before && valid && (it - 1)->second == it->first) {
Expand Down Expand Up @@ -99,28 +90,3 @@ void IndexSet::add_shifted(size_t index)
}
do_add(it, index);
}

size_t IndexSet::iterator::operator*() const
{
return m_data->first + m_offset;
}

IndexSet::iterator& IndexSet::iterator::operator++()
{
++m_offset;
if (m_offset + m_data->first == m_data->second) {
++m_data;
m_offset = 0;
}
return *this;
}

bool IndexSet::iterator::operator==(iterator other) const
{
return m_data == other.m_data && m_offset == other.m_offset;
}

bool IndexSet::iterator::operator!=(iterator other) const
{
return m_data != other.m_data || m_offset != other.m_offset;
}
42 changes: 20 additions & 22 deletions index_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,38 @@
namespace realm {
class IndexSet {
public:
struct iterator {
size_t operator*() const;
iterator& operator++();
bool operator==(iterator) const;
bool operator!=(iterator) const;
using value_type = std::pair<size_t, size_t>;
using iterator = std::vector<value_type>::iterator;
using const_iterator = std::vector<value_type>::const_iterator;

iterator(std::pair<size_t, size_t>* data) noexcept : m_data(data) { }

private:
std::pair<size_t, size_t>* m_data;
size_t m_offset = 0;
};

iterator begin() { return iterator(&m_ranges[0]); }
iterator end() { return iterator(&m_ranges[m_ranges.size()]); }

size_t size() const;
const_iterator begin() const { return m_ranges.begin(); }
const_iterator end() const { return m_ranges.end(); }
bool empty() const { return m_ranges.empty(); }
size_t size() const { return m_ranges.size(); }

// Add an index to the set, doing nothing if it's already present
void add(size_t index);
// Set the index set to a single range starting at 0 with length `len`

// Remove all indexes from the set and then add a single range starting from
// zero with the given length
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 at or
// after that point back by one
void insert_at(size_t index);

// Add an index which has had all of the ranges in the set before it removed
void add_shifted(size_t index);

private:
using Range = std::pair<size_t, size_t>;
std::vector<Range> m_ranges;
std::vector<value_type> m_ranges;

// Find the range which contains the index, or the first one after it if
// none do
std::vector<Range>::iterator find(size_t index);
void do_add(std::vector<Range>::iterator pos, size_t index);
iterator find(size_t index);
// Insert the index before the given position, combining existing ranges as
// applicable
void do_add(iterator pos, size_t index);
};
} // namespace realm

Expand Down
22 changes: 19 additions & 3 deletions realm_delegate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class RealmDelegate {
public:
virtual ~RealmDelegate() = default;

// Change information for a single field of a row
struct ColumnInfo {
bool changed = false;
enum class Kind {
Expand All @@ -41,10 +42,19 @@ class RealmDelegate {
IndexSet indices;
};

// Information about an observed row in a table
struct ObserverState {
// Initial table and row which is observed
// May be updated by row insertions and removals
size_t table_ndx;
size_t row_ndx;
void* info; // opaque user info

// Opaque userdata for the delegate's use
void* info;

// Populated with information about which columns were changed
// May be shorter than the actual number of columns if the later columns
// are not modified
std::vector<ColumnInfo> changes;

// Simple lexographic ordering
Expand All @@ -68,10 +78,16 @@ class RealmDelegate {

// The Realm's read version will change
// Only called if get_observed_row() returned a non-empty array.
virtual void will_change(std::vector<ObserverState> const&, std::vector<void*> const&) = 0;
// observers is the vector returned from get_observed_rows()
// invalidated is the `info` pointers for each observed object which was deleted
virtual void will_change(std::vector<ObserverState> const& observers,
std::vector<void*> const& invalidated) = 0;

// The Realm's read version has changed
virtual void did_change(std::vector<ObserverState> const&, std::vector<void*> const&) = 0;
// observers is the vector returned from get_observed_rows()
// invalidated is the `info` pointers for each observed object which was deleted
virtual void did_change(std::vector<ObserverState> const& observers,
std::vector<void*> const& invalidated) = 0;
};
} // namespace realm

Expand Down
Loading

0 comments on commit b129ebe

Please sign in to comment.