/* Index access routines */ 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "utils.h"
#include "hip_dm_c.h"
#include "ihipdmc.h" 

int load_idx_hip_dm_c (char* ifile,idx_hip_dm_c* index) 
{
/* Routine to load an index from a file. The structure for the index must
 * already exist as must the file of course
 */
   idx_hip_dm_c idx;
   FILE *fp;
   int i;

   if ( (fp=fopen(ifile,"r"))==NULL)
   {
	fprintf(stderr,"load_idx_hip_dm_c unable to open %s \n",ifile);
	return (-1);
   }
   if (index == NULL) 
   {
      fprintf (stderr,"load_idx_hip_dm_c NULL pointer for index\n");
      return (-1);
   }
   /*printf("Loading index idx_hip_dm_c from %s.. ",ifile);*/
   for (i=0;i<idx_hip_dm_c_entries;i++)
   {
	if ( (getnext_idx_hip_dm_c (fp,&index[i])) != 0)
   	{
	   fprintf(stderr,"load_idx_hip_dm_c problem with entry %d \n",i);
	   break;
	}
   /*     if  ( (i % 5000) == 0) printf("#");*/
   }
   if ( i < idx_hip_dm_c_entries) 
   {
	fprintf(stderr,"Premature end of index file \n");
   	return -1;
   }
   /* printf(" done \n"); */
   return 0;
} /* End of load_idx_hip_dm_c*/

int getnext_idx_hip_dm_c (FILE* fp, idx_hip_dm_c* entry)
{
   char buffer[idx_hip_dm_c_REC_LEN + 2];
   if (fgets((char *)&buffer,idx_hip_dm_c_REC_LEN+1,fp))
   { 
	char delimiter[3]="|\r\0";
	char *token=strtok((char *)&buffer,(char *)&delimiter); 
	read_ccdm(token,&entry->dmc_idx1);
	token=strtok(NULL,delimiter);
	strAsINT(token,&entry->dmc_idx2);
   return 0;
   }
   else
        return -1;
} /* End of getnext_idx_hip_dm_c */

long find_idx_hip_dm_c (ccdm* key)
{
   static int init=0;
   static idx_hip_dm_c index[idx_hip_dm_c_entries];
   int mid,lpos,rpos,found;
   if (init == 0)
   {
        if (load_idx_hip_dm_c("hip_dm_c.idx",(idx_hip_dm_c*)&index) !=0)
             return -1;
        init=1;
   }

   /*  Binary search */ 
   rpos = idx_hip_dm_c_entries; 
   lpos = 0;
   if (rpos > idx_hip_dm_c_entries) rpos = idx_hip_dm_c_entries;
   found = 0;
   while (!found && !(lpos==mid && rpos==mid || lpos > rpos))
   {
	mid =  (rpos + lpos) /2;
        found = index[mid].dmc_idx1.left.value == key->left.value;
     	if (key->left.value > index[mid].dmc_idx1.left.value )
	   lpos = mid+1;
	else
	   if (key->left.value < index[mid].dmc_idx1.left.value)
	      rpos = mid;
	/*printf (" %d,%d Betweeen %d,%d and %d,%d Key = %d\n",mid,index[mid].dmc_idx1.left.value,lpos,index[lpos].dmc_idx1.left.value,rpos,index[rpos].dmc_idx1.left.value,key->left.value);*/
   }
   if (found)
   {
	/*Go back to first entry with left*/
	while ( key->left.value == index[mid].dmc_idx1.left.value &&  
		mid >= 0)
	   mid--;
	mid +=1;
	/* search forward */
	while ( key->left.value == index[mid].dmc_idx1.left.value &&  
		key->right.value != index[mid].dmc_idx1.right.value && 
		mid <= idx_hip_dm_c_entries)
	   mid++;
	if ( key->right.value == index[mid].dmc_idx1.right.value &&
	      key->sign[0] == index[mid].dmc_idx1.sign[0])
	   return index[mid].dmc_idx2.value;
	else
	   return -1;
   } 
   else
   {
      return -1;
   }
   
} /* End of find_idx_hip_dm_c */


