From 97ff413f01f2e37685041095019866d5509f670e Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sun, 22 Jan 2023 23:33:13 +0100 Subject: [PATCH] Maintenance: Separate big macros into several smaller ones --- m-array.h | 19 ++++++++++++++----- m-deque.h | 35 +++++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/m-array.h b/m-array.h index 5aeec026..de2803e6 100644 --- a/m-array.h +++ b/m-array.h @@ -60,9 +60,9 @@ #define M_ARRAY_INIT_VALUE() \ { { 0, 0, NULL } } -/********************************************************************************/ -/********************************** INTERNAL ************************************/ -/********************************************************************************/ +/*****************************************************************************/ +/********************************** INTERNAL *********************************/ +/*****************************************************************************/ /* Deferred evaluation for the oplist definition, so that all arguments are evaluated before further expansion */ @@ -135,6 +135,7 @@ ,M_IF_METHOD(DEL, oplist)(DEL(M_GET_DEL oplist),) \ ) + /* Define the internal contract of an array */ #define M_ARRA4_CONTRACT(a) do { \ M_ASSERT (a != NULL); \ @@ -143,6 +144,7 @@ M_ASSERT (a->alloc == 0 || a->ptr != NULL); \ } while (0) + /* Deferred evaluation for the array definition, so that all arguments are fully evaluated before further expansion (ensuring good performance) @@ -167,6 +169,12 @@ - it_t: alias for the iterator of the array */ #define M_ARRA4_DEF_P3(name, type, oplist, array_t, it_t) \ + M_ARRA4_DEF_TYPE(name, type, oplist, array_t, it_t) \ + M_CHECK_COMPATIBLE_OPLIST(name, 1, type, oplist) \ + M_ARRA4_DEF_CORE(name, type, oplist, array_t, it_t) + +/* Define the types */ +#define M_ARRA4_DEF_TYPE(name, type, oplist, array_t, it_t) \ \ /* Define a dynamic array */ \ typedef struct M_C(name, _s) { \ @@ -187,8 +195,9 @@ typedef array_t M_C(name, _ct); \ typedef it_t M_C(name, _it_ct); \ typedef type M_C(name, _subtype_ct); \ - \ - M_CHECK_COMPATIBLE_OPLIST(name, 1, type, oplist) \ + +/* Define the core functions */ +#define M_ARRA4_DEF_CORE(name, type, oplist, array_t, it_t) \ \ static inline void \ M_C(name, _init)(array_t v) \ diff --git a/m-deque.h b/m-deque.h index 8961bf90..978bf77e 100644 --- a/m-deque.h +++ b/m-deque.h @@ -56,7 +56,10 @@ #define M_USE_DEQUE_DEFAULT_SIZE 8 #endif -/********************************** INTERNAL ************************************/ + +/*****************************************************************************/ +/********************************* INTERNAL **********************************/ +/*****************************************************************************/ /* Define the internal contract of a deque */ #define M_D3QU3_CONTRACT(d) do { \ @@ -93,16 +96,22 @@ - node_t: alias for node_t [ node ] */ #define M_D3QU3_DEF_P3(name, type, oplist, deque_t, it_t, node_t) \ + M_D3QU3_DEF_TYPE(name, type, oplist, deque_t, it_t, node_t) \ + M_CHECK_COMPATIBLE_OPLIST(name, 1, type, oplist) \ + M_D3QU3_DEF_CORE(name, type, oplist, deque_t, it_t, node_t) + +/* Define the types. + It is a linked list of buckets of the types, + each new created bucket size grows compared to the previous one + resulting in: + strict O(1) for push/pop + O(ln(n)) for random access. + No insert / delete operations are planned. + [Could be done in O(n) complexity if needed] + Define the bucket (aka node) structure. +*/ +#define M_D3QU3_DEF_TYPE(name, type, oplist, deque_t, it_t, node_t) \ \ - /* It is a linked list of buckets of the types, \ - each new created bucket size grows compared to the previous one \ - resulting in: \ - strict O(1) for push/pop \ - O(ln(n)) for random access. \ - No insert / delete operations are planned. \ - [Could be done in O(n) complexity if needed] \ - Define the bucket (aka node) structure. \ - */ \ typedef struct M_C(name, _node_s) { \ ILIST_INTERFACE(M_C(name, _node_list), struct M_C(name, _node_s)); \ size_t size; \ @@ -115,6 +124,7 @@ so we register as a DEL operator the FREE operator of the oplist. \ The interfaces are compatible. \ */ \ + /* FIXME: How can I separate public types and private implementation? */ \ ILIST_DEF(M_C(name, _node_list), node_t, (DEL(M_GET_FREE oplist)) ) \ \ /* Define an internal iterator */ \ @@ -153,8 +163,9 @@ typedef deque_t M_C(name, _ct); \ typedef type M_C(name, _subtype_ct); \ typedef it_t M_C(name, _it_ct); \ - \ - M_CHECK_COMPATIBLE_OPLIST(name, 1, type, oplist) \ + +/* Define the core functions */ +#define M_D3QU3_DEF_CORE(name, type, oplist, deque_t, it_t, node_t) \ \ /* Allocate a new node for a deque */ \ static inline node_t* \