Skip to content

Commit

Permalink
bump version, resize buffer on next larger power of 2
Browse files Browse the repository at this point in the history
using cntlz
  • Loading branch information
ikozyris committed Oct 28, 2024
1 parent 4e18543 commit a5d2f41
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
10 changes: 5 additions & 5 deletions headers/gapbuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions headers/headers.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include <ncurses.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <locale.h> // for setting locale
#include <sys/stat.h> // for file size
#include <list>
#include <deque>
#include <time.h>
#include <bit>
#include "funcdecl.h"
2 changes: 1 addition & 1 deletion utils/init.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "sizes.c"

#define name "Yocto 0.8-beta4"
#define name "yocto 0.8-rc1"

void init_curses()
{
Expand Down
11 changes: 4 additions & 7 deletions utils/key_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit a5d2f41

Please sign in to comment.