Add I/O for deque
This commit is contained in:
1 parent
cfb16e06cc
commit
d08e2bba95
2 files changed
+71
-7
No files matched your search
@@ -644,10 +644,72 @@
|
|||||||
DEQUEI_CONTRACT(d); \
|
DEQUEI_CONTRACT(d); \
|
||||||
} \
|
} \
|
||||||
, /* NO SWAP: TODO */) \
|
, /* NO SWAP: TODO */) \
|
||||||
|
\
|
||||||
|
M_IF_METHOD(GET_STR, oplist)( \
|
||||||
|
static inline void \
|
||||||
|
M_C(name, _get_str)(string_t str, deque_t deque, bool append) \
|
||||||
|
{ \
|
||||||
|
STRING_CONTRACT(str); \
|
||||||
|
DEQUEI_CONTRACT(deque); \
|
||||||
|
(append ? string_cat_str : string_set_str) (str, "["); \
|
||||||
|
it_t it; \
|
||||||
|
for (M_C(name, _it)(it, deque) ; \
|
||||||
|
!M_C(name, _end_p)(it); \
|
||||||
|
M_C(name, _next)(it)){ \
|
||||||
|
const type *item = M_C(name, _cref)(it); \
|
||||||
|
M_GET_GET_STR oplist (str, *item, true); \
|
||||||
|
if (!M_C(name, _last_p)(it)) \
|
||||||
|
string_push_back (str, M_GET_SEPARATOR oplist); \
|
||||||
|
} \
|
||||||
|
string_push_back (str, ']'); \
|
||||||
|
STRING_CONTRACT(str); \
|
||||||
|
} \
|
||||||
|
, /* no GET_STR */ ) \
|
||||||
|
\
|
||||||
|
M_IF_METHOD(OUT_STR, oplist)( \
|
||||||
|
static inline void \
|
||||||
|
M_C(name, _out_str)(FILE *file, const deque_t deque) \
|
||||||
|
{ \
|
||||||
|
DEQUEI_CONTRACT(deque); \
|
||||||
|
assert (file != NULL); \
|
||||||
|
fputc ('[', file); \
|
||||||
|
for (size_t i = 0; i < deque->size; i++) { \
|
||||||
|
const type *item = M_C(name, _cget)(deque, i); \
|
||||||
|
M_GET_OUT_STR oplist (file, *item); \
|
||||||
|
if (i != deque->size-1) \
|
||||||
|
fputc (M_GET_SEPARATOR oplist, file); \
|
||||||
|
} \
|
||||||
|
fputc (']', file); \
|
||||||
|
} \
|
||||||
|
, /* no OUT_STR */ ) \
|
||||||
|
\
|
||||||
|
M_IF_METHOD(IN_STR, oplist)( \
|
||||||
|
static inline bool \
|
||||||
|
M_C(name, _in_str)(deque_t deque, FILE *file) \
|
||||||
|
{ \
|
||||||
|
DEQUEI_CONTRACT(deque); \
|
||||||
|
assert (file != NULL); \
|
||||||
|
M_C(name,_clean)(deque); \
|
||||||
|
int c = fgetc(file); \
|
||||||
|
if (c != '[') return false; \
|
||||||
|
c = fgetc(file); \
|
||||||
|
if (c == ']') return true; \
|
||||||
|
ungetc(c, file); \
|
||||||
|
type item; \
|
||||||
|
M_GET_INIT oplist (item); \
|
||||||
|
do { \
|
||||||
|
bool b = M_GET_IN_STR oplist (item, file); \
|
||||||
|
c = fgetc(file); \
|
||||||
|
if (b == false || c == EOF) { break; } \
|
||||||
|
M_C(name, _push_back)(deque, item); \
|
||||||
|
} while (c == M_GET_SEPARATOR oplist); \
|
||||||
|
M_GET_CLEAR oplist (item); \
|
||||||
|
DEQUEI_CONTRACT(deque); \
|
||||||
|
return c == ']'; \
|
||||||
|
} \
|
||||||
|
, /* no IN_STR */ ) \
|
||||||
|
|
||||||
|
|
||||||
// TODO: IO [like array]
|
|
||||||
|
|
||||||
#define DEQUEI_OPLIST(name, oplist) \
|
#define DEQUEI_OPLIST(name, oplist) \
|
||||||
(INIT(M_C(name, _init)) \
|
(INIT(M_C(name, _init)) \
|
||||||
,INIT_SET(M_C(name, _init_set)) \
|
,INIT_SET(M_C(name, _init_set)) \
|
||||||
|
|||||||
+7
-5
@@ -66,8 +66,8 @@
|
|||||||
#define SHAREDI_PTR_DEF2(name, type, oplist) \
|
#define SHAREDI_PTR_DEF2(name, type, oplist) \
|
||||||
\
|
\
|
||||||
typedef struct M_C(name, _s){ \
|
typedef struct M_C(name, _s){ \
|
||||||
type *data; /* Pointer to the data */ \
|
type *data; /* Pointer to the data */ \
|
||||||
atomic_int cpt; /* Counter of how many points to the data */ \
|
atomic_int cpt; /* Counter of how many points to the data */ \
|
||||||
bool combineAlloc; /* Does the data and the ptr share the slot? */ \
|
bool combineAlloc; /* Does the data and the ptr share the slot? */ \
|
||||||
} *M_C(name, _t)[1]; \
|
} *M_C(name, _t)[1]; \
|
||||||
\
|
\
|
||||||
@@ -85,13 +85,15 @@
|
|||||||
static inline void \
|
static inline void \
|
||||||
M_C(name, _init2)(M_C(name, _t) shared, type *data) \
|
M_C(name, _init2)(M_C(name, _t) shared, type *data) \
|
||||||
{ \
|
{ \
|
||||||
|
assert (shared != NULL); \
|
||||||
|
/* The shared ptr get exclusive access to data */ \
|
||||||
struct M_C(name, _s) *ptr; \
|
struct M_C(name, _s) *ptr; \
|
||||||
if (data == NULL) { \
|
if (M_UNLIKELY (data == NULL)) { \
|
||||||
*shared = NULL; \
|
*shared = NULL; \
|
||||||
return; \
|
return; \
|
||||||
} \
|
} \
|
||||||
ptr = M_GET_NEW oplist (struct M_C(name, _s)); \
|
ptr = M_GET_NEW oplist (struct M_C(name, _s)); \
|
||||||
if (ptr == NULL) { \
|
if (M_UNLIKELY (ptr == NULL)) { \
|
||||||
M_MEMORY_FULL(sizeof(struct M_C(name, _s))); \
|
M_MEMORY_FULL(sizeof(struct M_C(name, _s))); \
|
||||||
return; \
|
return; \
|
||||||
} \
|
} \
|
||||||
@@ -108,7 +110,7 @@
|
|||||||
/* NOTE: Alloc 1 struct with both structures. */ \
|
/* NOTE: Alloc 1 struct with both structures. */ \
|
||||||
struct M_C(name, combine_s) *p = \
|
struct M_C(name, combine_s) *p = \
|
||||||
M_GET_NEW oplist (struct M_C(name, combine_s)); \
|
M_GET_NEW oplist (struct M_C(name, combine_s)); \
|
||||||
if (p == NULL) { \
|
if (M_UNLIKELY (p == NULL)) { \
|
||||||
M_MEMORY_FULL(sizeof(struct M_C(name, combine_s))); \
|
M_MEMORY_FULL(sizeof(struct M_C(name, combine_s))); \
|
||||||
return; \
|
return; \
|
||||||
} \
|
} \
|
||||||
|
|||||||
Reference in new issue
Block a user