rename PieceDescriptor to just Piece

This commit is contained in:
Josh Holtrop 2016-07-30 09:52:46 -04:00
parent cef49e9e6e
commit 7064afc1f1
5 changed files with 65 additions and 62 deletions

View File

@ -77,16 +77,16 @@ bool Buffer::write_to_file(const char * filename)
} }
uint32_t bytes_written = 0u; uint32_t bytes_written = 0u;
for (auto pd = piece_table->start_descriptor->next; for (auto piece = piece_table->start_piece->next;
pd != piece_table->end_descriptor; piece != piece_table->end_piece;
pd = pd->next) piece = piece->next)
{ {
if (!file.write(pd->start, pd->length)) if (!file.write(piece->start, piece->length))
{ {
return false; return false;
} }
bytes_written += pd->length; bytes_written += piece->length;
if (pd->eol()) if (piece->eol())
{ {
if (!file.write(LineEndings::spans[m_line_endings])) if (!file.write(LineEndings::spans[m_line_endings]))
{ {

View File

@ -4,40 +4,40 @@ PieceTable::PieceTable(const uint8_t * file_buffer, unsigned long file_buffer_si
{ {
m_file_buffer = file_buffer; m_file_buffer = file_buffer;
m_file_buffer_size = file_buffer_size; m_file_buffer_size = file_buffer_size;
start_descriptor = &m_piece_descriptors[PIECE_DESCRIPTOR_INDEX_START]; start_piece = &m_pieces[PIECE_INDEX_START];
end_descriptor = &m_piece_descriptors[PIECE_DESCRIPTOR_INDEX_END]; end_piece = &m_pieces[PIECE_INDEX_END];
start_descriptor->next = end_descriptor; start_piece->next = end_piece;
end_descriptor->prev = start_descriptor; end_piece->prev = start_piece;
m_piece_descriptor_index = 2u; m_piece_index = 2u;
cursor_position.pd = end_descriptor; cursor_position.piece = end_piece;
cursor_position.offset = 0u; cursor_position.offset = 0u;
} }
void PieceTable::append_initial_line_piece(uint8_t * start, uint32_t length, uint32_t n_chars, bool eol) void PieceTable::append_initial_line_piece(uint8_t * start, uint32_t length, uint32_t n_chars, bool eol)
{ {
PieceDescriptor * pd = add_piece_descriptor(); Piece * piece = add_piece();
pd->prev = end_descriptor->prev; piece->prev = end_piece->prev;
pd->next = end_descriptor; piece->next = end_piece;
pd->start = start; piece->start = start;
pd->length = length; piece->length = length;
pd->n_chars = n_chars; piece->n_chars = n_chars;
if (eol) if (eol)
pd->n_chars |= PIECE_DESCRIPTOR_N_CHARS_EOL_FLAG_MASK; piece->n_chars |= PIECE_DESCRIPTOR_N_CHARS_EOL_FLAG_MASK;
end_descriptor->prev->next = pd; end_piece->prev->next = piece;
end_descriptor->prev = pd; end_piece->prev = piece;
m_num_lines++; m_num_lines++;
if (cursor_position.pd == end_descriptor) if (cursor_position.piece == end_piece)
cursor_position.pd = pd; 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;
} }

View File

@ -5,21 +5,24 @@
#include "PagedBuffer.h" #include "PagedBuffer.h"
#include <utility> #include <utility>
#define PIECE_DESCRIPTOR_INDEX_START 0u
#define PIECE_DESCRIPTOR_INDEX_END 1u
#define PIECE_DESCRIPTOR_N_CHARS_EOL_FLAG_MASK 0x80000000u #define PIECE_DESCRIPTOR_N_CHARS_EOL_FLAG_MASK 0x80000000u
class PieceTable class PieceTable
{ {
public: public:
struct PieceDescriptor enum
{ {
/** The previous piece descriptor in the chain. */ PIECE_INDEX_START = 0,
PieceDescriptor * prev; PIECE_INDEX_END = 1,
};
/** The next piece descriptor in the chain. */ struct Piece
PieceDescriptor * next; {
/** The previous piece in the chain. */
Piece * prev;
/** The next piece in the chain. */
Piece * next;
/** Text that the piece describes. */ /** Text that the piece describes. */
uint8_t * start; uint8_t * start;
@ -33,20 +36,20 @@ public:
*/ */
uint32_t n_chars; 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); } 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; } 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; } void toggle_eol() { n_chars ^= PIECE_DESCRIPTOR_N_CHARS_EOL_FLAG_MASK; }
}; };
struct Position struct Position
{ {
/** The piece. */ /** The piece. */
PieceDescriptor * pd; Piece * piece;
/** Byte offset within the piece. */ /** Byte offset within the piece. */
uint32_t offset; uint32_t offset;
@ -62,37 +65,37 @@ public:
PieceTable(const uint8_t * file_buffer, unsigned long file_buffer_size); 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; } uint32_t get_num_lines() { return m_num_lines; }
PieceDescriptor * start_descriptor; Piece * start_piece;
PieceDescriptor * end_descriptor; Piece * end_piece;
Position cursor_position; Position cursor_position;
void append_initial_line_piece(uint8_t * start, uint32_t length, uint32_t n_chars, bool eol); 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: protected:
const uint8_t * m_file_buffer; const uint8_t * m_file_buffer;
unsigned long m_file_buffer_size; unsigned long m_file_buffer_size;
uint32_t m_num_lines; uint32_t m_num_lines;
/** Next available piece descriptor index. */ /** Next available piece index. */
uint32_t m_piece_descriptor_index; uint32_t m_piece_index;
PagedBuffer<uint8_t> m_append_buffer; PagedBuffer<uint8_t> m_append_buffer;
PagedBuffer<PieceDescriptor> m_piece_descriptors; PagedBuffer<Piece> m_pieces;
PagedBuffer<ChangeDescriptor> m_change_descriptors; PagedBuffer<ChangeDescriptor> m_change_descriptors;
}; };

View File

@ -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_shaders.text->set_color(1.0, 1.0, 1.0, 1.0);
m_buffer = buffer; m_buffer = buffer;
m_start_piece = m_buffer->piece_table->start_descriptor->next; m_start_piece = m_buffer->piece_table->start_piece->next;
resize(); resize();
@ -229,8 +229,8 @@ void Window::handle_key(uint32_t scancode, uint32_t mod)
void Window::scroll_down() void Window::scroll_down()
{ {
if ((m_start_piece != 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_descriptor)) (m_start_piece->next != m_buffer->piece_table->end_piece))
{ {
m_start_piece = m_start_piece->next; m_start_piece = m_start_piece->next;
redraw(); redraw();
@ -239,8 +239,8 @@ void Window::scroll_down()
void Window::scroll_up() void Window::scroll_up()
{ {
if ((m_start_piece != 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_descriptor)) (m_start_piece->prev != m_buffer->piece_table->start_piece))
{ {
m_start_piece = m_start_piece->prev; m_start_piece = m_start_piece->prev;
redraw(); redraw();
@ -270,13 +270,13 @@ void Window::redraw()
int line_height = m_font.get_line_height(); int line_height = m_font.get_line_height();
int x = 0; int x = 0;
int y = m_height - line_height; int y = m_height - line_height;
for (PieceTable::PieceDescriptor * pd = m_start_piece; for (PieceTable::Piece * piece = m_start_piece;
pd != m_buffer->piece_table->end_descriptor; piece != m_buffer->piece_table->end_piece;
pd = pd->next) 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); auto g = m_font.get_glyph(c);
m_shaders.text->set_position(x, y); m_shaders.text->set_position(x, y);
g->render(); g->render();
@ -290,7 +290,7 @@ void Window::redraw()
x += advance; x += advance;
} }
} }
if (pd->eol()) if (piece->eol())
{ {
y -= line_height; y -= line_height;
x = 0; x = 0;

View File

@ -35,7 +35,7 @@ protected:
std::shared_ptr<Buffer> m_buffer; std::shared_ptr<Buffer> m_buffer;
PieceTable::PieceDescriptor * m_start_piece; PieceTable::Piece * m_start_piece;
}; };
#endif #endif