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 <fstream>
#include "treap.hpp"
using std::string;
using std::vector;
@ -12,15 +14,16 @@ enum mode_type { insert, normal };
// Global variables
vector<string> 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;
}