Skip to content

Commit

Permalink
feat(crdt): add CrdtSortedSet and improve change compression
Browse files Browse the repository at this point in the history
Add CrdtSortedSet type, include <set> header, and enhance ChangeComparator
  • Loading branch information
sinkingsugar committed Oct 1, 2024
1 parent 8169dd4 commit e59f9ac
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions crdt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <vector>

template <typename T> using CrdtVector = std::vector<T>;
using CrdtString = std::string;
template <typename K, typename V> using CrdtMap = std::unordered_map<K, V>;
template <typename K> using CrdtSet = std::unordered_set<K>;
template <typename T, typename Comparator> using CrdtSortedSet = std::set<T, Comparator>;
using CrdtNodeId = uint64_t;
#endif

Expand Down Expand Up @@ -117,6 +119,8 @@ template <typename K, typename V> struct ChangeComparator {
if (a.db_version != b.db_version)
return a.db_version > b.db_version;
return a.node_id > b.node_id;
// Add this line:
return false; // Consider equal if all fields match
}
};

Expand Down Expand Up @@ -543,9 +547,9 @@ template <typename K, typename V> class CRDT : public std::enable_shared_from_th
/// * `changes` - A vector of changes to compress (will be modified in-place).
///
/// Complexity: O(n log n), where n is the number of changes
template <bool Sorted = false>
static void compress_changes(CrdtVector<Change<K, V>> &changes) {
if (changes.empty()) return;
template <bool Sorted = false> static void compress_changes(CrdtVector<Change<K, V>> &changes) {
if (changes.empty())
return;

auto new_end = compress_changes<Sorted>(changes.begin(), changes.end());
changes.erase(new_end, changes.end());
Expand All @@ -563,9 +567,9 @@ template <typename K, typename V> class CRDT : public std::enable_shared_from_th
/// Iterator to the new end of the range after compression.
///
/// Complexity: O(n log n), where n is the number of changes
template <bool Sorted = false, typename Iterator>
static Iterator compress_changes(Iterator begin, Iterator end) {
if (begin == end) return end;
template <bool Sorted = false, typename Iterator> static Iterator compress_changes(Iterator begin, Iterator end) {
if (begin == end)
return end;

if constexpr (!Sorted) {
// Sort changes using the ChangeComparator
Expand Down

0 comments on commit e59f9ac

Please sign in to comment.