Simplify SavepointBuffer implementation

This commit is contained in:
Kovid Goyal 2016-11-20 14:03:28 +05:30
parent 91a68f0089
commit 46ec0d4e9c
3 changed files with 7 additions and 21 deletions

View File

@ -235,8 +235,7 @@ typedef struct {
typedef struct {
Savepoint buf[SAVEPOINTS_SZ];
Savepoint *start_of_data;
Savepoint *end_of_data;
index_type start_of_data, count;
} SavepointBuffer;
#define PARSER_BUF_SZ 8192
@ -296,7 +295,6 @@ uint16_t* translation_table(char);
uint32_t decode_utf8(uint32_t*, uint32_t*, uint8_t byte);
Savepoint* savepoints_pop(SavepointBuffer *);
Savepoint* savepoints_push(SavepointBuffer *);
void savepoints_init(SavepointBuffer *);
void cursor_reset(Cursor*);
Cursor* cursor_copy(Cursor*);
void cursor_copy_to(Cursor *src, Cursor *dest);

View File

@ -7,26 +7,15 @@
#include "data-types.h"
#define ADVANCE(x) \
self->x = (self->x >= self->buf + SAVEPOINTS_SZ - 1) ? self->buf : self->x + 1;
#define RETREAT(x) \
self->x = self->x == self->buf ? self->buf + SAVEPOINTS_SZ - 1 : self->x - 1;
Savepoint* savepoints_push(SavepointBuffer *self) {
Savepoint *ans = self->end_of_data;
ADVANCE(end_of_data);
if (self->end_of_data == self->start_of_data) ADVANCE(start_of_data);
Savepoint *ans = self->buf + ((self->start_of_data + self->count) % SAVEPOINTS_SZ);
if (self->count == SAVEPOINTS_SZ) self->start_of_data = (self->start_of_data + 1) % SAVEPOINTS_SZ;
else self->count++;
return ans;
}
Savepoint* savepoints_pop(SavepointBuffer *self) {
if (self->start_of_data == self->end_of_data) return NULL;
RETREAT(end_of_data);
return self->end_of_data;
}
void savepoints_init(SavepointBuffer *self) {
self->end_of_data = self->buf;
self->start_of_data = self->buf;
if (self->count == 0) return NULL;
self->count--;
return self->buf + ((self->start_of_data + self->count) % SAVEPOINTS_SZ);
}

View File

@ -39,7 +39,6 @@ new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) {
if (self->cursor == NULL || self->main_linebuf == NULL || self->alt_linebuf == NULL || self->change_tracker == NULL || self->tabstops == NULL) {
Py_CLEAR(self); return NULL;
}
savepoints_init(&self->main_savepoints); savepoints_init(&self->alt_savepoints);
}
return (PyObject*) self;
}