Make append buffer contiguous and update iterators

This commit is contained in:
2026-08-06 19:39:59 +01:00
parent 5722731c6a
commit 189b9ab6ca
19 changed files with 463 additions and 506 deletions
+57 -8
View File
@@ -7,7 +7,7 @@
#include "utils/utils.h"
struct Shard {
enum struct ShardKind : uint8_t {
enum struct Kind : uint8_t {
Branch,
Petal
} kind;
@@ -19,7 +19,7 @@ struct Shard {
std::atomic_uint64_t refs;
Shard(ShardKind kind, uint64_t length, uint64_t lines, uint8_t height)
Shard(Kind kind, uint64_t length, uint64_t lines, uint8_t height)
: kind(kind), height(height), length(length), lines(lines), refs(1) {};
static void retain(Shard *n);
@@ -27,14 +27,13 @@ struct Shard {
static Shard *from_file(std::filesystem::path path, OriginalBuffer *b);
static std::vector<Shard *> from_swap(std::filesystem::path path, OriginalBuffer *b);
static Shard *new_empty(OriginalBuffer *b);
static std::pair<Shard *, Shard *> split(Shard *n, uint64_t offset);
static Shard *concat(Shard *left, Shard *right);
static Shard *merge(Shard *a, Shard *b);
static Shard *merge_leaves(Shard *a, Shard *b);
static Shard *append_leaf(Shard *root, Shard *leaf);
static Shard *build_balanced(Shard **pieces, uint64_t lo, uint64_t hi);
static Shard *append(Shard *root, Shard *leaf);
static Shard *build(Shard **pieces, uint64_t lo, uint64_t hi);
};
struct Branch : Shard {
@@ -43,7 +42,7 @@ struct Branch : Shard {
Branch(Shard *l, Shard *r)
: Shard(
ShardKind::Branch,
Kind::Branch,
l->length + r->length,
l->lines + r->lines,
1 + std::max(l->height, r->height)
@@ -56,10 +55,60 @@ struct Branch : Shard {
struct Petal : Shard {
Buffer *source;
uint64_t pos;
Petal(uint64_t length, uint64_t lines, Buffer *source, uint64_t pos)
: Shard(ShardKind::Petal, length, lines, 1),
: Shard(Kind::Petal, length, lines, 1),
source(source), pos(pos) {};
};
extern inline void dump_shard(Shard *node, int depth = 0) {
if (!node) {
std::cout << std::string(depth * 2, ' ') << "<null>\n";
return;
}
std::string indent(depth * 2, ' ');
std::cout << indent
<< "Shard@" << node
<< " kind=";
switch (node->kind) {
case Shard::Kind::Branch:
std::cout << "Branch";
break;
case Shard::Kind::Petal:
std::cout << "Petal";
break;
}
std::cout
<< " height=" << unsigned(node->height)
<< " length=" << node->length
<< " lines=" << node->lines
<< " refs=" << node->refs.load()
<< "\n";
if (node->kind == Shard::Kind::Branch) {
auto *branch = static_cast<Branch *>(node);
std::cout << indent << " left:\n";
dump_shard(branch->left, depth + 2);
std::cout << indent << " right:\n";
dump_shard(branch->right, depth + 2);
} else {
auto *petal = static_cast<Petal *>(node);
std::cout
<< indent << " source=" << petal->source
<< " pos=" << petal->pos
<< " length=" << petal->length
<< " lines=" << petal->lines
<< "\n";
}
if (!depth)
std::cout << "\n\n";
}