rename PieceDescriptor to just Piece
This commit is contained in:
parent
cef49e9e6e
commit
7064afc1f1
@ -77,16 +77,16 @@ bool Buffer::write_to_file(const char * filename)
|
||||
}
|
||||
|
||||
uint32_t bytes_written = 0u;
|
||||
for (auto pd = piece_table->start_descriptor->next;
|
||||
pd != piece_table->end_descriptor;
|
||||
pd = pd->next)
|
||||
for (auto piece = piece_table->start_piece->next;
|
||||
piece != piece_table->end_piece;
|
||||
piece = piece->next)
|
||||
{
|
||||
if (!file.write(pd->start, pd->length))
|
||||
if (!file.write(piece->start, piece->length))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
bytes_written += pd->length;
|
||||
if (pd->eol())
|
||||
bytes_written += piece->length;
|
||||
if (piece->eol())
|
||||
{
|
||||
if (!file.write(LineEndings::spans[m_line_endings]))
|
||||
{
|
||||
|
@ -4,40 +4,40 @@ PieceTable::PieceTable(const uint8_t * file_buffer, unsigned long file_buffer_si
|
||||
{
|
||||
m_file_buffer = file_buffer;
|
||||
m_file_buffer_size = file_buffer_size;
|
||||
start_descriptor = &m_piece_descriptors[PIECE_DESCRIPTOR_INDEX_START];
|
||||
end_descriptor = &m_piece_descriptors[PIECE_DESCRIPTOR_INDEX_END];
|
||||
start_descriptor->next = end_descriptor;
|
||||
end_descriptor->prev = start_descriptor;
|
||||
m_piece_descriptor_index = 2u;
|
||||
cursor_position.pd = end_descriptor;
|
||||
start_piece = &m_pieces[PIECE_INDEX_START];
|
||||
end_piece = &m_pieces[PIECE_INDEX_END];
|
||||
start_piece->next = end_piece;
|
||||
end_piece->prev = start_piece;
|
||||
m_piece_index = 2u;
|
||||
cursor_position.piece = end_piece;
|
||||
cursor_position.offset = 0u;
|
||||
}
|
||||
|
||||
void PieceTable::append_initial_line_piece(uint8_t * start, uint32_t length, uint32_t n_chars, bool eol)
|
||||
{
|
||||
PieceDescriptor * pd = add_piece_descriptor();
|
||||
pd->prev = end_descriptor->prev;
|
||||
pd->next = end_descriptor;
|
||||
pd->start = start;
|
||||
pd->length = length;
|
||||
pd->n_chars = n_chars;
|
||||
Piece * piece = add_piece();
|
||||
piece->prev = end_piece->prev;
|
||||
piece->next = end_piece;
|
||||
piece->start = start;
|
||||
piece->length = length;
|
||||
piece->n_chars = n_chars;
|
||||
if (eol)
|
||||
pd->n_chars |= PIECE_DESCRIPTOR_N_CHARS_EOL_FLAG_MASK;
|
||||
end_descriptor->prev->next = pd;
|
||||
end_descriptor->prev = pd;
|
||||
piece->n_chars |= PIECE_DESCRIPTOR_N_CHARS_EOL_FLAG_MASK;
|
||||
end_piece->prev->next = piece;
|
||||
end_piece->prev = piece;
|
||||
m_num_lines++;
|
||||
if (cursor_position.pd == end_descriptor)
|
||||
cursor_position.pd = pd;
|
||||
if (cursor_position.piece == end_piece)
|
||||
cursor_position.piece = piece;
|
||||
}
|
||||
|
||||
PieceTable::PieceDescriptor * PieceTable::get_start_of_line(PieceTable::PieceDescriptor * start) const
|
||||
PieceTable::Piece * PieceTable::get_start_of_line(PieceTable::Piece * start) const
|
||||
{
|
||||
PieceDescriptor * pd = start;
|
||||
Piece * piece = start;
|
||||
|
||||
while (pd->prev != start_descriptor && !pd->prev->eol())
|
||||
while (piece->prev != start_piece && !piece->prev->eol())
|
||||
{
|
||||
pd = pd->prev;
|
||||
piece = piece->prev;
|
||||
}
|
||||
|
||||
return pd;
|
||||
return piece;
|
||||
}
|
||||
|
@ -5,21 +5,24 @@
|
||||
#include "PagedBuffer.h"
|
||||
#include <utility>
|
||||
|
||||
#define PIECE_DESCRIPTOR_INDEX_START 0u
|
||||
#define PIECE_DESCRIPTOR_INDEX_END 1u
|
||||
|
||||
#define PIECE_DESCRIPTOR_N_CHARS_EOL_FLAG_MASK 0x80000000u
|
||||
|
||||
class PieceTable
|
||||
{
|
||||
public:
|
||||
struct PieceDescriptor
|
||||
enum
|
||||
{
|
||||
/** The previous piece descriptor in the chain. */
|
||||
PieceDescriptor * prev;
|
||||
PIECE_INDEX_START = 0,
|
||||
PIECE_INDEX_END = 1,
|
||||
};
|
||||
|
||||
/** The next piece descriptor in the chain. */
|
||||
PieceDescriptor * next;
|
||||
struct Piece
|
||||
{
|
||||
/** The previous piece in the chain. */
|
||||
Piece * prev;
|
||||
|
||||
/** The next piece in the chain. */
|
||||
Piece * next;
|
||||
|
||||
/** Text that the piece describes. */
|
||||
uint8_t * start;
|
||||
@ -33,20 +36,20 @@ public:
|
||||
*/
|
||||
uint32_t n_chars;
|
||||
|
||||
/** Get the number of characters pointed to by this descriptor. */
|
||||
/** Get the number of characters pointed to by this piece. */
|
||||
uint32_t get_n_chars() { return n_chars & (~PIECE_DESCRIPTOR_N_CHARS_EOL_FLAG_MASK); }
|
||||
|
||||
/** Get whether this descriptor is the end of a line. */
|
||||
/** Get whether this piece is the end of a line. */
|
||||
bool eol() { return (n_chars & PIECE_DESCRIPTOR_N_CHARS_EOL_FLAG_MASK) != 0u; }
|
||||
|
||||
/** Toggle whether this descriptor is the end of a line. */
|
||||
/** Toggle whether this piece is the end of a line. */
|
||||
void toggle_eol() { n_chars ^= PIECE_DESCRIPTOR_N_CHARS_EOL_FLAG_MASK; }
|
||||
};
|
||||
|
||||
struct Position
|
||||
{
|
||||
/** The piece. */
|
||||
PieceDescriptor * pd;
|
||||
Piece * piece;
|
||||
|
||||
/** Byte offset within the piece. */
|
||||
uint32_t offset;
|
||||
@ -62,37 +65,37 @@ public:
|
||||
|
||||
PieceTable(const uint8_t * file_buffer, unsigned long file_buffer_size);
|
||||
|
||||
PieceDescriptor * add_piece_descriptor()
|
||||
Piece * add_piece()
|
||||
{
|
||||
return &m_piece_descriptors[m_piece_descriptor_index++];
|
||||
return &m_pieces[m_piece_index++];
|
||||
}
|
||||
|
||||
PieceDescriptor & operator[](uint32_t index)
|
||||
Piece & operator[](uint32_t index)
|
||||
{
|
||||
return m_piece_descriptors[index];
|
||||
return m_pieces[index];
|
||||
}
|
||||
|
||||
uint32_t get_num_lines() { return m_num_lines; }
|
||||
|
||||
PieceDescriptor * start_descriptor;
|
||||
PieceDescriptor * end_descriptor;
|
||||
Piece * start_piece;
|
||||
Piece * end_piece;
|
||||
|
||||
Position cursor_position;
|
||||
|
||||
void append_initial_line_piece(uint8_t * start, uint32_t length, uint32_t n_chars, bool eol);
|
||||
|
||||
PieceDescriptor * get_start_of_line(PieceDescriptor * start) const;
|
||||
Piece * get_start_of_line(Piece * start) const;
|
||||
|
||||
protected:
|
||||
const uint8_t * m_file_buffer;
|
||||
unsigned long m_file_buffer_size;
|
||||
uint32_t m_num_lines;
|
||||
|
||||
/** Next available piece descriptor index. */
|
||||
uint32_t m_piece_descriptor_index;
|
||||
/** Next available piece index. */
|
||||
uint32_t m_piece_index;
|
||||
|
||||
PagedBuffer<uint8_t> m_append_buffer;
|
||||
PagedBuffer<PieceDescriptor> m_piece_descriptors;
|
||||
PagedBuffer<Piece> m_pieces;
|
||||
PagedBuffer<ChangeDescriptor> m_change_descriptors;
|
||||
};
|
||||
|
||||
|
@ -118,7 +118,7 @@ bool Window::create(std::shared_ptr<Buffer> buffer)
|
||||
m_shaders.text->set_color(1.0, 1.0, 1.0, 1.0);
|
||||
|
||||
m_buffer = buffer;
|
||||
m_start_piece = m_buffer->piece_table->start_descriptor->next;
|
||||
m_start_piece = m_buffer->piece_table->start_piece->next;
|
||||
|
||||
resize();
|
||||
|
||||
@ -229,8 +229,8 @@ void Window::handle_key(uint32_t scancode, uint32_t mod)
|
||||
|
||||
void Window::scroll_down()
|
||||
{
|
||||
if ((m_start_piece != m_buffer->piece_table->end_descriptor) &&
|
||||
(m_start_piece->next != m_buffer->piece_table->end_descriptor))
|
||||
if ((m_start_piece != m_buffer->piece_table->end_piece) &&
|
||||
(m_start_piece->next != m_buffer->piece_table->end_piece))
|
||||
{
|
||||
m_start_piece = m_start_piece->next;
|
||||
redraw();
|
||||
@ -239,8 +239,8 @@ void Window::scroll_down()
|
||||
|
||||
void Window::scroll_up()
|
||||
{
|
||||
if ((m_start_piece != m_buffer->piece_table->start_descriptor) &&
|
||||
(m_start_piece->prev != m_buffer->piece_table->start_descriptor))
|
||||
if ((m_start_piece != m_buffer->piece_table->start_piece) &&
|
||||
(m_start_piece->prev != m_buffer->piece_table->start_piece))
|
||||
{
|
||||
m_start_piece = m_start_piece->prev;
|
||||
redraw();
|
||||
@ -270,13 +270,13 @@ void Window::redraw()
|
||||
int line_height = m_font.get_line_height();
|
||||
int x = 0;
|
||||
int y = m_height - line_height;
|
||||
for (PieceTable::PieceDescriptor * pd = m_start_piece;
|
||||
pd != m_buffer->piece_table->end_descriptor;
|
||||
pd = pd->next)
|
||||
for (PieceTable::Piece * piece = m_start_piece;
|
||||
piece != m_buffer->piece_table->end_piece;
|
||||
piece = piece->next)
|
||||
{
|
||||
for (int i = 0, len = pd->length; i < len; i++)
|
||||
for (int i = 0, len = piece->length; i < len; i++)
|
||||
{
|
||||
uint8_t c = pd->start[i];
|
||||
uint8_t c = piece->start[i];
|
||||
auto g = m_font.get_glyph(c);
|
||||
m_shaders.text->set_position(x, y);
|
||||
g->render();
|
||||
@ -290,7 +290,7 @@ void Window::redraw()
|
||||
x += advance;
|
||||
}
|
||||
}
|
||||
if (pd->eol())
|
||||
if (piece->eol())
|
||||
{
|
||||
y -= line_height;
|
||||
x = 0;
|
||||
|
@ -35,7 +35,7 @@ protected:
|
||||
|
||||
std::shared_ptr<Buffer> m_buffer;
|
||||
|
||||
PieceTable::PieceDescriptor * m_start_piece;
|
||||
PieceTable::Piece * m_start_piece;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user