From 69f45fe50c1e3895a5833f7baaa8b536bf5e8139 Mon Sep 17 00:00:00 2001 From: Matuush Date: Tue, 27 May 2025 19:44:52 +0200 Subject: [PATCH] Implement bulk_get_line for faster file printing --- editor.cpp | 15 ++++++++++----- everything.hpp | 2 ++ treap.cpp | 42 ++++++++++++++++++++++++++++++++++-------- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/editor.cpp b/editor.cpp index 08c1e70..1c60a39 100644 --- a/editor.cpp +++ b/editor.cpp @@ -25,9 +25,14 @@ void Editor::print_line(count_type r) { } // Print file content void Editor::print_file(count_type start) { - for(count_type i = start; i < file_offset + LINES; i++) - if(i < file.size()) - print_line(i); + auto lines = file.bulk_get_line(start, LINES); + for(count_type i = 0; i < LINES; i++) + if(i < lines.size()) { + ::move(i, 0); + clrtoeol(); + ::move(i, 0); + adds(lines[i]); + } else clear_line(i); } @@ -92,7 +97,7 @@ void Editor::move_cursor(char ch) { else if(cur.r == LINES-1) { file_offset++; jump_line_end(); - print_file(); + print_file(file_offset); } break; @@ -105,7 +110,7 @@ void Editor::move_cursor(char ch) { else if(file_offset > 0) { file_offset--; jump_line_end(); - print_file(); + print_file(file_offset); } break; diff --git a/everything.hpp b/everything.hpp index 5ceab92..2af5abf 100644 --- a/everything.hpp +++ b/everything.hpp @@ -1,12 +1,14 @@ #pragma once #include #include +#include #include #include #define TAB_SIZE 2 using std::string; +using std::vector; typedef unsigned long long count_type; // Position diff --git a/treap.cpp b/treap.cpp index 4adc6d4..fd14971 100644 --- a/treap.cpp +++ b/treap.cpp @@ -106,19 +106,45 @@ void Treap::remove(count_type k) { // Accessing the file string Treap::get_line(count_type r, bool substitute_tab) { - string line = get(r); - if(!substitute_tab) + string line = find(r)->text; + if(substitute_tab) + return substitute_tabs(line); + else return line; - string ret = ""; - for(count_type i = 0; i < line.size(); ++i) { - if(line[i] == '\t') - for(int j = 0; j < TAB_SIZE; j++) - ret += ' '; +} +// Get multiple adjacent vertices +count_type Treap::bulk_find(vector *vec, line *l, count_type k, count_type count) { + if(l == nullptr) return count; + + // Wanted vertex is on the left + if(k <= get_size(l->left)) { + count_type r = bulk_find(vec, l->left, k, count); + if(r > 0) + vec->push_back(substitute_tabs(l->text)); + if(r > 1) + return bulk_find(vec, l->right, 0, r-1); else - ret += line[i]; + return 0; } + + // This is the wanted vertex + else if(k == get_size(l->left)+1) { + vec->push_back(substitute_tabs(l->text)); + if(count > 1) + return bulk_find(vec, l->right, 0, count-1); + else + return 0; + } + else + return bulk_find(vec, l->right, k - (1+get_size(l->left)), count); +} +// Access multiple adjacent vertices +vector Treap::bulk_get_line(count_type r, count_type count) { + vector ret(0); + bulk_find(&ret, root, r+1, count); return ret; } + // TODO tabs are a grid, not just N tabs count_type Treap::get_tab_offset(position p) { count_type tab_offset = 0;