From 179c7ce9bdee67af95cd2db1d057ca680513208b Mon Sep 17 00:00:00 2001 From: Matuush Date: Mon, 31 Mar 2025 20:13:26 +0200 Subject: [PATCH] Integrate treap into the code --- main.cpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/main.cpp b/main.cpp index 45e3e41..1f06cd5 100644 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,8 @@ #include #include +#include "treap.hpp" + using std::string; using std::vector; @@ -12,15 +14,16 @@ enum mode_type { insert, normal }; // Global variables -vector file(0); +treap file; int row = 0, col = 0; // Accessing the file -char get(int r, int s) { return file[r][s]; } -void set(int r, int s, char ch) { file[r][s] = ch; } -string get_line(int r) { return file[r]; } -void add_line(int r) { file.insert(file.begin() + r, ""); } -void remove_line(int r) { file.erase(file.begin() + r); } +string get_line(int r) { return file.find(r)->text; } +char get(int r, int s) { return get_line(r)[s]; } +void set(int r, int s, char ch) { file.find(r)->text[s] = ch; } +void add_line(int r, string text = "") { file.insert(r, text); } +void remove_line(int r) { file.remove(r); } + // For normal mode not to write to terminal void step_back() { @@ -36,9 +39,10 @@ bool load(string filename) { std::ifstream infile(filename); if(!infile.good()) return 1; + string line; while (std::getline(infile, line)) - file.push_back(line); + file.insert(file.size(), line); return 0; } @@ -47,8 +51,8 @@ bool save(string filename) { std::ofstream outfile(filename); if(!outfile.good()) return 1; - for(string line : file) - outfile << line << std::endl; + for(int i = 0; i < file.size(); ++i) + outfile << get_line(i) << std::endl; return 0; } @@ -57,7 +61,7 @@ void print_file() { clear(); for(int i = 0; i < file.size(); i++) { move(i, 0); - adds(file[i]); + adds(get_line(i)); } move(row, col); } @@ -102,7 +106,7 @@ void move_cursor(char ch) { break; case 'l': - if(col < file[row].size()) + if(col < get_line(row).size()) move(row, ++col); break; }