Update PieceDescriptor structure - use direct pointers for list links
This commit is contained in:
parent
ebf92f07ac
commit
8fcbad02f8
@ -56,16 +56,14 @@ bool Buffer::load_from_file(const char * filename)
|
|||||||
m_piece_table = std::make_shared<PieceTable>(m_file_buffer, m_file_buffer_size);
|
m_piece_table = std::make_shared<PieceTable>(m_file_buffer, m_file_buffer_size);
|
||||||
for (auto it = text_loader.begin(); it != text_loader.end(); it++)
|
for (auto it = text_loader.begin(); it != text_loader.end(); it++)
|
||||||
{
|
{
|
||||||
auto pd_n = m_piece_table->add_piece_descriptor();
|
PieceTable::PieceDescriptor * pd = m_piece_table->add_piece_descriptor();
|
||||||
PieceTable::PieceDescriptor & pd = pd_n.second;
|
pd->prev = m_piece_table->end_descriptor->prev;
|
||||||
pd.prev = m_piece_table->end_descriptor->prev;
|
pd->next = m_piece_table->end_descriptor;
|
||||||
pd.next = PIECE_DESCRIPTOR_INDEX_END;
|
pd->start = it->first;
|
||||||
pd.start = it->first - m_file_buffer;
|
pd->length = it->second;
|
||||||
pd.length = it->second;
|
pd->n_chars = it->second | PIECE_DESCRIPTOR_N_CHARS_EOL_FLAG_MASK; /* TODO: fix this for other encodings */
|
||||||
pd.n_chars = it->second; /* TODO: fix this for other encodings */
|
m_piece_table->end_descriptor->prev->next = pd;
|
||||||
pd.eol = 1u;
|
m_piece_table->end_descriptor->prev = pd;
|
||||||
(*m_piece_table)[m_piece_table->end_descriptor->prev].next = pd_n.first;
|
|
||||||
m_piece_table->end_descriptor->prev = pd_n.first;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -6,7 +6,7 @@ PieceTable::PieceTable(const uint8_t * file_buffer, unsigned long file_buffer_si
|
|||||||
m_file_buffer_size = file_buffer_size;
|
m_file_buffer_size = file_buffer_size;
|
||||||
start_descriptor = &m_piece_descriptors[PIECE_DESCRIPTOR_INDEX_START];
|
start_descriptor = &m_piece_descriptors[PIECE_DESCRIPTOR_INDEX_START];
|
||||||
end_descriptor = &m_piece_descriptors[PIECE_DESCRIPTOR_INDEX_END];
|
end_descriptor = &m_piece_descriptors[PIECE_DESCRIPTOR_INDEX_END];
|
||||||
start_descriptor->next = PIECE_DESCRIPTOR_INDEX_END;
|
start_descriptor->next = end_descriptor;
|
||||||
end_descriptor->prev = PIECE_DESCRIPTOR_INDEX_START;
|
end_descriptor->prev = start_descriptor;
|
||||||
m_piece_descriptor_index = 2u;
|
m_piece_descriptor_index = 2u;
|
||||||
}
|
}
|
||||||
|
@ -8,17 +8,39 @@
|
|||||||
#define PIECE_DESCRIPTOR_INDEX_START 0u
|
#define PIECE_DESCRIPTOR_INDEX_START 0u
|
||||||
#define PIECE_DESCRIPTOR_INDEX_END 1u
|
#define PIECE_DESCRIPTOR_INDEX_END 1u
|
||||||
|
|
||||||
|
#define PIECE_DESCRIPTOR_N_CHARS_EOL_FLAG_MASK 0x80000000u
|
||||||
|
|
||||||
class PieceTable
|
class PieceTable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
struct PieceDescriptor
|
struct PieceDescriptor
|
||||||
{
|
{
|
||||||
uint32_t prev;
|
/** The previous piece descriptor in the chain. */
|
||||||
uint32_t next;
|
PieceDescriptor * prev;
|
||||||
uint32_t start;
|
|
||||||
|
/** The next piece descriptor in the chain. */
|
||||||
|
PieceDescriptor * next;
|
||||||
|
|
||||||
|
/** Text that the piece describes. */
|
||||||
|
uint8_t * start;
|
||||||
|
|
||||||
|
/** Length of the text (in bytes). */
|
||||||
uint32_t length;
|
uint32_t length;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Number of characters in the text.
|
||||||
|
* Also, the highest bit is the eol flag.
|
||||||
|
*/
|
||||||
uint32_t n_chars;
|
uint32_t n_chars;
|
||||||
uint32_t eol;
|
|
||||||
|
/** Get the number of characters pointed to by this descriptor. */
|
||||||
|
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. */
|
||||||
|
bool eol() { return (n_chars & PIECE_DESCRIPTOR_N_CHARS_EOL_FLAG_MASK) != 0u; }
|
||||||
|
|
||||||
|
/** Toggle whether this descriptor is the end of a line. */
|
||||||
|
void toggle_eol() { n_chars ^= PIECE_DESCRIPTOR_N_CHARS_EOL_FLAG_MASK; }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ChangeDescriptor
|
struct ChangeDescriptor
|
||||||
@ -31,12 +53,9 @@ public:
|
|||||||
|
|
||||||
PieceTable(const uint8_t * file_buffer, unsigned long file_buffer_size);
|
PieceTable(const uint8_t * file_buffer, unsigned long file_buffer_size);
|
||||||
|
|
||||||
std::pair<uint32_t, PieceDescriptor &> add_piece_descriptor()
|
PieceDescriptor * add_piece_descriptor()
|
||||||
{
|
{
|
||||||
uint32_t index = m_piece_descriptor_index;
|
return &m_piece_descriptors[m_piece_descriptor_index++];
|
||||||
m_piece_descriptor_index++;
|
|
||||||
PieceDescriptor & pd = m_piece_descriptors[index];
|
|
||||||
return std::pair<uint32_t, PieceDescriptor &>(index, pd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PieceDescriptor & operator[](uint32_t index)
|
PieceDescriptor & operator[](uint32_t index)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user