74 lines
1.3 KiB
C++
74 lines
1.3 KiB
C++
#include <string>
|
|
#include <cstdlib>
|
|
#include <utility>
|
|
|
|
using std::string;
|
|
typedef unsigned long long priority_type;
|
|
typedef unsigned long long key_type;
|
|
#define RAND_MAX (1 << 62)
|
|
class line;
|
|
typedef std::pair<line*, line*> two_lines;
|
|
|
|
// Treap node representation of a line
|
|
class line {
|
|
priority_type priority;
|
|
key_type key;
|
|
string text;
|
|
line *left, *right;
|
|
|
|
public:
|
|
line(priority_type _p, string _t) : priority(_p), text(_t) {}
|
|
|
|
two_lines split(string s) { return two_lines(nullptr, nullptr); }
|
|
void join(line *l) {
|
|
// Take minimum of priorities of roots
|
|
// Join subtrees
|
|
}
|
|
|
|
line* find(key_type k) {
|
|
if(k < key) {
|
|
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 representation of a file
|
|
struct file {
|
|
line* root;
|
|
|
|
file() {
|
|
srand(120);
|
|
root = nullptr;
|
|
}
|
|
void insert(string s) {
|
|
line *l = new line(rand(), s);
|
|
|
|
if(root == nullptr) {
|
|
root = l;
|
|
return;
|
|
}
|
|
|
|
auto two = root->split(s);
|
|
two.first->join(l);
|
|
two.first->join(two.second);
|
|
}
|
|
void remove(string s) {
|
|
auto two = root->split(s);
|
|
two.first = two.first->split(s).first; // TODO sharp split
|
|
two.first->join(two.second);
|
|
}
|
|
line* find(key_type k) {
|
|
return root->find(k);
|
|
}
|
|
};
|