From e02dd9e7291e64eab1c0189d755c920b986c2f63 Mon Sep 17 00:00:00 2001 From: Matuush Date: Wed, 25 Jun 2025 00:16:00 +0200 Subject: [PATCH] Comment code and rename variables in treap --- everything.hpp | 1 - main.cpp | 11 ++++------- treap.cpp | 46 +++++++++++++++++++++++++++------------------- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/everything.hpp b/everything.hpp index d972e56..b61a983 100644 --- a/everything.hpp +++ b/everything.hpp @@ -79,7 +79,6 @@ class Treap { public: // General operations Treap() { srand(RANDOM_SEED); root.push(nullptr); } - void construct(vector &line); count_type size() { return get_size(get_root()); } void clear(); diff --git a/main.cpp b/main.cpp index eabaa6c..6148e85 100644 --- a/main.cpp +++ b/main.cpp @@ -48,14 +48,11 @@ int main(int argc, const char *argv[]) { } // Load file - string line; - vector lines; - while (std::getline(infile, line)) - lines.push_back(line); - - // Create a treap from file Treap file; - file.construct(lines); + + string line; + while (std::getline(infile, line)) + file.insert(file.size(), line, false); // Init initscr(); diff --git a/treap.cpp b/treap.cpp index cbb4cd4..e71d101 100644 --- a/treap.cpp +++ b/treap.cpp @@ -41,11 +41,13 @@ line* Treap::insert(line *l, count_type k, string s, bool new_version) { if(k <= get_size(l->left)) { line *son = insert(l->left, k, s, new_version); + // Update the node if(new_version) l = new line(l->priority, l->text, son, l->right, son); else l->set_left(son); + // Balance heap-like structure if(get_priority(l->left) > get_priority(l)) l = rotate_right(l); } @@ -54,11 +56,13 @@ line* Treap::insert(line *l, count_type k, string s, bool new_version) { else { line *son = insert(l->right, k - (get_size(l->left)+1), s, new_version); + // Update the node if(new_version) l = new line(l->priority, l->text, l->left, son, son); else l->set_right(son); + // Balance heap-like structure if(get_priority(l->right) > get_priority(l)) l = rotate_left(l); } @@ -74,7 +78,7 @@ line* Treap::remove(line *l, count_type k) { } // Remove this line else if(k == get_size(l->left)) { - // Picking the son we can + // Picking the son we can (the easy cases) if(lson == nullptr && rson == nullptr) return nullptr; else if(lson == nullptr) @@ -87,7 +91,10 @@ line* Treap::remove(line *l, count_type k) { line *son = new line(lson); line l_copy = line(l->priority, l->text, son, l->right, nullptr); + // Rotate to get the son higher rotate_right(&l_copy); + + // Remove under son->right = remove(&l_copy, get_size(l_copy.left)); son->next_in_version = son->right; son->update_size(); @@ -98,7 +105,10 @@ line* Treap::remove(line *l, count_type k) { line *son = new line(rson); line l_copy = line(l->priority, l->text, l->left, son, nullptr); + // Rotate to get the son higher rotate_left(&l_copy); + + // Remove under son->left = remove(&l_copy, get_size(l_copy.left)); son->next_in_version = son->left; son->update_size(); @@ -149,12 +159,7 @@ line* Treap::find(line *l, count_type k) { return find(l->right, k - (1+get_size(l->left)) ); } -void Treap::construct(vector &line) { - for(string text : line) - insert(size(), text, false); -} - -// Undo +// Version control void Treap::delete_version(line *l) { if(l == nullptr) return; @@ -179,10 +184,12 @@ count_type Treap::undo() { } return last.row; } - void Treap::compress_versions(count_type count) { + // Remove all compressed change info for(count_type i = 1; i < count; ++i) changes.pop(); + + // Convert them into one grand-info if(count > 0) changes.top().count = count; } @@ -192,6 +199,7 @@ void Treap::clear() { // Clear all versions while(root.size() > 1) undo(); + // Clear original file while(size() > 0) remove(0); @@ -248,12 +256,8 @@ void Treap::update(count_type k, string s, bool new_version) { // Accessing the file string Treap::get_line(count_type r, bool substitute_tab) { - line *l = find(get_root(), r); - if(l == nullptr) { - std::cerr << r << " not found.\n"; - return ""; - } - string line = l->text; + string line = find(get_root(), r)->text; + if(substitute_tab) return substitute_tabs(line); else @@ -263,7 +267,7 @@ string Treap::get_line(count_type r, bool substitute_tab) { count_type Treap::bulk_find(vector *vec, line *l, count_type k, count_type count) { if(l == nullptr) return count; - // Wanted vertex is on the left + // Wanted vertex is on the left (this may be wanted afterwards) if(k <= get_size(l->left)) { count_type r = bulk_find(vec, l->left, k, count); if(r > 0) @@ -282,6 +286,8 @@ count_type Treap::bulk_find(vector *vec, line *l, count_type k, count_ty else return 0; } + + // Wanted vertex is on the right (this is never wanted) else return bulk_find(vec, l->right, k - (1+get_size(l->left)), count); } @@ -295,23 +301,25 @@ vector Treap::bulk_get_line(count_type r, count_type count) { void Treap::insert(position p, string t) { string text = get_line(p.r, false); text.insert(p.c-get_tab_offset(p), t); + if(text != get_line(p.r, false)) update(p.r, text); } void Treap::remove(position p, count_type len) { string text = get_line(p.r, false); text.erase(p.c-get_tab_offset(p),len); + if(text != get_line(p.r, false)) update(p.r, text); } // Split line void Treap::split_line(position p, bool should_compress) { string line = get_line(p.r, false); - count_type place = p.c - get_tab_offset(p); - string newline = line.substr(place, line.size()); + count_type real_c = p.c - get_tab_offset(p); + string newline = line.substr(real_c, line.size()); - if(line.size()-place > 0) - remove(p, line.size()-place); + if(line.size()-real_c > 0) + remove(p, line.size()-real_c); insert(p.r+1, newline); if(should_compress)