Fix iteration and petal append bugs.

This commit is contained in:
2026-08-10 14:21:46 +01:00
parent e56f784c9a
commit 23b7b8c8a8
6 changed files with 33 additions and 35 deletions
+1
View File
@@ -7,3 +7,4 @@ build/
.cache/ .cache/
sample.txt sample.txt
old*
+1 -2
View File
@@ -30,8 +30,7 @@ struct Shard {
static std::vector<Shard *> from_swap(std::filesystem::path &path, OriginalBuffer *b); static std::vector<Shard *> from_swap(std::filesystem::path &path, OriginalBuffer *b);
static std::pair<Shard *, Shard *> split(Shard *n, uint64_t offset); static std::pair<Shard *, Shard *> split(Shard *n, uint64_t offset);
static Shard *concat(Shard *left, Shard *right); static Shard *concat(Shard *a, Shard *b);
static Shard *merge(Shard *a, Shard *b);
static Shard *merge_leaves(Shard *a, Shard *b); static Shard *merge_leaves(Shard *a, Shard *b);
static Shard *append(Shard *root, Shard *leaf); static Shard *append(Shard *root, Shard *leaf);
static Shard *build(Shard **pieces, uint64_t lo, uint64_t hi); static Shard *build(Shard **pieces, uint64_t lo, uint64_t hi);
+1 -1
View File
@@ -34,7 +34,7 @@ bool Ed::handle(std::string cmd, bool eof) {
throw ed_error("Line 0 is invalid."); throw ed_error("Line 0 is invalid.");
if (line > vase.lines()) if (line > vase.lines())
throw ed_error("Line position too high."); throw ed_error("Line position too high.");
Iterator it = vase.iterate(line - 1, Direction::Backward); Iterator it = vase.iterate(line - 1, Direction::Forward);
it.next(); it.next();
std::cout << it.line << std::endl; std::cout << it.line << std::endl;
} break; } break;
+18 -15
View File
@@ -88,24 +88,16 @@ void PetalIterator::seek_line(uint64_t line) {
} else { } else {
auto *b = (Branch *)curr; auto *b = (Branch *)curr;
uint64_t left_lines = b->left->lines; uint64_t left_lines = b->left->lines;
if (dir == Direction::Forward) { if (line <= left_lines) {
if (line < left_lines) { if (dir == Direction::Forward)
stack.push_back(b->right); stack.push_back(b->right);
curr = b->left; curr = b->left;
} else {
line -= left_lines;
global_offset += b->left->length;
curr = b->right;
}
} else { } else {
if (line <= left_lines) { line -= left_lines;
curr = b->left; if (dir == Direction::Backward)
} else {
line -= left_lines;
stack.push_back(b->left); stack.push_back(b->left);
global_offset += b->left->length; global_offset += b->left->length;
curr = b->right; curr = b->right;
}
} }
} }
} }
@@ -115,6 +107,17 @@ void PetalIterator::seek_line(uint64_t line) {
Petal *PetalIterator::_next(uint64_t *offset) { Petal *PetalIterator::_next(uint64_t *offset) {
while (true) { while (true) {
if (petal) { if (petal) {
if (dir == Direction::Forward) {
if (petal_offset == petal->length) {
petal = nullptr;
continue;
}
} else {
if (petal_offset == 0) {
petal = nullptr;
continue;
}
}
auto ret = petal; auto ret = petal;
last_offset = global_offset; last_offset = global_offset;
if (dir == Direction::Forward) if (dir == Direction::Forward)
+11 -16
View File
@@ -89,28 +89,25 @@ Shard *balance(Shard *node) {
return node; return node;
} }
Shard *Shard::merge(Shard *a, Shard *b) { Shard *Shard::concat(Shard *a, Shard *b) {
if (!a) if (!a)
return (Shard::retain(b), b); return (Shard::retain(b), b);
if (!b) if (!b)
return (Shard::retain(a), a); return (Shard::retain(a), a);
if (a->height > b->height + 1) { if (a->height > b->height + 1) {
Branch *ba = (Branch *)a; Branch *ba = (Branch *)a;
Shard *r = merge(ba->right, b); Shard *r = concat(ba->right, b);
Shard *out = balance(new Branch(ba->left, r)); Shard *out = balance(new Branch(ba->left, r));
Shard::release(r); Shard::release(r);
return out; return out;
} }
if (b->height > a->height + 1) { if (b->height > a->height + 1) {
Branch *bb = (Branch *)b; Branch *bb = (Branch *)b;
Shard *l = merge(a, bb->left); Shard *l = concat(a, bb->left);
Shard *out = balance(new Branch(l, bb->right)); Shard *out = balance(new Branch(l, bb->right));
Shard::release(l); Shard::release(l);
return out; return out;
} }
return balance(new Branch(a, b)); return balance(new Branch(a, b));
} }
@@ -129,12 +126,12 @@ std::pair<Shard *, Shard *> Shard::split(Shard *n, uint64_t offset) {
Branch *b = (Branch *)n; Branch *b = (Branch *)n;
if (offset < b->left->length) { if (offset < b->left->length) {
auto [a, b2] = split(b->left, offset); auto [a, b2] = split(b->left, offset);
Shard *right = merge(b2, b->right); Shard *right = concat(b2, b->right);
Shard::release(b2); Shard::release(b2);
return {a, right}; return {a, right};
} else { } else {
auto [a, b2] = split(b->right, offset - b->left->length); auto [a, b2] = split(b->right, offset - b->left->length);
Shard *left = merge(b->left, a); Shard *left = concat(b->left, a);
Shard::release(a); Shard::release(a);
return {left, b2}; return {left, b2};
} }
@@ -162,11 +159,13 @@ std::pair<Shard *, Shard *> Shard::split(Shard *n, uint64_t offset) {
Shard *Shard::merge_leaves(Shard *a, Shard *b) { Shard *Shard::merge_leaves(Shard *a, Shard *b) {
if (a->kind != Shard::Kind::Petal || b->kind != Shard::Kind::Petal) if (a->kind != Shard::Kind::Petal || b->kind != Shard::Kind::Petal)
return merge(a, b); return concat(a, b);
if (a->length + b->length > PETAL_SIZE_MAX)
return concat(a, b);
Petal *pa = (Petal *)a; Petal *pa = (Petal *)a;
Petal *pb = (Petal *)b; Petal *pb = (Petal *)b;
if (!(pa->source == pb->source && pa->pos + pa->length == pb->pos)) if (!(pa->source == pb->source && pa->pos + pa->length == pb->pos))
return merge(a, b); return concat(a, b);
return new Petal( return new Petal(
pa->length + pb->length, pa->length + pb->length,
pa->lines + pb->lines, pa->lines + pb->lines,
@@ -180,7 +179,7 @@ Shard *Shard::append(Shard *root, Shard *leaf) {
Shard::retain(leaf); Shard::retain(leaf);
return leaf; return leaf;
} }
if (root->kind == Shard::Kind::Petal && root->length < PETAL_SIZE_MAX) if (root->kind == Shard::Kind::Petal)
return merge_leaves(root, leaf); return merge_leaves(root, leaf);
Branch *b = (Branch *)root; Branch *b = (Branch *)root;
auto new_right = append(b->right, leaf); auto new_right = append(b->right, leaf);
@@ -189,10 +188,6 @@ Shard *Shard::append(Shard *root, Shard *leaf) {
return out; return out;
} }
Shard *Shard::concat(Shard *left, Shard *right) {
return merge(left, right);
}
Shard *Shard::build(Shard **pieces, uint64_t lo, uint64_t hi) { Shard *Shard::build(Shard **pieces, uint64_t lo, uint64_t hi) {
if (hi - lo == 1) if (hi - lo == 1)
return pieces[lo]; return pieces[lo];
@@ -206,7 +201,7 @@ Shard *Shard::build(Shard **pieces, uint64_t lo, uint64_t hi) {
} }
static bool write_all(int fd, const void *data, size_t len) { static bool write_all(int fd, const void *data, size_t len) {
const char *p = static_cast<const char *>(data); const char *p = (const char *)data;
while (len > 0) { while (len > 0) {
ssize_t n = write(fd, p, len); ssize_t n = write(fd, p, len);
if (n > 0) { if (n > 0) {
+1 -1
View File
@@ -93,7 +93,7 @@ Vase &Vase::operator=(Vase &&other) noexcept {
uint64_t Vase::length() { uint64_t Vase::length() {
if (!root) if (!root)
return 0; return 0;
return root->length; return root->length + posix_ending;
} }
uint64_t Vase::lines() { uint64_t Vase::lines() {