From d047e44fb02b5ead6c17f8e029e668d5e158db7d Mon Sep 17 00:00:00 2001 From: Green Sky Date: Fri, 16 Dec 2022 16:21:28 +0100 Subject: [PATCH] minor cleanup and notes --- version0/list.hpp | 7 +++---- version0/test2.cpp | 17 +++++++++-------- version0/text_document.hpp | 35 ++++++++--------------------------- 3 files changed, 20 insertions(+), 39 deletions(-) diff --git a/version0/list.hpp b/version0/list.hpp index b348fb3..11e77a0 100644 --- a/version0/list.hpp +++ b/version0/list.hpp @@ -35,6 +35,7 @@ struct List { } }; + // TODO: move ops up? // almost the same as entry struct OpAdd { ListID id; @@ -61,7 +62,7 @@ struct List { std::optional value; }; - // TODO: use something better + // TODO: use something better, edit: this seems fine std::vector list; // number of not deleted entries @@ -192,7 +193,6 @@ struct List { // do nothing } } - } list.emplace(list.begin() + insert_idx, Entry{ @@ -227,12 +227,11 @@ struct List { } verify(); - assert(false); - // not found return false; } + // TODO: only in debug? void verify(void) const { size_t actual_size = 0; for (const auto& it : list) { diff --git a/version0/test2.cpp b/version0/test2.cpp index a3616dc..6b02983 100644 --- a/version0/test2.cpp +++ b/version0/test2.cpp @@ -320,7 +320,7 @@ void test2CharDocAdds(size_t seed) { // for modifying 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++) { genAdd(rng, doctmp); } @@ -364,14 +364,14 @@ void testChange1(size_t seed) { Doc doctmp = doc; { // 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++) { genDel(rng, doctmp); } } { // 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++) { genAdd(rng, doctmp); } @@ -399,9 +399,10 @@ void testChange1(size_t seed) { } int main(void) { + const size_t loops = 10'000; { 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"; testEmptyDocAdds(1337+i); std::cout << std::string(40, '-') << "\n"; @@ -412,7 +413,7 @@ int main(void) { { 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"; test1CharDocAdds(1337+i); std::cout << std::string(40, '-') << "\n"; @@ -423,7 +424,7 @@ int main(void) { { 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"; test1CharDocDels(1337+i); std::cout << std::string(40, '-') << "\n"; @@ -434,7 +435,7 @@ int main(void) { { 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"; test2CharDocAdds(1337+i); std::cout << std::string(40, '-') << "\n"; @@ -445,7 +446,7 @@ int main(void) { { 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"; testChange1(1337+i); std::cout << std::string(40, '-') << "\n"; diff --git a/version0/text_document.hpp b/version0/text_document.hpp index 5d51bd3..489b8f8 100644 --- a/version0/text_document.hpp +++ b/version0/text_document.hpp @@ -2,11 +2,9 @@ #include "./list.hpp" -#include #include -#include -#include // debug +//#include // debug namespace GreenCRDT { @@ -17,6 +15,7 @@ struct TextDocument { using Op = std::variant; + // TODO: implement struct Cursor { AgentType who; typename ListType::ListID pos; @@ -163,23 +162,6 @@ struct TextDocument { // generates ops from the difference // note: rn it only creates 1 diff patch std::vector 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 (state.list.empty()) { // no op @@ -198,7 +180,6 @@ struct TextDocument { text ); } - // neither empty // find start and end of changes @@ -227,8 +208,8 @@ struct TextDocument { return {}; } - 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.size: " << state.list.size() << "(" << getText().size() << ")" << " text.size: " << text.size() << "\n"; + //std::cout << "list_start: " << list_start << " text_start: " << text_start << "\n"; // +1 so i can have unsigned size_t list_end = state.list.size(); @@ -248,7 +229,7 @@ struct TextDocument { 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 ops; @@ -258,10 +239,10 @@ struct TextDocument { state.list[list_start].id, 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) 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), 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()); }