LCOV - code coverage report
Current view: top level - glib/girepository/cmph - cmph.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 104 556 18.7 %
Date: 2024-05-07 05:15:23 Functions: 17 44 38.6 %
Branches: 14 176 8.0 %

           Branch data     Line data    Source code
       1                 :            : #include "cmph.h"
       2                 :            : #include "cmph_structs.h"
       3                 :            : #include "chm.h"
       4                 :            : #include "bmz.h" 
       5                 :            : #include "bmz8.h" 
       6                 :            : #include "brz.h" 
       7                 :            : #include "fch.h" 
       8                 :            : #include "bdz.h"
       9                 :            : #include "bdz_ph.h"
      10                 :            : #include "chd_ph.h"
      11                 :            : #include "chd.h"
      12                 :            : 
      13                 :            : #include <stdlib.h>
      14                 :            : #include <assert.h>
      15                 :            : #include <string.h>
      16                 :            : //#define DEBUG
      17                 :            : #include "debug.h"
      18                 :            : 
      19                 :            : const char *cmph_names[] = {"bmz", "bmz8", "chm", "brz", "fch", "bdz", "bdz_ph", "chd_ph", "chd", NULL };
      20                 :            : 
      21                 :            : typedef struct 
      22                 :            : {
      23                 :            :         void *vector;
      24                 :            :         cmph_uint32 position; // access position when data is a vector  
      25                 :            : } cmph_vector_t;
      26                 :            : 
      27                 :            : 
      28                 :            : 
      29                 :            : /** 
      30                 :            :  * Support a vector of struct as the source of keys.
      31                 :            :  *
      32                 :            :  * E.g. The keys could be the fieldB's in a vector of struct rec where 
      33                 :            :  * struct rec is defined as:
      34                 :            :  * struct rec {
      35                 :            :  *    fieldA;
      36                 :            :  *    fieldB;
      37                 :            :  *    fieldC;
      38                 :            :  * }
      39                 :            :  */
      40                 :            : typedef struct 
      41                 :            : {
      42                 :            :         void *vector;                                   /* Pointer to the vector of struct */
      43                 :            :         cmph_uint32 position;                   /* current position */
      44                 :            :         cmph_uint32 struct_size;        /* The size of the struct */
      45                 :            :         cmph_uint32 key_offset;         /* The byte offset of the key in the struct */
      46                 :            :         cmph_uint32 key_len;            /* The length of the key */
      47                 :            : } cmph_struct_vector_t;
      48                 :            : 
      49                 :            : 
      50                 :            : static cmph_io_adapter_t *cmph_io_vector_new(void * vector, cmph_uint32 nkeys);
      51                 :            : static void cmph_io_vector_destroy(cmph_io_adapter_t * key_source);
      52                 :            : 
      53                 :            : static cmph_io_adapter_t *cmph_io_struct_vector_new(void * vector, cmph_uint32 struct_size, cmph_uint32 key_offset, cmph_uint32 key_len, cmph_uint32 nkeys);
      54                 :            : static void cmph_io_struct_vector_destroy(cmph_io_adapter_t * key_source);
      55                 :            : 
      56                 :          0 : static int key_nlfile_read(void *data, char **key, cmph_uint32 *keylen)
      57                 :            : {
      58                 :          0 :         FILE *fd = (FILE *)data;
      59                 :          0 :         *key = NULL;
      60                 :          0 :         *keylen = 0;
      61                 :            :         while(1)
      62                 :          0 :         {
      63                 :            :                 char buf[BUFSIZ];
      64                 :          0 :                 char *c = fgets(buf, BUFSIZ, fd); 
      65         [ #  # ]:          0 :                 if (c == NULL) return -1;
      66         [ #  # ]:          0 :                 if (feof(fd)) return -1;
      67                 :          0 :                 *key = (char *)realloc(*key, *keylen + strlen(buf) + 1);
      68                 :          0 :                 memcpy(*key + *keylen, buf, strlen(buf));
      69                 :          0 :                 *keylen += (cmph_uint32)strlen(buf);
      70         [ #  # ]:          0 :                 if (buf[strlen(buf) - 1] != '\n') continue;
      71                 :          0 :                 break;
      72                 :            :         }
      73   [ #  #  #  # ]:          0 :         if ((*keylen) && (*key)[*keylen - 1] == '\n')
      74                 :            :         {
      75                 :          0 :                 (*key)[(*keylen) - 1] = 0;
      76                 :          0 :                 --(*keylen);
      77                 :            :         }
      78                 :          0 :         return (int)(*keylen);
      79                 :            : }
      80                 :            : 
      81                 :          0 : static int key_byte_vector_read(void *data, char **key, cmph_uint32 *keylen)
      82                 :            : {
      83                 :          0 :         cmph_vector_t *cmph_vector = (cmph_vector_t *)data;
      84                 :          0 :         cmph_uint8 **keys_vd = (cmph_uint8 **)cmph_vector->vector;
      85                 :            :         size_t size;
      86                 :          0 :         memcpy(keylen, keys_vd[cmph_vector->position], sizeof(*keylen));
      87                 :          0 :         size = *keylen;
      88                 :          0 :         *key = (char *)malloc(size);
      89                 :          0 :         memcpy(*key, keys_vd[cmph_vector->position] + sizeof(*keylen), size);
      90                 :          0 :         cmph_vector->position = cmph_vector->position + 1;
      91                 :          0 :         return (int)(*keylen);
      92                 :            : 
      93                 :            : }
      94                 :            : 
      95                 :          0 : static int key_struct_vector_read(void *data, char **key, cmph_uint32 *keylen)
      96                 :            : {
      97                 :          0 :     cmph_struct_vector_t *cmph_struct_vector = (cmph_struct_vector_t *)data;
      98                 :          0 :     char *keys_vd = (char *)cmph_struct_vector->vector;
      99                 :            :     size_t size;
     100                 :          0 :     *keylen = cmph_struct_vector->key_len;
     101                 :          0 :     size = *keylen;
     102                 :          0 :     *key = (char *)malloc(size);
     103                 :          0 :     memcpy(*key, (keys_vd + (cmph_struct_vector->position * cmph_struct_vector->struct_size) + cmph_struct_vector->key_offset), size);
     104                 :          0 :     cmph_struct_vector->position = cmph_struct_vector->position + 1;
     105                 :          0 :     return (int)(*keylen);
     106                 :            : }
     107                 :            : 
     108                 :         10 : static int key_vector_read(void *data, char **key, cmph_uint32 *keylen)
     109                 :            : {
     110                 :         10 :         cmph_vector_t *cmph_vector = (cmph_vector_t *)data;
     111                 :         10 :         char **keys_vd = (char **)cmph_vector->vector;
     112                 :            :         size_t size;
     113                 :         10 :         *keylen = (cmph_uint32)strlen(keys_vd[cmph_vector->position]);
     114                 :         10 :         size = *keylen;
     115                 :         10 :         *key = (char *)malloc(size + 1);
     116                 :         10 :         strcpy(*key, keys_vd[cmph_vector->position]);
     117                 :         10 :         cmph_vector->position = cmph_vector->position + 1;
     118                 :         10 :         return (int)(*keylen);
     119                 :            : 
     120                 :            : }
     121                 :            : 
     122                 :            : 
     123                 :          0 : static void key_nlfile_dispose(void *data, char *key, cmph_uint32 keylen)
     124                 :            : {
     125                 :          0 :         free(key);
     126                 :          0 : }
     127                 :            : 
     128                 :         10 : static void key_vector_dispose(void *data, char *key, cmph_uint32 keylen)
     129                 :            : {
     130                 :         10 :         free(key);
     131                 :         10 : }
     132                 :            : 
     133                 :          0 : static void key_nlfile_rewind(void *data)
     134                 :            : {
     135                 :          0 :         FILE *fd = (FILE *)data;
     136                 :          0 :         rewind(fd);
     137                 :          0 : }
     138                 :            : 
     139                 :          0 : static void key_struct_vector_rewind(void *data)
     140                 :            : {
     141                 :          0 :         cmph_struct_vector_t *cmph_struct_vector = (cmph_struct_vector_t *)data;
     142                 :          0 :         cmph_struct_vector->position = 0;
     143                 :          0 : }
     144                 :            : 
     145                 :          3 : static void key_vector_rewind(void *data)
     146                 :            : {
     147                 :          3 :         cmph_vector_t *cmph_vector = (cmph_vector_t *)data;
     148                 :          3 :         cmph_vector->position = 0;
     149                 :          3 : }
     150                 :            : 
     151                 :          0 : static cmph_uint32 count_nlfile_keys(FILE *fd)
     152                 :            : {
     153                 :          0 :         cmph_uint32 count = 0;
     154                 :          0 :         rewind(fd);
     155                 :            :         while(1)
     156                 :          0 :         {
     157                 :            :                 char buf[BUFSIZ];
     158         [ #  # ]:          0 :                 if (fgets(buf, BUFSIZ, fd) == NULL) break;
     159         [ #  # ]:          0 :                 if (feof(fd)) break;
     160         [ #  # ]:          0 :                 if (buf[strlen(buf) - 1] != '\n') continue;
     161                 :          0 :                 ++count;
     162                 :            :         }
     163                 :          0 :         rewind(fd);
     164                 :          0 :         return count;
     165                 :            : }
     166                 :            : 
     167                 :          0 : cmph_io_adapter_t *cmph_io_nlfile_adapter(FILE * keys_fd)
     168                 :            : {
     169                 :          0 :   cmph_io_adapter_t * key_source = (cmph_io_adapter_t *)malloc(sizeof(cmph_io_adapter_t));
     170         [ #  # ]:          0 :   assert(key_source);
     171                 :          0 :   key_source->data = (void *)keys_fd;
     172                 :          0 :   key_source->nkeys = count_nlfile_keys(keys_fd);
     173                 :          0 :   key_source->read = key_nlfile_read;
     174                 :          0 :   key_source->dispose = key_nlfile_dispose;
     175                 :          0 :   key_source->rewind = key_nlfile_rewind;
     176                 :          0 :   return key_source;
     177                 :            : }
     178                 :            : 
     179                 :          0 : void cmph_io_nlfile_adapter_destroy(cmph_io_adapter_t * key_source)
     180                 :            : {
     181                 :          0 :         free(key_source);
     182                 :          0 : }
     183                 :            : 
     184                 :          0 : cmph_io_adapter_t *cmph_io_nlnkfile_adapter(FILE * keys_fd, cmph_uint32 nkeys)
     185                 :            : {
     186                 :          0 :   cmph_io_adapter_t * key_source = (cmph_io_adapter_t *)malloc(sizeof(cmph_io_adapter_t));
     187         [ #  # ]:          0 :   assert(key_source);
     188                 :          0 :   key_source->data = (void *)keys_fd;
     189                 :          0 :   key_source->nkeys = nkeys;
     190                 :          0 :   key_source->read = key_nlfile_read;
     191                 :          0 :   key_source->dispose = key_nlfile_dispose;
     192                 :          0 :   key_source->rewind = key_nlfile_rewind;
     193                 :          0 :   return key_source;
     194                 :            : }
     195                 :            : 
     196                 :          0 : void cmph_io_nlnkfile_adapter_destroy(cmph_io_adapter_t * key_source)
     197                 :            : {
     198                 :          0 :         free(key_source);
     199                 :          0 : }
     200                 :            : 
     201                 :            : 
     202                 :          0 : static cmph_io_adapter_t *cmph_io_struct_vector_new(void * vector, cmph_uint32 struct_size, cmph_uint32 key_offset, cmph_uint32 key_len, cmph_uint32 nkeys)
     203                 :            : {
     204                 :          0 :         cmph_io_adapter_t * key_source = (cmph_io_adapter_t *)malloc(sizeof(cmph_io_adapter_t));
     205                 :          0 :         cmph_struct_vector_t * cmph_struct_vector = (cmph_struct_vector_t *)malloc(sizeof(cmph_struct_vector_t));
     206         [ #  # ]:          0 :         assert(key_source);
     207         [ #  # ]:          0 :         assert(cmph_struct_vector);
     208                 :          0 :         cmph_struct_vector->vector = vector;
     209                 :          0 :         cmph_struct_vector->position = 0;
     210                 :          0 :         cmph_struct_vector->struct_size = struct_size;
     211                 :          0 :         cmph_struct_vector->key_offset = key_offset;
     212                 :          0 :         cmph_struct_vector->key_len = key_len;
     213                 :          0 :         key_source->data = (void *)cmph_struct_vector;
     214                 :          0 :         key_source->nkeys = nkeys;
     215                 :          0 :         return key_source;
     216                 :            : }
     217                 :            : 
     218                 :          0 : static void cmph_io_struct_vector_destroy(cmph_io_adapter_t * key_source)
     219                 :            : {
     220                 :          0 :         cmph_struct_vector_t *cmph_struct_vector = (cmph_struct_vector_t *)key_source->data;
     221                 :          0 :         cmph_struct_vector->vector = NULL;
     222                 :          0 :         free(cmph_struct_vector);
     223                 :          0 :         free(key_source);
     224                 :          0 : }
     225                 :            : 
     226                 :          3 : static cmph_io_adapter_t *cmph_io_vector_new(void * vector, cmph_uint32 nkeys)
     227                 :            : {
     228                 :          3 :         cmph_io_adapter_t * key_source = (cmph_io_adapter_t *)malloc(sizeof(cmph_io_adapter_t));
     229                 :          3 :         cmph_vector_t * cmph_vector = (cmph_vector_t *)malloc(sizeof(cmph_vector_t));
     230         [ -  + ]:          3 :         assert(key_source);
     231         [ -  + ]:          3 :         assert(cmph_vector);
     232                 :          3 :         cmph_vector->vector = vector;
     233                 :          3 :         cmph_vector->position = 0;
     234                 :          3 :         key_source->data = (void *)cmph_vector;
     235                 :          3 :         key_source->nkeys = nkeys;
     236                 :          3 :         return key_source;
     237                 :            : }
     238                 :            : 
     239                 :          3 : static void cmph_io_vector_destroy(cmph_io_adapter_t * key_source)
     240                 :            : {
     241                 :          3 :         cmph_vector_t *cmph_vector = (cmph_vector_t *)key_source->data;
     242                 :          3 :         cmph_vector->vector = NULL;
     243                 :          3 :         free(cmph_vector);
     244                 :          3 :         free(key_source);
     245                 :          3 : }
     246                 :            : 
     247                 :          0 : cmph_io_adapter_t *cmph_io_byte_vector_adapter(cmph_uint8 ** vector, cmph_uint32 nkeys)
     248                 :            : {
     249                 :          0 :         cmph_io_adapter_t * key_source = cmph_io_vector_new(vector, nkeys);
     250                 :          0 :         key_source->read = key_byte_vector_read;
     251                 :          0 :         key_source->dispose = key_vector_dispose;
     252                 :          0 :         key_source->rewind = key_vector_rewind;
     253                 :          0 :         return key_source;
     254                 :            : }
     255                 :          0 : void cmph_io_byte_vector_adapter_destroy(cmph_io_adapter_t * key_source)
     256                 :            : {
     257                 :          0 :         cmph_io_vector_destroy(key_source);
     258                 :          0 : }
     259                 :            : 
     260                 :          0 : cmph_io_adapter_t *cmph_io_struct_vector_adapter(void * vector, cmph_uint32 struct_size, cmph_uint32 key_offset, cmph_uint32 key_len, cmph_uint32 nkeys)
     261                 :            : {
     262                 :          0 :         cmph_io_adapter_t * key_source = cmph_io_struct_vector_new(vector, struct_size, key_offset, key_len, nkeys);
     263                 :          0 :         key_source->read = key_struct_vector_read;
     264                 :          0 :         key_source->dispose = key_vector_dispose;
     265                 :          0 :         key_source->rewind = key_struct_vector_rewind;
     266                 :          0 :         return key_source;      
     267                 :            : }
     268                 :            : 
     269                 :          0 : void cmph_io_struct_vector_adapter_destroy(cmph_io_adapter_t * key_source)
     270                 :            : {
     271                 :          0 :         cmph_io_struct_vector_destroy(key_source);      
     272                 :          0 : }
     273                 :            : 
     274                 :          3 : cmph_io_adapter_t *cmph_io_vector_adapter(char ** vector, cmph_uint32 nkeys)
     275                 :            : {
     276                 :          3 :         cmph_io_adapter_t * key_source = cmph_io_vector_new(vector, nkeys);
     277                 :          3 :         key_source->read = key_vector_read;
     278                 :          3 :         key_source->dispose = key_vector_dispose;
     279                 :          3 :         key_source->rewind = key_vector_rewind;
     280                 :          3 :         return key_source;
     281                 :            : }
     282                 :            : 
     283                 :          3 : void cmph_io_vector_adapter_destroy(cmph_io_adapter_t * key_source)
     284                 :            : {
     285                 :          3 :         cmph_io_vector_destroy(key_source);
     286                 :          3 : }
     287                 :            : 
     288                 :          3 : cmph_config_t *cmph_config_new(cmph_io_adapter_t *key_source)
     289                 :            : {
     290                 :          3 :         cmph_config_t *mph = NULL;
     291                 :          3 :         mph = __config_new(key_source);
     292         [ -  + ]:          3 :         assert(mph);
     293                 :          3 :         mph->algo = CMPH_CHM; // default value
     294                 :          3 :         mph->data = chm_config_new();
     295                 :          3 :         return mph;
     296                 :            : }
     297                 :            : 
     298                 :          3 : void cmph_config_set_algo(cmph_config_t *mph, CMPH_ALGO algo)
     299                 :            : {
     300         [ +  - ]:          3 :         if (algo != mph->algo)
     301                 :            :         {
     302   [ +  -  -  -  :          3 :                 switch (mph->algo)
          -  -  -  -  -  
                      - ]
     303                 :            :                 {
     304                 :          3 :                         case CMPH_CHM:
     305                 :          3 :                                 chm_config_destroy(mph);
     306                 :          3 :                                 break;
     307                 :          0 :                         case CMPH_BMZ:
     308                 :          0 :                                 bmz_config_destroy(mph);
     309                 :          0 :                                 break;
     310                 :          0 :                         case CMPH_BMZ8:
     311                 :          0 :                                 bmz8_config_destroy(mph);
     312                 :          0 :                                 break;
     313                 :          0 :                         case CMPH_BRZ:
     314                 :          0 :                                 brz_config_destroy(mph);
     315                 :          0 :                                 break;
     316                 :          0 :                         case CMPH_FCH:
     317                 :          0 :                                 fch_config_destroy(mph);
     318                 :          0 :                                 break;
     319                 :          0 :                         case CMPH_BDZ:
     320                 :          0 :                                 bdz_config_destroy(mph);
     321                 :          0 :                                 break;
     322                 :          0 :                         case CMPH_BDZ_PH:
     323                 :          0 :                                 bdz_ph_config_destroy(mph);
     324                 :          0 :                                 break;
     325                 :          0 :                         case CMPH_CHD_PH:
     326                 :          0 :                                 chd_ph_config_destroy(mph);
     327                 :          0 :                                 break;
     328                 :          0 :                         case CMPH_CHD:
     329                 :          0 :                                 chd_config_destroy(mph);
     330                 :          0 :                                 break;
     331                 :          0 :                         default:
     332                 :          0 :                                 assert(0);
     333                 :            :                 }
     334   [ -  -  -  -  :          3 :                 switch(algo)
          -  +  -  -  -  
                      - ]
     335                 :            :                 {
     336                 :          0 :                         case CMPH_CHM:
     337                 :          0 :                                 mph->data = chm_config_new();
     338                 :          0 :                                 break;
     339                 :          0 :                         case CMPH_BMZ:
     340                 :          0 :                                 mph->data = bmz_config_new();
     341                 :          0 :                                 break;
     342                 :          0 :                         case CMPH_BMZ8:
     343                 :          0 :                                 mph->data = bmz8_config_new();
     344                 :          0 :                                 break;
     345                 :          0 :                         case CMPH_BRZ:
     346                 :          0 :                                 mph->data = brz_config_new();
     347                 :          0 :                                 break;
     348                 :          0 :                         case CMPH_FCH:
     349                 :          0 :                                 mph->data = fch_config_new();
     350                 :          0 :                                 break;
     351                 :          3 :                         case CMPH_BDZ:
     352                 :          3 :                                 mph->data = bdz_config_new();
     353                 :          3 :                                 break;
     354                 :          0 :                         case CMPH_BDZ_PH:
     355                 :          0 :                                 mph->data = bdz_ph_config_new();
     356                 :          0 :                                 break;
     357                 :          0 :                         case CMPH_CHD_PH:
     358                 :          0 :                                 mph->data = chd_ph_config_new();
     359                 :          0 :                                 break;
     360                 :          0 :                         case CMPH_CHD:
     361                 :          0 :                                 mph->data = chd_config_new(mph);
     362                 :          0 :                                 break;
     363                 :          0 :                         default:
     364                 :          0 :                                 assert(0);
     365                 :            :                 }
     366                 :            :         }
     367                 :          3 :         mph->algo = algo;
     368                 :          3 : }
     369                 :            : 
     370                 :          0 : void cmph_config_set_tmp_dir(cmph_config_t *mph, cmph_uint8 *tmp_dir)
     371                 :            : {
     372         [ #  # ]:          0 :         if (mph->algo == CMPH_BRZ) 
     373                 :            :         {
     374                 :          0 :                 brz_config_set_tmp_dir(mph, tmp_dir);
     375                 :            :         }
     376                 :          0 : }
     377                 :            : 
     378                 :            : 
     379                 :          0 : void cmph_config_set_mphf_fd(cmph_config_t *mph, FILE *mphf_fd)
     380                 :            : {
     381         [ #  # ]:          0 :         if (mph->algo == CMPH_BRZ) 
     382                 :            :         {
     383                 :          0 :                 brz_config_set_mphf_fd(mph, mphf_fd);
     384                 :            :         }
     385                 :          0 : }
     386                 :            : 
     387                 :          0 : void cmph_config_set_b(cmph_config_t *mph, cmph_uint32 b)
     388                 :            : {
     389         [ #  # ]:          0 :         if (mph->algo == CMPH_BRZ) 
     390                 :            :         {
     391                 :          0 :                 brz_config_set_b(mph, b);
     392                 :            :         }
     393         [ #  # ]:          0 :         else if (mph->algo == CMPH_BDZ) 
     394                 :            :         {
     395                 :          0 :                 bdz_config_set_b(mph, b);
     396                 :            :         }
     397         [ #  # ]:          0 :         else if (mph->algo == CMPH_CHD_PH) 
     398                 :            :         {
     399                 :          0 :                 chd_ph_config_set_b(mph, b);
     400                 :            :         }
     401         [ #  # ]:          0 :         else if (mph->algo == CMPH_CHD) 
     402                 :            :         {
     403                 :          0 :                 chd_config_set_b(mph, b);
     404                 :            :         }
     405                 :          0 : }
     406                 :            : 
     407                 :          0 : void cmph_config_set_keys_per_bin(cmph_config_t *mph, cmph_uint32 keys_per_bin)
     408                 :            : {
     409         [ #  # ]:          0 :         if (mph->algo == CMPH_CHD_PH) 
     410                 :            :         {
     411                 :          0 :                 chd_ph_config_set_keys_per_bin(mph, keys_per_bin);
     412                 :            :         }
     413         [ #  # ]:          0 :         else if (mph->algo == CMPH_CHD) 
     414                 :            :         {
     415                 :          0 :                 chd_config_set_keys_per_bin(mph, keys_per_bin);
     416                 :            :         }
     417                 :          0 : }
     418                 :            : 
     419                 :          0 : void cmph_config_set_memory_availability(cmph_config_t *mph, cmph_uint32 memory_availability)
     420                 :            : {
     421         [ #  # ]:          0 :         if (mph->algo == CMPH_BRZ) 
     422                 :            :         {
     423                 :          0 :                 brz_config_set_memory_availability(mph, memory_availability);
     424                 :            :         }
     425                 :          0 : }
     426                 :            : 
     427                 :          3 : void cmph_config_destroy(cmph_config_t *mph)
     428                 :            : {
     429         [ +  - ]:          3 :         if(mph)
     430                 :            :         {
     431                 :            :                 DEBUGP("Destroying mph with algo %s\n", cmph_names[mph->algo]);
     432   [ -  -  -  -  :          3 :                 switch (mph->algo)
          -  +  -  -  -  
                      - ]
     433                 :            :                 {
     434                 :          0 :                         case CMPH_CHM:
     435                 :          0 :                                 chm_config_destroy(mph);
     436                 :          0 :                                 break;
     437                 :          0 :                         case CMPH_BMZ: /* included -- Fabiano */
     438                 :          0 :                                 bmz_config_destroy(mph);
     439                 :          0 :                                 break;
     440                 :          0 :                         case CMPH_BMZ8: /* included -- Fabiano */
     441                 :          0 :                                 bmz8_config_destroy(mph);
     442                 :          0 :                                 break;
     443                 :          0 :                         case CMPH_BRZ: /* included -- Fabiano */
     444                 :          0 :                                 brz_config_destroy(mph);
     445                 :          0 :                                 break;
     446                 :          0 :                         case CMPH_FCH: /* included -- Fabiano */
     447                 :          0 :                                 fch_config_destroy(mph);
     448                 :          0 :                                 break;
     449                 :          3 :                         case CMPH_BDZ: /* included -- Fabiano */
     450                 :          3 :                                 bdz_config_destroy(mph);
     451                 :          3 :                                 break;
     452                 :          0 :                         case CMPH_BDZ_PH: /* included -- Fabiano */
     453                 :          0 :                                 bdz_ph_config_destroy(mph);
     454                 :          0 :                                 break;
     455                 :          0 :                         case CMPH_CHD_PH: /* included -- Fabiano */
     456                 :          0 :                                 chd_ph_config_destroy(mph);
     457                 :          0 :                                 break;
     458                 :          0 :                         case CMPH_CHD: /* included -- Fabiano */
     459                 :          0 :                                 chd_config_destroy(mph);
     460                 :          0 :                                 break;
     461                 :          0 :                         default:
     462                 :          0 :                                 assert(0);
     463                 :            :                 }
     464                 :          3 :                 __config_destroy(mph);
     465                 :            :         }
     466                 :          3 : }
     467                 :            : 
     468                 :          0 : void cmph_config_set_verbosity(cmph_config_t *mph, cmph_uint32 verbosity)
     469                 :            : {
     470                 :          0 :         mph->verbosity = verbosity;
     471                 :          0 : }
     472                 :            : 
     473                 :          0 : void cmph_config_set_hashfuncs(cmph_config_t *mph, CMPH_HASH *hashfuncs)
     474                 :            : {
     475   [ #  #  #  #  :          0 :         switch (mph->algo)
          #  #  #  #  #  
                      # ]
     476                 :            :         {
     477                 :          0 :                 case CMPH_CHM:
     478                 :          0 :                         chm_config_set_hashfuncs(mph, hashfuncs);
     479                 :          0 :                         break;
     480                 :          0 :                 case CMPH_BMZ: /* included -- Fabiano */
     481                 :          0 :                         bmz_config_set_hashfuncs(mph, hashfuncs);
     482                 :          0 :                         break;
     483                 :          0 :                 case CMPH_BMZ8: /* included -- Fabiano */
     484                 :          0 :                         bmz8_config_set_hashfuncs(mph, hashfuncs);
     485                 :          0 :                         break;
     486                 :          0 :                 case CMPH_BRZ: /* included -- Fabiano */
     487                 :          0 :                         brz_config_set_hashfuncs(mph, hashfuncs);
     488                 :          0 :                         break;
     489                 :          0 :                 case CMPH_FCH: /* included -- Fabiano */
     490                 :          0 :                         fch_config_set_hashfuncs(mph, hashfuncs);
     491                 :          0 :                         break;
     492                 :          0 :                 case CMPH_BDZ: /* included -- Fabiano */
     493                 :          0 :                         bdz_config_set_hashfuncs(mph, hashfuncs);
     494                 :          0 :                         break;
     495                 :          0 :                 case CMPH_BDZ_PH: /* included -- Fabiano */
     496                 :          0 :                         bdz_ph_config_set_hashfuncs(mph, hashfuncs);
     497                 :          0 :                         break;
     498                 :          0 :                 case CMPH_CHD_PH: /* included -- Fabiano */
     499                 :          0 :                         chd_ph_config_set_hashfuncs(mph, hashfuncs);
     500                 :          0 :                         break;
     501                 :          0 :                 case CMPH_CHD: /* included -- Fabiano */
     502                 :          0 :                         chd_config_set_hashfuncs(mph, hashfuncs);
     503                 :          0 :                         break;
     504                 :          0 :                 default:
     505                 :          0 :                         break;
     506                 :            :         }
     507                 :          0 :         return;
     508                 :            : }
     509                 :          0 : void cmph_config_set_graphsize(cmph_config_t *mph, double c)
     510                 :            : {
     511                 :          0 :         mph->c = c;
     512                 :          0 :         return;
     513                 :            : }
     514                 :            : 
     515                 :          3 : cmph_t *cmph_new(cmph_config_t *mph)
     516                 :            : {
     517                 :          3 :         cmph_t *mphf = NULL;
     518                 :          3 :         double c = mph->c;
     519                 :            : 
     520                 :            :         DEBUGP("Creating mph with algorithm %s\n", cmph_names[mph->algo]);
     521   [ -  -  -  -  :          3 :         switch (mph->algo)   
          -  +  -  -  -  
                      - ]
     522                 :            :         {
     523                 :          0 :                 case CMPH_CHM:
     524                 :            :                         DEBUGP("Creating chm hash\n");
     525                 :          0 :                         mphf = chm_new(mph, c);
     526                 :          0 :                         break;
     527                 :          0 :                 case CMPH_BMZ: /* included -- Fabiano */
     528                 :            :                         DEBUGP("Creating bmz hash\n");
     529                 :          0 :                         mphf = bmz_new(mph, c);
     530                 :          0 :                         break;
     531                 :          0 :                 case CMPH_BMZ8: /* included -- Fabiano */
     532                 :            :                         DEBUGP("Creating bmz8 hash\n");
     533                 :          0 :                         mphf = bmz8_new(mph, c);
     534                 :          0 :                         break;
     535                 :          0 :                 case CMPH_BRZ: /* included -- Fabiano */
     536                 :            :                         DEBUGP("Creating brz hash\n");
     537         [ #  # ]:          0 :                         if (c >= 2.0) brz_config_set_algo(mph, CMPH_FCH);
     538                 :          0 :                         else brz_config_set_algo(mph, CMPH_BMZ8);
     539                 :          0 :                         mphf = brz_new(mph, c);
     540                 :          0 :                         break;
     541                 :          0 :                 case CMPH_FCH: /* included -- Fabiano */
     542                 :            :                         DEBUGP("Creating fch hash\n");
     543                 :          0 :                         mphf = fch_new(mph, c);
     544                 :          0 :                         break;
     545                 :          3 :                 case CMPH_BDZ: /* included -- Fabiano */
     546                 :            :                         DEBUGP("Creating bdz hash\n");
     547                 :          3 :                         mphf = bdz_new(mph, c);
     548                 :          3 :                         break;
     549                 :          0 :                 case CMPH_BDZ_PH: /* included -- Fabiano */
     550                 :            :                         DEBUGP("Creating bdz_ph hash\n");
     551                 :          0 :                         mphf = bdz_ph_new(mph, c);
     552                 :          0 :                         break;
     553                 :          0 :                 case CMPH_CHD_PH: /* included -- Fabiano */
     554                 :            :                         DEBUGP("Creating chd_ph hash\n");
     555                 :          0 :                         mphf = chd_ph_new(mph, c);
     556                 :          0 :                         break;
     557                 :          0 :                 case CMPH_CHD: /* included -- Fabiano */
     558                 :            :                         DEBUGP("Creating chd hash\n");
     559                 :          0 :                         mphf = chd_new(mph, c);
     560                 :          0 :                         break;
     561                 :          0 :                 default:
     562                 :          0 :                         assert(0);
     563                 :            :         }
     564                 :          3 :         return mphf;
     565                 :            : }
     566                 :            : 
     567                 :          0 : int cmph_dump(cmph_t *mphf, FILE *f)
     568                 :            : {
     569   [ #  #  #  #  :          0 :         switch (mphf->algo)
          #  #  #  #  #  
                      # ]
     570                 :            :         {
     571                 :          0 :                 case CMPH_CHM:
     572                 :          0 :                         return chm_dump(mphf, f);
     573                 :          0 :                 case CMPH_BMZ: /* included -- Fabiano */
     574                 :          0 :                         return bmz_dump(mphf, f);
     575                 :          0 :                 case CMPH_BMZ8: /* included -- Fabiano */
     576                 :          0 :                         return bmz8_dump(mphf, f);
     577                 :          0 :                 case CMPH_BRZ: /* included -- Fabiano */
     578                 :          0 :                         return brz_dump(mphf, f);
     579                 :          0 :                 case CMPH_FCH: /* included -- Fabiano */
     580                 :          0 :                         return fch_dump(mphf, f);
     581                 :          0 :                 case CMPH_BDZ: /* included -- Fabiano */
     582                 :          0 :                         return bdz_dump(mphf, f);
     583                 :          0 :                 case CMPH_BDZ_PH: /* included -- Fabiano */
     584                 :          0 :                         return bdz_ph_dump(mphf, f);
     585                 :          0 :                 case CMPH_CHD_PH: /* included -- Fabiano */
     586                 :          0 :                         return chd_ph_dump(mphf, f);
     587                 :          0 :                 case CMPH_CHD: /* included -- Fabiano */
     588                 :          0 :                         return chd_dump(mphf, f);
     589                 :          0 :                 default:
     590                 :          0 :                         assert(0);
     591                 :            :         }
     592                 :            :         assert(0);
     593                 :            :         return 0;
     594                 :            : }
     595                 :          0 : cmph_t *cmph_load(FILE *f)
     596                 :            : {
     597                 :          0 :         cmph_t *mphf = NULL;
     598                 :            :         DEBUGP("Loading mphf generic parts\n");
     599                 :          0 :         mphf =  __cmph_load(f);
     600         [ #  # ]:          0 :         if (mphf == NULL) return NULL;
     601                 :            :         DEBUGP("Loading mphf algorithm dependent parts\n");
     602                 :            : 
     603   [ #  #  #  #  :          0 :         switch (mphf->algo)
          #  #  #  #  #  
                      # ]
     604                 :            :         {
     605                 :          0 :                 case CMPH_CHM:
     606                 :          0 :                         chm_load(f, mphf);
     607                 :          0 :                         break;
     608                 :          0 :                 case CMPH_BMZ: /* included -- Fabiano */
     609                 :            :                         DEBUGP("Loading bmz algorithm dependent parts\n");
     610                 :          0 :                         bmz_load(f, mphf);
     611                 :          0 :                         break;
     612                 :          0 :                 case CMPH_BMZ8: /* included -- Fabiano */
     613                 :            :                         DEBUGP("Loading bmz8 algorithm dependent parts\n");
     614                 :          0 :                         bmz8_load(f, mphf);
     615                 :          0 :                         break;
     616                 :          0 :                 case CMPH_BRZ: /* included -- Fabiano */
     617                 :            :                         DEBUGP("Loading brz algorithm dependent parts\n");
     618                 :          0 :                         brz_load(f, mphf);
     619                 :          0 :                         break;
     620                 :          0 :                 case CMPH_FCH: /* included -- Fabiano */
     621                 :            :                         DEBUGP("Loading fch algorithm dependent parts\n");
     622                 :          0 :                         fch_load(f, mphf);
     623                 :          0 :                         break;
     624                 :          0 :                 case CMPH_BDZ: /* included -- Fabiano */
     625                 :            :                         DEBUGP("Loading bdz algorithm dependent parts\n");
     626                 :          0 :                         bdz_load(f, mphf);
     627                 :          0 :                         break;
     628                 :          0 :                 case CMPH_BDZ_PH: /* included -- Fabiano */
     629                 :            :                         DEBUGP("Loading bdz_ph algorithm dependent parts\n");
     630                 :          0 :                         bdz_ph_load(f, mphf);
     631                 :          0 :                         break;
     632                 :          0 :                 case CMPH_CHD_PH: /* included -- Fabiano */
     633                 :            :                         DEBUGP("Loading chd_ph algorithm dependent parts\n");
     634                 :          0 :                         chd_ph_load(f, mphf);
     635                 :          0 :                         break;
     636                 :          0 :                 case CMPH_CHD: /* included -- Fabiano */
     637                 :            :                         DEBUGP("Loading chd algorithm dependent parts\n");
     638                 :          0 :                         chd_load(f, mphf);
     639                 :          0 :                         break;
     640                 :          0 :                 default:
     641                 :          0 :                         assert(0);
     642                 :            :         }
     643                 :            :         DEBUGP("Loaded mphf\n");
     644                 :          0 :         return mphf;
     645                 :            : }
     646                 :            : 
     647                 :            : 
     648                 :          3 : cmph_uint32 cmph_search(cmph_t *mphf, const char *key, cmph_uint32 keylen)
     649                 :            : {
     650                 :            :         DEBUGP("mphf algorithm: %u \n", mphf->algo);
     651   [ -  -  -  -  :          3 :         switch(mphf->algo)
          -  +  -  -  -  
                      - ]
     652                 :            :         {
     653                 :          0 :                 case CMPH_CHM:
     654                 :          0 :                         return chm_search(mphf, key, keylen);
     655                 :          0 :                 case CMPH_BMZ: /* included -- Fabiano */
     656                 :            :                         DEBUGP("bmz algorithm search\n");                      
     657                 :          0 :                         return bmz_search(mphf, key, keylen);
     658                 :          0 :                 case CMPH_BMZ8: /* included -- Fabiano */
     659                 :            :                         DEBUGP("bmz8 algorithm search\n");                     
     660                 :          0 :                         return bmz8_search(mphf, key, keylen);
     661                 :          0 :                 case CMPH_BRZ: /* included -- Fabiano */
     662                 :            :                         DEBUGP("brz algorithm search\n");                      
     663                 :          0 :                         return brz_search(mphf, key, keylen);
     664                 :          0 :                 case CMPH_FCH: /* included -- Fabiano */
     665                 :            :                         DEBUGP("fch algorithm search\n");                      
     666                 :          0 :                         return fch_search(mphf, key, keylen);
     667                 :          3 :                 case CMPH_BDZ: /* included -- Fabiano */
     668                 :            :                         DEBUGP("bdz algorithm search\n");                      
     669                 :          3 :                         return bdz_search(mphf, key, keylen);
     670                 :          0 :                 case CMPH_BDZ_PH: /* included -- Fabiano */
     671                 :            :                         DEBUGP("bdz_ph algorithm search\n");                   
     672                 :          0 :                         return bdz_ph_search(mphf, key, keylen);
     673                 :          0 :                 case CMPH_CHD_PH: /* included -- Fabiano */
     674                 :            :                         DEBUGP("chd_ph algorithm search\n");                   
     675                 :          0 :                         return chd_ph_search(mphf, key, keylen);
     676                 :          0 :                 case CMPH_CHD: /* included -- Fabiano */
     677                 :            :                         DEBUGP("chd algorithm search\n");                      
     678                 :          0 :                         return chd_search(mphf, key, keylen);
     679                 :          0 :                 default:
     680                 :          0 :                         assert(0);
     681                 :            :         }
     682                 :            :         assert(0);
     683                 :            :         return 0;
     684                 :            : }
     685                 :            : 
     686                 :          5 : cmph_uint32 cmph_size(cmph_t *mphf)
     687                 :            : {
     688                 :          5 :         return mphf->size;
     689                 :            : }
     690                 :            :         
     691                 :          3 : void cmph_destroy(cmph_t *mphf)
     692                 :            : {
     693   [ -  -  -  -  :          3 :         switch(mphf->algo)
          -  +  -  -  -  
                      - ]
     694                 :            :         {
     695                 :          0 :                 case CMPH_CHM:
     696                 :          0 :                         chm_destroy(mphf);
     697                 :          0 :                         return;
     698                 :          0 :                 case CMPH_BMZ: /* included -- Fabiano */
     699                 :          0 :                         bmz_destroy(mphf);
     700                 :          0 :                         return;
     701                 :          0 :                 case CMPH_BMZ8: /* included -- Fabiano */
     702                 :          0 :                         bmz8_destroy(mphf);
     703                 :          0 :                         return;
     704                 :          0 :                 case CMPH_BRZ: /* included -- Fabiano */
     705                 :          0 :                         brz_destroy(mphf);
     706                 :          0 :                         return;
     707                 :          0 :                 case CMPH_FCH: /* included -- Fabiano */
     708                 :          0 :                         fch_destroy(mphf);
     709                 :          0 :                         return;
     710                 :          3 :                 case CMPH_BDZ: /* included -- Fabiano */
     711                 :          3 :                         bdz_destroy(mphf);
     712                 :          3 :                         return;
     713                 :          0 :                 case CMPH_BDZ_PH: /* included -- Fabiano */
     714                 :          0 :                         bdz_ph_destroy(mphf);
     715                 :          0 :                         return;
     716                 :          0 :                 case CMPH_CHD_PH: /* included -- Fabiano */
     717                 :          0 :                         chd_ph_destroy(mphf);
     718                 :          0 :                         return;
     719                 :          0 :                 case CMPH_CHD: /* included -- Fabiano */
     720                 :          0 :                         chd_destroy(mphf);
     721                 :          0 :                         return;
     722                 :          0 :                 default: 
     723                 :          0 :                         assert(0);
     724                 :            :         }
     725                 :            :         assert(0);
     726                 :            :         return;
     727                 :            : }
     728                 :            : 
     729                 :            : 
     730                 :            : /** \fn void cmph_pack(cmph_t *mphf, void *packed_mphf);
     731                 :            :  *  \brief Support the ability to pack a perfect hash function into a preallocated contiguous memory space pointed by packed_mphf.
     732                 :            :  *  \param mphf pointer to the resulting mphf
     733                 :            :  *  \param packed_mphf pointer to the contiguous memory area used to store the resulting mphf. The size of packed_mphf must be at least cmph_packed_size() 
     734                 :            :  */
     735                 :          2 : void cmph_pack(cmph_t *mphf, void *packed_mphf)
     736                 :            : {
     737                 :            :         // packing algorithm type to be used in cmph.c
     738                 :          2 :         cmph_uint32 * ptr = (cmph_uint32 *) packed_mphf; 
     739                 :          2 :         *ptr++ = mphf->algo;
     740                 :            :         DEBUGP("mphf->algo = %u\n", mphf->algo);
     741   [ -  -  -  -  :          2 :         switch(mphf->algo)
          -  +  -  -  -  
                      - ]
     742                 :            :         {
     743                 :          0 :                 case CMPH_CHM:
     744                 :          0 :                         chm_pack(mphf, ptr);
     745                 :          0 :                         break;
     746                 :          0 :                 case CMPH_BMZ: /* included -- Fabiano */
     747                 :          0 :                         bmz_pack(mphf, ptr);
     748                 :          0 :                         break;
     749                 :          0 :                 case CMPH_BMZ8: /* included -- Fabiano */
     750                 :          0 :                         bmz8_pack(mphf, ptr);
     751                 :          0 :                         break;
     752                 :          0 :                 case CMPH_BRZ: /* included -- Fabiano */
     753                 :          0 :                         brz_pack(mphf, ptr);
     754                 :          0 :                         break;
     755                 :          0 :                 case CMPH_FCH: /* included -- Fabiano */
     756                 :          0 :                         fch_pack(mphf, ptr);
     757                 :          0 :                         break;
     758                 :          2 :                 case CMPH_BDZ: /* included -- Fabiano */
     759                 :          2 :                         bdz_pack(mphf, ptr);
     760                 :          2 :                         break;
     761                 :          0 :                 case CMPH_BDZ_PH: /* included -- Fabiano */
     762                 :          0 :                         bdz_ph_pack(mphf, ptr);
     763                 :          0 :                         break;
     764                 :          0 :                 case CMPH_CHD_PH: /* included -- Fabiano */
     765                 :          0 :                         chd_ph_pack(mphf, ptr);
     766                 :          0 :                         break;
     767                 :          0 :                 case CMPH_CHD: /* included -- Fabiano */
     768                 :          0 :                         chd_pack(mphf, ptr);
     769                 :          0 :                         break;
     770                 :          0 :                 default: 
     771                 :          0 :                         assert(0);
     772                 :            :         }
     773                 :          2 :         return;
     774                 :            : }
     775                 :            : 
     776                 :            : /** \fn cmph_uint32 cmph_packed_size(cmph_t *mphf);
     777                 :            :  *  \brief Return the amount of space needed to pack mphf.
     778                 :            :  *  \param mphf pointer to a mphf
     779                 :            :  *  \return the size of the packed function or zero for failures
     780                 :            :  */ 
     781                 :          2 : cmph_uint32 cmph_packed_size(cmph_t *mphf)
     782                 :            : {
     783   [ -  -  -  -  :          2 :         switch(mphf->algo)
          -  +  -  -  -  
                      - ]
     784                 :            :         {
     785                 :          0 :                 case CMPH_CHM:
     786                 :          0 :                         return chm_packed_size(mphf);
     787                 :          0 :                 case CMPH_BMZ: /* included -- Fabiano */
     788                 :          0 :                         return bmz_packed_size(mphf);
     789                 :          0 :                 case CMPH_BMZ8: /* included -- Fabiano */
     790                 :          0 :                         return bmz8_packed_size(mphf);
     791                 :          0 :                 case CMPH_BRZ: /* included -- Fabiano */
     792                 :          0 :                         return brz_packed_size(mphf);
     793                 :          0 :                 case CMPH_FCH: /* included -- Fabiano */
     794                 :          0 :                         return fch_packed_size(mphf);
     795                 :          2 :                 case CMPH_BDZ: /* included -- Fabiano */
     796                 :          2 :                         return bdz_packed_size(mphf);
     797                 :          0 :                 case CMPH_BDZ_PH: /* included -- Fabiano */
     798                 :          0 :                         return bdz_ph_packed_size(mphf);
     799                 :          0 :                 case CMPH_CHD_PH: /* included -- Fabiano */
     800                 :          0 :                         return chd_ph_packed_size(mphf);
     801                 :          0 :                 case CMPH_CHD: /* included -- Fabiano */
     802                 :          0 :                         return chd_packed_size(mphf);
     803                 :          0 :                 default: 
     804                 :          0 :                         assert(0);
     805                 :            :         }
     806                 :            :         return 0; // FAILURE
     807                 :            : }
     808                 :            : 
     809                 :            : /** cmph_uint32 cmph_search(void *packed_mphf, const char *key, cmph_uint32 keylen);
     810                 :            :  *  \brief Use the packed mphf to do a search. 
     811                 :            :  *  \param  packed_mphf pointer to the packed mphf
     812                 :            :  *  \param key key to be hashed
     813                 :            :  *  \param keylen key length in bytes
     814                 :            :  *  \return The mphf value
     815                 :            :  */
     816                 :         68 : cmph_uint32 cmph_search_packed(void *packed_mphf, const char *key, cmph_uint32 keylen)
     817                 :            : {
     818                 :         68 :         cmph_uint32 *ptr = (cmph_uint32 *)packed_mphf;
     819                 :            : //      fprintf(stderr, "algo:%u\n", *ptr);
     820   [ -  -  -  -  :         68 :         switch(*ptr)
          -  +  -  -  -  
                      - ]
     821                 :            :         {
     822                 :          0 :                 case CMPH_CHM:
     823                 :          0 :                         return chm_search_packed(++ptr, key, keylen);
     824                 :          0 :                 case CMPH_BMZ: /* included -- Fabiano */
     825                 :          0 :                         return bmz_search_packed(++ptr, key, keylen);
     826                 :          0 :                 case CMPH_BMZ8: /* included -- Fabiano */
     827                 :          0 :                         return bmz8_search_packed(++ptr, key, keylen);
     828                 :          0 :                 case CMPH_BRZ: /* included -- Fabiano */
     829                 :          0 :                         return brz_search_packed(++ptr, key, keylen);
     830                 :          0 :                 case CMPH_FCH: /* included -- Fabiano */
     831                 :          0 :                         return fch_search_packed(++ptr, key, keylen);
     832                 :         68 :                 case CMPH_BDZ: /* included -- Fabiano */
     833                 :         68 :                         return bdz_search_packed(++ptr, key, keylen);
     834                 :          0 :                 case CMPH_BDZ_PH: /* included -- Fabiano */
     835                 :          0 :                         return bdz_ph_search_packed(++ptr, key, keylen);
     836                 :          0 :                 case CMPH_CHD_PH: /* included -- Fabiano */
     837                 :          0 :                         return chd_ph_search_packed(++ptr, key, keylen);
     838                 :          0 :                 case CMPH_CHD: /* included -- Fabiano */
     839                 :          0 :                         return chd_search_packed(++ptr, key, keylen);
     840                 :          0 :                 default: 
     841                 :          0 :                         assert(0);
     842                 :            :         }
     843                 :            :         return 0; // FAILURE
     844                 :            : }

Generated by: LCOV version 1.14