Clean up treap accessing
This commit is contained in:
parent
569d1c530c
commit
d7f45730c2
4 changed files with 37 additions and 28 deletions
|
@ -35,27 +35,29 @@ class Treap {
|
||||||
|
|
||||||
line* join(line *a, line *b);
|
line* join(line *a, line *b);
|
||||||
line* join(two_lines two) { return join(two.first, two.second); }
|
line* join(two_lines two) { return join(two.first, two.second); }
|
||||||
line* find(line* l, count_type k);
|
line* find(line *l, count_type k);
|
||||||
|
|
||||||
public:
|
|
||||||
Treap() { srand(120); root = nullptr; }
|
|
||||||
|
|
||||||
line* find(count_type k);
|
line* find(count_type k);
|
||||||
string get(count_type k) { return find(k)->text; }
|
count_type bulk_find(vector<string> *vec, line *l, count_type k, count_type count);
|
||||||
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); }
|
|
||||||
|
|
||||||
// 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);
|
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);
|
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 insert(position p, string t);
|
||||||
void remove(position p, count_type len = 1);
|
void remove(position p, count_type len = 1);
|
||||||
void split_line(position p);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//// Selection
|
//// Selection
|
||||||
|
@ -67,7 +69,8 @@ class Selection {
|
||||||
public:
|
public:
|
||||||
Selection() {}
|
Selection() {}
|
||||||
Selection(Treap *_file) { file = _file; }
|
Selection(Treap *_file) { file = _file; }
|
||||||
position pos; // selection initial position
|
position pos; // last selection position
|
||||||
|
|
||||||
void remove_selection(position p);
|
void remove_selection(position p);
|
||||||
void copy_selection(position p);
|
void copy_selection(position p);
|
||||||
void paste_selection(position p);
|
void paste_selection(position p);
|
||||||
|
|
2
main.cpp
2
main.cpp
|
@ -21,7 +21,7 @@ int main(int argc, char* argv[]) {
|
||||||
Treap file;
|
Treap file;
|
||||||
string line;
|
string line;
|
||||||
while (std::getline(infile, line))
|
while (std::getline(infile, line))
|
||||||
file.append(line);
|
file.insert(file.size(), line);
|
||||||
|
|
||||||
// Init
|
// Init
|
||||||
initscr();
|
initscr();
|
||||||
|
|
|
@ -43,15 +43,15 @@ void Selection::paste_selection(position p) {
|
||||||
if(content.size() == 0) return;
|
if(content.size() == 0) return;
|
||||||
|
|
||||||
// Insert last line inside of this
|
// 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;
|
if(content.size() == 1) return;
|
||||||
|
|
||||||
// Insert first line in front and split
|
// Insert first line in front and split
|
||||||
file->split_line(p);
|
file->split_line(p);
|
||||||
file->insert(p, content.get(0));
|
file->insert(p, content.get_line(0, false));
|
||||||
if(content.size() == 2) return;
|
if(content.size() == 2) return;
|
||||||
|
|
||||||
// Insert lines
|
// Insert lines
|
||||||
for(count_type i = content.size()-2; i > 0; --i)
|
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));
|
||||||
}
|
}
|
||||||
|
|
22
treap.cpp
22
treap.cpp
|
@ -1,14 +1,23 @@
|
||||||
#include "everything.hpp"
|
#include "everything.hpp"
|
||||||
|
|
||||||
// Treap representation of a file
|
|
||||||
line* root;
|
|
||||||
|
|
||||||
// Get size uf a subtreap
|
// Get size uf a subtreap
|
||||||
count_type Treap::get_size(line* l) {
|
count_type Treap::get_size(line* l) {
|
||||||
if(l == nullptr) return 0;
|
if(l == nullptr) return 0;
|
||||||
return l->size;
|
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
|
// Split treap by k-th element
|
||||||
two_lines Treap::split(line *l, count_type k) {
|
two_lines Treap::split(line *l, count_type k) {
|
||||||
if(l == nullptr) return {nullptr, nullptr};
|
if(l == nullptr) return {nullptr, nullptr};
|
||||||
|
@ -57,14 +66,14 @@ line* Treap::find(line* l, count_type k) {
|
||||||
else
|
else
|
||||||
return find(l->right, k - (1+get_size(l->left)) );
|
return find(l->right, k - (1+get_size(l->left)) );
|
||||||
}
|
}
|
||||||
|
|
||||||
// File access
|
|
||||||
line* Treap::find(count_type k) {
|
line* Treap::find(count_type k) {
|
||||||
if(k >= root->size)
|
if(k >= root->size)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
// Don't find index k, but k-th line -> +1
|
// Don't find index k, but k-th line -> +1
|
||||||
return find(root, k+1);
|
return find(root, k+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// File access
|
||||||
void Treap::clear() {
|
void Treap::clear() {
|
||||||
while(size() > 0) {
|
while(size() > 0) {
|
||||||
auto two = split(root, 1);
|
auto two = split(root, 1);
|
||||||
|
@ -122,9 +131,6 @@ count_type Treap::get_tab_offset(position p) {
|
||||||
}
|
}
|
||||||
return tab_offset;
|
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) {
|
void Treap::insert(position p, string t) {
|
||||||
find(p.r)->text.insert(p.c-get_tab_offset(p), t);
|
find(p.r)->text.insert(p.c-get_tab_offset(p), t);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue