Skip to content

Commit

Permalink
Adding new example of generic
Browse files Browse the repository at this point in the history
  • Loading branch information
P-p-H-d committed Apr 4, 2024
1 parent 8602a25 commit 955c8bf
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,8 @@ M_LET(list, list_uint_t) {
}
```

See [M-GENERIC](#M-GENERIC) header for details.
See the [example](https://github.com/P-p-H-d/mlib/blob/master/example/ex11-generic01.c)
and [M-GENERIC](#M-GENERIC) header for details.

## Performance

Expand Down
92 changes: 92 additions & 0 deletions example/ex11-generic02.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#if HAVE_GMP

#include <stdio.h>
#include <gmp.h>

#include "m-array.h"
#include "m-list.h"
#include "m-generic.h"

// Register a namespace for generic
#define M_GENERIC_ORG_1() (USER)
#define M_GENERIC_ORG_USER_COMP_1() (CORE)

// Register the oplist of a mpz_t. It is a classic oplist.
#define M_OPL_mpz_t() M_OPEXTEND(M_CLASSIC_OPLIST(mpz), \
INIT_WITH(mpz_init_set_ui), EMPLACE_TYPE(unsigned int), \
OUT_STR( API( mpz_out_str, VOID, ARG1, 10, ARG2)), \
GENTYPE(__mpz_struct *), TYPE(mpz_t) )
// Register the oplist as a generic type
#define M_GENERIC_ORG_USER_COMP_CORE_OPLIST_1() M_OPL_mpz_t()

// Define an instance of an array of mpz_t (both type and function)
ARRAY_DEF(array_mpz, mpz_t)
// Register the oplist of the created instance of array of mpz_t
#define M_OPL_array_mpz_t() ARRAY_OPLIST(array_mpz, M_OPL_mpz_t())
// Register the oplist as a generic type
#define M_GENERIC_ORG_USER_COMP_CORE_OPLIST_10() M_OPL_array_mpz_t()

// Define an instance of a list of mpz_t (both type and function)
LIST_DEF(list_mpz, mpz_t)
// Register the oplist of the created instance of array of mpz_t
#define M_OPL_list_mpz_t() LIST_OPLIST(list_mpz, M_OPL_mpz_t())
// Register the oplist as a generic type
#define M_GENERIC_ORG_USER_COMP_CORE_OPLIST_11() M_OPL_list_mpz_t()

// Define an instance of a list of unsigned int (both type and function)
LIST_DEF(list_uint, unsigned)
// Register the oplist of the created instance of list of unsigned
#define M_OPL_list_uint_t() LIST_OPLIST(list_uint, M_BASIC_OPLIST)
// Register the oplist as a generic type
#define M_GENERIC_ORG_USER_COMP_CORE_OPLIST_12() M_OPL_list_uint_t()

// Define an instance of a array of unsigned int (both type and function)
ARRAY_DEF(array_uint, unsigned)
// Register the oplist of the created instance of list of unsigned
#define M_OPL_array_uint_t() ARRAY_OPLIST(array_uint, M_BASIC_OPLIST)
// Register the oplist as a generic type
#define M_GENERIC_ORG_USER_COMP_CORE_OPLIST_13() M_OPL_array_uint_t()

int main(void) {

print("LIST UINT = ");
let( (list, 17, 42), list_uint_t) {
push(list, 1742);
for each (item, list) {
print(*item, " ");
}
print("\n","List=", list, "\n");
}

print("Array UINT = ");
let( (list, 17, 42), array_uint_t) {
push(list, 1742);
for each (item, list) {
print(*item, " ");
}
print("\n","List=", list, "\n");
}

print("LIST MPZ = ");
let( (list, (17), (42)), list_mpz_t) {
list_mpz_emplace_back(list, 1742); // Generic emplace is not supported yet
for each (item, list) {
print(*item, " ");
}
print("\n","List=", list, "\n");
}

print("ARRAY MPZ = ");
let( (list, (17), (42)), array_mpz_t) {
array_mpz_emplace_back(list, 1742);
for each (item, list) {
print(*item, " ");
}
print("\n","List=", list, "\n");
}
exit(0);
}

#else
int main(void) {}
#endif

0 comments on commit 955c8bf

Please sign in to comment.