And wrote array_impl.h. No more code duplication in this respect! Now for some more loose ends before we move on to controls.
This commit is contained in:
parent
dca2e5f038
commit
b5a109c063
|
@ -6,72 +6,5 @@
|
|||
|
||||
#define sharedbitsPrefix uipriv
|
||||
#include "../sharedbits/alloc_impl.h"
|
||||
#include "../sharedbits/array_impl.h"
|
||||
#undef sharedbitsPrefix
|
||||
|
||||
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;
|
||||
size_t nBytesAdded, nBytesRemaining;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
nBytesRemaining = (arr->len - pos) * arr->elemsize;
|
||||
nBytesAdded = n * arr->elemsize;
|
||||
new = ((uint8_t *) (arr->buf)) + (pos * arr->elemsize);
|
||||
old = new + nBytesAdded;
|
||||
memmove(old, new, nBytesRemaining);
|
||||
memset(new, 0, nBytesAdded);
|
||||
|
||||
arr->len += n;
|
||||
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;
|
||||
memmove(dest, src, nBytesRemaining);
|
||||
src = dest + nBytesRemaining;
|
||||
memset(src, 0, nBytesDeleted);
|
||||
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);
|
||||
}
|
||||
|
||||
void *uiprivArrayBsearch(const uiprivArray *arr, const void *key, int (*compare)(const void *, const void *))
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -17,4 +17,72 @@ void sharedbitsPrefixName(ArrayFreeFull)(sharedbitsPrefixName(Array) *arr)
|
|||
memset(arr, 0, sizeof (sharedbitsPrefixName(Array)));
|
||||
}
|
||||
|
||||
void *sharedbitsPrefixName(ArrayAppend)(sharedbitsPrefixName(Array) *arr, size_t n)
|
||||
{
|
||||
return sharedbitsPrefixName(ArrayInsertAt)(arr, arr->len, n);
|
||||
}
|
||||
|
||||
void *sharedbitsPrefixName(ArrayInsertAt)(sharedbitsPrefixName(Array) *arr, size_t pos, size_t n)
|
||||
{
|
||||
uint8_t *old, *new;
|
||||
size_t nBytesAdded, nBytesRemaining;
|
||||
|
||||
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 = sharedbitsPrefixName(Realloc)(arr->buf,
|
||||
arr->cap * arr->elemsize,
|
||||
(arr->cap + nGrow) * arr->elemsize,
|
||||
arr->what);
|
||||
arr->cap += nGrow;
|
||||
}
|
||||
|
||||
nBytesRemaining = (arr->len - pos) * arr->elemsize;
|
||||
nBytesAdded = n * arr->elemsize;
|
||||
new = ((uint8_t *) (arr->buf)) + (pos * arr->elemsize);
|
||||
old = new + nBytesAdded;
|
||||
memmove(old, new, nBytesRemaining);
|
||||
memset(new, 0, nBytesAdded);
|
||||
|
||||
arr->len += n;
|
||||
return new;
|
||||
}
|
||||
|
||||
void sharedbitsPrefixName(ArrayDelete)(sharedbitsPrefixName(Array) *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;
|
||||
memmove(dest, src, nBytesRemaining);
|
||||
src = dest + nBytesRemaining;
|
||||
memset(src, 0, nBytesDeleted);
|
||||
arr->len -= n;
|
||||
}
|
||||
|
||||
void sharedbitsPrefixName(ArrayDeleteItem)(sharedbitsPrefixName(Array) *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
|
||||
sharedbitsPrefixName(ArrayDelete)(arr, (p8 - buf8) / arr->elemsize, n);
|
||||
}
|
||||
|
||||
void *sharedbitsPrefixName(ArrayBsearch)(const sharedbitsPrefixName(Array) *arr, const void *key, int (*compare)(const void *, const void *))
|
||||
{
|
||||
return bsearch(key, arr->buf, arr->len, arr->elemsize, compare);
|
||||
}
|
||||
|
||||
void sharedbitsPrefixName(ArrayQsort)(sharedbitsPrefixName(Array) *arr, int (*compare)(const void *, const void *))
|
||||
{
|
||||
qsort(arr->buf, arr->len, arr->elemsize, compare);
|
||||
}
|
||||
|
||||
#include "end.h"
|
||||
|
|
|
@ -23,76 +23,9 @@ void testingprivInternalError(const char *fmt, ...)
|
|||
|
||||
#define sharedbitsPrefix testingpriv
|
||||
#include "../../sharedbits/alloc_impl.h"
|
||||
#include "../../sharedbits/array_impl.h"
|
||||
#undef sharedbitsPrefix
|
||||
|
||||
void *testingprivArrayAppend(testingprivArray *arr, size_t n)
|
||||
{
|
||||
return testingprivArrayInsertAt(arr, arr->len, n);
|
||||
}
|
||||
|
||||
void *testingprivArrayInsertAt(testingprivArray *arr, size_t pos, size_t n)
|
||||
{
|
||||
uint8_t *old, *new;
|
||||
size_t nBytesAdded, nBytesRemaining;
|
||||
|
||||
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 = testingprivRealloc(arr->buf,
|
||||
arr->cap * arr->elemsize,
|
||||
(arr->cap + nGrow) * arr->elemsize,
|
||||
arr->what);
|
||||
arr->cap += nGrow;
|
||||
}
|
||||
|
||||
nBytesRemaining = (arr->len - pos) * arr->elemsize;
|
||||
nBytesAdded = n * arr->elemsize;
|
||||
new = ((uint8_t *) (arr->buf)) + (pos * arr->elemsize);
|
||||
old = new + nBytesAdded;
|
||||
memmove(old, new, nBytesRemaining);
|
||||
memset(new, 0, nBytesAdded);
|
||||
|
||||
arr->len += n;
|
||||
return new;
|
||||
}
|
||||
|
||||
void testingprivArrayDelete(testingprivArray *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;
|
||||
memmove(dest, src, nBytesRemaining);
|
||||
src = dest + nBytesRemaining;
|
||||
memset(src, 0, nBytesDeleted);
|
||||
arr->len -= n;
|
||||
}
|
||||
|
||||
void testingprivArrayDeleteItem(testingprivArray *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
|
||||
testingprivArrayDelete(arr, (p8 - buf8) / arr->elemsize, n);
|
||||
}
|
||||
|
||||
void *testingprivArrayBsearch(const testingprivArray *arr, const void *key, int (*compare)(const void *, const void *))
|
||||
{
|
||||
return bsearch(key, arr->buf, arr->len, arr->elemsize, compare);
|
||||
}
|
||||
|
||||
void testingprivArrayQsort(testingprivArray *arr, int (*compare)(const void *, const void *))
|
||||
{
|
||||
qsort(arr->buf, arr->len, arr->elemsize, compare);
|
||||
}
|
||||
|
||||
int testingprivVsnprintf(char *s, size_t n, const char *format, va_list ap)
|
||||
{
|
||||
int ret;
|
||||
|
|
Loading…
Reference in New Issue