This commit is contained in:
2026-07-28 02:57:13 +01:00
parent 737c6c1d20
commit 57dda2589e
14 changed files with 369 additions and 84 deletions
+40
View File
@@ -0,0 +1,40 @@
#pragma once
#include "pch.h"
inline int read_file(const char *path, char **text, uint32_t *len) {
FILE *f = fopen(path, "rb");
if (!f)
return 0;
fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, 0, SEEK_SET);
if (size < 0 || size > UINT32_MAX) {
fclose(f);
return 0;
}
*len = (uint32_t)size;
*text = (char *)malloc(*len + 1);
if (!*text) {
fclose(f);
return 0;
}
size_t read = fread(*text, 1, *len, f);
fclose(f);
if (read != *len) {
free(*text);
*text = NULL;
*len = 0;
return 0;
}
(*text)[*len] = '\0'; // handy if treating it as a C string
return 1;
}
+1
View File
@@ -30,6 +30,7 @@ extern "C" {
#include <fstream>
#include <functional>
#include <immintrin.h>
#include <iostream>
#include <limits.h>
#include <map>
#include <mutex>
@@ -1,9 +1,12 @@
#pragma once
#include "buffer.h"
#include "pch.h"
#include <cstdint>
struct AppendBuffer {
struct AppendBuffer : Buffer {
static constexpr uint32_t CHUNK_SIZE = 1 << 16;
using TChunk = char[CHUNK_SIZE];
std::vector<TChunk *> buf;
TChunk *t_current;
@@ -22,9 +25,14 @@ struct AppendBuffer {
inline void key(char c);
void 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);
private:
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 void _append(const char *text, uint32_t length);
};
+10
View File
@@ -0,0 +1,10 @@
#pragma once
#include "pch.h"
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 ~Buffer() = default;
};
@@ -1,8 +1,9 @@
#pragma once
#include "buffer.h"
#include "pch.h"
struct OriginalBuffer {
struct OriginalBuffer : Buffer {
char *buf;
uint32_t length;
std::vector<uint32_t> newlines;
@@ -23,4 +24,7 @@ struct OriginalBuffer {
~OriginalBuffer() {
free(buf);
}
const char *read(uint32_t pos, uint32_t *out_len);
uint32_t count_lines(uint32_t pos, uint32_t length);
};
+9 -7
View File
@@ -1,5 +1,6 @@
#pragma once
#include "buffer/buffer.h"
#include "pch.h"
struct Shard {
@@ -14,7 +15,7 @@ struct Shard {
std::atomic_uint32_t refs;
Shard(ShardKind kind, uint32_t length, uint32_t lines)
: kind(kind), length(length), lines(lines) {};
: kind(kind), length(length), lines(lines), refs(0) {};
};
struct ShardPtr {
@@ -53,17 +54,18 @@ struct Branch : Shard {
Branch(Shard *l, Shard *r)
: Shard(ShardKind::Branch, l->length + r->length, l->lines + r->lines),
left(ShardPtr(l)), right(ShardPtr(r)) {};
left(l), right(r) {};
};
struct Petal : Shard {
enum struct Kind {
Original,
Append
} source;
Buffer *source;
uint32_t pos;
Petal(uint32_t length, uint32_t lines, Kind source, uint32_t pos)
Petal(uint32_t length, uint32_t lines, Buffer *source, uint32_t pos)
: Shard(ShardKind::Petal, length, lines), source(source), pos(pos) {};
};
std::pair<ShardPtr, ShardPtr> split(Shard *n, uint32_t offset);
void print_shard(const Shard *shard, int depth = 0);
+3 -3
View File
@@ -1,7 +1,7 @@
#pragma once
#include "append_buffer.h"
#include "original_buffer.h"
#include "buffer/append.h"
#include "buffer/original.h"
#include "pch.h"
#include "shard.h"
@@ -21,7 +21,7 @@ struct Vase {
root = new Petal(
length,
original.newlines.size(),
Petal::Kind::Original,
&original,
0
);
}