Integrate treap into the code

This commit is contained in:
Matúš Púll 2025-03-31 20:13:26 +02:00
parent ef426253ca
commit 179c7ce9bd

View file

@ -4,6 +4,8 @@
#include <vector> #include <vector>
#include <fstream> #include <fstream>
#include "treap.hpp"
using std::string; using std::string;
using std::vector; using std::vector;
@ -12,15 +14,16 @@ enum mode_type { insert, normal };
// Global variables // Global variables
vector<string> file(0); treap file;
int row = 0, col = 0; int row = 0, col = 0;
// Accessing the file // Accessing the file
char get(int r, int s) { return file[r][s]; } string get_line(int r) { return file.find(r)->text; }
void set(int r, int s, char ch) { file[r][s] = ch; } char get(int r, int s) { return get_line(r)[s]; }
string get_line(int r) { return file[r]; } void set(int r, int s, char ch) { file.find(r)->text[s] = ch; }
void add_line(int r) { file.insert(file.begin() + r, ""); } void add_line(int r, string text = "") { file.insert(r, text); }
void remove_line(int r) { file.erase(file.begin() + r); } void remove_line(int r) { file.remove(r); }
// For normal mode not to write to terminal // For normal mode not to write to terminal
void step_back() { void step_back() {
@ -36,9 +39,10 @@ bool load(string filename) {
std::ifstream infile(filename); std::ifstream infile(filename);
if(!infile.good()) if(!infile.good())
return 1; return 1;
string line; string line;
while (std::getline(infile, line)) while (std::getline(infile, line))
file.push_back(line); file.insert(file.size(), line);
return 0; return 0;
} }
@ -47,8 +51,8 @@ bool save(string filename) {
std::ofstream outfile(filename); std::ofstream outfile(filename);
if(!outfile.good()) if(!outfile.good())
return 1; return 1;
for(string line : file) for(int i = 0; i < file.size(); ++i)
outfile << line << std::endl; outfile << get_line(i) << std::endl;
return 0; return 0;
} }
@ -57,7 +61,7 @@ void print_file() {
clear(); clear();
for(int i = 0; i < file.size(); i++) { for(int i = 0; i < file.size(); i++) {
move(i, 0); move(i, 0);
adds(file[i]); adds(get_line(i));
} }
move(row, col); move(row, col);
} }
@ -102,7 +106,7 @@ void move_cursor(char ch) {
break; break;
case 'l': case 'l':
if(col < file[row].size()) if(col < get_line(row).size())
move(row, ++col); move(row, ++col);
break; break;
} }