mirror of
https://github.com/Green-Sky/crdt_tests.git
synced 2024-12-22 15:53:24 +01:00
minor cleanup and notes
This commit is contained in:
parent
b5c312a558
commit
d047e44fb0
@ -35,6 +35,7 @@ struct List {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// TODO: move ops up?
|
||||||
// almost the same as entry
|
// almost the same as entry
|
||||||
struct OpAdd {
|
struct OpAdd {
|
||||||
ListID id;
|
ListID id;
|
||||||
@ -61,7 +62,7 @@ struct List {
|
|||||||
std::optional<ValueType> value;
|
std::optional<ValueType> value;
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: use something better
|
// TODO: use something better, edit: this seems fine
|
||||||
std::vector<Entry> list;
|
std::vector<Entry> list;
|
||||||
|
|
||||||
// number of not deleted entries
|
// number of not deleted entries
|
||||||
@ -192,7 +193,6 @@ struct List {
|
|||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
list.emplace(list.begin() + insert_idx, Entry{
|
list.emplace(list.begin() + insert_idx, Entry{
|
||||||
@ -227,12 +227,11 @@ struct List {
|
|||||||
}
|
}
|
||||||
|
|
||||||
verify();
|
verify();
|
||||||
assert(false);
|
|
||||||
|
|
||||||
// not found
|
// not found
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: only in debug?
|
||||||
void verify(void) const {
|
void verify(void) const {
|
||||||
size_t actual_size = 0;
|
size_t actual_size = 0;
|
||||||
for (const auto& it : list) {
|
for (const auto& it : list) {
|
||||||
|
@ -320,7 +320,7 @@ void test2CharDocAdds(size_t seed) {
|
|||||||
// for modifying
|
// for modifying
|
||||||
Doc doctmp = doc;
|
Doc doctmp = doc;
|
||||||
|
|
||||||
const size_t loop_count = (rng() % 4)+1;
|
const size_t loop_count = (rng() % 6)+1;
|
||||||
for (size_t i = 0; i < loop_count; i++) {
|
for (size_t i = 0; i < loop_count; i++) {
|
||||||
genAdd(rng, doctmp);
|
genAdd(rng, doctmp);
|
||||||
}
|
}
|
||||||
@ -364,14 +364,14 @@ void testChange1(size_t seed) {
|
|||||||
Doc doctmp = doc;
|
Doc doctmp = doc;
|
||||||
|
|
||||||
{ // dels
|
{ // dels
|
||||||
const size_t loop_count = (rng() % 4)+1;
|
const size_t loop_count = (rng() % 6)+1;
|
||||||
for (size_t i = 0; i < loop_count; i++) {
|
for (size_t i = 0; i < loop_count; i++) {
|
||||||
genDel(rng, doctmp);
|
genDel(rng, doctmp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
{ // adds
|
{ // adds
|
||||||
const size_t loop_count = (rng() % 4)+1;
|
const size_t loop_count = (rng() % 6)+1;
|
||||||
for (size_t i = 0; i < loop_count; i++) {
|
for (size_t i = 0; i < loop_count; i++) {
|
||||||
genAdd(rng, doctmp);
|
genAdd(rng, doctmp);
|
||||||
}
|
}
|
||||||
@ -399,9 +399,10 @@ void testChange1(size_t seed) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
const size_t loops = 10'000;
|
||||||
{
|
{
|
||||||
std::cout << "testEmptyDocAdds:\n";
|
std::cout << "testEmptyDocAdds:\n";
|
||||||
for (size_t i = 0; i < 1'000; i++) {
|
for (size_t i = 0; i < loops; i++) {
|
||||||
std::cout << "i " << i << "\n";
|
std::cout << "i " << i << "\n";
|
||||||
testEmptyDocAdds(1337+i);
|
testEmptyDocAdds(1337+i);
|
||||||
std::cout << std::string(40, '-') << "\n";
|
std::cout << std::string(40, '-') << "\n";
|
||||||
@ -412,7 +413,7 @@ int main(void) {
|
|||||||
|
|
||||||
{
|
{
|
||||||
std::cout << "test1CharDocAdds:\n";
|
std::cout << "test1CharDocAdds:\n";
|
||||||
for (size_t i = 0; i < 1'000; i++) {
|
for (size_t i = 0; i < loops; i++) {
|
||||||
std::cout << "i " << i << "\n";
|
std::cout << "i " << i << "\n";
|
||||||
test1CharDocAdds(1337+i);
|
test1CharDocAdds(1337+i);
|
||||||
std::cout << std::string(40, '-') << "\n";
|
std::cout << std::string(40, '-') << "\n";
|
||||||
@ -423,7 +424,7 @@ int main(void) {
|
|||||||
|
|
||||||
{
|
{
|
||||||
std::cout << "test1CharDocDels:\n";
|
std::cout << "test1CharDocDels:\n";
|
||||||
for (size_t i = 0; i < 1'000; i++) {
|
for (size_t i = 0; i < loops; i++) {
|
||||||
std::cout << "i " << i << "\n";
|
std::cout << "i " << i << "\n";
|
||||||
test1CharDocDels(1337+i);
|
test1CharDocDels(1337+i);
|
||||||
std::cout << std::string(40, '-') << "\n";
|
std::cout << std::string(40, '-') << "\n";
|
||||||
@ -434,7 +435,7 @@ int main(void) {
|
|||||||
|
|
||||||
{
|
{
|
||||||
std::cout << "test2CharDocAdds:\n";
|
std::cout << "test2CharDocAdds:\n";
|
||||||
for (size_t i = 0; i < 1'000; i++) {
|
for (size_t i = 0; i < loops; i++) {
|
||||||
std::cout << "i " << i << "\n";
|
std::cout << "i " << i << "\n";
|
||||||
test2CharDocAdds(1337+i);
|
test2CharDocAdds(1337+i);
|
||||||
std::cout << std::string(40, '-') << "\n";
|
std::cout << std::string(40, '-') << "\n";
|
||||||
@ -445,7 +446,7 @@ int main(void) {
|
|||||||
|
|
||||||
{
|
{
|
||||||
std::cout << "testChange1:\n";
|
std::cout << "testChange1:\n";
|
||||||
for (size_t i = 0; i < 1'000; i++) {
|
for (size_t i = 0; i < loops; i++) {
|
||||||
std::cout << "i " << i << "\n";
|
std::cout << "i " << i << "\n";
|
||||||
testChange1(1337+i);
|
testChange1(1337+i);
|
||||||
std::cout << std::string(40, '-') << "\n";
|
std::cout << std::string(40, '-') << "\n";
|
||||||
|
@ -2,11 +2,9 @@
|
|||||||
|
|
||||||
#include "./list.hpp"
|
#include "./list.hpp"
|
||||||
|
|
||||||
#include <optional>
|
|
||||||
#include <variant>
|
#include <variant>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <iostream> // debug
|
//#include <iostream> // debug
|
||||||
|
|
||||||
namespace GreenCRDT {
|
namespace GreenCRDT {
|
||||||
|
|
||||||
@ -17,6 +15,7 @@ struct TextDocument {
|
|||||||
|
|
||||||
using Op = std::variant<typename ListType::OpAdd, typename ListType::OpDel>;
|
using Op = std::variant<typename ListType::OpAdd, typename ListType::OpDel>;
|
||||||
|
|
||||||
|
// TODO: implement
|
||||||
struct Cursor {
|
struct Cursor {
|
||||||
AgentType who;
|
AgentType who;
|
||||||
typename ListType::ListID pos;
|
typename ListType::ListID pos;
|
||||||
@ -163,23 +162,6 @@ struct TextDocument {
|
|||||||
// generates ops from the difference
|
// generates ops from the difference
|
||||||
// note: rn it only creates 1 diff patch
|
// note: rn it only creates 1 diff patch
|
||||||
std::vector<Op> merge(std::string_view text) {
|
std::vector<Op> merge(std::string_view text) {
|
||||||
// cases:
|
|
||||||
// - [ ] text is empty
|
|
||||||
// - [x] doc is empty (deep)
|
|
||||||
// - [ ] doc is empty (shallow) // interesting?
|
|
||||||
//
|
|
||||||
// - [x] no changes -> change_start will go through to end
|
|
||||||
//
|
|
||||||
// not at start or end:
|
|
||||||
// - [ ] single char added -> doc.start > doc.end && text.start == text.end -> emit add
|
|
||||||
// - [ ] single char deleted -> doc.start == doc.end && text.start > text.end -> emit del
|
|
||||||
// - [ ] single char replaced -> doc.start == doc.end && text.start == text.end -> emit del, emit add
|
|
||||||
//
|
|
||||||
// - [ ] 2 chars added(together) -> doc.start > doc.end && text.start < text.end -> emit 2add
|
|
||||||
// - [ ] 2 chars deleted(together) -> doc.start < doc.end && text.start > text.end -> emit 2del
|
|
||||||
// - [ ] 2 chars replaced(together) -> doc.start == doc.end && text.start == text.end -> emit 2del, 2add
|
|
||||||
|
|
||||||
|
|
||||||
if (text.empty()) {
|
if (text.empty()) {
|
||||||
if (state.list.empty()) {
|
if (state.list.empty()) {
|
||||||
// no op
|
// no op
|
||||||
@ -198,7 +180,6 @@ struct TextDocument {
|
|||||||
text
|
text
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// neither empty
|
// neither empty
|
||||||
|
|
||||||
// find start and end of changes
|
// find start and end of changes
|
||||||
@ -227,8 +208,8 @@ struct TextDocument {
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "list.size: " << state.list.size() << "(" << getText().size() << ")" << " text.size: " << text.size() << "\n";
|
//std::cout << "list.size: " << state.list.size() << "(" << getText().size() << ")" << " text.size: " << text.size() << "\n";
|
||||||
std::cout << "list_start: " << list_start << " text_start: " << text_start << "\n";
|
//std::cout << "list_start: " << list_start << " text_start: " << text_start << "\n";
|
||||||
|
|
||||||
// +1 so i can have unsigned
|
// +1 so i can have unsigned
|
||||||
size_t list_end = state.list.size();
|
size_t list_end = state.list.size();
|
||||||
@ -248,7 +229,7 @@ struct TextDocument {
|
|||||||
text_end--;
|
text_end--;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "list_end: " << list_end << " text_end: " << text_end << "\n";
|
//std::cout << "list_end: " << list_end << " text_end: " << text_end << "\n";
|
||||||
|
|
||||||
std::vector<Op> ops;
|
std::vector<Op> ops;
|
||||||
|
|
||||||
@ -258,10 +239,10 @@ struct TextDocument {
|
|||||||
state.list[list_start].id,
|
state.list[list_start].id,
|
||||||
list_end < state.list.size() ? std::make_optional(state.list[list_end].id) : std::nullopt
|
list_end < state.list.size() ? std::make_optional(state.list[list_end].id) : std::nullopt
|
||||||
);
|
);
|
||||||
std::cout << "deleted: " << ops.size() << "\n";
|
//std::cout << "deleted: " << ops.size() << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "text between: " << getText() << "\n";
|
//std::cout << "text between: " << getText() << "\n";
|
||||||
|
|
||||||
// 2. add range (add all text_start - text_end)
|
// 2. add range (add all text_start - text_end)
|
||||||
if (text_start < text_end) {
|
if (text_start < text_end) {
|
||||||
@ -270,7 +251,7 @@ struct TextDocument {
|
|||||||
list_start == state.list.size() ? std::nullopt :std::make_optional(state.list.at(list_start).id),
|
list_start == state.list.size() ? std::nullopt :std::make_optional(state.list.at(list_start).id),
|
||||||
text.substr(text_start, text_end-text_start)
|
text.substr(text_start, text_end-text_start)
|
||||||
);
|
);
|
||||||
std::cout << "added: " << tmp_add_ops.size() << "\n";
|
//std::cout << "added: " << tmp_add_ops.size() << "\n";
|
||||||
ops.insert(ops.end(), tmp_add_ops.begin(), tmp_add_ops.end());
|
ops.insert(ops.end(), tmp_add_ops.begin(), tmp_add_ops.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user