From d7f45730c27f048746e0bd107537a233566ca76c Mon Sep 17 00:00:00 2001 From: Matuush Date: Tue, 27 May 2025 19:43:47 +0200 Subject: [PATCH] Clean up treap accessing --- everything.hpp | 35 +++++++++++++++++++---------------- main.cpp | 2 +- selection.cpp | 6 +++--- treap.cpp | 22 ++++++++++++++-------- 4 files changed, 37 insertions(+), 28 deletions(-) diff --git a/everything.hpp b/everything.hpp index 5a7597f..5ceab92 100644 --- a/everything.hpp +++ b/everything.hpp @@ -35,27 +35,29 @@ 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(line *l, count_type k); 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 *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 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); diff --git a/main.cpp b/main.cpp index 2de9694..ebd0ec7 100644 --- a/main.cpp +++ b/main.cpp @@ -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(); diff --git a/selection.cpp b/selection.cpp index 5139a2a..47ed586 100644 --- a/selection.cpp +++ b/selection.cpp @@ -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)); } diff --git a/treap.cpp b/treap.cpp index 0688f5a..4adc6d4 100644 --- a/treap.cpp +++ b/treap.cpp @@ -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); }