From 0c07fd231d6de047cc0037b0de37c691561e34fa Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Mon, 3 Dec 2012 02:25:05 +0100 Subject: organize buffer ring as a tail-q (double-linked list with tail pointer) * new buffers are added at the list tail * when closing a buffer, the next one is selected or the previous one if it is the tail * the ring may be traversed in reverse order * undoing a buffer close (Ring::UndoTokenEdit) could be cleaned up to only use standard macros (is slightly less efficient though) --- qbuffers.h | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'qbuffers.h') diff --git a/qbuffers.h b/qbuffers.h index 0fd588d..05ec388 100644 --- a/qbuffers.h +++ b/qbuffers.h @@ -263,17 +263,11 @@ class Buffer { UndoTokenClose(Buffer *_buffer) : UndoToken(), buffer(_buffer) {} - void - run(void) - { - buffer->close(); - /* NOTE: the buffer is NOT deleted on Token destruction */ - delete buffer; - } + void run(void); }; public: - LIST_ENTRY(Buffer) buffers; + TAILQ_ENTRY(Buffer) buffers; gchar *filename; gint dot; @@ -300,7 +294,14 @@ public: inline Buffer *& next(void) { - return LIST_NEXT(this, buffers); + return TAILQ_NEXT(this, buffers); + } + inline Buffer *& + prev(void) + { + TAILQ_HEAD(Head, Buffer); + + return TAILQ_PREV(this, Head, buffers); } inline void @@ -329,7 +330,6 @@ public: bool load(const gchar *filename); - void close(void); inline void undo_close(void) { @@ -376,21 +376,26 @@ extern class Ring { } }; - LIST_HEAD(Head, Buffer) head; + TAILQ_HEAD(Head, Buffer) head; public: Buffer *current; Ring() : current(NULL) { - LIST_INIT(&head); + TAILQ_INIT(&head); } ~Ring(); inline Buffer * first(void) { - return LIST_FIRST(&head); + return TAILQ_FIRST(&head); + } + inline Buffer * + last(void) + { + return TAILQ_LAST(&head, Head); } Buffer *find(const gchar *filename); @@ -411,6 +416,7 @@ public: bool save(const gchar *filename); + void close(Buffer *buffer); void close(void); inline void undo_close(void) -- cgit v1.2.3