2019-05-16 22:02:03 -05:00
|
|
|
// 16 may 2019
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "ui.h"
|
|
|
|
#include "uipriv.h"
|
|
|
|
|
2019-05-30 22:11:08 -05:00
|
|
|
#define sharedbitsPrefix uipriv
|
|
|
|
#include "../sharedbits/alloc_impl.h"
|
|
|
|
#undef sharedbitsPrefix
|
2019-05-19 11:52:06 -05:00
|
|
|
|
|
|
|
void *uiprivArrayAppend(uiprivArray *arr, size_t n)
|
|
|
|
{
|
|
|
|
return uiprivArrayInsertAt(arr, arr->len, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *uiprivArrayInsertAt(uiprivArray *arr, size_t pos, size_t n)
|
|
|
|
{
|
|
|
|
uint8_t *old, *new;
|
2019-05-19 13:37:32 -05:00
|
|
|
size_t nBytesAdded, nBytesRemaining;
|
2019-05-19 11:52:06 -05:00
|
|
|
|
|
|
|
if ((arr->len + n) >= arr->cap) {
|
|
|
|
size_t nGrow;
|
|
|
|
|
|
|
|
// always grow by a perfect multiple of arr->nGrow
|
|
|
|
nGrow = n + (arr->nGrow - (n % arr->nGrow));
|
|
|
|
arr->buf = uiprivRealloc(arr->buf,
|
|
|
|
arr->cap * arr->elemsize,
|
|
|
|
(arr->cap + nGrow) * arr->elemsize,
|
|
|
|
arr->what);
|
|
|
|
arr->cap += nGrow;
|
|
|
|
}
|
|
|
|
|
2019-05-19 13:37:32 -05:00
|
|
|
nBytesRemaining = (arr->len - pos) * arr->elemsize;
|
|
|
|
nBytesAdded = n * arr->elemsize;
|
2019-05-19 11:52:06 -05:00
|
|
|
new = ((uint8_t *) (arr->buf)) + (pos * arr->elemsize);
|
2019-05-19 13:37:32 -05:00
|
|
|
old = new + nBytesAdded;
|
|
|
|
memmove(old, new, nBytesRemaining);
|
|
|
|
memset(new, 0, nBytesAdded);
|
2019-05-20 10:12:16 -05:00
|
|
|
|
|
|
|
arr->len += n;
|
2019-05-19 11:52:06 -05:00
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiprivArrayDelete(uiprivArray *arr, size_t pos, size_t n)
|
|
|
|
{
|
|
|
|
uint8_t *src, *dest;
|
|
|
|
size_t nBytesDeleted, nBytesRemaining;
|
|
|
|
|
|
|
|
nBytesDeleted = n * arr->elemsize;
|
|
|
|
nBytesRemaining = (arr->len - pos - n) * arr->elemsize;
|
|
|
|
dest = ((uint8_t *) (arr->buf)) + (pos * arr->elemsize);
|
|
|
|
src = dest + nBytesDeleted;
|
2019-05-19 13:37:32 -05:00
|
|
|
memmove(dest, src, nBytesRemaining);
|
|
|
|
src = dest + nBytesRemaining;
|
|
|
|
memset(src, 0, nBytesDeleted);
|
2019-05-19 11:52:06 -05:00
|
|
|
arr->len -= n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiprivArrayDeleteItem(uiprivArray *arr, void *p, size_t n)
|
|
|
|
{
|
|
|
|
uint8_t *p8, *buf8;
|
|
|
|
|
|
|
|
p8 = (uint8_t *) p;
|
|
|
|
buf8 = (uint8_t *) (arr->buf);
|
|
|
|
// TODO write this in a way that doesn't mix ptrdiff_t and size_t
|
|
|
|
uiprivArrayDelete(arr, (p8 - buf8) / arr->elemsize, n);
|
|
|
|
}
|
|
|
|
|
2019-05-19 13:37:32 -05:00
|
|
|
void *uiprivArrayBsearch(const uiprivArray *arr, const void *key, int (*compare)(const void *, const void *))
|
2019-05-19 11:52:06 -05:00
|
|
|
{
|
|
|
|
return bsearch(key, arr->buf, arr->len, arr->elemsize, compare);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiprivArrayQsort(uiprivArray *arr, int (*compare)(const void *, const void *))
|
|
|
|
{
|
|
|
|
qsort(arr->buf, arr->len, arr->elemsize, compare);
|
|
|
|
}
|