(root)/
glib-2.79.0/
girepository/
cmph/
bdz_ph.c
       1  #include "bdz_ph.h"
       2  #include "cmph_structs.h"
       3  #include "bdz_structs_ph.h"
       4  #include "hash.h"
       5  #include "bitbool.h"
       6  
       7  #include <math.h>
       8  #include <stdlib.h>
       9  #include <stdio.h>
      10  #include <assert.h>
      11  #include <string.h>
      12  #include <errno.h>
      13  //#define DEBUG
      14  #include "debug.h"
      15  #define UNASSIGNED 3
      16  #define NULL_EDGE 0xffffffff
      17  
      18  
      19  static cmph_uint8 pow3_table[5] = {1,3,9,27,81};
      20  static cmph_uint8 lookup_table[5][256] = {
      21   {0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 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 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
      26  };
      27  
      28  typedef struct 
      29  {
      30  	cmph_uint32 vertices[3];
      31  	cmph_uint32 next_edges[3];
      32  }bdz_ph_edge_t;
      33  
      34  typedef cmph_uint32 * bdz_ph_queue_t;
      35  
      36  static void bdz_ph_alloc_queue(bdz_ph_queue_t * queuep, cmph_uint32 nedges)
      37  {
      38  	(*queuep)=malloc(nedges*sizeof(cmph_uint32));
      39  };
      40  static void bdz_ph_free_queue(bdz_ph_queue_t * queue)
      41  {
      42  	free(*queue);
      43  };
      44  
      45  typedef struct 
      46  {
      47  	cmph_uint32 nedges;
      48  	bdz_ph_edge_t * edges;
      49  	cmph_uint32 * first_edge;
      50  	cmph_uint8 * vert_degree;	
      51  }bdz_ph_graph3_t;
      52  
      53  
      54  static void bdz_ph_alloc_graph3(bdz_ph_graph3_t * graph3, cmph_uint32 nedges, cmph_uint32 nvertices)
      55  {
      56  	graph3->edges=malloc(nedges*sizeof(bdz_ph_edge_t));
      57  	graph3->first_edge=malloc(nvertices*sizeof(cmph_uint32));
      58  	graph3->vert_degree=malloc((size_t)nvertices);	
      59  };
      60  static void bdz_ph_init_graph3(bdz_ph_graph3_t * graph3, cmph_uint32 nedges, cmph_uint32 nvertices)
      61  {
      62  	memset(graph3->first_edge,0xff,nvertices*sizeof(cmph_uint32));
      63  	memset(graph3->vert_degree,0,(size_t)nvertices);
      64  	graph3->nedges=0;
      65  };
      66  static void bdz_ph_free_graph3(bdz_ph_graph3_t *graph3)
      67  {
      68  	free(graph3->edges);
      69  	free(graph3->first_edge);
      70  	free(graph3->vert_degree);
      71  };
      72  
      73  static void bdz_ph_partial_free_graph3(bdz_ph_graph3_t *graph3)
      74  {
      75  	free(graph3->first_edge);
      76  	free(graph3->vert_degree);
      77  	graph3->first_edge = NULL;
      78  	graph3->vert_degree = NULL;
      79  };
      80  
      81  static void bdz_ph_add_edge(bdz_ph_graph3_t * graph3, cmph_uint32 v0, cmph_uint32 v1, cmph_uint32 v2)
      82  {
      83  	graph3->edges[graph3->nedges].vertices[0]=v0;
      84  	graph3->edges[graph3->nedges].vertices[1]=v1;
      85  	graph3->edges[graph3->nedges].vertices[2]=v2;
      86  	graph3->edges[graph3->nedges].next_edges[0]=graph3->first_edge[v0];
      87  	graph3->edges[graph3->nedges].next_edges[1]=graph3->first_edge[v1];
      88  	graph3->edges[graph3->nedges].next_edges[2]=graph3->first_edge[v2];
      89  	graph3->first_edge[v0]=graph3->first_edge[v1]=graph3->first_edge[v2]=graph3->nedges;
      90  	graph3->vert_degree[v0]++;
      91  	graph3->vert_degree[v1]++;
      92  	graph3->vert_degree[v2]++;
      93  	graph3->nedges++;
      94  };
      95  
      96  static void bdz_ph_dump_graph(bdz_ph_graph3_t* graph3, cmph_uint32 nedges, cmph_uint32 nvertices)
      97  {
      98  	cmph_uint32 i;
      99  	for(i=0;i<nedges;i++){
     100  		printf("\nedge %d %d %d %d ",i,graph3->edges[i].vertices[0],
     101  			graph3->edges[i].vertices[1],graph3->edges[i].vertices[2]);
     102  		printf(" nexts %d %d %d",graph3->edges[i].next_edges[0],
     103  				graph3->edges[i].next_edges[1],graph3->edges[i].next_edges[2]);
     104  	};
     105  	
     106  	for(i=0;i<nvertices;i++){
     107  		printf("\nfirst for vertice %d %d ",i,graph3->first_edge[i]);
     108  	
     109  	};
     110  };
     111  
     112  static void bdz_ph_remove_edge(bdz_ph_graph3_t * graph3, cmph_uint32 curr_edge)
     113  {
     114  	cmph_uint32 i,j=0,vert,edge1,edge2;
     115  	for(i=0;i<3;i++){
     116  		vert=graph3->edges[curr_edge].vertices[i];
     117  		edge1=graph3->first_edge[vert];
     118  		edge2=NULL_EDGE;
     119  		while(edge1!=curr_edge&&edge1!=NULL_EDGE){
     120  			edge2=edge1;
     121  			if(graph3->edges[edge1].vertices[0]==vert){
     122  				j=0;
     123  			} else if(graph3->edges[edge1].vertices[1]==vert){
     124  				j=1;
     125  			} else 
     126  				j=2;
     127  			edge1=graph3->edges[edge1].next_edges[j];
     128  		};
     129  		if(edge1==NULL_EDGE){
     130  			printf("\nerror remove edge %d dump graph",curr_edge);
     131  			bdz_ph_dump_graph(graph3,graph3->nedges,graph3->nedges+graph3->nedges/4);
     132  			exit(-1);
     133  		};
     134  		
     135  		if(edge2!=NULL_EDGE){
     136  			graph3->edges[edge2].next_edges[j] = 
     137  				graph3->edges[edge1].next_edges[i];
     138  		} else 
     139  			graph3->first_edge[vert]=
     140  				graph3->edges[edge1].next_edges[i];
     141  		graph3->vert_degree[vert]--;
     142  	};
     143  	
     144  };
     145  
     146  static int bdz_ph_generate_queue(cmph_uint32 nedges, cmph_uint32 nvertices, bdz_ph_queue_t queue, bdz_ph_graph3_t* graph3)
     147  {
     148  	cmph_uint32 i,v0,v1,v2;
     149  	cmph_uint32 queue_head=0,queue_tail=0;
     150  	cmph_uint32 curr_edge;
     151  	cmph_uint32 tmp_edge;
     152  	cmph_uint8 * marked_edge =malloc((size_t)(nedges >> 3) + 1);
     153  	memset(marked_edge, 0, (size_t)(nedges >> 3) + 1);
     154  
     155  	for(i=0;i<nedges;i++){
     156  		v0=graph3->edges[i].vertices[0];
     157  		v1=graph3->edges[i].vertices[1];
     158  		v2=graph3->edges[i].vertices[2];
     159  		if(graph3->vert_degree[v0]==1 || 
     160  				graph3->vert_degree[v1]==1 ||
     161  				graph3->vert_degree[v2]==1){
     162  			if(!GETBIT(marked_edge,i)) {
     163  				queue[queue_head++]=i;
     164  				SETBIT(marked_edge,i);
     165  			}
     166  		};
     167  	};
     168  	while(queue_tail!=queue_head){
     169  		curr_edge=queue[queue_tail++];
     170  		bdz_ph_remove_edge(graph3,curr_edge);
     171  		v0=graph3->edges[curr_edge].vertices[0];
     172  		v1=graph3->edges[curr_edge].vertices[1];
     173  		v2=graph3->edges[curr_edge].vertices[2];
     174  		if(graph3->vert_degree[v0]==1 ) {
     175  			tmp_edge=graph3->first_edge[v0];
     176  			if(!GETBIT(marked_edge,tmp_edge)) {
     177  				queue[queue_head++]=tmp_edge;
     178  				SETBIT(marked_edge,tmp_edge);
     179  			};
     180  			
     181  		};
     182  		if(graph3->vert_degree[v1]==1) {
     183  			tmp_edge=graph3->first_edge[v1];
     184  			if(!GETBIT(marked_edge,tmp_edge)){
     185  				queue[queue_head++]=tmp_edge;
     186  				SETBIT(marked_edge,tmp_edge);
     187  			};
     188  			
     189  		};
     190  		if(graph3->vert_degree[v2]==1){
     191  			tmp_edge=graph3->first_edge[v2];
     192  			if(!GETBIT(marked_edge,tmp_edge)){
     193  				queue[queue_head++]=tmp_edge;
     194  				SETBIT(marked_edge,tmp_edge);
     195  			};
     196  		};
     197  	};
     198  	free(marked_edge);
     199  	return (int)queue_head - (int)nedges;/* returns 0 if successful otherwies return negative number*/
     200  };
     201  
     202  static int bdz_ph_mapping(cmph_config_t *mph, bdz_ph_graph3_t* graph3, bdz_ph_queue_t queue);
     203  static void assigning(bdz_ph_config_data_t *bdz_ph, bdz_ph_graph3_t* graph3, bdz_ph_queue_t queue);
     204  static void bdz_ph_optimization(bdz_ph_config_data_t *bdz_ph);
     205  
     206  bdz_ph_config_data_t *bdz_ph_config_new(void)
     207  {
     208  	bdz_ph_config_data_t *bdz_ph;
     209  	bdz_ph = (bdz_ph_config_data_t *)malloc(sizeof(bdz_ph_config_data_t));
     210  	assert(bdz_ph);
     211  	memset(bdz_ph, 0, sizeof(bdz_ph_config_data_t));
     212  	bdz_ph->hashfunc = CMPH_HASH_JENKINS;
     213  	bdz_ph->g = NULL;
     214  	bdz_ph->hl = NULL;
     215  	return bdz_ph;
     216  }
     217  
     218  void bdz_ph_config_destroy(cmph_config_t *mph)
     219  {
     220  	bdz_ph_config_data_t *data = (bdz_ph_config_data_t *)mph->data;
     221  	DEBUGP("Destroying algorithm dependent data\n");
     222  	free(data);
     223  }
     224  
     225  void bdz_ph_config_set_hashfuncs(cmph_config_t *mph, CMPH_HASH *hashfuncs)
     226  {
     227  	bdz_ph_config_data_t *bdz_ph = (bdz_ph_config_data_t *)mph->data;
     228  	CMPH_HASH *hashptr = hashfuncs;
     229  	cmph_uint32 i = 0;
     230  	while(*hashptr != CMPH_HASH_COUNT)
     231  	{
     232  		if (i >= 1) break; //bdz_ph only uses one linear hash function
     233  		bdz_ph->hashfunc = *hashptr;	
     234  		++i, ++hashptr;
     235  	}
     236  }
     237  
     238  cmph_t *bdz_ph_new(cmph_config_t *mph, double c)
     239  {
     240  	cmph_t *mphf = NULL;
     241  	bdz_ph_data_t *bdz_phf = NULL;
     242  	cmph_uint32 iterations;
     243  	bdz_ph_queue_t edges;
     244  	bdz_ph_graph3_t graph3;
     245  	bdz_ph_config_data_t *bdz_ph = (bdz_ph_config_data_t *)mph->data;
     246  	#ifdef CMPH_TIMING
     247  	double construction_time_begin = 0.0;
     248  	double construction_time = 0.0;
     249  	ELAPSED_TIME_IN_SECONDS(&construction_time_begin);
     250  	#endif
     251  
     252  
     253  	if (c == 0) c = 1.23; // validating restrictions over parameter c.
     254  	DEBUGP("c: %f\n", c);
     255  	bdz_ph->m = mph->key_source->nkeys;	
     256  	bdz_ph->r = (cmph_uint32)ceil((c * mph->key_source->nkeys)/3);	
     257  	if ((bdz_ph->r % 2) == 0) bdz_ph->r += 1;
     258  	bdz_ph->n = 3*bdz_ph->r;
     259  
     260  	
     261  	bdz_ph_alloc_graph3(&graph3, bdz_ph->m, bdz_ph->n);
     262  	bdz_ph_alloc_queue(&edges,bdz_ph->m);
     263  	DEBUGP("Created hypergraph\n");
     264  	
     265  	DEBUGP("m (edges): %u n (vertices): %u  r: %u c: %f \n", bdz_ph->m, bdz_ph->n, bdz_ph->r, c);
     266  
     267  	// Mapping step
     268  	iterations = 100;
     269  	if (mph->verbosity)
     270  	{
     271  		fprintf(stderr, "Entering mapping step for mph creation of %u keys with graph sized %u\n", bdz_ph->m, bdz_ph->n);
     272  	}
     273  	while(1)
     274  	{
     275  		int ok;
     276  		DEBUGP("linear hash function \n");
     277  		bdz_ph->hl = hash_state_new(bdz_ph->hashfunc, 15);
     278  
     279  		ok = bdz_ph_mapping(mph, &graph3, edges);
     280  		if (!ok)
     281  		{
     282  			--iterations;
     283  			hash_state_destroy(bdz_ph->hl);
     284  			bdz_ph->hl = NULL;
     285  			DEBUGP("%u iterations remaining\n", iterations);
     286  			if (mph->verbosity)
     287  			{
     288  				fprintf(stderr, "acyclic graph creation failure - %u iterations remaining\n", iterations);
     289  			}
     290  			if (iterations == 0) break;
     291  		} 
     292  		else break;
     293  	}
     294  	
     295  	if (iterations == 0)
     296  	{
     297  //		free(bdz_ph->g);
     298  		bdz_ph_free_queue(&edges);
     299  		bdz_ph_free_graph3(&graph3);
     300  		return NULL;
     301  	}
     302  	bdz_ph_partial_free_graph3(&graph3);
     303  	// Assigning step
     304  	if (mph->verbosity)
     305  	{
     306  		fprintf(stderr, "Entering assigning step for mph creation of %u keys with graph sized %u\n", bdz_ph->m, bdz_ph->n);
     307  	}
     308  	assigning(bdz_ph, &graph3, edges);
     309  
     310  	bdz_ph_free_queue(&edges);
     311  	bdz_ph_free_graph3(&graph3);
     312  	
     313  	if (mph->verbosity)
     314  	{
     315  		fprintf(stderr, "Starting optimization step\n");
     316  	}
     317  
     318  	bdz_ph_optimization(bdz_ph);
     319  
     320  	#ifdef CMPH_TIMING
     321  	ELAPSED_TIME_IN_SECONDS(&construction_time);
     322  	#endif
     323  	mphf = (cmph_t *)malloc(sizeof(cmph_t));
     324  	mphf->algo = mph->algo;
     325  	bdz_phf = (bdz_ph_data_t *)malloc(sizeof(bdz_ph_data_t));
     326  	bdz_phf->g = bdz_ph->g;
     327  	bdz_ph->g = NULL; //transfer memory ownership
     328  	bdz_phf->hl = bdz_ph->hl;
     329  	bdz_ph->hl = NULL; //transfer memory ownership
     330  	bdz_phf->n = bdz_ph->n;
     331  	bdz_phf->m = bdz_ph->m;
     332  	bdz_phf->r = bdz_ph->r;
     333  	mphf->data = bdz_phf;
     334  	mphf->size = bdz_ph->n;
     335  
     336  	DEBUGP("Successfully generated minimal perfect hash\n");
     337  	if (mph->verbosity)
     338  	{
     339  		fprintf(stderr, "Successfully generated minimal perfect hash function\n");
     340  	}
     341  
     342  	#ifdef CMPH_TIMING	
     343  	register cmph_uint32 space_usage = bdz_ph_packed_size(mphf)*8;
     344  	register cmph_uint32 keys_per_bucket = 1;
     345  	construction_time = construction_time - construction_time_begin;
     346  	fprintf(stdout, "%u\t%.2f\t%u\t%.4f\t%.4f\n", bdz_ph->m, bdz_ph->m/(double)bdz_ph->n, keys_per_bucket, construction_time, space_usage/(double)bdz_ph->m);
     347  	#endif	
     348  
     349  	return mphf;
     350  }
     351  
     352  		
     353  static int bdz_ph_mapping(cmph_config_t *mph, bdz_ph_graph3_t* graph3, bdz_ph_queue_t queue)
     354  {
     355  	cmph_uint32 e;
     356  	int cycles = 0;
     357  	cmph_uint32 hl[3];
     358  	
     359  	bdz_ph_config_data_t *bdz_ph = (bdz_ph_config_data_t *)mph->data;
     360  	bdz_ph_init_graph3(graph3, bdz_ph->m, bdz_ph->n);
     361  	mph->key_source->rewind(mph->key_source->data);
     362  	for (e = 0; e < mph->key_source->nkeys; ++e)
     363  	{
     364  		cmph_uint32 h0, h1, h2;
     365  		cmph_uint32 keylen;
     366  		char *key = NULL;
     367  		mph->key_source->read(mph->key_source->data, &key, &keylen);		
     368  		hash_vector(bdz_ph->hl, key, keylen, hl);
     369  		h0 = hl[0] % bdz_ph->r;
     370  		h1 = hl[1] % bdz_ph->r + bdz_ph->r;
     371  		h2 = hl[2] % bdz_ph->r + (bdz_ph->r << 1);
     372  		mph->key_source->dispose(mph->key_source->data, key, keylen);
     373  		bdz_ph_add_edge(graph3,h0,h1,h2);
     374  	}
     375  	cycles = bdz_ph_generate_queue(bdz_ph->m, bdz_ph->n, queue, graph3);	
     376  	return (cycles == 0);
     377  }
     378  
     379  static void assigning(bdz_ph_config_data_t *bdz_ph, bdz_ph_graph3_t* graph3, bdz_ph_queue_t queue)
     380  {
     381  	cmph_uint32 i;
     382  	cmph_uint32 nedges=graph3->nedges;
     383  	cmph_uint32 curr_edge;
     384  	cmph_uint32 v0,v1,v2;
     385  	cmph_uint8 * marked_vertices =malloc((size_t)(bdz_ph->n >> 3) + 1);
     386  	cmph_uint32 sizeg = (cmph_uint32)ceil(bdz_ph->n/4.0);
     387  	bdz_ph->g = (cmph_uint8 *)calloc((size_t)sizeg, sizeof(cmph_uint8));	
     388  	memset(marked_vertices, 0, (size_t)(bdz_ph->n >> 3) + 1);
     389  	//memset(bdz_ph->g, 0xff, sizeg);
     390  
     391  	for(i=nedges-1;i+1>=1;i--){
     392  		curr_edge=queue[i];
     393  		v0=graph3->edges[curr_edge].vertices[0];
     394  		v1=graph3->edges[curr_edge].vertices[1];
     395  		v2=graph3->edges[curr_edge].vertices[2];
     396  		DEBUGP("B:%u %u %u -- %u %u %u\n", v0, v1, v2, GETVALUE(bdz_ph->g, v0), GETVALUE(bdz_ph->g, v1), GETVALUE(bdz_ph->g, v2));
     397  		if(!GETBIT(marked_vertices, v0)){
     398  			if(!GETBIT(marked_vertices,v1))
     399  			{
     400  				//SETVALUE(bdz_ph->g, v1, UNASSIGNED); 
     401  				SETBIT(marked_vertices, v1);
     402  			}
     403  			if(!GETBIT(marked_vertices,v2))
     404  			{
     405  				//SETVALUE(bdz_ph->g, v2, UNASSIGNED);		
     406  				SETBIT(marked_vertices, v2);
     407  			}			
     408  			SETVALUE0(bdz_ph->g, v0, (6-(GETVALUE(bdz_ph->g, v1) + GETVALUE(bdz_ph->g,v2)))%3);
     409  			SETBIT(marked_vertices, v0);
     410  		} else if(!GETBIT(marked_vertices, v1)) {
     411  			if(!GETBIT(marked_vertices, v2))
     412  			{
     413  				//SETVALUE(bdz_ph->g, v2, UNASSIGNED);
     414  				SETBIT(marked_vertices, v2);
     415  			}
     416  			SETVALUE0(bdz_ph->g, v1, (7 - (GETVALUE(bdz_ph->g, v0)+GETVALUE(bdz_ph->g, v2)))%3);
     417  			SETBIT(marked_vertices, v1);
     418  		}else {
     419  			SETVALUE0(bdz_ph->g, v2, (8-(GETVALUE(bdz_ph->g,v0)+GETVALUE(bdz_ph->g, v1)))%3);
     420  			SETBIT(marked_vertices, v2);
     421  		}		
     422  		DEBUGP("A:%u %u %u -- %u %u %u\n", v0, v1, v2, GETVALUE(bdz_ph->g, v0), GETVALUE(bdz_ph->g, v1), GETVALUE(bdz_ph->g, v2));
     423  	};
     424  	free(marked_vertices);
     425  }
     426  
     427  static void bdz_ph_optimization(bdz_ph_config_data_t *bdz_ph)
     428  {
     429  	cmph_uint32 i;
     430  	cmph_uint8 byte = 0;
     431  	cmph_uint32 sizeg = (cmph_uint32)ceil(bdz_ph->n/5.0);
     432  	cmph_uint8 * new_g = (cmph_uint8 *)calloc((size_t)sizeg, sizeof(cmph_uint8));	
     433  	cmph_uint8 value;
     434  	cmph_uint32 idx;
     435  	for(i = 0; i < bdz_ph->n; i++) 
     436  	{	
     437              idx = i/5;
     438              byte = new_g[idx];
     439              value = GETVALUE(bdz_ph->g, i);
     440              byte = (cmph_uint8) (byte + value*pow3_table[i%5U]);
     441              new_g[idx] = byte;
     442          }
     443  	free(bdz_ph->g);
     444  	bdz_ph->g = new_g;
     445  }
     446  
     447  
     448  int bdz_ph_dump(cmph_t *mphf, FILE *fd)
     449  {
     450  	char *buf = NULL;
     451  	cmph_uint32 buflen;
     452  	cmph_uint32 sizeg = 0;
     453  	register size_t nbytes;
     454  	bdz_ph_data_t *data = (bdz_ph_data_t *)mphf->data;
     455  #ifdef DEBUG
     456  	cmph_uint32 i;
     457  #endif
     458  
     459  	__cmph_dump(mphf, fd);
     460  
     461  	hash_state_dump(data->hl, &buf, &buflen);
     462  	DEBUGP("Dumping hash state with %u bytes to disk\n", buflen);
     463  	nbytes = fwrite(&buflen, sizeof(cmph_uint32), (size_t)1, fd);
     464  	nbytes = fwrite(buf, (size_t)buflen, (size_t)1, fd);
     465  	free(buf);
     466  
     467  	nbytes = fwrite(&(data->n), sizeof(cmph_uint32), (size_t)1, fd);
     468  	nbytes = fwrite(&(data->m), sizeof(cmph_uint32), (size_t)1, fd);
     469  	nbytes = fwrite(&(data->r), sizeof(cmph_uint32), (size_t)1, fd);
     470  	sizeg = (cmph_uint32)ceil(data->n/5.0);	
     471  	nbytes = fwrite(data->g, sizeof(cmph_uint8)*sizeg, (size_t)1, fd);
     472  
     473  	if (nbytes == 0 && ferror(fd)) {
     474            fprintf(stderr, "ERROR: %s\n", strerror(errno));
     475            return 0;
     476          }
     477  	#ifdef DEBUG
     478  	fprintf(stderr, "G: ");
     479  	for (i = 0; i < data->n; ++i) fprintf(stderr, "%u ", GETVALUE(data->g, i));
     480  	fprintf(stderr, "\n");
     481  	#endif
     482  	return 1;
     483  }
     484  
     485  void bdz_ph_load(FILE *f, cmph_t *mphf)
     486  {
     487  	char *buf = NULL;
     488  	cmph_uint32 buflen;
     489  	cmph_uint32 sizeg = 0;
     490  	register size_t nbytes;
     491  	bdz_ph_data_t *bdz_ph = (bdz_ph_data_t *)malloc(sizeof(bdz_ph_data_t));
     492  
     493  	DEBUGP("Loading bdz_ph mphf\n");
     494  	mphf->data = bdz_ph;
     495  
     496  	nbytes = fread(&buflen, sizeof(cmph_uint32), (size_t)1, f);
     497  	DEBUGP("Hash state has %u bytes\n", buflen);
     498  	buf = (char *)malloc((size_t)buflen);
     499  	nbytes = fread(buf, (size_t)buflen, (size_t)1, f);
     500  	bdz_ph->hl = hash_state_load(buf, buflen);
     501  	free(buf);
     502  	
     503  
     504  	DEBUGP("Reading m and n\n");
     505  	nbytes = fread(&(bdz_ph->n), sizeof(cmph_uint32), (size_t)1, f);	
     506  	nbytes = fread(&(bdz_ph->m), sizeof(cmph_uint32), (size_t)1, f);	
     507  	nbytes = fread(&(bdz_ph->r), sizeof(cmph_uint32), (size_t)1, f);	
     508  	sizeg = (cmph_uint32)ceil(bdz_ph->n/5.0);
     509  	bdz_ph->g = (cmph_uint8 *)calloc((size_t)sizeg, sizeof(cmph_uint8));
     510  	nbytes = fread(bdz_ph->g, sizeg*sizeof(cmph_uint8), (size_t)1, f);
     511  
     512  	if (nbytes == 0 && ferror(f)) {
     513            fprintf(stderr, "ERROR: %s\n", strerror(errno));
     514          }
     515  	return;
     516  }
     517  		
     518  
     519  cmph_uint32 bdz_ph_search(cmph_t *mphf, const char *key, cmph_uint32 keylen)
     520  {
     521  	register bdz_ph_data_t *bdz_ph = mphf->data;
     522  	cmph_uint32 hl[3];
     523  	register cmph_uint8 byte0, byte1, byte2;
     524  	register cmph_uint32 vertex;
     525  
     526  	hash_vector(bdz_ph->hl, key, keylen,hl);
     527  	hl[0] = hl[0] % bdz_ph->r;
     528  	hl[1] = hl[1] % bdz_ph->r + bdz_ph->r;
     529  	hl[2] = hl[2] % bdz_ph->r + (bdz_ph->r << 1);
     530  
     531  	byte0 = bdz_ph->g[hl[0]/5];
     532  	byte1 = bdz_ph->g[hl[1]/5];
     533  	byte2 = bdz_ph->g[hl[2]/5];
     534  	
     535  	byte0 = lookup_table[hl[0]%5U][byte0];
     536  	byte1 = lookup_table[hl[1]%5U][byte1];
     537  	byte2 = lookup_table[hl[2]%5U][byte2];
     538  	vertex = hl[(byte0 + byte1 + byte2)%3];
     539  		
     540  	return vertex;
     541  }
     542  
     543  
     544  void bdz_ph_destroy(cmph_t *mphf)
     545  {
     546  	bdz_ph_data_t *data = (bdz_ph_data_t *)mphf->data;
     547  	free(data->g);	
     548  	hash_state_destroy(data->hl);
     549  	free(data);
     550  	free(mphf);
     551  }
     552  
     553  /** \fn void bdz_ph_pack(cmph_t *mphf, void *packed_mphf);
     554   *  \brief Support the ability to pack a perfect hash function into a preallocated contiguous memory space pointed by packed_mphf.
     555   *  \param mphf pointer to the resulting mphf
     556   *  \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() 
     557   */
     558  void bdz_ph_pack(cmph_t *mphf, void *packed_mphf)
     559  {
     560  	bdz_ph_data_t *data = (bdz_ph_data_t *)mphf->data;
     561  	cmph_uint8 * ptr = packed_mphf;
     562  	cmph_uint32 sizeg;
     563  
     564  	// packing hl type
     565  	CMPH_HASH hl_type = hash_get_type(data->hl);
     566  	*((cmph_uint32 *) ptr) = hl_type;
     567  	ptr += sizeof(cmph_uint32);
     568  
     569  	// packing hl
     570  	hash_state_pack(data->hl, ptr);
     571  	ptr += hash_state_packed_size(hl_type);
     572  
     573  	// packing r
     574  	*((cmph_uint32 *) ptr) = data->r;
     575  	ptr += sizeof(data->r);
     576  
     577  	// packing g
     578  	sizeg = (cmph_uint32)ceil(data->n/5.0);
     579  	memcpy(ptr, data->g,  sizeof(cmph_uint8)*sizeg);
     580  }
     581  
     582  /** \fn cmph_uint32 bdz_ph_packed_size(cmph_t *mphf);
     583   *  \brief Return the amount of space needed to pack mphf.
     584   *  \param mphf pointer to a mphf
     585   *  \return the size of the packed function or zero for failures
     586   */ 
     587  cmph_uint32 bdz_ph_packed_size(cmph_t *mphf)
     588  {
     589  	bdz_ph_data_t *data = (bdz_ph_data_t *)mphf->data;
     590  	CMPH_HASH hl_type = hash_get_type(data->hl); 
     591  	cmph_uint32 sizeg = (cmph_uint32)ceil(data->n/5.0);
     592  	return (cmph_uint32) (sizeof(CMPH_ALGO) + hash_state_packed_size(hl_type) + 2*sizeof(cmph_uint32) + sizeof(cmph_uint8)*sizeg);
     593  }
     594  
     595  /** cmph_uint32 bdz_ph_search(void *packed_mphf, const char *key, cmph_uint32 keylen);
     596   *  \brief Use the packed mphf to do a search. 
     597   *  \param  packed_mphf pointer to the packed mphf
     598   *  \param key key to be hashed
     599   *  \param keylen key legth in bytes
     600   *  \return The mphf value
     601   */
     602  cmph_uint32 bdz_ph_search_packed(void *packed_mphf, const char *key, cmph_uint32 keylen)
     603  {
     604  	
     605  	register CMPH_HASH hl_type  = *(cmph_uint32 *)packed_mphf;
     606  	register cmph_uint8 *hl_ptr = (cmph_uint8 *)(packed_mphf) + 4;
     607  	
     608  	register cmph_uint8 * ptr = hl_ptr + hash_state_packed_size(hl_type);
     609  
     610  	register cmph_uint32 r = *((cmph_uint32*) ptr);
     611  	register cmph_uint8 * g = ptr + 4;
     612  	
     613  	cmph_uint32 hl[3];
     614  	register cmph_uint8 byte0, byte1, byte2;
     615  	register cmph_uint32 vertex;
     616  
     617  	hash_vector_packed(hl_ptr, hl_type, key, keylen, hl);
     618  	
     619  	hl[0] = hl[0] % r;
     620  	hl[1] = hl[1] % r + r;
     621  	hl[2] = hl[2] % r + (r << 1);
     622  
     623  	byte0 = g[hl[0]/5];
     624  	byte1 = g[hl[1]/5];
     625  	byte2 = g[hl[2]/5];
     626  	
     627  	byte0 = lookup_table[hl[0]%5][byte0];
     628  	byte1 = lookup_table[hl[1]%5][byte1];
     629  	byte2 = lookup_table[hl[2]%5][byte2];
     630  	vertex = hl[(byte0 + byte1 + byte2)%3];
     631  		
     632  	return vertex;
     633  }