revert accidental 3rd library reformat
This commit is contained in:
@@ -33,11 +33,11 @@
|
||||
#define SC_ARRAY_H
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SC_ARRAY_VERSION "2.0.0"
|
||||
|
||||
@@ -53,31 +53,31 @@
|
||||
#endif
|
||||
|
||||
#define sc_array_def(T, name) \
|
||||
struct sc_array_##name { \
|
||||
bool oom; \
|
||||
size_t cap; \
|
||||
size_t size; \
|
||||
/* NOLINTNEXTLINE */ \
|
||||
T *elems; \
|
||||
}
|
||||
struct sc_array_##name { \
|
||||
bool oom; \
|
||||
size_t cap; \
|
||||
size_t size; \
|
||||
/* NOLINTNEXTLINE */ \
|
||||
T *elems; \
|
||||
}
|
||||
/**
|
||||
* Init array
|
||||
* @param a array
|
||||
*/
|
||||
#define sc_array_init(a) \
|
||||
do { \
|
||||
memset((a), 0, sizeof(*(a))); \
|
||||
} while (0)
|
||||
do { \
|
||||
memset((a), 0, sizeof(*(a))); \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* Destroy array
|
||||
* @param a array
|
||||
*/
|
||||
#define sc_array_term(a) \
|
||||
do { \
|
||||
sc_array_free((a)->elems); \
|
||||
sc_array_init(a); \
|
||||
} while (0)
|
||||
do { \
|
||||
sc_array_free((a)->elems); \
|
||||
sc_array_init(a); \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* Add elem to array, call sc_array_oom(v) to see if 'add' failed because of out
|
||||
@@ -87,38 +87,39 @@
|
||||
* @param k elem
|
||||
*/
|
||||
#define sc_array_add(a, k) \
|
||||
do { \
|
||||
const size_t _max = SC_ARRAY_MAX / sizeof(*(a)->elems); \
|
||||
size_t _cap; \
|
||||
void *_p; \
|
||||
do { \
|
||||
const size_t _max = SC_ARRAY_MAX / sizeof(*(a)->elems); \
|
||||
size_t _cap; \
|
||||
void *_p; \
|
||||
\
|
||||
if ((a)->cap == (a)->size) { \
|
||||
if ((a)->cap > _max / 2) { \
|
||||
(a)->oom = true; \
|
||||
break; \
|
||||
} \
|
||||
_cap = (a)->cap == 0 ? 8 : (a)->cap * 2; \
|
||||
_p = sc_array_realloc((a)->elems, _cap * sizeof(*((a)->elems))); \
|
||||
if (_p == NULL) { \
|
||||
(a)->oom = true; \
|
||||
break; \
|
||||
} \
|
||||
(a)->cap = _cap; \
|
||||
(a)->elems = _p; \
|
||||
} \
|
||||
(a)->oom = false; \
|
||||
(a)->elems[(a)->size++] = k; \
|
||||
} while (0)
|
||||
if ((a)->cap == (a)->size) { \
|
||||
if ((a)->cap > _max / 2) { \
|
||||
(a)->oom = true; \
|
||||
break; \
|
||||
} \
|
||||
_cap = (a)->cap == 0 ? 8 : (a)->cap * 2; \
|
||||
_p = sc_array_realloc((a)->elems, \
|
||||
_cap * sizeof(*((a)->elems))); \
|
||||
if (_p == NULL) { \
|
||||
(a)->oom = true; \
|
||||
break; \
|
||||
} \
|
||||
(a)->cap = _cap; \
|
||||
(a)->elems = _p; \
|
||||
} \
|
||||
(a)->oom = false; \
|
||||
(a)->elems[(a)->size++] = k; \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* Deletes items from the array without deallocating underlying memory
|
||||
* @param a array
|
||||
*/
|
||||
#define sc_array_clear(a) \
|
||||
do { \
|
||||
(a)->size = 0; \
|
||||
(a)->oom = false; \
|
||||
} while (0)
|
||||
do { \
|
||||
(a)->size = 0; \
|
||||
(a)->oom = false; \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* @param a array
|
||||
@@ -146,17 +147,17 @@
|
||||
* @param i element index, If 'i' is out of the range, result is undefined.
|
||||
*/
|
||||
#define sc_array_del(a, i) \
|
||||
do { \
|
||||
size_t idx = (i); \
|
||||
assert(idx < (a)->size); \
|
||||
do { \
|
||||
size_t idx = (i); \
|
||||
assert(idx < (a)->size); \
|
||||
\
|
||||
const size_t _cnt = (a)->size - (idx)-1; \
|
||||
if (_cnt > 0) { \
|
||||
memmove(&((a)->elems[idx]), &((a)->elems[idx + 1]), \
|
||||
_cnt * sizeof(*((a)->elems))); \
|
||||
} \
|
||||
(a)->size--; \
|
||||
} while (0)
|
||||
const size_t _cnt = (a)->size - (idx) - 1; \
|
||||
if (_cnt > 0) { \
|
||||
memmove(&((a)->elems[idx]), &((a)->elems[idx + 1]), \
|
||||
_cnt * sizeof(*((a)->elems))); \
|
||||
} \
|
||||
(a)->size--; \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* Deletes the element at index i, replaces last element with the deleted
|
||||
@@ -169,21 +170,21 @@
|
||||
* @param i index. If 'i' is out of the range, result is undefined.
|
||||
*/
|
||||
#define sc_array_del_unordered(a, i) \
|
||||
do { \
|
||||
size_t idx = (i); \
|
||||
assert(idx < (a)->size); \
|
||||
(a)->elems[idx] = (a)->elems[(--(a)->size)]; \
|
||||
} while (0)
|
||||
do { \
|
||||
size_t idx = (i); \
|
||||
assert(idx < (a)->size); \
|
||||
(a)->elems[idx] = (a)->elems[(--(a)->size)]; \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* Deletes the last element. If current size is zero, result is undefined.
|
||||
* @param a array
|
||||
*/
|
||||
#define sc_array_del_last(a) \
|
||||
do { \
|
||||
assert((a)->size != 0); \
|
||||
(a)->size--; \
|
||||
} while (0)
|
||||
do { \
|
||||
assert((a)->size != 0); \
|
||||
(a)->size--; \
|
||||
} while (0)
|
||||
|
||||
/**
|
||||
* Sorts the array using qsort()
|
||||
@@ -191,7 +192,7 @@
|
||||
* @param cmp comparator, check qsort() documentation for details
|
||||
*/
|
||||
#define sc_array_sort(a, cmp) \
|
||||
(qsort((a)->elems, (a)->size, sizeof(*(a)->elems), cmp))
|
||||
(qsort((a)->elems, (a)->size, sizeof(*(a)->elems), cmp))
|
||||
|
||||
/**
|
||||
* Returns last element. If array is empty, result is undefined.
|
||||
@@ -204,8 +205,8 @@
|
||||
* @param elem elem
|
||||
*/
|
||||
#define sc_array_foreach(a, elem) \
|
||||
for (size_t _k = 1, _i = 0; _k && _i != (a)->size; _k = !_k, _i++) \
|
||||
for ((elem) = (a)->elems[_i]; _k; _k = !_k)
|
||||
for (size_t _k = 1, _i = 0; _k && _i != (a)->size; _k = !_k, _i++) \
|
||||
for ((elem) = (a)->elems[_i]; _k; _k = !_k)
|
||||
|
||||
// (type, name)
|
||||
sc_array_def(int, int);
|
||||
|
||||
Reference in New Issue
Block a user