96 lines
1.9 KiB
C++
96 lines
1.9 KiB
C++
#include "editor.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;
|
|
}
|
|
|
|
// Split treap by k-th element
|
|
two_lines treap::split(line *l, count_type k) {
|
|
if(l == nullptr) return {nullptr, nullptr};
|
|
|
|
if(get_size(l->left) >= k) {
|
|
// In the left subtree
|
|
auto two = split(l->left, k);
|
|
l->left = two.second;
|
|
l->size -= get_size(two.first);
|
|
return {two.first, l};
|
|
}
|
|
else {
|
|
// In the right subtree
|
|
auto two = split(l->right, k - (1+get_size(l->left)));
|
|
l->right = two.first;
|
|
l->size -= get_size(two.second);
|
|
return {l, two.second};
|
|
}
|
|
}
|
|
|
|
// Join two treaps
|
|
line* treap::join(line *a, line *b) {
|
|
if(a == nullptr) return b;
|
|
if(b == nullptr) return a;
|
|
|
|
if(a->priority < b->priority) {
|
|
a->size += get_size(b);
|
|
a->right = join(a->right, b);
|
|
return a;
|
|
}
|
|
else {
|
|
b->size += get_size(a);
|
|
b->left = join(a, b->left);
|
|
return b;
|
|
}
|
|
}
|
|
|
|
// Find k-th line of file
|
|
line* treap::find(line* l, count_type k) {
|
|
if(l == nullptr) return nullptr;
|
|
|
|
if(k <= get_size(l->left))
|
|
return find(l->left, k);
|
|
else if(k == get_size(l->left)+1)
|
|
return l;
|
|
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);
|
|
}
|
|
void treap::clear() {
|
|
while(size() > 0) {
|
|
auto two = split(root, 1);
|
|
root = two.second;
|
|
delete two.first;
|
|
}
|
|
}
|
|
|
|
// Line insert
|
|
void treap::insert(count_type k, string s) {
|
|
line *l = new line(rand(), s);
|
|
|
|
if(root == nullptr) {
|
|
root = l;
|
|
return;
|
|
}
|
|
|
|
auto two = split(root, k);
|
|
two.first = join(two.first, l);
|
|
root = join(two);
|
|
}
|
|
// Line removal
|
|
void treap::remove(count_type k) {
|
|
auto two = split(root, k+1);
|
|
auto first_split = split(two.first, k);
|
|
delete first_split.second;
|
|
two.first = first_split.first;
|
|
root = join(two);
|
|
}
|