Add a line content iterator.
This commit is contained in:
+14
-8
@@ -4,18 +4,18 @@
|
||||
#include "vase/vase.h"
|
||||
|
||||
int main() {
|
||||
const char *text_o = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\n";
|
||||
const char *text_o =
|
||||
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqr\n"
|
||||
"st\n"
|
||||
"uv\n"
|
||||
"wx\n"
|
||||
"yzabcdefghijklmnopqrstuvwxyz\n";
|
||||
|
||||
uint32_t len = strlen(text_o);
|
||||
char *text = strdup(text_o);
|
||||
|
||||
Vase vase = Vase(text, len);
|
||||
|
||||
std::cout << vase.to_string() << "\n";
|
||||
|
||||
print_shard(vase.root);
|
||||
|
||||
std::cout << "\n->\n\n";
|
||||
|
||||
vase.insert(14, "gr\ntt", 5);
|
||||
vase.insert(58, "gr\ntt", 5);
|
||||
vase.insert(100, "gr\ntt", 5);
|
||||
@@ -28,7 +28,13 @@ int main() {
|
||||
|
||||
print_shard(vase.root);
|
||||
|
||||
std::cout << "\n"
|
||||
LineIterator it(vase.root, 3);
|
||||
std::string line;
|
||||
while (it.next(line)) {
|
||||
std::cout << line << "\n";
|
||||
}
|
||||
|
||||
std::cout << "\n\n"
|
||||
<< vase.to_string();
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -108,6 +108,25 @@ uint32_t AppendBuffer::find_newline(uint32_t target, uint32_t low, uint32_t high
|
||||
return chunk * CHUNK_SIZE + l;
|
||||
}
|
||||
|
||||
inline uint32_t AppendBuffer::newline_value_at(uint32_t flat_index) {
|
||||
uint32_t chunk = flat_index / CHUNK_SIZE;
|
||||
uint32_t off = flat_index % CHUNK_SIZE;
|
||||
return (*newlines[chunk])[off];
|
||||
}
|
||||
|
||||
uint32_t AppendBuffer::next_newline(uint32_t pos) {
|
||||
uint32_t total = CHUNK_SIZE * (uint32_t)(newlines.size() - 1) + l_offset;
|
||||
uint32_t idx = find_newline(pos, 0, total);
|
||||
return idx >= total ? UINT32_MAX : newline_value_at(idx);
|
||||
}
|
||||
|
||||
uint32_t AppendBuffer::nth_newline(uint32_t pos, uint32_t n) {
|
||||
uint32_t total = CHUNK_SIZE * (uint32_t)(newlines.size() - 1) + l_offset;
|
||||
uint32_t idx0 = find_newline(pos, 0, total);
|
||||
uint32_t idx = idx0 + (n - 1);
|
||||
return idx >= total ? UINT32_MAX : newline_value_at(idx);
|
||||
}
|
||||
|
||||
uint32_t AppendBuffer::count_lines(uint32_t pos, uint32_t length) {
|
||||
if (newlines.empty())
|
||||
return 0;
|
||||
|
||||
@@ -22,3 +22,15 @@ uint32_t OriginalBuffer::count_lines(uint32_t pos, uint32_t length) {
|
||||
|
||||
return (uint32_t)(end - start);
|
||||
}
|
||||
|
||||
uint32_t OriginalBuffer::next_newline(uint32_t pos) {
|
||||
auto it = std::lower_bound(newlines.begin(), newlines.end(), pos);
|
||||
return it == newlines.end() ? UINT32_MAX : *it;
|
||||
}
|
||||
|
||||
uint32_t OriginalBuffer::nth_newline(uint32_t pos, uint32_t n) {
|
||||
auto it = std::lower_bound(newlines.begin(), newlines.end(), pos);
|
||||
if (uint32_t(newlines.end() - it) < n)
|
||||
return UINT32_MAX;
|
||||
return *(it + (n - 1));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
#include "vase/line_iter.h"
|
||||
|
||||
LineIterator::LineIterator(Shard *r, uint32_t start_line) : root(r) {
|
||||
if (!root)
|
||||
return;
|
||||
Shard::retain(root);
|
||||
seek_line(start_line);
|
||||
}
|
||||
|
||||
LineIterator::~LineIterator() {
|
||||
Shard::release(root);
|
||||
}
|
||||
|
||||
void LineIterator::descend(Shard *s) {
|
||||
while (s->kind == Shard::ShardKind::Branch) {
|
||||
auto *b = static_cast<Branch *>(s);
|
||||
stack.push_back(b);
|
||||
s = b->left;
|
||||
}
|
||||
petal = static_cast<Petal *>(s);
|
||||
petal_offset = 0;
|
||||
}
|
||||
|
||||
bool LineIterator::advance_petal() {
|
||||
while (!stack.empty()) {
|
||||
Branch *top = stack.back();
|
||||
stack.pop_back();
|
||||
descend(top->right);
|
||||
if (petal->length > 0)
|
||||
return true;
|
||||
}
|
||||
petal = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
void LineIterator::seek_line(uint32_t line_index) {
|
||||
stack.clear();
|
||||
if (line_index == 0) {
|
||||
descend(root);
|
||||
return;
|
||||
}
|
||||
uint32_t nth = line_index; // we want the position right after the nth '\n' (1-indexed)
|
||||
Shard *s = root;
|
||||
while (s->kind == Shard::ShardKind::Branch) {
|
||||
auto *b = static_cast<Branch *>(s);
|
||||
if (nth <= b->left->lines) {
|
||||
stack.push_back(b); // still need to visit b->right later
|
||||
s = b->left;
|
||||
} else {
|
||||
nth -= b->left->lines; // left fully skipped, don't revisit it
|
||||
s = b->right;
|
||||
}
|
||||
}
|
||||
petal = static_cast<Petal *>(s);
|
||||
uint32_t nl_pos = petal->source->nth_newline(petal->pos, nth);
|
||||
petal_offset = (nl_pos - petal->pos) + 1;
|
||||
if (petal_offset >= petal->length)
|
||||
advance_petal();
|
||||
}
|
||||
|
||||
bool LineIterator::next(std::string &line) {
|
||||
line.clear();
|
||||
if (!petal)
|
||||
return false;
|
||||
|
||||
bool produced = false;
|
||||
while (petal) {
|
||||
produced = true;
|
||||
uint32_t abs_pos = petal->pos + petal_offset;
|
||||
uint32_t petal_end = petal->pos + petal->length;
|
||||
|
||||
uint32_t nl = petal->source->next_newline(abs_pos);
|
||||
uint32_t stop = (nl != UINT32_MAX && nl < petal_end) ? nl : petal_end;
|
||||
|
||||
uint32_t remaining = stop - abs_pos;
|
||||
uint32_t cur = abs_pos;
|
||||
while (remaining) {
|
||||
uint32_t got;
|
||||
const char *data = petal->source->read(cur, &got);
|
||||
uint32_t take = std::min(got, remaining);
|
||||
line.append(data, take);
|
||||
cur += take;
|
||||
remaining -= take;
|
||||
}
|
||||
petal_offset = cur - petal->pos;
|
||||
|
||||
if (stop == nl) {
|
||||
petal_offset += 1;
|
||||
if (petal_offset >= petal->length)
|
||||
advance_petal();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!advance_petal())
|
||||
break;
|
||||
}
|
||||
return produced;
|
||||
}
|
||||
Reference in New Issue
Block a user