Ditto 4.11.1
 
Loading...
Searching...
No Matches
Differ.hpp
1#ifndef _DITTO_DIFFER_
2#define _DITTO_DIFFER_
3
4#include "QueryResult.hpp"
5
6#include <cstddef>
7#include <cstdint>
8#include <map>
9#include <memory>
10#include <set>
11#include <utility>
12
13namespace ditto {
14
18using IndexSet = std::set<std::size_t>;
19
26struct Diff {
27public:
33 struct Move {
37 std::size_t from;
38
42 std::size_t to;
43
47 Move(std::size_t from, std::size_t to) : from(from), to(to) {}
48
52 Move() : from(0), to(0) {}
53
54 Move(const Move &other) = default;
55 Move &operator=(const Move &other) = default;
56 Move(Move &&other) = default;
57 Move &operator=(Move &&other) = default;
58
59 bool operator==(const Move &other) const {
60 return from == other.from && to == other.to;
61 }
62
63 bool operator!=(const Move &other) const { return !(*this == other); }
64
65 bool operator<(const Move &other) const {
66 return from < other.from || (from == other.from && to < other.to);
67 }
68 };
69
74 IndexSet insertions;
75
80 IndexSet deletions;
81
86 IndexSet updates;
87
92 std::vector<Move> moves;
93
97 Diff() = default;
98
102 Diff(IndexSet insertions, IndexSet deletions, IndexSet updates,
103 std::vector<Move> moves)
104 : insertions(std::move(insertions)), deletions(std::move(deletions)),
105 updates(std::move(updates)), moves(std::move(moves)) {}
106
108
112 explicit Diff(const nlohmann::json &json);
114
115 Diff(const Diff &other) = default;
116 Diff &operator=(const Diff &other) = default;
117 Diff(Diff &&other) = default;
118 Diff &operator=(Diff &&other) = default;
119
123 bool operator==(const Diff &other) const;
124
128 bool operator!=(const Diff &other) const;
129};
130
137class Differ {
138private:
139 std::shared_ptr<dittoffi_differ_t> ffi_differ;
140
141public:
145 Differ();
146
147 ~Differ();
148
149 Differ(const Differ &) = delete;
150 Differ &operator=(const Differ &) = delete;
151 Differ(Differ &&) = default;
152 Differ &operator=(Differ &&) = default;
153
169 Diff diff(const std::vector<QueryResultItem> &items);
170};
171
172} // namespace ditto
173
174#endif
Diff diff(const std::vector< QueryResultItem > &items)
Calculate the diff of the provided items against the last set of items that were passed to this diffe...
Definition Differ.cpp:44
Differ()
Constructor.
Definition Differ.cpp:39
Representation of a move from one index to another in a vector.
Definition Differ.hpp:33
std::size_t from
The index in the old array from which the item has been moved.
Definition Differ.hpp:37
std::size_t to
The index in the new array to which the item has been moved.
Definition Differ.hpp:42
Move()
Default constructor.
Definition Differ.hpp:52
Move(std::size_t from, std::size_t to)
Constructor with initial values.
Definition Differ.hpp:47
Represents differences between two arrays of items.
Definition Differ.hpp:26
IndexSet insertions
The set of indexes in the new array at which the new items have been inserted.
Definition Differ.hpp:74
Diff()=default
Default constructor.
IndexSet updates
The set of indexes in the new array at which the items have been updated.
Definition Differ.hpp:86
IndexSet deletions
The set of indexes in the old array at which the items have been deleted.
Definition Differ.hpp:80
std::vector< Move > moves
A set of objects each representing a move of an item from a particular index in the old array to a pa...
Definition Differ.hpp:92
Diff(IndexSet insertions, IndexSet deletions, IndexSet updates, std::vector< Move > moves)
Constructor with initial values.
Definition Differ.hpp:102
bool operator==(const Diff &other) const
Test for equality between two diffs.
Definition Differ.cpp:28
bool operator!=(const Diff &other) const
Test for inequality between two diffs.
Definition Differ.cpp:33