Make basic (unbalanced) insertion work.
This commit is contained in:
+30
-46
@@ -1,64 +1,49 @@
|
||||
#include "vase/shard.h"
|
||||
|
||||
std::pair<ShardPtr, ShardPtr> split(Shard *n, uint32_t offset) {
|
||||
std::pair<ShardPtr, ShardPtr> split_shard(Shard *n, uint32_t offset) {
|
||||
if (!n)
|
||||
return {nullptr, nullptr};
|
||||
|
||||
if (offset == 0)
|
||||
return {nullptr, ShardPtr(n)};
|
||||
|
||||
if (offset == n->length)
|
||||
return {ShardPtr(n), nullptr};
|
||||
|
||||
if (n->kind == Shard::ShardKind::Branch) {
|
||||
Branch *b = static_cast<Branch *>(n);
|
||||
Branch *b = (Branch *)n;
|
||||
|
||||
if (offset < b->left.ptr->length) {
|
||||
auto [a, b2] = split(b->left.ptr, offset);
|
||||
|
||||
return {
|
||||
a,
|
||||
ShardPtr(new Branch(
|
||||
b2.ptr,
|
||||
b->right.ptr
|
||||
))
|
||||
};
|
||||
auto [a, b2] = split_shard(b->left.ptr, offset);
|
||||
return {a, ShardPtr(new Branch(b2.ptr, b->right.ptr))};
|
||||
} else {
|
||||
auto [a, b2] = split(
|
||||
b->right.ptr,
|
||||
offset - b->left.ptr->length
|
||||
);
|
||||
|
||||
return {
|
||||
ShardPtr(new Branch(
|
||||
b->left.ptr,
|
||||
a.ptr
|
||||
)),
|
||||
b2
|
||||
};
|
||||
auto [a, b2] = split_shard(b->right.ptr, offset - b->left.ptr->length);
|
||||
return {ShardPtr(new Branch(b->left.ptr, a.ptr)), b2};
|
||||
}
|
||||
} else {
|
||||
Petal *p = (Petal *)n;
|
||||
|
||||
auto left = new Petal(
|
||||
offset,
|
||||
p->source->count_lines(0, offset),
|
||||
p->source,
|
||||
p->pos
|
||||
);
|
||||
auto right = new Petal(
|
||||
p->length - offset,
|
||||
p->source->count_lines(offset, p->length),
|
||||
p->source,
|
||||
p->pos + offset
|
||||
);
|
||||
|
||||
return {ShardPtr(left), ShardPtr(right)};
|
||||
}
|
||||
}
|
||||
|
||||
Petal *p = static_cast<Petal *>(n);
|
||||
|
||||
auto left = new Petal(
|
||||
offset,
|
||||
p->source->count_lines(0, offset),
|
||||
p->source,
|
||||
p->pos
|
||||
);
|
||||
|
||||
auto right = new Petal(
|
||||
p->length - offset,
|
||||
p->source->count_lines(offset, p->length),
|
||||
p->source,
|
||||
p->pos + offset
|
||||
);
|
||||
|
||||
return {
|
||||
ShardPtr(left),
|
||||
ShardPtr(right)
|
||||
};
|
||||
ShardPtr concat_shard(ShardPtr left, ShardPtr right) {
|
||||
if (!left.ptr)
|
||||
return right;
|
||||
if (!right.ptr)
|
||||
return left;
|
||||
return new Branch(left.ptr, right.ptr);
|
||||
}
|
||||
|
||||
void print_shard(const Shard *shard, int depth) {
|
||||
@@ -68,7 +53,6 @@ void print_shard(const Shard *shard, int depth) {
|
||||
}
|
||||
|
||||
std::string indent(depth * 2, ' ');
|
||||
|
||||
std::cout << indent;
|
||||
|
||||
if (shard->kind == Shard::ShardKind::Branch) {
|
||||
|
||||
Reference in New Issue
Block a user