1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2016 Realm Inc.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 ////////////////////////////////////////////////////////////////////////////
19 #ifndef REALM_COLLECTION_CHANGE_BUILDER_HPP
20 #define REALM_COLLECTION_CHANGE_BUILDER_HPP
22 #include "collection_notifications.hpp"
24 #include <realm/util/optional.hpp>
26 #include <unordered_map>
30 class CollectionChangeBuilder : public CollectionChangeSet {
32 CollectionChangeBuilder(CollectionChangeBuilder const&) = default;
33 CollectionChangeBuilder(CollectionChangeBuilder&&) = default;
34 CollectionChangeBuilder& operator=(CollectionChangeBuilder const&) = default;
35 CollectionChangeBuilder& operator=(CollectionChangeBuilder&&) = default;
37 CollectionChangeBuilder(IndexSet deletions = {},
38 IndexSet insertions = {},
39 IndexSet modification = {},
40 std::vector<Move> moves = {});
42 // Calculate where rows need to be inserted or deleted from old_rows to turn
43 // it into new_rows, and check all matching rows for modifications
44 // If `move_candidates` is supplied they it will be used to do more accurate
45 // determination of which rows moved. This is only supported when the rows
46 // are in table order (i.e. not sorted or from a LinkList)
47 static CollectionChangeBuilder calculate(std::vector<size_t> const& old_rows,
48 std::vector<size_t> const& new_rows,
49 std::function<bool (size_t)> row_did_change,
50 util::Optional<IndexSet> const& move_candidates = util::none);
52 // generic operations {
53 CollectionChangeSet finalize() &&;
54 void merge(CollectionChangeBuilder&&);
56 void insert(size_t ndx, size_t count=1, bool track_moves=true);
57 void modify(size_t ndx, size_t col=-1);
58 void erase(size_t ndx);
59 void clear(size_t old_size);
62 // operations only implemented for LinkList semantics {
63 void clean_up_stale_moves();
64 void move(size_t from, size_t to);
67 // operations only implemented for Row semantics {
68 void move_over(size_t ndx, size_t last_ndx, bool track_moves=true);
69 // must be followed by move_over(old_ndx, ...)
70 // precondition: `new_ndx` must be a new insertion
71 void subsume(size_t old_ndx, size_t new_ndx, bool track_moves=true);
72 void swap(size_t ndx_1, size_t ndx_2, bool track_moves=true);
74 void parse_complete();
77 void insert_column(size_t ndx);
78 void move_column(size_t from, size_t to);
81 std::unordered_map<size_t, size_t> m_move_mapping;
82 bool m_track_columns = true;
84 template<typename Func>
85 void for_each_col(Func&& f);
92 #endif // REALM_COLLECTION_CHANGE_BUILDER_HPP