Line is represented by line number as key in treap

This commit is contained in:
Matúš Púll 2025-03-18 15:17:59 +01:00
parent 386de7a8ed
commit 9525d3763a

View file

@ -4,12 +4,15 @@
using std::string; using std::string;
typedef unsigned long long priority_type; typedef unsigned long long priority_type;
typedef unsigned long long key_type;
#define RAND_MAX (1 << 62) #define RAND_MAX (1 << 62)
class line; class line;
typedef std::pair<line*, line*> two_lines; typedef std::pair<line*, line*> two_lines;
// Treap node representation of a line
class line { class line {
priority_type priority; priority_type priority;
key_type key;
string text; string text;
line *left, *right; line *left, *right;
@ -17,14 +20,30 @@ public:
line(priority_type _p, string _t) : priority(_p), text(_t) {} line(priority_type _p, string _t) : priority(_p), text(_t) {}
two_lines split(string s) { return two_lines(nullptr, nullptr); } two_lines split(string s) { return two_lines(nullptr, nullptr); }
void join(line *l) {} void join(line *l) {
// Take minimum of priorities of roots
// Join subtrees
}
void insert(line *l) {} line* find(key_type k) {
void remove(string s) {} if(k < key) {
line* find(string s) { return nullptr; } if(left != nullptr)
return left->find(k);
else
return nullptr;
}
else if(k > key) {
if(right != nullptr)
return right->find(k);
else
return nullptr;
}
else
return this;
}
}; };
// Treap file representation // Treap representation of a file
struct file { struct file {
line* root; line* root;
@ -34,6 +53,12 @@ struct file {
} }
void insert(string s) { void insert(string s) {
line *l = new line(rand(), s); line *l = new line(rand(), s);
if(root == nullptr) {
root = l;
return;
}
auto two = root->split(s); auto two = root->split(s);
two.first->join(l); two.first->join(l);
two.first->join(two.second); two.first->join(two.second);
@ -43,7 +68,7 @@ struct file {
two.first = two.first->split(s).first; // TODO sharp split two.first = two.first->split(s).first; // TODO sharp split
two.first->join(two.second); two.first->join(two.second);
} }
line* find(string s) { line* find(key_type k) {
return root->find(s); return root->find(k);
} }
}; };