Implement bulk_get_line for faster file printing
This commit is contained in:
parent
d7f45730c2
commit
69f45fe50c
3 changed files with 46 additions and 13 deletions
15
editor.cpp
15
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;
|
||||
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
#pragma once
|
||||
#include <ncurses.h>
|
||||
#include <curses.h>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#define TAB_SIZE 2
|
||||
|
||||
using std::string;
|
||||
using std::vector;
|
||||
typedef unsigned long long count_type;
|
||||
|
||||
// Position
|
||||
|
|
44
treap.cpp
44
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)
|
||||
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 += ' ';
|
||||
string line = find(r)->text;
|
||||
if(substitute_tab)
|
||||
return substitute_tabs(line);
|
||||
else
|
||||
ret += line[i];
|
||||
return line;
|
||||
}
|
||||
// Get multiple adjacent vertices
|
||||
count_type Treap::bulk_find(vector<string> *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
|
||||
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<string> Treap::bulk_get_line(count_type r, count_type count) {
|
||||
vector<string> 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;
|
||||
|
|
Loading…
Reference in a new issue