Major changes to api and structure.

- Remove newline caching
- limit petal sizes
- make public facing api use points only
- fix iterators (chunk & line)
This commit is contained in:
2026-08-02 11:07:04 +01:00
parent 89b24a7488
commit 51e193d28b
16 changed files with 355 additions and 422 deletions
+8
View File
@@ -0,0 +1,8 @@
#pragma once
#include "pch.h"
struct Point {
uint32_t row;
uint32_t col;
};
+2 -18
View File
@@ -12,31 +12,15 @@ struct AppendBuffer : Buffer {
uint32_t current_offset{0};
uint32_t t_offset{0};
using LChunk = uint32_t[CHUNK_SIZE];
std::vector<LChunk *> newlines;
LChunk *l_current;
uint32_t l_offset{0};
AppendBuffer();
~AppendBuffer();
uint32_t key(char c);
uint32_t append(const char *text, uint32_t length, uint32_t *line_count);
uint32_t append(const char *text, uint32_t length);
const char *read(uint32_t pos, uint32_t *out_len);
uint32_t count_lines(uint32_t pos, uint32_t length);
uint32_t next_newline(uint32_t pos);
uint32_t nth_newline(uint32_t pos, uint32_t n);
private:
inline uint32_t newline_value_at(uint32_t flat_index);
inline uint32_t length();
inline void new_text_chunk();
inline void new_line_chunk();
inline uint32_t find_newline(uint32_t target, uint32_t low, uint32_t high);
inline uint32_t _append(const char *text, uint32_t length);
};
+1 -3
View File
@@ -4,8 +4,6 @@
struct Buffer {
virtual const char *read(uint32_t pos, uint32_t *out_len) = 0;
virtual uint32_t count_lines(uint32_t pos, uint32_t length) = 0;
virtual uint32_t next_newline(uint32_t pos) = 0;
virtual uint32_t nth_newline(uint32_t pos, uint32_t n) = 0;
virtual inline uint32_t length() = 0;
virtual ~Buffer() = default;
};
+3 -6
View File
@@ -5,16 +5,13 @@
struct OriginalBuffer : Buffer {
char *buf;
uint32_t length;
std::vector<uint32_t> newlines;
uint32_t len;
// TODO: replace with file io or even lazy loaded file io for large files/logs etc. later.
OriginalBuffer(char *buf, uint32_t length);
OriginalBuffer(char *buf, uint32_t len);
~OriginalBuffer();
const char *read(uint32_t pos, uint32_t *out_len);
uint32_t count_lines(uint32_t pos, uint32_t length);
uint32_t next_newline(uint32_t pos);
uint32_t nth_newline(uint32_t pos, uint32_t n);
uint32_t length();
};
+5 -2
View File
@@ -8,11 +8,14 @@ struct ChunkIterator {
std::vector<Shard *> stack;
Petal *petal = nullptr;
uint32_t petal_offset = 0;
uint32_t global_offset;
ChunkIterator(Shard *r, uint32_t offset = 0);
ChunkIterator(Shard *r);
~ChunkIterator();
void seek(uint32_t offset);
void seek_offset(uint32_t offset);
void seek_line(uint32_t offset);
bool next(const char **data, uint32_t *out_len);
uint32_t byte_offset();
};
+4 -3
View File
@@ -5,16 +5,17 @@
#include "pch.h"
struct LineIterator {
uint32_t offset;
ChunkIterator it;
const char *cursor = nullptr;
uint32_t remaining = 0;
bool eof = false;
uint32_t remaining = 0;
uint32_t line_offset = 0;
LineIterator(Shard *r, uint32_t start_line);
~LineIterator();
~LineIterator() = default;
bool next(std::string &line);
uint32_t byte_offset();
};
+7 -20
View File
@@ -1,7 +1,9 @@
#pragma once
#include "buffer/buffer.h"
#include "buffer/original.h"
#include "pch.h"
#include "utils/utils.h"
struct Shard {
enum struct ShardKind : uint8_t {
@@ -19,17 +21,8 @@ struct Shard {
Shard(ShardKind kind, uint32_t length, uint32_t lines, uint8_t height)
: kind(kind), height(height), length(length), lines(lines), refs(1) {};
virtual ~Shard() = default;
static void retain(Shard *n) {
n->refs++;
}
static void release(Shard *n) {
if (!n || --n->refs > 0)
return;
delete n;
}
static void retain(Shard *n);
static void release(Shard *n);
};
struct Branch : Shard {
@@ -47,11 +40,6 @@ struct Branch : Shard {
retain(left);
retain(right);
};
~Branch() {
release(left);
release(right);
}
};
struct Petal : Shard {
@@ -64,12 +52,11 @@ struct Petal : Shard {
source(source), pos(pos) {};
};
Shard *create_shards(Buffer *o);
std::pair<Shard *, Shard *> split_shard(Shard *n, uint32_t offset);
Shard *concat_shard(Shard *left, Shard *right);
Shard *merge(Shard *a, Shard *b);
Shard *merge_leaves(Shard *a, Shard *b);
Shard *append_leaf(Shard *root, Shard *leaf);
uint32_t offset_of(Shard *s, uint32_t line_number, uint32_t col);
void print_shard(const Shard *shard, int depth = 0);
Shard *build_balanced(Shard **pieces, uint32_t lo, uint32_t hi);
+21 -23
View File
@@ -2,46 +2,44 @@
#include "buffer/append.h"
#include "buffer/original.h"
#include "iterators/chunk.h"
#include "iterators/line.h"
#include "pch.h"
#include "search.h"
#include "shard.h"
#include "utils/utils.h"
struct Vase {
OriginalBuffer original;
AppendBuffer append;
/*std::vector<Shard *> undo; // TODO: later
uint8_t top; // of the undo stack.
uint8_t max; // for redo when no edits have been done after some undo.*/
Shard *root;
Vase(char *data, uint32_t length);
~Vase();
uint32_t length();
std::string to_string();
static void flatten(Shard *s, std::string &out);
void type(uint32_t offset, char key);
void insert(uint32_t offset, const char *data, uint32_t len);
void erase(uint32_t cursor, int64_t amount);
Point resolve_column(uint32_t line, uint32_t column);
// Grapheme clusters or the specail cluster "\r\n'
void erase_clusters(uint32_t cursor, int64_t amount);
// moves in clusters from line,col and sets new position to line,col returns false if not enough space to jump.
bool jump_clusters(uint32_t *line, uint32_t *col, int64_t amount);
// need to also make helpers for line width.
LineIterator iterate(uint32_t line);
void input(Point *point, char key);
void insert(Point *point, const char *data, uint32_t len);
void erase(Point *point, int64_t amount);
bool jump(Point *point, int64_t amount);
bool clamp(Point *point);
void regex_search_replace(
std::string_view pattern,
uint32_t start_offset, uint32_t end_offset,
Point start, Point end,
std::string_view replace, std::string_view options
);
private:
OriginalBuffer original;
AppendBuffer append;
Shard *root;
/*std::vector<Shard *> undo; // TODO: later
uint16_t top; // of the undo stack.
uint16_t max; // for redo when no edits have been done after some undo.*/
uint32_t offset_of(Point point);
static void flatten(Shard *s, std::string &out);
};