Add vector_resize

This commit is contained in:
Nikita Ivanov 2022-06-07 00:17:38 +05:00
parent 55cb74435b
commit 0ae355868d
No known key found for this signature in database
GPG Key ID: 6E656AC5B97B5133
2 changed files with 36 additions and 16 deletions

View File

@ -37,25 +37,31 @@ void vector_free(Vector *vec)
free(vec); free(vec);
} }
void vector_append_arr(Vector *vec, void *arr, size_t len) static void resize_if_needed(Vector *vec, size_t new_len)
{ {
void *p; void *p;
size_t cap = vec->cap; size_t cap = vec->cap;
while (cap < vec->len + len) while (new_len > cap)
cap *= 2; cap *= 2;
if (cap != vec->cap) { if (cap == vec->cap)
if (!(p = realloc(vec->buf, vec->size * cap))) { return;
vector_free(vec);
PRINTINTERR(FUNCFAILED("realloc"), ERRNOS);
abort();
}
vec->buf = p; if (!(p = realloc(vec->buf, vec->size * cap))) {
vec->cap = cap; vector_free(vec);
PRINTINTERR(FUNCFAILED("realloc"), ERRNOS);
abort();
} }
vec->buf = p;
vec->cap = cap;
}
void vector_append_arr(Vector *vec, void *arr, size_t len)
{
resize_if_needed(vec, vec->len + len);
memcpy(vec->buf + vec->len * vec->size, arr, len * vec->size); memcpy(vec->buf + vec->len * vec->size, arr, len * vec->size);
vec->len += len; vec->len += len;
} }
@ -69,3 +75,10 @@ void *vector_get(Vector *vec, size_t i)
{ {
return vec->buf + i * vec->size; return vec->buf + i * vec->size;
} }
void vector_resize(Vector *vec, size_t len)
{
resize_if_needed(vec, len);
vec->len = len;
}

View File

@ -14,13 +14,14 @@
#define VECTOR_SIGN_V(name, type, func, ret, ...) \ #define VECTOR_SIGN_V(name, type, func, ret, ...) \
VECTOR_SIGN(name, type, func, ret, \ VECTOR_SIGN(name, type, func, ret, \
Vector##name *v __VA_OPT__(, ) __VA_ARGS__) Vector##name *vec __VA_OPT__(, ) __VA_ARGS__)
#define VECTOR_SIGN_NEW(name, type) VECTOR_SIGN(name, type, new, Vector##name *, size_t cap) #define VECTOR_SIGN_NEW(name, type) VECTOR_SIGN(name, type, new, Vector##name *, size_t cap)
#define VECTOR_SIGN_FREE(name, type) VECTOR_SIGN_V(name, type, free, void) #define VECTOR_SIGN_FREE(name, type) VECTOR_SIGN_V(name, type, free, void)
#define VECTOR_SIGN_APPEND_ARR(name, type) VECTOR_SIGN_V(name, type, append_arr, void, type *arr, size_t len) #define VECTOR_SIGN_APPEND_ARR(name, type) VECTOR_SIGN_V(name, type, append_arr, void, type *arr, size_t len)
#define VECTOR_SIGN_APPEND(name, type) VECTOR_SIGN_V(name, type, append, void, type val) #define VECTOR_SIGN_APPEND(name, type) VECTOR_SIGN_V(name, type, append, void, type val)
#define VECTOR_SIGN_GET(name, type) VECTOR_SIGN_V(name, type, get, type *, size_t i) #define VECTOR_SIGN_GET(name, type) VECTOR_SIGN_V(name, type, get, type *, size_t i)
#define VECTOR_SIGN_RESIZE(name, type) VECTOR_SIGN_V(name, type, resize, void, size_t len)
#define VECTOR_GEN_SOURCE_(name, type, spec) \ #define VECTOR_GEN_SOURCE_(name, type, spec) \
inline spec VECTOR_SIGN_NEW(name, type) \ inline spec VECTOR_SIGN_NEW(name, type) \
@ -29,19 +30,23 @@
} \ } \
inline spec VECTOR_SIGN_FREE(name, type) \ inline spec VECTOR_SIGN_FREE(name, type) \
{ \ { \
vector_free((Vector *)v); \ vector_free((Vector *)vec); \
} \ } \
inline spec VECTOR_SIGN_APPEND_ARR(name, type) \ inline spec VECTOR_SIGN_APPEND_ARR(name, type) \
{ \ { \
vector_append_arr((Vector *)v, arr, len); \ vector_append_arr((Vector *)vec, arr, len); \
} \ } \
inline spec VECTOR_SIGN_APPEND(name, type) \ inline spec VECTOR_SIGN_APPEND(name, type) \
{ \ { \
vector_append((Vector *)v, &val); \ vector_append((Vector *)vec, &val); \
} \ } \
inline spec VECTOR_SIGN_GET(name, type) \ inline spec VECTOR_SIGN_GET(name, type) \
{ \ { \
return (type *)vector_get((Vector *)v, i); \ return (type *)vector_get((Vector *)vec, i); \
} \
inline spec VECTOR_SIGN_RESIZE(name, type) \
{ \
vector_resize((Vector *)vec, len); \
} }
#define VECTOR_GEN_SOURCE(name, type) VECTOR_GEN_SOURCE_(name, type, ) #define VECTOR_GEN_SOURCE(name, type) VECTOR_GEN_SOURCE_(name, type, )
@ -55,7 +60,8 @@
VECTOR_SIGN_FREE(name, type); \ VECTOR_SIGN_FREE(name, type); \
VECTOR_SIGN_APPEND_ARR(name, type); \ VECTOR_SIGN_APPEND_ARR(name, type); \
VECTOR_SIGN_APPEND(name, type); \ VECTOR_SIGN_APPEND(name, type); \
VECTOR_SIGN_GET(name, type); VECTOR_SIGN_GET(name, type); \
VECTOR_SIGN_RESIZE(name, type);
VECTOR_TYPE(, void); VECTOR_TYPE(, void);
@ -64,6 +70,7 @@ void vector_free(Vector *vec);
void vector_append_arr(Vector *vec, void *arr, size_t len); void vector_append_arr(Vector *vec, void *arr, size_t len);
void vector_append(Vector *vec, void *arr); void vector_append(Vector *vec, void *arr);
void *vector_get(Vector *vec, size_t i); void *vector_get(Vector *vec, size_t i);
void vector_resize(Vector *vec, size_t len);
VECTOR_GEN_HEADER(Char, char) VECTOR_GEN_HEADER(Char, char)