Clean up treap accessing

This commit is contained in:
Matúš Púll 2025-05-27 19:43:47 +02:00
parent 569d1c530c
commit d7f45730c2
4 changed files with 37 additions and 28 deletions

View file

@ -36,26 +36,28 @@ class Treap {
line* join(line *a, line *b);
line* join(two_lines two) { return join(two.first, two.second); }
line* find(line *l, count_type k);
public:
Treap() { srand(120); root = nullptr; }
line* find(count_type k);
string get(count_type k) { return find(k)->text; }
void clear();
void insert(count_type k, string s);
void append(string s) { insert(size(), s); }
void remove(count_type k);
void pop() { remove(size()); }
count_type size() { return get_size(root); }
count_type bulk_find(vector<string> *vec, line *l, count_type k, count_type count);
// Treap access functions
string substitute_tabs(string s);
public:
// General operations
Treap() { srand(120); root = nullptr; }
count_type size() { return get_size(root); }
void clear();
// Line operations
string get_line(count_type r, bool substitute_tab = 1);
vector<string> bulk_get_line(count_type r, count_type count);
count_type get_tab_offset(position p);
void set(position p, char ch);
void split_line(position p);
void insert(count_type k, string s);
void remove(count_type k);
// Substring operations
void insert(position p, string t);
void remove(position p, count_type len = 1);
void split_line(position p);
};
//// Selection
@ -67,7 +69,8 @@ class Selection {
public:
Selection() {}
Selection(Treap *_file) { file = _file; }
position pos; // selection initial position
position pos; // last selection position
void remove_selection(position p);
void copy_selection(position p);
void paste_selection(position p);

View file

@ -21,7 +21,7 @@ int main(int argc, char* argv[]) {
Treap file;
string line;
while (std::getline(infile, line))
file.append(line);
file.insert(file.size(), line);
// Init
initscr();

View file

@ -43,15 +43,15 @@ void Selection::paste_selection(position p) {
if(content.size() == 0) return;
// Insert last line inside of this
file->insert(p, content.get(content.size()-1));
file->insert(p, content.get_line(content.size()-1, false));
if(content.size() == 1) return;
// Insert first line in front and split
file->split_line(p);
file->insert(p, content.get(0));
file->insert(p, content.get_line(0, false));
if(content.size() == 2) return;
// Insert lines
for(count_type i = content.size()-2; i > 0; --i)
file->insert(p.r, content.get(i));
file->insert(p.r, content.get_line(i, false));
}

View file

@ -1,14 +1,23 @@
#include "everything.hpp"
// Treap representation of a file
line* root;
// Get size uf a subtreap
count_type Treap::get_size(line* l) {
if(l == nullptr) return 0;
return l->size;
}
string Treap::substitute_tabs(string s) {
string ret = "";
for(count_type i = 0; i < s.size(); ++i) {
if(s[i] == '\t')
for(int j = 0; j < TAB_SIZE; j++)
ret += ' ';
else
ret += s[i];
}
return ret;
}
// Split treap by k-th element
two_lines Treap::split(line *l, count_type k) {
if(l == nullptr) return {nullptr, nullptr};
@ -57,14 +66,14 @@ line* Treap::find(line* l, count_type k) {
else
return find(l->right, k - (1+get_size(l->left)) );
}
// File access
line* Treap::find(count_type k) {
if(k >= root->size)
return nullptr;
// Don't find index k, but k-th line -> +1
return find(root, k+1);
}
// File access
void Treap::clear() {
while(size() > 0) {
auto two = split(root, 1);
@ -122,9 +131,6 @@ count_type Treap::get_tab_offset(position p) {
}
return tab_offset;
}
void Treap::set(position p, char ch) {
find(p.r)->text[p.c-get_tab_offset(p)] = ch;
}
void Treap::insert(position p, string t) {
find(p.r)->text.insert(p.c-get_tab_offset(p), t);
}