Comment code and rename variables in treap

This commit is contained in:
Matúš Púll 2025-06-25 00:16:00 +02:00
parent 495bf542cf
commit e02dd9e729
3 changed files with 31 additions and 27 deletions

View file

@ -79,7 +79,6 @@ class Treap {
public: public:
// General operations // General operations
Treap() { srand(RANDOM_SEED); root.push(nullptr); } Treap() { srand(RANDOM_SEED); root.push(nullptr); }
void construct(vector<string> &line);
count_type size() { return get_size(get_root()); } count_type size() { return get_size(get_root()); }
void clear(); void clear();

View file

@ -48,14 +48,11 @@ int main(int argc, const char *argv[]) {
} }
// Load file // Load file
string line;
vector<string> lines;
while (std::getline(infile, line))
lines.push_back(line);
// Create a treap from file
Treap file; Treap file;
file.construct(lines);
string line;
while (std::getline(infile, line))
file.insert(file.size(), line, false);
// Init // Init
initscr(); initscr();

View file

@ -41,11 +41,13 @@ line* Treap::insert(line *l, count_type k, string s, bool new_version) {
if(k <= get_size(l->left)) { if(k <= get_size(l->left)) {
line *son = insert(l->left, k, s, new_version); line *son = insert(l->left, k, s, new_version);
// Update the node
if(new_version) if(new_version)
l = new line(l->priority, l->text, son, l->right, son); l = new line(l->priority, l->text, son, l->right, son);
else else
l->set_left(son); l->set_left(son);
// Balance heap-like structure
if(get_priority(l->left) > get_priority(l)) if(get_priority(l->left) > get_priority(l))
l = rotate_right(l); l = rotate_right(l);
} }
@ -54,11 +56,13 @@ line* Treap::insert(line *l, count_type k, string s, bool new_version) {
else { else {
line *son = insert(l->right, k - (get_size(l->left)+1), s, new_version); line *son = insert(l->right, k - (get_size(l->left)+1), s, new_version);
// Update the node
if(new_version) if(new_version)
l = new line(l->priority, l->text, l->left, son, son); l = new line(l->priority, l->text, l->left, son, son);
else else
l->set_right(son); l->set_right(son);
// Balance heap-like structure
if(get_priority(l->right) > get_priority(l)) if(get_priority(l->right) > get_priority(l))
l = rotate_left(l); l = rotate_left(l);
} }
@ -74,7 +78,7 @@ line* Treap::remove(line *l, count_type k) {
} }
// Remove this line // Remove this line
else if(k == get_size(l->left)) { 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) if(lson == nullptr && rson == nullptr)
return nullptr; return nullptr;
else if(lson == nullptr) else if(lson == nullptr)
@ -87,7 +91,10 @@ line* Treap::remove(line *l, count_type k) {
line *son = new line(lson); line *son = new line(lson);
line l_copy = line(l->priority, l->text, son, l->right, nullptr); line l_copy = line(l->priority, l->text, son, l->right, nullptr);
// Rotate to get the son higher
rotate_right(&l_copy); rotate_right(&l_copy);
// Remove under
son->right = remove(&l_copy, get_size(l_copy.left)); son->right = remove(&l_copy, get_size(l_copy.left));
son->next_in_version = son->right; son->next_in_version = son->right;
son->update_size(); son->update_size();
@ -98,7 +105,10 @@ line* Treap::remove(line *l, count_type k) {
line *son = new line(rson); line *son = new line(rson);
line l_copy = line(l->priority, l->text, l->left, son, nullptr); line l_copy = line(l->priority, l->text, l->left, son, nullptr);
// Rotate to get the son higher
rotate_left(&l_copy); rotate_left(&l_copy);
// Remove under
son->left = remove(&l_copy, get_size(l_copy.left)); son->left = remove(&l_copy, get_size(l_copy.left));
son->next_in_version = son->left; son->next_in_version = son->left;
son->update_size(); 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)) ); return find(l->right, k - (1+get_size(l->left)) );
} }
void Treap::construct(vector<string> &line) { // Version control
for(string text : line)
insert(size(), text, false);
}
// Undo
void Treap::delete_version(line *l) { void Treap::delete_version(line *l) {
if(l == nullptr) if(l == nullptr)
return; return;
@ -179,10 +184,12 @@ count_type Treap::undo() {
} }
return last.row; return last.row;
} }
void Treap::compress_versions(count_type count) { void Treap::compress_versions(count_type count) {
// Remove all compressed change info
for(count_type i = 1; i < count; ++i) for(count_type i = 1; i < count; ++i)
changes.pop(); changes.pop();
// Convert them into one grand-info
if(count > 0) if(count > 0)
changes.top().count = count; changes.top().count = count;
} }
@ -192,6 +199,7 @@ void Treap::clear() {
// Clear all versions // Clear all versions
while(root.size() > 1) while(root.size() > 1)
undo(); undo();
// Clear original file // Clear original file
while(size() > 0) while(size() > 0)
remove(0); remove(0);
@ -248,12 +256,8 @@ void Treap::update(count_type k, string s, bool new_version) {
// Accessing the file // Accessing the file
string Treap::get_line(count_type r, bool substitute_tab) { string Treap::get_line(count_type r, bool substitute_tab) {
line *l = find(get_root(), r); string line = find(get_root(), r)->text;
if(l == nullptr) {
std::cerr << r << " not found.\n";
return "";
}
string line = l->text;
if(substitute_tab) if(substitute_tab)
return substitute_tabs(line); return substitute_tabs(line);
else else
@ -263,7 +267,7 @@ string Treap::get_line(count_type r, bool substitute_tab) {
count_type Treap::bulk_find(vector<string> *vec, line *l, count_type k, count_type count) { count_type Treap::bulk_find(vector<string> *vec, line *l, count_type k, count_type count) {
if(l == nullptr) return 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)) { if(k <= get_size(l->left)) {
count_type r = bulk_find(vec, l->left, k, count); count_type r = bulk_find(vec, l->left, k, count);
if(r > 0) if(r > 0)
@ -282,6 +286,8 @@ count_type Treap::bulk_find(vector<string> *vec, line *l, count_type k, count_ty
else else
return 0; return 0;
} }
// Wanted vertex is on the right (this is never wanted)
else else
return bulk_find(vec, l->right, k - (1+get_size(l->left)), count); return bulk_find(vec, l->right, k - (1+get_size(l->left)), count);
} }
@ -295,23 +301,25 @@ vector<string> Treap::bulk_get_line(count_type r, count_type count) {
void Treap::insert(position p, string t) { void Treap::insert(position p, string t) {
string text = get_line(p.r, false); string text = get_line(p.r, false);
text.insert(p.c-get_tab_offset(p), t); text.insert(p.c-get_tab_offset(p), t);
if(text != get_line(p.r, false)) if(text != get_line(p.r, false))
update(p.r, text); update(p.r, text);
} }
void Treap::remove(position p, count_type len) { void Treap::remove(position p, count_type len) {
string text = get_line(p.r, false); string text = get_line(p.r, false);
text.erase(p.c-get_tab_offset(p),len); text.erase(p.c-get_tab_offset(p),len);
if(text != get_line(p.r, false)) if(text != get_line(p.r, false))
update(p.r, text); update(p.r, text);
} }
// Split line // Split line
void Treap::split_line(position p, bool should_compress) { void Treap::split_line(position p, bool should_compress) {
string line = get_line(p.r, false); string line = get_line(p.r, false);
count_type place = p.c - get_tab_offset(p); count_type real_c = p.c - get_tab_offset(p);
string newline = line.substr(place, line.size()); string newline = line.substr(real_c, line.size());
if(line.size()-place > 0) if(line.size()-real_c > 0)
remove(p, line.size()-place); remove(p, line.size()-real_c);
insert(p.r+1, newline); insert(p.r+1, newline);
if(should_compress) if(should_compress)