@@ -248,6 +248,7 @@ void bdz_config_destroy(cmph_config_t *mph)
248248{
249249 bdz_config_data_t * data = (bdz_config_data_t * )mph -> data ;
250250 DEBUGP ("Destroying algorithm dependent data\n" );
251+ free (mph -> ordering_table );
251252 free (data );
252253}
253254
@@ -277,7 +278,8 @@ cmph_t *bdz_new(cmph_config_t *mph, double c)
277278 bdz_queue_t edges ;
278279 bdz_graph3_t graph3 ;
279280 bdz_config_data_t * bdz = (bdz_config_data_t * )mph -> data ;
280- cmph_uint32 * ordering_table = NULL ;
281+ cmph_uint32 packed_co_size = 0 ;
282+ cmph_uint8 * packed_co = NULL ;
281283#ifdef CMPH_TIMING
282284 double construction_time_begin = 0.0 ;
283285 double construction_time = 0.0 ;
@@ -353,9 +355,11 @@ cmph_t *bdz_new(cmph_config_t *mph, double c)
353355 bdz -> m , bdz -> n );
354356 ranking (bdz );
355357
358+ mphf = (cmph_t * )xmalloc (sizeof (cmph_t ));
356359 if (mph -> do_ordering_table ) {
357- ordering_table = (cmph_uint32 * )xmalloc (bdz -> m * sizeof (cmph_uint32 ));
358- assert (ordering_table );
360+ compressed_seq_t co ;
361+ mph -> ordering_table = (cmph_uint32 * )xmalloc (bdz -> m * sizeof (cmph_uint32 ));
362+ assert (mph -> ordering_table );
359363 DEBUGP ("Create ordering table\n" );
360364 mph -> key_source -> rewind (mph -> key_source -> data );
361365 for (cmph_uint32 i = 0 ; i < bdz -> m ; i ++ ) {
@@ -369,17 +373,24 @@ cmph_t *bdz_new(cmph_config_t *mph, double c)
369373 hl [2 ] = hl [2 ] % bdz -> r + (bdz -> r << 1 );
370374 vertex = hl [(GETVALUE (bdz -> g , hl [0 ]) + GETVALUE (bdz -> g , hl [1 ]) + GETVALUE (bdz -> g , hl [2 ])) % 3 ];
371375 h = rank (bdz -> b , bdz -> ranktable , bdz -> g , vertex );
372- ordering_table [h ] = i ;
376+ mph -> ordering_table [h ] = i ;
373377 mph -> key_source -> dispose (key );
374378 }
379+ compressed_seq_init (& co );
380+ compressed_seq_generate (& co , mph -> ordering_table , bdz -> m );
381+ packed_co_size = compressed_seq_packed_size (& co );
382+ packed_co = (cmph_uint8 * )xcalloc (packed_co_size , sizeof (cmph_uint8 ));
383+ compressed_seq_pack (& co , packed_co );
384+ compressed_seq_destroy (& co );
375385 }
376386
377387#ifdef CMPH_TIMING
378388 ELAPSED_TIME_IN_SECONDS (& construction_time );
379389#endif
380- mphf = (cmph_t * )xmalloc (sizeof (cmph_t ));
381390 mphf -> algo = mph -> algo ;
382- mphf -> o = ordering_table ;
391+ mphf -> packed_co = packed_co ;
392+ packed_co = NULL ;
393+ mphf -> packed_co_size = packed_co_size ;
383394 bdzf = (bdz_data_t * )xmalloc (sizeof (bdz_data_t ));
384395 bdzf -> g = bdz -> g ;
385396 bdz -> g = NULL ; //transfer memory ownership
@@ -505,6 +516,8 @@ static void ranking(bdz_config_data_t *bdz)
505516 }
506517}
507518
519+ #define COMPRESSED_O
520+
508521int bdz_compile (cmph_t * mphf , cmph_config_t * mph , FILE * out )
509522{
510523 bdz_data_t * bdz = (bdz_data_t * )mphf -> data ;
@@ -570,12 +583,22 @@ int bdz_compile(cmph_t *mphf, cmph_config_t *mph, FILE *out)
570583#endif
571584 fprintf (out , " return rank(vertex);\n" );
572585 fprintf (out , "};\n" );
573- if (mphf -> o ) {
574- uint32_compile (out , "ordering_table" , mphf -> o , bdz -> m );
575- fprintf (out , "uint32_t %s_order(uint32_t id) {\n" , mph -> c_prefix );
576- fprintf (out , " assert(id < %u);\n" , bdz -> m );
577- fprintf (out , " return ordering_table[id];\n" );
578- fprintf (out , "}\n" );
586+ if (mph -> ordering_table ) {
587+ #ifdef COMPRESSED_O
588+ compressed_seq_t co = {0 };
589+ compressed_seq_unpack ((uint32_t * )mphf -> packed_co , & co );
590+ compressed_seq_data_compile (out , "ordering_seq" , & co , 0 );
591+ compressed_seq_query_compile (out , & co , 0 ); // the function
592+ fprintf (out , "uint32_t %s_order(uint32_t id) {\n" , mph -> c_prefix );
593+ fprintf (out , " assert(id < %u);\n" , bdz -> m );
594+ fprintf (out , " return compressed_seq_query(&ordering_seq, id);\n" );
595+ #else
596+ uint32_compile (out , "ordering_table" , mph -> ordering_table , data -> m );
597+ fprintf (out , "uint32_t %s_order(uint32_t id) {\n" , mph -> c_prefix );
598+ fprintf (out , " assert(id < %u);\n" , bdz -> m );
599+ fprintf (out , " return ordering_table[id];\n" );
600+ #endif
601+ fprintf (out , "}\n" );
579602 }
580603 fprintf (out , "\nuint32_t %s_size(void) {\n" , mph -> c_prefix );
581604 fprintf (out , " return %u;\n}\n" , bdz -> m );
@@ -620,14 +643,11 @@ int bdz_dump(cmph_t *mphf, FILE *fd)
620643 fprintf (stderr , "%u " , data -> ranktable [i ]);
621644 fprintf (stderr , "\n" );
622645#endif
623- if (mphf -> o ) {
624- DEBUGP ("Dumping ordering_table\n" );
625- CHK_FWRITE (mphf -> o , sizeof (cmph_uint32 ), data -> m , fd );
626- #ifdef DEBUG
627- fprintf (stderr , "O: " );
628- for (cmph_uint32 i = 0 ; i < data -> m ; ++ i ) fprintf (stderr , "%u " , mphf -> o [i ]);
629- fprintf (stderr , "\n" );
630- #endif
646+ // version 2.1 extension
647+ if (mphf -> packed_co_size ) {
648+ DEBUGP ("Dumping packed ordering table\n" );
649+ CHK_FWRITE (& (mphf -> packed_co_size ), sizeof (cmph_uint32 ), (size_t )1 , fd );
650+ CHK_FWRITE (mphf -> packed_co , mphf -> packed_co_size ,(size_t )1 , fd );
631651 }
632652 return 1 ;
633653}
@@ -669,22 +689,15 @@ void bdz_load(FILE *f, cmph_t *mphf)
669689 fprintf (stderr , "%u " , GETVALUE (bdz -> g , i ));
670690 fprintf (stderr , "\n" );
671691#endif
672- //loading the optional ordering table.
673- mphf -> o = (cmph_uint32 * )xmalloc (sizeof (cmph_uint32 ) * bdz -> m );
692+ //loading the optional ordering table. since version 2.1
674693 cmph_uint32 nread =
675- fread (mphf -> o , sizeof (cmph_uint32 ), (size_t )bdz -> m , f );
676- if (nread != bdz -> m ) {
677- free (mphf -> o );
678- mphf -> o = NULL ;
679- }
680- #ifdef DEBUG
681- if (mphf -> o ) {
682- fprintf (stderr , "O: " );
683- for (cmph_uint32 i = 0 ; i < bdz -> m ; ++ i )
684- fprintf (stderr , "%u " , mphf -> o [i ]);
685- fprintf (stderr , "\n" );
694+ fread (& (mphf -> packed_co_size ), sizeof (cmph_uint32 ), (size_t )1 , f );
695+ if (nread == 1 ) {
696+ mphf -> packed_co = (cmph_uint8 * )xmalloc (mphf -> packed_co_size );
697+ CHK_FREAD (mphf -> packed_co , mphf -> packed_co_size , (size_t )1 , f );
698+ } else {
699+ mphf -> packed_co_size = 0 ;
686700 }
687- #endif
688701 return ;
689702}
690703
@@ -730,11 +743,11 @@ cmph_uint32 bdz_search(cmph_t *mphf, const char *key, cmph_uint32 keylen)
730743void bdz_destroy (cmph_t * mphf )
731744{
732745 bdz_data_t * data = (bdz_data_t * )mphf -> data ;
733- free (mphf -> o );
734746 free (data -> g );
735747 hash_state_destroy (data -> hl );
736748 free (data -> ranktable );
737749 free (data );
750+
738751 free (mphf );
739752}
740753
0 commit comments