2015-05-05 21:53:50 -05:00
|
|
|
// 5 may 2015
|
|
|
|
#include <string.h>
|
|
|
|
#include "ui.h"
|
|
|
|
#include "uipriv.h"
|
|
|
|
|
2015-05-06 12:37:23 -05:00
|
|
|
struct ptrArray *newPtrArray(void)
|
2015-05-05 21:53:50 -05:00
|
|
|
{
|
|
|
|
return uiNew(struct ptrArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ptrArrayDestroy(struct ptrArray *p)
|
|
|
|
{
|
2015-05-06 09:14:37 -05:00
|
|
|
if (p->len != 0)
|
|
|
|
complain("attempt to destroy ptrarray %p while it still has pointers inside", p);
|
2015-05-05 21:53:50 -05:00
|
|
|
uiFree(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define grow 32
|
|
|
|
|
|
|
|
void ptrArrayAppend(struct ptrArray *p, void *d)
|
|
|
|
{
|
|
|
|
if (p->len >= p->cap) {
|
|
|
|
p->cap += grow;
|
|
|
|
p->ptrs = (void **) uiRealloc(p->ptrs, p->cap * sizeof (void *));
|
|
|
|
}
|
2015-05-06 12:11:39 -05:00
|
|
|
p->ptrs[p->len] = d;
|
|
|
|
p->len++;
|
2015-05-05 21:53:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void ptrArrayInsertBefore(struct ptrArray *p, uintmax_t i, void *d)
|
|
|
|
{
|
|
|
|
if (i >= p->len)
|
|
|
|
complain("index out of range in ptrArrayInsertBefore()");
|
|
|
|
if (p->len >= p->cap) {
|
|
|
|
p->cap += grow;
|
|
|
|
p->ptrs = (void **) uiRealloc(p->ptrs, p->cap * sizeof (void *));
|
|
|
|
}
|
2015-05-06 12:11:39 -05:00
|
|
|
// thanks to ValleyBell
|
|
|
|
memmove(&(p->ptrs[i + 1]), &(p->ptrs[i]), (p->len - i) * sizeof (void *));
|
2015-05-05 21:53:50 -05:00
|
|
|
p->ptrs[i] = d;
|
2015-05-06 12:11:39 -05:00
|
|
|
p->len++;
|
2015-05-05 21:53:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void ptrArrayDelete(struct ptrArray *p, uintmax_t i)
|
|
|
|
{
|
|
|
|
if (i >= p->len)
|
|
|
|
complain("index out of range in ptrArrayRemove()");
|
2015-05-06 12:11:39 -05:00
|
|
|
// thanks to ValleyBell
|
|
|
|
memmove(&(p->ptrs[i]), &(p->ptrs[i + 1]), (p->len - i - 1) * sizeof (void *));
|
|
|
|
p->ptrs[p->len - 1] = NULL;
|
2015-05-05 21:53:50 -05:00
|
|
|
p->len--;
|
|
|
|
}
|