Rearrange project files.
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "../constants.h"
|
||||
#include "buffer.h"
|
||||
#include "pch.h"
|
||||
|
||||
struct AppendBuffer : Buffer {
|
||||
char *buf = nullptr;
|
||||
uint64_t allocated_capacity = 0;
|
||||
uint64_t current_size = 0;
|
||||
int fd = -1;
|
||||
|
||||
AppendBuffer(std::filesystem::path base_dir);
|
||||
~AppendBuffer();
|
||||
|
||||
uint64_t append(const char c);
|
||||
uint64_t append(const char *text, uint64_t len);
|
||||
const char *read(uint64_t pos) override;
|
||||
uint64_t length() override;
|
||||
|
||||
private:
|
||||
void grow(uint64_t len);
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "pch.h"
|
||||
|
||||
struct Buffer {
|
||||
virtual const char *read(uint64_t pos) = 0;
|
||||
virtual uint64_t length() = 0;
|
||||
virtual ~Buffer() = default;
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "buffer.h"
|
||||
#include "pch.h"
|
||||
|
||||
struct OriginalBuffer : Buffer {
|
||||
const char *buf;
|
||||
uint64_t len;
|
||||
int fd = -1;
|
||||
|
||||
OriginalBuffer(std::filesystem::path base_dir);
|
||||
~OriginalBuffer();
|
||||
|
||||
void initialize();
|
||||
const char *read(uint64_t pos) override;
|
||||
uint64_t length() override;
|
||||
};
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "pch.h"
|
||||
|
||||
constexpr uint64_t PETAL_SIZE_MAX = 32 * 1024;
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "../shard.h"
|
||||
#include "pch.h"
|
||||
|
||||
enum struct Direction : uint8_t {
|
||||
Forward,
|
||||
Backward
|
||||
};
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "../shard.h"
|
||||
#include "pch.h"
|
||||
#include "petal.h"
|
||||
|
||||
struct LineIterator {
|
||||
PetalIterator it;
|
||||
const char *chunk;
|
||||
uint64_t len;
|
||||
Direction dir;
|
||||
uint64_t current_offset = 0;
|
||||
uint64_t last_line_offset = 0;
|
||||
|
||||
LineIterator(Shard *root, uint64_t line_num, Direction dir);
|
||||
~LineIterator() = default;
|
||||
|
||||
bool next(std::string *line);
|
||||
uint64_t byte_offset();
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include "../shard.h"
|
||||
#include "iter.h"
|
||||
#include "pch.h"
|
||||
|
||||
struct PetalIterator {
|
||||
Direction dir;
|
||||
Shard *root = nullptr;
|
||||
std::vector<Shard *> stack;
|
||||
Petal *petal = nullptr;
|
||||
uint64_t global_offset = 0;
|
||||
uint64_t last_offset = 0;
|
||||
uint64_t petal_offset = 0;
|
||||
uint64_t global_line = 0;
|
||||
|
||||
PetalIterator(Shard *r, Direction dir);
|
||||
~PetalIterator() = default;
|
||||
|
||||
void seek_offset(uint64_t offset);
|
||||
void seek_line(uint64_t offset);
|
||||
bool next(const char **data, uint64_t *out_len);
|
||||
uint64_t byte_offset();
|
||||
|
||||
private:
|
||||
Petal *_next(uint64_t *offset);
|
||||
};
|
||||
@@ -0,0 +1,113 @@
|
||||
#pragma once
|
||||
|
||||
#include "buffer/buffer.h"
|
||||
#include "buffer/original.h"
|
||||
#include "constants.h"
|
||||
#include "pch.h"
|
||||
|
||||
struct Shard {
|
||||
enum struct Kind : uint8_t {
|
||||
Branch,
|
||||
Petal
|
||||
} kind;
|
||||
|
||||
uint8_t height;
|
||||
|
||||
uint64_t length;
|
||||
uint64_t lines;
|
||||
|
||||
std::atomic_uint64_t refs;
|
||||
|
||||
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);
|
||||
static void release(Shard *n);
|
||||
|
||||
static Shard *from_file(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 Shard *concat(Shard *left, Shard *right);
|
||||
static Shard *merge(Shard *a, Shard *b);
|
||||
static Shard *merge_leaves(Shard *a, Shard *b);
|
||||
static Shard *append(Shard *root, Shard *leaf);
|
||||
static Shard *build(Shard **pieces, uint64_t lo, uint64_t hi);
|
||||
};
|
||||
|
||||
struct Branch : Shard {
|
||||
Shard *left;
|
||||
Shard *right;
|
||||
|
||||
Branch(Shard *l, Shard *r)
|
||||
: Shard(
|
||||
Kind::Branch,
|
||||
l->length + r->length,
|
||||
l->lines + r->lines,
|
||||
1 + std::max(l->height, r->height)
|
||||
),
|
||||
left(l), right(r) {
|
||||
retain(left);
|
||||
retain(right);
|
||||
};
|
||||
};
|
||||
|
||||
struct Petal : Shard {
|
||||
Buffer *source;
|
||||
uint64_t pos;
|
||||
|
||||
Petal(uint64_t length, uint64_t lines, Buffer *source, uint64_t pos)
|
||||
: 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";
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
#pragma once
|
||||
|
||||
#include "buffer/append.h"
|
||||
#include "buffer/original.h"
|
||||
#include "constants.h"
|
||||
#include "iterators/line.h"
|
||||
#include "pch.h"
|
||||
#include "search.h"
|
||||
#include "shard.h"
|
||||
|
||||
struct Point {
|
||||
uint64_t row;
|
||||
uint64_t col;
|
||||
};
|
||||
|
||||
struct Range {
|
||||
Point start;
|
||||
Point end;
|
||||
};
|
||||
|
||||
struct RegexGroup {
|
||||
uint64_t start{UINT32_MAX};
|
||||
uint64_t end{UINT32_MAX};
|
||||
};
|
||||
|
||||
struct RegexMatch {
|
||||
uint64_t start;
|
||||
uint64_t end;
|
||||
RegexGroup groups[9]{};
|
||||
};
|
||||
|
||||
struct ReplacePart {
|
||||
enum struct PartType {
|
||||
FullMatch,
|
||||
CaptureGroup,
|
||||
Constant
|
||||
} type;
|
||||
|
||||
std::variant<uint8_t, Shard *> value;
|
||||
};
|
||||
|
||||
struct Vase {
|
||||
OriginalBuffer *original;
|
||||
AppendBuffer *append;
|
||||
Shard *root;
|
||||
std::filesystem::path path;
|
||||
std::filesystem::path swapdir;
|
||||
|
||||
Vase(std::filesystem::path path, std::filesystem::path swapdir);
|
||||
~Vase();
|
||||
|
||||
uint64_t length();
|
||||
std::string to_string();
|
||||
std::string to_string(Range range);
|
||||
|
||||
Point resolve_column(uint64_t line, uint64_t column);
|
||||
|
||||
LineIterator iterate(uint64_t line);
|
||||
|
||||
void insert(Point *point, char key);
|
||||
void insert(Point *point, const char *data, uint64_t len);
|
||||
void erase(Point *point, uint64_t amount, Direction dir);
|
||||
void erase(Range range);
|
||||
void replace(Range range, const char *data, uint64_t len);
|
||||
|
||||
void move_clusters(Point *point, uint64_t amount, Direction dir);
|
||||
void move_lines(Point *point, uint64_t amount, Direction dir);
|
||||
void clamp(Point *point);
|
||||
|
||||
void regex_search_replace(
|
||||
std::string_view pattern, Range range,
|
||||
std::string_view replace, std::string_view options
|
||||
);
|
||||
|
||||
std::vector<Range> regex_search(
|
||||
std::string_view pattern, Range range, std::string_view options
|
||||
);
|
||||
|
||||
bool undo();
|
||||
bool redo();
|
||||
void snapshot();
|
||||
void prune_history(uint64_t n);
|
||||
|
||||
bool save();
|
||||
bool save_swap();
|
||||
|
||||
uint64_t offset_of(Point point);
|
||||
Point point_of(uint64_t offset);
|
||||
|
||||
private:
|
||||
std::vector<Shard *> history;
|
||||
uint64_t history_top;
|
||||
|
||||
void _insert(Point *point, const char *data, uint64_t len);
|
||||
|
||||
std::vector<ReplacePart> parse_replace(std::string_view s);
|
||||
std::vector<RegexMatch> _regex_search(
|
||||
std::string_view pattern, Range range, std::string_view options
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user