From a5d2f41e8667c33ceef7a7c7c9fe5aad1cd96557 Mon Sep 17 00:00:00 2001 From: ikozyris <80053394+ikozyris@users.noreply.github.com> Date: Mon, 28 Oct 2024 15:28:58 +0200 Subject: [PATCH] bump version, resize buffer on next larger power of 2 using cntlz --- headers/gapbuffer.hpp | 10 +++++----- headers/headers.h | 3 +-- utils/init.c | 2 +- utils/key_func.cpp | 11 ++++------- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/headers/gapbuffer.hpp b/headers/gapbuffer.hpp index 292ace6..5c73d74 100644 --- a/headers/gapbuffer.hpp +++ b/headers/gapbuffer.hpp @@ -62,7 +62,7 @@ void resize(gap_buf &a, unsigned size) void mv_curs(gap_buf &a, unsigned pos) { if (a.gps == a.gpe) [[unlikely]] - resize(a, a.cpt * 2); + resize(a, std::__bit_ceil(a.cpt)); if (pos > a.gps) // move gap to right memmove(a.buffer + a.gps, a.buffer + a.gpe + 1, pos - a.gps); else if (pos < a.gps) // move gap to left @@ -77,7 +77,7 @@ void mv_curs(gap_buf &a, unsigned pos) void insert_c(gap_buf &a, unsigned pos, char ch) { if (a.len == a.cpt) [[unlikely]] - resize(a, a.cpt * 2); + resize(a, std::__bit_ceil(a.cpt)); if (ingap(a, pos)) { [[likely]] a[pos] = ch; ++a.gps; @@ -92,7 +92,7 @@ void insert_s(gap_buf &a, unsigned pos, const char *str, unsigned len) { // is this uneeded? (later it is also checked indirectly) if (a.len + len >= a.cpt) [[unlikely]] - resize(a, (a.cpt + len) * 2); + resize(a, std::__bit_ceil(a.len + len + 2)); if (gaplen(a) <= len) mv_curs(a, pos); memcpy(a.buffer + pos, str, len); @@ -103,7 +103,7 @@ void insert_s(gap_buf &a, unsigned pos, const char *str, unsigned len) void apnd_c(gap_buf &a, char ch) { if (a.len >= a.cpt) [[unlikely]] - resize(a, a.cpt * 2); + resize(a, std::__bit_ceil(a.cpt)); a[a.len] = ch; ++a.len; ++a.gps; @@ -112,7 +112,7 @@ void apnd_c(gap_buf &a, char ch) void apnd_s(gap_buf &a, const char *str, unsigned size) { if (a.len + size >= a.cpt) [[unlikely]] - resize(a, (a.cpt + size) * 2); + resize(a, std::__bit_ceil(a.len + size + 2)); memcpy(a.buffer + a.len, str, size); a.len += size; a.gps = a.len; diff --git a/headers/headers.h b/headers/headers.h index cabe0ce..ac4c84b 100644 --- a/headers/headers.h +++ b/headers/headers.h @@ -1,10 +1,9 @@ #include #include #include -#include #include // for setting locale #include // for file size #include #include -#include +#include #include "funcdecl.h" diff --git a/utils/init.c b/utils/init.c index 1ac3285..f2acaba 100644 --- a/utils/init.c +++ b/utils/init.c @@ -1,6 +1,6 @@ #include "sizes.c" -#define name "Yocto 0.8-beta4" +#define name "yocto 0.8-rc1" void init_curses() { diff --git a/utils/key_func.cpp b/utils/key_func.cpp index d589c26..c87b170 100755 --- a/utils/key_func.cpp +++ b/utils/key_func.cpp @@ -7,11 +7,11 @@ void stats() for (auto &i : text) sumlen += i.len; #ifndef RELEASE - snprintf(_tmp, min(maxx, 256), "maxx %u len %u ofx %ld wrap %u x: %u | y: %u ", - maxx, it->len, ofx, !wrap.empty() ? wrap.back() : 0, x, y); + snprintf(_tmp, min(maxx, 256), "maxx %u len %u cpt %u ofx %ld wrap %u x: %u y: %u ", + maxx, it->len, it->cpt, ofx, !wrap.empty() ? wrap.back() : 0, x, y); #else - snprintf(_tmp, min(maxx, 256), "length %u y %u x %u sum len %u lines %lu ofx %ld ", - it->len, ry, x, sumlen, curnum, ofx); + snprintf(_tmp, min(maxx, 256), "length %u cpt %u y %u x %u sum len %u lines %lu wrap %lu ", + it->len, it->cpt, ry, x, sumlen, curnum, wrap.size()); #endif print2header(_tmp, 1); free(_tmp); @@ -107,7 +107,6 @@ void command() void enter() { insert_c(*it, rx, '\n'); - gap_buf *t = (gap_buf*)malloc(sizeof(gap_buf)); init(*t); if (it->gpe < it->cpt - 2) { // newline is not at the end @@ -117,8 +116,6 @@ void enter() it->len = rx + 1; it->gpe = it->cpt - 1; } - - // somewhere below iterator is invalidated ++it; ++curnum; text.insert(it, *t);