17 static void *get_element_offset(
const typevec_t *vec,
size_t index)
21 return ((
char*)vec->data) + (index * vec->
width);
24 static void copy_elements(
const typevec_t *vec,
void *dst,
const void *src,
size_t count)
31 static void typevec_ensure(
typevec_t *vec,
size_t extra)
79 return typevec_create(width, len, arena);
85 typevec_t *
self = typevec_create(width, len, arena);
95 typevec_t *
self = typevec_create(width, count, arena);
98 copy_elements(
self, self->data, src, count);
107 CTASSERTF(start <= end, "start %zu > end %zu
", start, end);
108 CTASSERTF(end <= typevec_len(vec), "end %zu out of bounds %zu
", end, typevec_len(vec));
110 size_t len = end - start;
111 typevec_t *self = typevec_create(vec->width, len, vec->arena);
114 copy_elements(self, self->data, typevec_offset(vec, start), len);
120 size_t typevec_len(const typevec_t *vec)
122 CTASSERT(vec != NULL);
128 void typevec_set(typevec_t *vec, size_t index, const void *src)
130 CTASSERT(vec != NULL);
131 CTASSERT(src != NULL);
133 void *dst = typevec_offset(vec, index);
134 copy_elements(vec, dst, src, 1);
138 void typevec_get(const typevec_t *vec, size_t index, void *dst)
140 CTASSERT(vec != NULL);
141 CTASSERT(dst != NULL);
143 void *src = typevec_offset(vec, index);
144 copy_elements(vec, dst, src, 1);
148 void typevec_tail(const typevec_t *vec, void *dst)
150 CTASSERT(typevec_len(vec) > 0);
152 typevec_get(vec, vec->used - 1, dst);
156 void *typevec_push(typevec_t *vec, const void *src)
158 CTASSERT(vec != NULL);
160 typevec_ensure(vec, 1);
162 void *dst = get_element_offset(vec, vec->used++);
163 copy_elements(vec, dst, src, 1);
169 void typevec_append(typevec_t *vec, const void *src, size_t len)
171 CTASSERT(vec != NULL);
172 CTASSERT(src != NULL);
174 typevec_ensure(vec, len);
176 void *dst = get_element_offset(vec, vec->used);
177 ctu_memcpy(dst, src, vec->width * len);
182 void typevec_pop(typevec_t *vec, void *dst)
184 CTASSERT(typevec_len(vec) > 0);
186 void *src = typevec_offset(vec, --vec->used);
187 copy_elements(vec, dst, src, 1);
191 void *typevec_offset(const typevec_t *vec, size_t index)
193 CTASSERTF(index < typevec_len(vec), "index %zu out of bounds %zu
", index, typevec_len(vec));
195 return ((char*)vec->data) + (index * vec->width);
199 void *typevec_data(const typevec_t *vec)
201 CTASSERT(vec != NULL);
206 void typevec_sort(IN_NOTNULL typevec_t *vec, int (*cmp)(const void *, const void *))
208 CTASSERT(vec != NULL);
209 CTASSERT(cmp != NULL);
211 // TODO: we cant do this, some platforms dont have qsort
213 qsort(vec->data, vec->used, vec->width, cmp);
217 void typevec_reset(typevec_t *vec)
219 CTASSERT(vec != NULL);
#define STA_DECL
sal2 annotation on function implementations to copy annotations from the declaration
CT_NOALIAS CT_BASE_API void * ctu_memcpy(STA_WRITES(size) void *CT_RESTRICT dst, STA_READS(size) const void *CT_RESTRICT src, size_t size)
copy memory from one location to another equivalent to memcpy but with safety checks
CT_NODISCARD CT_ARENA_API void * arena_realloc(STA_RELEASE void *ptr, size_t new_size, size_t old_size, arena_t *arena)
resize a memory allocation from a custom allocator
#define ARENA_MALLOC(size, name, parent, arena)
allocate memory from a custom allocator
#define CTASSERT(expr)
assert a condition, prints the condition as a message
#define CTASSERTF(expr,...)
assert a condition with a message and optional format arguments
STA_DECL typevec_t * typevec_of(size_t width, size_t len, arena_t *arena)
create a new typed vector with an initial size and length
STA_DECL typevec_t typevec_make(size_t width, size_t len, arena_t *arena)
create a new typed vector on the stack
STA_DECL void typevec_init(typevec_t *vec, size_t width, size_t len, arena_t *arena)
initialize a typed vector
const typevec_t kEmptyTypevec
STA_DECL typevec_t * typevec_slice(const typevec_t *vec, size_t start, size_t end)
create a new typevec from an existing typevec
STA_DECL typevec_t * typevec_new(size_t width, size_t len, arena_t *arena)
create a new typed vector on the heap
A vector with a fixed type size.
size_t used
The number of elements used.
size_t width
The size of each element.
size_t size
The number of elements allocated.
STA_DECL typevec_t * typevec_of_array(size_t width, const void *src, size_t count, arena_t *arena)