Changes.
This commit is contained in:
parent
29fe592108
commit
d2bc0e161d
|
@ -2,7 +2,8 @@
|
||||||
`name` is an identifier prefix that satisfies `C` naming conventions when
|
`name` is an identifier prefix that satisfies `C` naming conventions when
|
||||||
mangled and `type` is defined tag-type associated therewith. When expanding
|
mangled and `type` is defined tag-type associated therewith. When expanding
|
||||||
the array, resizing may be necessary and incurs amortised cost; any pointers
|
the array, resizing may be necessary and incurs amortised cost; any pointers
|
||||||
to this memory may become stale. @param{MIN_ARRAY_CAPACITY} Defaults to 3. */
|
to this memory may become stale.
|
||||||
|
@param[MIN_ARRAY_CAPACITY] Must be greater than 1; defaults to 3. */
|
||||||
|
|
||||||
#include <stdlib.h> /* size_t realloc free */
|
#include <stdlib.h> /* size_t realloc free */
|
||||||
#include <errno.h> /* errno */
|
#include <errno.h> /* errno */
|
||||||
|
@ -10,7 +11,7 @@
|
||||||
|
|
||||||
#define MIN_ARRAY_IDLE { 0, 0, 0 }
|
#define MIN_ARRAY_IDLE { 0, 0, 0 }
|
||||||
#ifndef MIN_ARRAY_CAPACITY
|
#ifndef MIN_ARRAY_CAPACITY
|
||||||
#define MIN_ARRAY_CAPACITY 3
|
#define MIN_ARRAY_CAPACITY 3 /* > 1 */
|
||||||
#endif
|
#endif
|
||||||
#define MIN_ARRAY(name, type) \
|
#define MIN_ARRAY(name, type) \
|
||||||
struct name##_array { type *data; size_t size, capacity; }; \
|
struct name##_array { type *data; size_t size, capacity; }; \
|
||||||
|
@ -20,10 +21,9 @@ static void name##_array(struct name##_array *const a) \
|
||||||
/** Destroys `a` and returns it to idle. */ \
|
/** Destroys `a` and returns it to idle. */ \
|
||||||
static void name##_array_(struct name##_array *const a) \
|
static void name##_array_(struct name##_array *const a) \
|
||||||
{ assert(a); free(a->data); name##_array(a); } \
|
{ assert(a); free(a->data); name##_array(a); } \
|
||||||
/** Ensures `min_capacity` of `a`. @param[min_capacity] If zero, does nothing.
|
/** Ensures `min_capacity` of `a`. @return Success; otherwise, `errno` will be
|
||||||
@return Success; otherwise, `errno` will be set.
|
set. @throws[ERANGE] Tried allocating more then can fit in an object or
|
||||||
@throws[ERANGE] Tried allocating more then can fit in an object or doesn't
|
doesn't follow POSIX. @throws[realloc] */ \
|
||||||
follow POSIX. @throws[realloc] */ \
|
|
||||||
static int name##_array_reserve(struct name##_array *const a, \
|
static int name##_array_reserve(struct name##_array *const a, \
|
||||||
const size_t min) { \
|
const size_t min) { \
|
||||||
size_t c0; \
|
size_t c0; \
|
||||||
|
@ -53,9 +53,9 @@ static int name##_array_reserve(struct name##_array *const a, \
|
||||||
return 1; \
|
return 1; \
|
||||||
} \
|
} \
|
||||||
/** The capacity of `a` will be increased to at least `n` elements beyond the
|
/** The capacity of `a` will be increased to at least `n` elements beyond the
|
||||||
size. Invalidates any pointers in `a`.
|
size. It will invalidate pointers in `a` if `n` is greater than the buffer
|
||||||
@return The start of the buffered space at the back of the array. If `a` is
|
space. @return The start of the buffered space at the back of the array. If
|
||||||
idle and `buffer` is zero, a null pointer is returned, otherwise null
|
`a` is idle and `buffer` is zero, a null pointer is returned, otherwise null
|
||||||
indicates an error. @throws[realloc, ERANGE] */ \
|
indicates an error. @throws[realloc, ERANGE] */ \
|
||||||
static type *name##_array_buffer(struct name##_array *const a, \
|
static type *name##_array_buffer(struct name##_array *const a, \
|
||||||
const size_t n) { \
|
const size_t n) { \
|
||||||
|
@ -65,10 +65,9 @@ static type *name##_array_buffer(struct name##_array *const a, \
|
||||||
? a->data + a->size : 0; \
|
? a->data + a->size : 0; \
|
||||||
} \
|
} \
|
||||||
/** Adds `n` elements to the back of `a`. It will invalidate pointers in `a` if
|
/** Adds `n` elements to the back of `a`. It will invalidate pointers in `a` if
|
||||||
`n` is greater than the buffer space.
|
`n` is greater than the buffer space. @return A pointer to the elements. If
|
||||||
@return A pointer to the elements. If `a` is idle and `n` is zero, a null
|
`a` is idle and `n` is zero, a null pointer will be returned, otherwise null
|
||||||
pointer will be returned, otherwise null indicates an error.
|
indicates an error. @throws[realloc, ERANGE] */ \
|
||||||
@throws[realloc, ERANGE] */ \
|
|
||||||
static type *name##_array_append(struct name##_array *const a, \
|
static type *name##_array_append(struct name##_array *const a, \
|
||||||
const size_t n) { \
|
const size_t n) { \
|
||||||
type *buffer; \
|
type *buffer; \
|
||||||
|
|
Loading…
Reference in New Issue