From 510b10b96b546fcc6c6b6be85050305ddd192a41 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Sat, 5 Feb 2022 17:30:11 +0800 Subject: replace some hash table usage by tuples Previously I used hash tables, which consume too much memory. Now the critical parts are replaced by a new hand-written library called "tuple.h". Now we can easily parse larger inputs. I haven't tested its limits, though. --- src/tuple.h | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 src/tuple.h (limited to 'src/tuple.h') diff --git a/src/tuple.h b/src/tuple.h new file mode 100644 index 0000000..3c3f0fa --- /dev/null +++ b/src/tuple.h @@ -0,0 +1,116 @@ +#ifndef TUPLE_H +#define TUPLE_H +#include "util.h" +#include "pair.h" + +/* This file implements the tuples that we need. A tuple is an array + of arrays of arrays, ..., ad infinitum. + + Since the lengths of each layer of the arrays are different, but + uniform within each layer, we store the lengths in an array at the + top level. So there are two kinds of data types: one with an array + of lengths and the other without. */ + +#define TUP(N) typedef struct PASTER(tuple, N, _s) PASTER(tuple, N,) +#define LTUP(N) typedef struct PASTER(luple, N, _s) PASTER(luple, N,) + +#define LONSTRUCT(N) PASTER(luple, N,) *PASTER(new, _luple, N) \ + (NUM *len) + +#define LESTRUCT(N) void PASTER(destroy_luple, N,) \ + (PASTER(luple, N,) *tup) + +#define ADDL(N) BOOL PASTER(add_to_luple, N,) \ + (PASTER(luple, N,) *lup, PASTER(pair, N,) label, \ + NUM val) + +#define INDEXL(N) HP_ATTR \ + NUM *PASTER(luple, N, _find) \ + (PASTER(luple, N,) *lup, PASTER(pair, N,) label) + +#define LENTHL(N) P_ATTR NUM *PASTER(luple, N, _len) \ + (PASTER(luple, N,) *lup) + +typedef struct PASTER(tuple, 1, _s) PASTER(tuple, 1,); + +TUP(2); +TUP(3); +TUP(4); +TUP(5); +TUP(6); + +LTUP(2); +LTUP(3); +LTUP(4); +LTUP(5); +LTUP(6); + +tuple4 *new_tuple4(); + +LONSTRUCT(2); +LONSTRUCT(3); +LONSTRUCT(4); +LONSTRUCT(5); +LONSTRUCT(6); + +void destroy_tuple4(tuple4 tup, NUM *len); + +LESTRUCT(2); +LESTRUCT(3); +LESTRUCT(4); +LESTRUCT(5); +LESTRUCT(6); + +ADDL(2); +ADDL(3); +ADDL(4); +ADDL(5); +ADDL(6); + +H_ATTR BOOL add_to_luple6_pt_2(luple6 *lup, pair2 label); + +INDEXL(2); +INDEXL(3); +INDEXL(4); +INDEXL(5); +INDEXL(6); + +LENTHL(2); +LENTHL(3); +LENTHL(4); +LENTHL(5); +LENTHL(6); + +HP_ATTR BOOL luple3_pf_2(luple3 *lup, pair2 label); +HP_ATTR BOOL luple5_pf_3(luple5 *lup, pair3 label); +HP_ATTR BOOL luple6_pf_2(luple6 *lup, pair2 label); + +typedef void (* map_pair3_t) (pair3 label); +typedef void (* map_pair5_t) (pair5 label); +typedef void (* map_pair6_t) (pair6 label); + +H_ATTR void luple3_map_2(luple3 *lup, pair2 label, map_pair3_t fn); + +/* H_ATTR void tuple5_map_3(tuple5 *lup, pair3 label, map_pair5_t fn); */ + +H_ATTR void luple5_map_3(luple5 *lup, pair3 label, map_pair5_t fn); + +H_ATTR void luple5_free_1(luple5 *lup, NUM label); + +/* MAX bounds the last coordinate, i.e. the W coordinate. If -1 then + there is no bound. */ +H_ATTR void luple6_map_2(luple6 *lup, pair2 label, + NUM max, map_pair6_t fn); + +H_ATTR void luple6_map_5(luple6 *tup, pair5 label, map_pair6_t fn); + +#undef LONSTRUCT +#undef LESTRUCT +#undef LTUP +#undef TUP +#undef ADDL +#undef INDEXL +#undef INDEXPL +#undef LENTHL + +#endif -- cgit v1.2.3-18-g5258