Skip to content

Commit

Permalink
array: delete oneliners
Browse files Browse the repository at this point in the history
same as buffer commit Array is completely visible
  • Loading branch information
rnpnr committed Jan 12, 2025
1 parent 0f31ef5 commit 1b483c2
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 79 deletions.
8 changes: 0 additions & 8 deletions array.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,6 @@ bool array_remove(Array *arr, size_t idx) {
return true;
}

size_t array_length(const Array *arr) {
return arr->len;
}

size_t array_capacity(const Array *arr) {
return arr->count;
}

bool array_truncate(Array *arr, size_t len) {
if (len <= arr->len) {
arr->len = len;
Expand Down
4 changes: 0 additions & 4 deletions array.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ bool array_add_ptr(Array*, void *item);
* @endrst
*/
bool array_remove(Array*, size_t idx);
/** Number of elements currently stored in the array. */
size_t array_length(const Array*);
/** Number of elements which can be stored without enlarging the array. */
size_t array_capacity(const Array*);
/** Remove all elements with index greater or equal to ``length``, keep allocated memory. */
bool array_truncate(Array*, size_t length);
/**
Expand Down
4 changes: 2 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,7 @@ static const char *selections_rotate(Vis *vis, const char *keys, const Arg *arg)
if (next)
line_next = text_lineno_by_pos(txt, view_cursors_pos(next));
if (!next || (columns > 1 && line != line_next)) {
size_t len = array_length(&arr);
size_t len = arr.len;
size_t off = arg->i > 0 ? count % len : len - (count % len);
for (size_t i = 0; i < len; i++) {
size_t j = (i + off) % len;
Expand Down Expand Up @@ -1714,7 +1714,7 @@ static const char *selections_intersect(Vis *vis, const char *keys, const Arg *a

static void complement(Array *ret, Array *a, Filerange *universe) {
size_t pos = universe->start;
for (size_t i = 0, len = array_length(a); i < len; i++) {
for (size_t i = 0, len = a->len; i < len; i++) {
Filerange *r = array_get(a, i);
if (pos < r->start) {
Filerange new = text_range_new(pos, r->start);
Expand Down
64 changes: 32 additions & 32 deletions test/core/array-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ static bool item_compare(Item *a, Item *b) {
static void test_small_objects(void) {
Array arr;
array_init_sized(&arr, sizeof(int));
ok(array_length(&arr) == 0, "Initialization");
ok(arr.len == 0, "Initialization");
ok(!array_set(&arr, 0, NULL) && errno == EINVAL, "Set with invalid index");
ok(array_get(&arr, 0) == NULL && errno == EINVAL, "Get with invalid index");
ok(array_peek(&arr) == NULL && array_length(&arr) == 0, "Peek empty array");
ok(array_pop(&arr) == NULL && array_length(&arr) == 0, "Pop empty array");
ok(array_peek(&arr) == NULL && arr.len == 0, "Peek empty array");
ok(array_pop(&arr) == NULL && arr.len == 0, "Pop empty array");

for (size_t i = 0; i < len; i++) {
int *v;
ok(array_add(&arr, &values[i]) && array_length(&arr) == i+1,
ok(array_add(&arr, &values[i]) && arr.len == i+1,
"Add integer: %zu = %d", i, values[i]);
ok((v = array_get(&arr, i)) && *v == values[i],
"Get integer: %zu = %d", i, *v);
}

for (size_t i = 0; i < len; i++) {
ok(array_set(&arr, i, &values[len-i-1]) && array_length(&arr) == len,
ok(array_set(&arr, i, &values[len-i-1]) && arr.len == len,
"Set array element: %zu = %d", i, values[len-i-1]);
}

Expand All @@ -49,44 +49,44 @@ static void test_small_objects(void) {
}

int *v;
ok((v = array_peek(&arr)) && *v == values[0] && array_length(&arr) == len, "Peek populated array");
ok((v = array_pop(&arr)) && *v == values[0] && array_length(&arr) == len-1, "Pop populated array");
ok((v = array_peek(&arr)) && *v == values[1] && array_length(&arr) == len-1, "Peek after pop");
ok((v = array_peek(&arr)) && *v == values[0] && arr.len == len, "Peek populated array");
ok((v = array_pop(&arr)) && *v == values[0] && arr.len == len-1, "Pop populated array");
ok((v = array_peek(&arr)) && *v == values[1] && arr.len == len-1, "Peek after pop");

array_clear(&arr);
ok(array_length(&arr) == 0 && array_get(&arr, 0) == NULL && errno == EINVAL, "Clear");
ok(arr.len == 0 && array_get(&arr, 0) == NULL && errno == EINVAL, "Clear");

for (size_t i = 0; i < len; i++) {
ok(array_add(&arr, &values[i]) && array_length(&arr) == i+1,
ok(array_add(&arr, &values[i]) && arr.len == i+1,
"Re-add integer: %zu = %d", i, values[i]);
}

int old, *tmp;
ok((tmp = array_get(&arr, 0)) && (old = *tmp) && array_set(&arr, 0, NULL) &&
array_get(&arr, 0) == tmp && *tmp == 0 && array_set(&arr, 0, &old) &&
array_get(&arr, 0) == tmp && *tmp == old, "Set array element NULL");
ok(!array_set(&arr, array_length(&arr), &values[0]) && errno == EINVAL, "Get past end of array");
ok(!array_get(&arr, array_length(&arr)) && errno == EINVAL, "Get past end of array");
ok(!array_set(&arr, arr.len, &values[0]) && errno == EINVAL, "Get past end of array");
ok(!array_get(&arr, arr.len) && errno == EINVAL, "Get past end of array");

ok(!array_remove(&arr, array_length(&arr)) && errno == EINVAL, "Remove past end of array");
ok(!array_remove(&arr, arr.len) && errno == EINVAL, "Remove past end of array");

size_t len_before = array_length(&arr);
ok(array_remove(&arr, 2) && array_length(&arr) == len_before-1 &&
size_t len_before = arr.len;
ok(array_remove(&arr, 2) && arr.len == len_before-1 &&
(v = array_get(&arr, 0)) && *v == values[0] &&
(v = array_get(&arr, 1)) && *v == values[1] &&
(v = array_get(&arr, 2)) && *v == values[3] &&
(v = array_get(&arr, 3)) && *v == values[4],
"Remove element 2");

len_before = array_length(&arr);
ok(array_remove(&arr, 0) && array_length(&arr) == len_before-1 &&
len_before = arr.len;
ok(array_remove(&arr, 0) && arr.len == len_before-1 &&
(v = array_get(&arr, 0)) && *v == values[1] &&
(v = array_get(&arr, 1)) && *v == values[3] &&
(v = array_get(&arr, 2)) && *v == values[4],
"Remove first element");

len_before = array_length(&arr);
ok(array_remove(&arr, len_before-1) && array_length(&arr) == len_before-1 &&
len_before = arr.len;
ok(array_remove(&arr, len_before-1) && arr.len == len_before-1 &&
(v = array_get(&arr, 0)) && *v == values[1] &&
(v = array_get(&arr, 1)) && *v == values[3],
"Remove last element");
Expand All @@ -97,7 +97,7 @@ static void test_small_objects(void) {
static void test_large_objects(void) {
Array arr;
array_init_sized(&arr, sizeof(Item));
ok(array_length(&arr) == 0 && array_get(&arr, 0) == NULL && errno == EINVAL,
ok(arr.len == 0 && array_get(&arr, 0) == NULL && errno == EINVAL,
"Initialization");

Item items[len];
Expand All @@ -106,15 +106,15 @@ static void test_large_objects(void) {
snprintf(items[i].key, sizeof items[i].key, "key: %zu", i);
items[i].value = values[i];
Item *item;
ok(array_add(&arr, &items[i]) && array_length(&arr) == i+1,
ok(array_add(&arr, &items[i]) && arr.len == i+1,
"Add item: %zu = { '%s' = %d }", i, items[i].key, items[i].value);
ok((item = array_get(&arr, i)) && item != &items[i] && item_compare(item, &items[i]),
"Get item: %zu = { '%s' = %d }", i, item->key, item->value);
}

for (size_t i = 0; i < len; i++) {
Item *item = &items[len-i-1];
ok(array_set(&arr, i, item) && array_length(&arr) == len,
ok(array_set(&arr, i, item) && arr.len == len,
"Set array element: %zu = { '%s' = %d }", i, item->key, item->value);
}

Expand All @@ -124,13 +124,13 @@ static void test_large_objects(void) {
"Get item: %zu = { '%s' = %d }", i, item->key, item->value);
}

ok(!array_add_ptr(&arr, &items[0]) && errno == ENOTSUP && array_length(&arr) == len,
ok(!array_add_ptr(&arr, &items[0]) && errno == ENOTSUP && arr.len == len,
"Adding pointer to non pointer array");
ok(!array_set_ptr(&arr, 0, &items[0]) && errno == ENOTSUP && item_compare(array_get(&arr, 0), &items[len-1]),
"Setting pointer in non pointer array");

array_clear(&arr);
ok(array_length(&arr) == 0 && array_get(&arr, 0) == NULL && errno == EINVAL, "Clear");
ok(arr.len == 0 && array_get(&arr, 0) == NULL && errno == EINVAL, "Clear");

array_release(&arr);
}
Expand All @@ -140,7 +140,7 @@ static void test_pointers(void) {
Array arr;

array_init_sized(&arr, 1);
ok(array_length(&arr) == 0 && array_get_ptr(&arr, 0) == NULL && errno == ENOTSUP,
ok(arr.len == 0 && array_get_ptr(&arr, 0) == NULL && errno == ENOTSUP,
"Initialization with size 1");

ok(!array_add_ptr(&arr, &arr) && errno == ENOTSUP && array_get_ptr(&arr, 0) == NULL,
Expand All @@ -154,7 +154,7 @@ static void test_pointers(void) {
array_release(&arr);

array_init(&arr);
ok(array_length(&arr) == 0 && array_get_ptr(&arr, 0) == NULL && errno == EINVAL,
ok(arr.len == 0 && array_get_ptr(&arr, 0) == NULL && errno == EINVAL,
"Initialization");

Item *items[len];
Expand All @@ -167,15 +167,15 @@ static void test_pointers(void) {

for (size_t i = 0; i < len; i++) {
Item *item;
ok(array_add_ptr(&arr, items[i]) && array_length(&arr) == i+1,
ok(array_add_ptr(&arr, items[i]) && arr.len == i+1,
"Add item: %zu = %p", i, (void*)items[i]);
ok((item = array_get_ptr(&arr, i)) && item == items[i],
"Get item: %zu = %p", i, (void*)item);
}

for (size_t i = 0; i < len; i++) {
Item *item = items[len-i-1];
ok(array_set_ptr(&arr, i, item) && array_length(&arr) == len,
ok(array_set_ptr(&arr, i, item) && arr.len == len,
"Set item: %zu = %p", i, (void*)item);
}

Expand All @@ -189,14 +189,14 @@ static void test_pointers(void) {
ok((tmp = array_get_ptr(&arr, 0)) && array_set_ptr(&arr, 0, NULL) &&
array_get_ptr(&arr, 0) == NULL && array_set_ptr(&arr, 0, tmp) &&
array_get_ptr(&arr, 0) == tmp, "Set pointer NULL");
ok(!array_set_ptr(&arr, array_length(&arr), items[0]) && errno == EINVAL, "Set pointer past end of array");
ok(!array_get_ptr(&arr, array_length(&arr)) && errno == EINVAL, "Get pointer past end of array");
ok(!array_set_ptr(&arr, arr.len, items[0]) && errno == EINVAL, "Set pointer past end of array");
ok(!array_get_ptr(&arr, arr.len) && errno == EINVAL, "Get pointer past end of array");

array_clear(&arr);
ok(array_length(&arr) == 0 && array_get_ptr(&arr, 0) == NULL && errno == EINVAL, "Clear");
ok(arr.len == 0 && array_get_ptr(&arr, 0) == NULL && errno == EINVAL, "Clear");

for (size_t i = 0; i < len; i++) {
ok(array_add_ptr(&arr, items[i]) && array_length(&arr) == i+1,
ok(array_add_ptr(&arr, items[i]) && arr.len == i+1,
"Re-add item: %zu = %p", i, (void*)items[i]);
}
array_release_full(&arr);
Expand Down
14 changes: 7 additions & 7 deletions text.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ static size_t lines_count(Text *txt, size_t pos, size_t len);
/* stores the given data in a block, allocates a new one if necessary. Returns
* a pointer to the storage location or NULL if allocation failed. */
static const char *block_store(Text *txt, const char *data, size_t len) {
Block *blk = array_get_ptr(&txt->blocks, array_length(&txt->blocks)-1);
Block *blk = array_get_ptr(&txt->blocks, txt->blocks.len - 1);
if (!blk || !block_capacity(blk, len)) {
blk = block_alloc(len);
if (!blk)
Expand All @@ -141,15 +141,15 @@ static const char *block_store(Text *txt, const char *data, size_t len) {

/* cache the given piece if it is the most recently changed one */
static void cache_piece(Text *txt, Piece *p) {
Block *blk = array_get_ptr(&txt->blocks, array_length(&txt->blocks)-1);
Block *blk = array_get_ptr(&txt->blocks, txt->blocks.len - 1);
if (!blk || p->data < blk->data || p->data + p->len != blk->data + blk->len)
return;
txt->cache = p;
}

/* check whether the given piece was the most recently modified one */
static bool cache_contains(Text *txt, Piece *p) {
Block *blk = array_get_ptr(&txt->blocks, array_length(&txt->blocks)-1);
Block *blk = array_get_ptr(&txt->blocks, txt->blocks.len - 1);
Revision *rev = txt->current_revision;
if (!blk || !txt->cache || txt->cache != p || !rev || !rev->change)
return false;
Expand All @@ -173,7 +173,7 @@ static bool cache_contains(Text *txt, Piece *p) {
static bool cache_insert(Text *txt, Piece *p, size_t off, const char *data, size_t len) {
if (!cache_contains(txt, p))
return false;
Block *blk = array_get_ptr(&txt->blocks, array_length(&txt->blocks)-1);
Block *blk = array_get_ptr(&txt->blocks, txt->blocks.len - 1);
size_t bufpos = p->data + off - blk->data;
if (!block_insert(blk, bufpos, data, len))
return false;
Expand All @@ -190,7 +190,7 @@ static bool cache_insert(Text *txt, Piece *p, size_t off, const char *data, size
static bool cache_delete(Text *txt, Piece *p, size_t off, size_t len) {
if (!cache_contains(txt, p))
return false;
Block *blk = array_get_ptr(&txt->blocks, array_length(&txt->blocks)-1);
Block *blk = array_get_ptr(&txt->blocks, txt->blocks.len - 1);
size_t end;
size_t bufpos = p->data + off - blk->data;
if (!addu(off, len, &end) || end > p->len || !block_delete(blk, bufpos, len))
Expand Down Expand Up @@ -776,7 +776,7 @@ void text_free(Text *txt) {
piece_free(p);
}

for (size_t i = 0, len = array_length(&txt->blocks); i < len; i++)
for (size_t i = 0, len = txt->blocks.len; i < len; i++)
block_free(array_get_ptr(&txt->blocks, i));
array_release(&txt->blocks);

Expand All @@ -789,7 +789,7 @@ bool text_modified(const Text *txt) {

bool text_mmaped(const Text *txt, const char *ptr) {
uintptr_t addr = (uintptr_t)ptr;
for (size_t i = 0, len = array_length(&txt->blocks); i < len; i++) {
for (size_t i = 0, len = txt->blocks.len; i < len; i++) {
Block *blk = array_get_ptr(&txt->blocks, i);
if ((blk->type == BLOCK_TYPE_MMAP_ORIG || blk->type == BLOCK_TYPE_MMAP) &&
(uintptr_t)(blk->data) <= addr && addr < (uintptr_t)(blk->data + blk->size))
Expand Down
4 changes: 2 additions & 2 deletions vis-lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -1697,7 +1697,7 @@ static int registers_index(lua_State *L) {
if (reg >= VIS_REG_INVALID)
return 1;
Array data = vis_register_get(vis, reg);
for (size_t i = 0, len = array_length(&data); i < len; i++) {
for (size_t i = 0, len = data.len; i < len; i++) {
TextString *string = array_get(&data, i);
lua_pushunsigned(L, i+1);
lua_pushlstring(L, string->data, string->len);
Expand Down Expand Up @@ -2860,7 +2860,7 @@ static int window_marks_index(lua_State *L) {
return 1;

Array arr = vis_mark_get(win, mark);
for (size_t i = 0, len = array_length(&arr); i < len; i++) {
for (size_t i = 0, len = arr.len; i < len; i++) {
Filerange *range = array_get(&arr, i);
lua_pushunsigned(L, i+1);
pushrange(L, range);
Expand Down
22 changes: 11 additions & 11 deletions vis-marks.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ void vis_mark_normalize(Array *a) {
}

bool vis_mark_equal(Array *a, Array *b) {
size_t len = array_length(a);
if (len != array_length(b))
if (a->len != b->len)
return false;
size_t len = a->len;
for (size_t i = 0; i < len; i++) {
if (!text_range_equal(array_get(a, i), array_get(b, i)))
return false;
Expand Down Expand Up @@ -72,7 +72,7 @@ static Array mark_get(Win *win, Array *mark) {
array_init_sized(&sel, sizeof(Filerange));
if (!mark)
return sel;
size_t len = array_length(mark);
size_t len = mark->len;
array_reserve(&sel, len);
for (size_t i = 0; i < len; i++) {
SelectionRegion *sr = array_get(mark, i);
Expand All @@ -92,7 +92,7 @@ static void mark_set(Win *win, Array *mark, Array *sel) {
if (!mark)
return;
array_clear(mark);
for (size_t i = 0, len = array_length(sel); i < len; i++) {
for (size_t i = 0, len = sel->len; i < len; i++) {
SelectionRegion ss;
Filerange *r = array_get(sel, i);
if (view_regions_save(&win->view, r, &ss))
Expand All @@ -115,10 +115,10 @@ void marklist_init(MarkList *list, size_t max) {
}

void marklist_release(MarkList *list) {
for (size_t i = 0, len = array_length(&list->prev); i < len; i++)
for (size_t i = 0, len = list->prev.len; i < len; i++)
array_release(array_get(&list->prev, i));
array_release(&list->prev);
for (size_t i = 0, len = array_length(&list->next); i < len; i++)
for (size_t i = 0, len = list->next.len; i < len; i++)
array_release(array_get(&list->next, i));
array_release(&list->next);
}
Expand All @@ -133,12 +133,12 @@ static bool marklist_push(Win *win, MarkList *list, Array *sel) {
return true;
}

for (size_t i = 0, len = array_length(&list->next); i < len; i++)
for (size_t i = 0, len = list->next.len; i < len; i++)
array_release(array_get(&list->next, i));
array_clear(&list->next);
Array arr;
mark_init(&arr);
if (array_length(&list->prev) >= array_capacity(&list->prev)) {
if (list->prev.len >= list->prev.count) {
Array *tmp = array_get(&list->prev, 0);
arr = *tmp;
array_remove(&list->prev, 0);
Expand Down Expand Up @@ -170,12 +170,12 @@ static bool marklist_prev(Win *win, MarkList *list) {
if (restore)
goto out;

while (array_length(&list->prev) > 1) {
while (list->prev.len > 1) {
Array *prev = array_pop(&list->prev);
array_push(&list->next, prev);
prev = array_peek(&list->prev);
Array sel = mark_get(win, prev);
restore = array_length(&sel) > 0;
restore = sel.len > 0;
if (restore)
view_selections_set_all(view, &sel, anchored);
array_release(&sel);
Expand All @@ -195,7 +195,7 @@ static bool marklist_next(Win *win, MarkList *list) {
if (!next)
return false;
Array sel = mark_get(win, next);
if (array_length(&sel) > 0) {
if (sel.len > 0) {
view_selections_set_all(view, &sel, anchored);
array_release(&sel);
array_push(&list->prev, next);
Expand Down
Loading

0 comments on commit 1b483c2

Please sign in to comment.