Simplify example code

This commit is contained in:
Patrick Pelissier committed 2019-08-11 10:43:35 +02:00
1 parent c4ab76a330
commit d31ccfe8a4
1 file changed
+45 -33
+45 -33
View File
@@ -3,7 +3,9 @@
#include "m-string.h" #include "m-string.h"
#include "m-tuple.h" #include "m-tuple.h"
/* Provide several examples of basic types usage with array. /* Provide several examples of different types usage with array,
with the oplist to use for each case.
Each time it creates and initializes an array, Each time it creates and initializes an array,
fills it with some number, fills it with some number,
computes its sum, computes its sum,
@@ -13,10 +15,12 @@
- integers, - integers,
- floats, - floats,
- pointers, - pointers,
- structures. - structures,
- tuples
*/ */
/* Generate prototypes and inline functions /* Generate prototypes and inline functions
for basic types. for basic types.
For integer and floats, there is no need to specify For integer and floats, there is no need to specify
@@ -45,6 +49,8 @@ test_bool(int n)
return s; return s;
} }
/* Same with type char */ /* Same with type char */
ARRAY_DEF(r_char, char) ARRAY_DEF(r_char, char)
@@ -68,6 +74,8 @@ test_char(int n)
return s; return s;
} }
/* Same with type signed int. */ /* Same with type signed int. */
ARRAY_DEF(r_int, signed int) ARRAY_DEF(r_int, signed int)
@@ -91,6 +99,8 @@ test_int(int n)
return s; return s;
} }
/* Same with type signed int. /* Same with type signed int.
But with an explicit oplist. But with an explicit oplist.
This generates exactly the same code than r_int This generates exactly the same code than r_int
@@ -117,6 +127,8 @@ test_int2(int n)
return s; return s;
} }
/* Same with type unsigned long long */ /* Same with type unsigned long long */
ARRAY_DEF(r_ullong, unsigned long long) ARRAY_DEF(r_ullong, unsigned long long)
@@ -140,6 +152,8 @@ test_ullong(int n)
return s; return s;
} }
/* Same with type float */ /* Same with type float */
ARRAY_DEF(r_float, float) ARRAY_DEF(r_float, float)
@@ -163,6 +177,8 @@ test_float(int n)
return s; return s;
} }
/* Same with type double and an explicit oplist*/ /* Same with type double and an explicit oplist*/
ARRAY_DEF(r_double, double, M_DEFAULT_OPLIST) ARRAY_DEF(r_double, double, M_DEFAULT_OPLIST)
@@ -186,6 +202,9 @@ test_double(int n)
return s; return s;
} }
/* Helper function strdup */
static char *my_strdup(const char *p) static char *my_strdup(const char *p)
{ {
size_t s = strlen(p); size_t s = strlen(p);
@@ -243,6 +262,8 @@ test_cstring(int n)
return s; return s;
} }
/* Same with type string_t of M*LIB, representing a variable string. /* Same with type string_t of M*LIB, representing a variable string.
We don't need explicit oplist to tell M*LIB! We don't need explicit oplist to tell M*LIB!
The oplist to use is STRING_OPLIST, The oplist to use is STRING_OPLIST,
@@ -280,12 +301,15 @@ test_string(int n)
return s; return s;
} }
/* Same with type string_t of M*LIB, representing a variable string /* Same with type string_t of M*LIB, representing a variable string
and its explicit oplist. And said above, it generates exactly the and its explicit oplist. And said above, it generates exactly the
same code. */ same code. */
ARRAY_DEF(r_string2, string_t, STRING_OPLIST) ARRAY_DEF(r_string2, string_t, STRING_OPLIST)
/* Same with type 'volatile unsigned int *' /* Same with type 'volatile unsigned int *'
The array stores pointers to another integer, The array stores pointers to another integer,
allocated somewhere else. allocated somewhere else.
@@ -327,6 +351,7 @@ test_vintptr(int n)
} }
/* Same with a structure type. /* Same with a structure type.
We need to tell M*LIB to handle the basic type We need to tell M*LIB to handle the basic type
as POD; as POD;
@@ -345,32 +370,28 @@ test_rockme(int n)
{ {
r_rockme_t array; r_rockme_t array;
// Allocate a secondary table to store the structure
// for the purpose of this test.
struct rock_me_out *tab = calloc(n, sizeof(struct rock_me_out));
if (!tab) abort();
r_rockme_init(array); r_rockme_init(array);
for(int i = 0; i < n; i++) { for(int i = 0; i < n; i++) {
tab[i].n = i*i - i; struct rock_me_out rock;
// Push the pointer to this integer in the array rock.n = i*i - i;
r_rockme_push_back(array, tab[i]); // Push the struct in the array
r_rockme_push_back(array, rock);
} }
int s = 0; int s = 0;
for(int i = 0; i < n; i++) { for(int i = 0; i < n; i++) {
// Get the pointer to the integer. // Get the pointer to the struct
struct rock_me_out *p = r_rockme_get(array, i); struct rock_me_out *p = r_rockme_get(array, i);
s += p->n; s += p->n;
} }
r_rockme_clear(array); r_rockme_clear(array);
free(tab);
return s; return s;
} }
/* Same with a structure type defined with [1]: /* Same with a structure type defined with [1]:
We need to tell M*LIB to handle the basic type We need to tell M*LIB to handle the basic type
as a special array; as a special array;
@@ -389,32 +410,27 @@ test_rockme2(int n)
{ {
r_rockme2_t array; r_rockme2_t array;
// Allocate a secondary table to store the structure
// for the purpose of this test.
rock_me_in *tab = calloc(n, sizeof(struct rock_me_in));
if (!tab) abort();
r_rockme2_init(array); r_rockme2_init(array);
for(int i = 0; i < n; i++) { for(int i = 0; i < n; i++) {
tab[i]->n = i*i - i; rock_me_in rock;
// Push the pointer to this integer in the array rock->n = i*i - i;
r_rockme2_push_back(array, tab[i]); r_rockme2_push_back(array, rock);
} }
int s = 0; int s = 0;
for(int i = 0; i < n; i++) { for(int i = 0; i < n; i++) {
// Get the pointer to the integer. // Get the pointer to the struct.
rock_me_in *p = r_rockme2_get(array, i); rock_me_in *p = r_rockme2_get(array, i);
s += (*p)->n; s += (*p)->n;
} }
r_rockme2_clear(array); r_rockme2_clear(array);
free(tab);
return s; return s;
} }
/* Same with a structure type defined with [1] and a global registeration. /* Same with a structure type defined with [1] and a global registeration.
We need to tell M*LIB to handle the basic type We need to tell M*LIB to handle the basic type
as a special array. as a special array.
@@ -438,32 +454,27 @@ test_rockme2b(int n)
{ {
r_rockme2b_t array; r_rockme2b_t array;
// Allocate a secondary table to store the structure
// for the purpose of this test.
rock_me_in *tab = calloc(n, sizeof(struct rock_me_in));
if (!tab) abort();
r_rockme2b_init(array); r_rockme2b_init(array);
for(int i = 0; i < n; i++) { for(int i = 0; i < n; i++) {
tab[i]->n = i*i - i; rock_me_in rock;
// Push the pointer to this integer in the array rock->n = i*i - i;
r_rockme2b_push_back(array, tab[i]); r_rockme2b_push_back(array, rock);
} }
int s = 0; int s = 0;
for(int i = 0; i < n; i++) { for(int i = 0; i < n; i++) {
// Get the pointer to the integer. // Get the pointer to the struct.
rock_me_in *p = r_rockme2b_get(array, i); rock_me_in *p = r_rockme2b_get(array, i);
s += (*p)->n; s += (*p)->n;
} }
r_rockme2b_clear(array); r_rockme2b_clear(array);
free(tab);
return s; return s;
} }
/* Same with a structure type defined as a tuple. /* Same with a structure type defined as a tuple.
We first define a tuple. No need to specify the oplist as it uses basic C types We first define a tuple. No need to specify the oplist as it uses basic C types
@@ -505,6 +516,7 @@ test_rockme3(int n)
} }
/* Same with a structure type defined as a tuple using global registeration. /* Same with a structure type defined as a tuple using global registeration.
We first define a tuple. No need to specify the oplist as it uses basic C types We first define a tuple. No need to specify the oplist as it uses basic C types