| 1 |
|
| 2 |
|
| 3 |
#include <stdint.h> |
| 4 |
#include "fintervaldb.h" |
| 5 |
|
| 6 |
int C_int_max=INT_MAX; /* KLUDGE TO LET PYREX CODE ACCESS VALUE OF INT_MAX MACRO */ |
| 7 |
|
| 8 |
|
| 9 |
int imstart_qsort_cmp(const void *void_a,const void *void_b) |
| 10 |
{ /* STRAIGHTFORWARD COMPARISON OF SIGNED start VALUES, LONGER INTERVALS 1ST */ |
| 11 |
IntervalMap *a=(IntervalMap *)void_a,*b=(IntervalMap *)void_b; |
| 12 |
if (a->start<b->start) |
| 13 |
return -1; |
| 14 |
else if (a->start>b->start) |
| 15 |
return 1; |
| 16 |
else if (a->end>b->end) /* SAME START: PUT LONGER INTERVAL 1ST */ |
| 17 |
return -1; |
| 18 |
else if (a->end<b->end) /* CONTAINED INTERVAL SHOULD FOLLOW LARGER INTERVAL*/ |
| 19 |
return 1; |
| 20 |
else |
| 21 |
return 0; |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#ifdef MERGE_INTERVAL_ORIENTATIONS |
| 28 |
int im_qsort_cmp(const void *void_a,const void *void_b) |
| 29 |
{ /* MERGE FORWARD AND REVERSE INTERVALS AS IF THEY WERE ALL IN FORWARD ORI */ |
| 30 |
int a_start,a_end,b_start,b_end; |
| 31 |
IntervalMap *a=(IntervalMap *)void_a,*b=(IntervalMap *)void_b; |
| 32 |
SET_INTERVAL_POSITIVE(*a,a_start,a_end); |
| 33 |
SET_INTERVAL_POSITIVE(*b,b_start,b_end); |
| 34 |
if (a_start<b_start) |
| 35 |
return -1; |
| 36 |
else if (a_start>b_start) |
| 37 |
return 1; |
| 38 |
else if (a_end>b_end) /* SAME START: PUT LONGER INTERVAL 1ST */ |
| 39 |
return -1; |
| 40 |
else if (a_end<b_end) /* CONTAINED INTERVAL SHOULD FOLLOW LARGER INTERVAL*/ |
| 41 |
return 1; |
| 42 |
else |
| 43 |
return 0; |
| 44 |
} |
| 45 |
#endif |
| 46 |
|
| 47 |
int sublist_qsort_cmp(const void *void_a,const void *void_b) |
| 48 |
{ /* SORT IN SUBLIST ORDER, SECONDARILY BY start */ |
| 49 |
IntervalMap *a=(IntervalMap *)void_a,*b=(IntervalMap *)void_b; |
| 50 |
if (a->sublist<b->sublist) |
| 51 |
return -1; |
| 52 |
else if (a->sublist>b->sublist) |
| 53 |
return 1; |
| 54 |
else if (START_POSITIVE(*a) < START_POSITIVE(*b)) |
| 55 |
return -1; |
| 56 |
else if (START_POSITIVE(*a) > START_POSITIVE(*b)) |
| 57 |
return 1; |
| 58 |
else |
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
/* int target_qsort_cmp(const void *void_a,const void *void_b) */ |
| 65 |
/* { /\* SORT IN target_id ORDER, SECONDARILY BY target_start *\/ */ |
| 66 |
/* IntervalMap *a=(IntervalMap *)void_a,*b=(IntervalMap *)void_b; */ |
| 67 |
/* if (a->target_id<b->target_id) */ |
| 68 |
/* return -1; */ |
| 69 |
/* else if (a->target_id>b->target_id) */ |
| 70 |
/* return 1; */ |
| 71 |
/* else if (a->target_start < b->target_start) */ |
| 72 |
/* return -1; */ |
| 73 |
/* else if (a->target_start > b->target_start) */ |
| 74 |
/* return 1; */ |
| 75 |
/* else */ |
| 76 |
/* return 0; */ |
| 77 |
/* } */ |
| 78 |
|
| 79 |
|
| 80 |
SublistHeader *build_nested_list_inplace(IntervalMap im[],int n, |
| 81 |
int *p_n,int *p_nlists) |
| 82 |
{ |
| 83 |
int i=0,parent,nlists=1,isublist=0,total=0,temp=0; |
| 84 |
SublistHeader *subheader=NULL; |
| 85 |
|
| 86 |
#ifdef ALL_POSITIVE_ORIENTATION |
| 87 |
reorient_intervals(n,im,1); /* FORCE ALL INTERVALS INTO POSITIVE ORI */ |
| 88 |
#endif |
| 89 |
#ifdef MERGE_INTERVAL_ORIENTATIONS |
| 90 |
qsort(im,n,sizeof(IntervalMap),im_qsort_cmp); /* SORT BY start, CONTAINMENT */ |
| 91 |
#else |
| 92 |
qsort(im,n,sizeof(IntervalMap),imstart_qsort_cmp); /* SORT BY start, CONTAINMENT */ |
| 93 |
#endif |
| 94 |
nlists=1; |
| 95 |
for(i=1;i<n;++i){ |
| 96 |
if(!(END_POSITIVE(im[i])>END_POSITIVE(im[i-1]) /* i NOT CONTAINED */ |
| 97 |
|| (END_POSITIVE(im[i])==END_POSITIVE(im[i-1]) /* SAME INTERVAL! */ |
| 98 |
&& START_POSITIVE(im[i])==START_POSITIVE(im[i-1])))){ |
| 99 |
nlists++; |
| 100 |
/* printf("%d (%d,%d) -> (%d,%d) %d\n", nlists, im[i-1].start, */ |
| 101 |
/* im[i-1].end, im[i].start,im[i].end,i); */ |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/* printf("%d lists?!\n", nlists); */ |
| 106 |
*p_nlists=nlists-1; |
| 107 |
|
| 108 |
if(nlists==1){ |
| 109 |
*p_n=n; |
| 110 |
CALLOC(subheader,1,SublistHeader); /* RETURN A DUMMY ARRAY, SINCE NULL RETURN IS ERROR CODE */ |
| 111 |
return subheader; |
| 112 |
} |
| 113 |
|
| 114 |
CALLOC(subheader,nlists+1,SublistHeader); /* SUBLIST HEADER INDEX */ |
| 115 |
|
| 116 |
im[0].sublist=0; |
| 117 |
subheader[0].start= -1; |
| 118 |
subheader[0].len=1; |
| 119 |
parent=0; |
| 120 |
nlists=1; |
| 121 |
isublist=1; |
| 122 |
for(i=1;i<n;){ |
| 123 |
if(isublist && (END_POSITIVE(im[i])>END_POSITIVE(im[parent]) /* i NOT CONTAINED */ |
| 124 |
|| (END_POSITIVE(im[i])==END_POSITIVE(im[parent]) /* SAME INTERVAL! */ |
| 125 |
&& START_POSITIVE(im[i])==START_POSITIVE(im[parent])))){ |
| 126 |
subheader[isublist].start=subheader[im[parent].sublist].len-1; /* RECORD PARENT RELATIVE POSITION */ |
| 127 |
isublist=im[parent].sublist; |
| 128 |
parent=subheader[im[parent].sublist].start; |
| 129 |
} |
| 130 |
else{ |
| 131 |
if(subheader[isublist].len==0){ |
| 132 |
nlists++; |
| 133 |
} |
| 134 |
subheader[isublist].len++; |
| 135 |
im[i].sublist=isublist; |
| 136 |
parent=i; |
| 137 |
isublist=nlists; |
| 138 |
subheader[isublist].start=parent; |
| 139 |
i++; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
while(isublist>0){ /* pop remaining stack */ |
| 144 |
subheader[isublist].start=subheader[im[parent].sublist].len-1; /* RECORD PARENT RELATIVE POSITION */ |
| 145 |
isublist=im[parent].sublist; |
| 146 |
parent=subheader[im[parent].sublist].start; |
| 147 |
} |
| 148 |
|
| 149 |
*p_n=subheader[0].len; |
| 150 |
|
| 151 |
total=0; |
| 152 |
for(i=0;i<nlists+1;++i){ |
| 153 |
temp=subheader[i].len; |
| 154 |
subheader[i].len=total; |
| 155 |
total+=temp; |
| 156 |
}; |
| 157 |
|
| 158 |
/* SUBHEADER.LEN IS NOW START OF THE SUBLIST */ |
| 159 |
|
| 160 |
for(i=1;i<n;i+=1){ |
| 161 |
if(im[i].sublist>im[i-1].sublist){ |
| 162 |
subheader[im[i].sublist].start+=subheader[im[i-1].sublist].len; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/* SUBHEADER.START IS NOW ABS POSITION OF PARENT */ |
| 167 |
|
| 168 |
qsort(im,n,sizeof(IntervalMap),sublist_qsort_cmp); |
| 169 |
/* AT THIS POINT SUBLISTS ARE GROUPED TOGETHER, READY TO PACK */ |
| 170 |
|
| 171 |
isublist=0; |
| 172 |
subheader[0].start=0; |
| 173 |
subheader[0].len=0; |
| 174 |
for(i=0;i<n;++i){ |
| 175 |
if(im[i].sublist>isublist){ |
| 176 |
/* printf("Entering sublist %d (%d,%d)\n", im[i].sublist, im[i].start,im[i].end); */ |
| 177 |
isublist=im[i].sublist; |
| 178 |
parent=subheader[isublist].start; |
| 179 |
/* printf("Parent (%d,%d) is at %d, list start is at %d\n", */ |
| 180 |
/* im[parent].start, im[parent].end, subheader[isublist].start,i); */ |
| 181 |
im[parent].sublist=isublist-1; |
| 182 |
subheader[isublist].len=0; |
| 183 |
subheader[isublist].start=i; |
| 184 |
} |
| 185 |
subheader[isublist].len++; |
| 186 |
im[i].sublist= -1; |
| 187 |
} |
| 188 |
|
| 189 |
nlists--; |
| 190 |
memmove(subheader,subheader+1,nlists*sizeof(SublistHeader)); |
| 191 |
|
| 192 |
return subheader; |
| 193 |
handle_malloc_failure: |
| 194 |
/* FREE ANY MALLOCS WE PERFORMED*/ |
| 195 |
FREE(subheader); |
| 196 |
return NULL; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
SublistHeader *build_nested_list(IntervalMap im[],int n, |
| 202 |
int *p_n,int *p_nlists) |
| 203 |
{ |
| 204 |
int i=0,j,k,parent,nsub=0,nlists=0; |
| 205 |
IntervalMap *imsub=NULL; |
| 206 |
SublistHeader *subheader=NULL; |
| 207 |
|
| 208 |
#ifdef ALL_POSITIVE_ORIENTATION |
| 209 |
reorient_intervals(n,im,1); /* FORCE ALL INTERVALS INTO POSITIVE ORI */ |
| 210 |
#endif |
| 211 |
#ifdef MERGE_INTERVAL_ORIENTATIONS |
| 212 |
qsort(im,n,sizeof(IntervalMap),im_qsort_cmp); /* SORT BY start, CONTAINMENT */ |
| 213 |
#else |
| 214 |
qsort(im,n,sizeof(IntervalMap),imstart_qsort_cmp); /* SORT BY start, CONTAINMENT */ |
| 215 |
#endif |
| 216 |
while (i<n) { /* TOP LEVEL LIST SCAN */ |
| 217 |
parent=i; |
| 218 |
i=parent+1; |
| 219 |
while (i<n && parent>=0) { /* RECURSIVE ALGORITHM OF ALEX ALEKSEYENKO */ |
| 220 |
if (END_POSITIVE(im[i])>END_POSITIVE(im[parent]) /* i NOT CONTAINED */ |
| 221 |
|| (END_POSITIVE(im[i])==END_POSITIVE(im[parent]) /* SAME INTERVAL! */ |
| 222 |
&& START_POSITIVE(im[i])==START_POSITIVE(im[parent]))) |
| 223 |
parent=im[parent].sublist; /* POP RECURSIVE STACK*/ |
| 224 |
else { /* i CONTAINED IN parent*/ |
| 225 |
im[i].sublist=parent; /* MARK AS CONTAINED IN parent */ |
| 226 |
nsub++; /* COUNT TOTAL #SUBLIST ENTRIES */ |
| 227 |
parent=i; /* AND PUSH ONTO RECURSIVE STACK */ |
| 228 |
i++; /* ADVANCE TO NEXT INTERVAL */ |
| 229 |
} |
| 230 |
} |
| 231 |
} /* AT THIS POINT sublist IS EITHER -1 IF NOT IN SUBLIST, OR INDICATES parent*/ |
| 232 |
|
| 233 |
if (nsub>0) { /* WE HAVE SUBLISTS TO PROCESS */ |
| 234 |
CALLOC(imsub,nsub,IntervalMap); /* TEMPORARY ARRAY FOR REPACKING SUBLISTS */ |
| 235 |
for (i=j=0;i<n;i++) { /* GENERATE LIST FOR SORTING; ASSIGN HEADER INDEXES*/ |
| 236 |
parent=im[i].sublist; |
| 237 |
/* printf("Interval %i has parent %d\n", i, parent); */ |
| 238 |
if (parent>=0) {/* IN A SUBLIST */ |
| 239 |
imsub[j].start=i; |
| 240 |
imsub[j].sublist=parent; |
| 241 |
j++; |
| 242 |
if (im[parent].sublist<0){ /* A NEW PARENT! SET HIS SUBLIST HEADER INDEX */ |
| 243 |
/* printf("Setting parent %d to sublist %d\n", parent, nlists += 1); */ |
| 244 |
im[parent].sublist=nlists++; |
| 245 |
} |
| 246 |
} |
| 247 |
im[i].sublist= -1; /* RESET TO DEFAULT VALUE: NO SUBLIST */ |
| 248 |
} |
| 249 |
qsort(imsub,nsub,sizeof(IntervalMap),sublist_qsort_cmp); |
| 250 |
/* AT THIS POINT SUBLISTS ARE GROUPED TOGETHER, READY TO PACK */ |
| 251 |
|
| 252 |
CALLOC(subheader,nlists,SublistHeader); /* SUBLIST HEADER INDEX */ |
| 253 |
for (i=0;i<nsub;i++) { /* COPY SUBLIST ENTRIES TO imsub */ |
| 254 |
j=imsub[i].start; |
| 255 |
/* printf("j: %d\n", j); */ |
| 256 |
parent=imsub[i].sublist; |
| 257 |
memcpy(imsub+i,im+j,sizeof(IntervalMap)); /* COPY INTERVAL */ |
| 258 |
k=im[parent].sublist; |
| 259 |
/* printf("k is %d\n", k); */ |
| 260 |
if (subheader[k].len==0) /* START A NEW SUBLIST */ |
| 261 |
subheader[k].start=i; |
| 262 |
subheader[k].len++; /* COUNT THE SUBLIST ENTRIES */ |
| 263 |
im[j].start=im[j].end= -1; /* MARK FOR DELETION */ |
| 264 |
} /* DONE COPYING ALL SUBLISTS TO imsub */ |
| 265 |
|
| 266 |
for (i=j=0;i<n;i++) /* COMPRESS THE LIST TO REMOVE SUBLISTS */ |
| 267 |
if (im[i].start!= -1 || im[i].end!= -1) { /* NOT IN A SUBLIST, SO KEEP */ |
| 268 |
if (j<i) /* COPY TO NEW COMPACTED LOCATION */ |
| 269 |
memcpy(im+j,im+i,sizeof(IntervalMap)); |
| 270 |
j++; |
| 271 |
} |
| 272 |
|
| 273 |
memcpy(im+j,imsub,nsub*sizeof(IntervalMap)); /* COPY THE SUBLISTS */ |
| 274 |
for (i=0;i<nlists;i++) /* ADJUST start ADDRESSES FOR SHIFT*/ |
| 275 |
subheader[i].start += j; |
| 276 |
FREE(imsub); |
| 277 |
*p_n = j; /* COPY THE COMPRESSED LIST SIZES BACK TO CALLER*/ |
| 278 |
} |
| 279 |
else { /* NO SUBLISTS: HANDLE THIS CASE CAREFULLY */ |
| 280 |
*p_n = n; |
| 281 |
CALLOC(subheader,1,SublistHeader); /* RETURN A DUMMY ARRAY, SINCE NULL RETURN IS ERROR CODE */ |
| 282 |
} |
| 283 |
*p_nlists=nlists; /* RETURN COUNT OF NUMBER OF SUBLISTS */ |
| 284 |
return subheader; |
| 285 |
handle_malloc_failure: |
| 286 |
FREE(imsub); /* FREE ANY MALLOCS WE PERFORMED*/ |
| 287 |
FREE(subheader); |
| 288 |
return NULL; |
| 289 |
} |
| 290 |
|
| 291 |
int *alloc_array(int n){ |
| 292 |
/* var = (int*) malloc(n * sizeof(int)); */ |
| 293 |
int *arr = NULL; |
| 294 |
CALLOC(arr,n,int); |
| 295 |
handle_malloc_failure: |
| 296 |
return NULL; |
| 297 |
}; |
| 298 |
|
| 299 |
|
| 300 |
IntervalMap *interval_map_alloc(int n) |
| 301 |
{ |
| 302 |
IntervalMap *im=NULL; |
| 303 |
CALLOC(im,n,IntervalMap); |
| 304 |
return im; |
| 305 |
handle_malloc_failure: |
| 306 |
return NULL; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
inline int64_t find_overlap_start(double start,double end,IntervalMap im[],int n) |
| 312 |
{ |
| 313 |
int64_t l=0, mid, r; |
| 314 |
|
| 315 |
r=n-1; |
| 316 |
while (l<r) { |
| 317 |
mid=(l+r)/2; |
| 318 |
if (END_POSITIVE(im[mid])<=start) |
| 319 |
l=mid+1; |
| 320 |
else |
| 321 |
r=mid; |
| 322 |
} |
| 323 |
if (l<n && HAS_OVERLAP_POSITIVE(im[l],start,end)) |
| 324 |
return l; /* l IS START OF OVERLAP */ |
| 325 |
else |
| 326 |
return -1; /* NO OVERLAP FOUND */ |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
int find_index_start(int start,int end,IntervalIndex im[],int n) |
| 333 |
{ |
| 334 |
int l=0,mid,r; |
| 335 |
|
| 336 |
r=n-1; |
| 337 |
while (l<r) { |
| 338 |
mid=(l+r)/2; |
| 339 |
if (END_POSITIVE(im[mid])<=start) |
| 340 |
l=mid+1; |
| 341 |
else |
| 342 |
r=mid; |
| 343 |
} |
| 344 |
return l; /* l IS START OF POSSIBLE OVERLAP */ |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
inline double find_suboverlap_start(double start,double end, int64_t isub, IntervalMap im[], |
| 350 |
SublistHeader subheader[]) |
| 351 |
{ |
| 352 |
int i; |
| 353 |
|
| 354 |
if (isub>=0) { |
| 355 |
i=find_overlap_start(start,end,im+subheader[isub].start,subheader[isub].len); |
| 356 |
if (i>=0) |
| 357 |
return i+subheader[isub].start; |
| 358 |
} |
| 359 |
return -1; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
IntervalIterator *interval_iterator_alloc(void) |
| 364 |
{ |
| 365 |
IntervalIterator *it=NULL; |
| 366 |
CALLOC(it,1,IntervalIterator); |
| 367 |
return it; |
| 368 |
handle_malloc_failure: |
| 369 |
return NULL; |
| 370 |
} |
| 371 |
|
| 372 |
int free_interval_iterator(IntervalIterator *it) |
| 373 |
{ |
| 374 |
IntervalIterator *it2,*it_next; |
| 375 |
if (!it) |
| 376 |
return 0; |
| 377 |
FREE_ITERATOR_STACK(it,it2,it_next); |
| 378 |
return 0; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
IntervalIterator *reset_interval_iterator(IntervalIterator *it) |
| 383 |
{ |
| 384 |
ITERATOR_STACK_TOP(it); |
| 385 |
it->n=0; |
| 386 |
return it; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
void reorient_intervals(int n,IntervalMap im[],int ori_sign) |
| 391 |
{ |
| 392 |
int i,tmp; |
| 393 |
for (i=0;i<n;i++) { |
| 394 |
if ((im[i].start>=0 ? 1:-1)!=ori_sign) { /* ORIENTATION MISMATCH */ |
| 395 |
tmp=im[i].start; /* SO REVERSE THIS INTERVAL MAPPING */ |
| 396 |
im[i].start= -im[i].end; |
| 397 |
im[i].end = -tmp; |
| 398 |
/* tmp=im[i].target_start; */ |
| 399 |
/* im[i].target_start= -im[i].target_end; */ |
| 400 |
/* im[i].target_end = -tmp; */ |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
double find_intervals(IntervalIterator *it0, double start, double end, |
| 407 |
IntervalMap im[],int n, |
| 408 |
SublistHeader subheader[], int nlists, |
| 409 |
IntervalMap buf[], int nbuf, |
| 410 |
int *p_nreturn, IntervalIterator **it_return) |
| 411 |
{ |
| 412 |
IntervalIterator *it=NULL,*it2=NULL; |
| 413 |
int64_t ibuf=0,j,k,ori_sign=1; |
| 414 |
if (!it0) { /* ALLOCATE AN ITERATOR IF NOT SUPPLIED*/ |
| 415 |
CALLOC(it,1,IntervalIterator); |
| 416 |
} |
| 417 |
else |
| 418 |
it=it0; |
| 419 |
|
| 420 |
#if defined(ALL_POSITIVE_ORIENTATION) || defined(MERGE_INTERVAL_ORIENTATIONS) |
| 421 |
if (start<0) { /* NEED TO CONVERT TO POSITIVE ORIENTATION */ |
| 422 |
j=start; |
| 423 |
start= -end; |
| 424 |
end= -j; |
| 425 |
ori_sign = -1; |
| 426 |
} |
| 427 |
#endif |
| 428 |
if (it->n == 0) { /* DEFAULT: SEARCH THE TOP NESTED LIST */ |
| 429 |
it->n=n; |
| 430 |
it->i=find_overlap_start(start,end,im,n); |
| 431 |
} |
| 432 |
|
| 433 |
do { |
| 434 |
while (it->i>=0 && it->i<it->n && HAS_OVERLAP_POSITIVE(im[it->i],start,end)) { |
| 435 |
memcpy(buf+ibuf,im + it->i,sizeof(IntervalMap)); /*SAVE THIS HIT TO BUFFER */ |
| 436 |
ibuf++; |
| 437 |
k=im[it->i].sublist; /* GET SUBLIST OF i IF ANY */ |
| 438 |
it->i++; /* ADVANCE TO NEXT INTERVAL */ |
| 439 |
if (k>=0 && (j=find_suboverlap_start(start,end,k,im,subheader))>=0) { |
| 440 |
PUSH_ITERATOR_STACK(it,it2,IntervalIterator); /* RECURSE TO SUBLIST */ |
| 441 |
it2->i = j; /* START OF OVERLAPPING HITS IN THIS SUBLIST */ |
| 442 |
it2->n = subheader[k].start+subheader[k].len; /* END OF SUBLIST */ |
| 443 |
it=it2; /* PUSH THE ITERATOR STACK */ |
| 444 |
} |
| 445 |
if (ibuf>=nbuf){ /* FILLED THE BUFFER, RETURN THE RESULTS SO FAR */ |
| 446 |
goto finally_return_result; |
| 447 |
} |
| 448 |
} |
| 449 |
} while (POP_ITERATOR_STACK(it)); /* IF STACK EXHAUSTED, EXIT */ |
| 450 |
if (!it0) /* FREE THE ITERATOR WE CREATED. NO NEED TO RETURN IT TO USER */ |
| 451 |
free_interval_iterator(it); |
| 452 |
it=NULL; /* ITERATOR IS EXHAUSTED */ |
| 453 |
|
| 454 |
finally_return_result: |
| 455 |
#if defined(ALL_POSITIVE_ORIENTATION) || defined(MERGE_INTERVAL_ORIENTATIONS) |
| 456 |
reorient_intervals(ibuf,buf,ori_sign); /* REORIENT INTERVALS TO MATCH QUERY ORI */ |
| 457 |
#endif |
| 458 |
|
| 459 |
*p_nreturn=ibuf; /* #INTERVALS FOUND IN THIS PASS */ |
| 460 |
*it_return=it; /* HAND BACK ITERATOR FOR CONTINUING THE SEARCH, IF ANY */ |
| 461 |
return 0; /* SIGNAL THAT NO ERROR OCCURRED */ |
| 462 |
handle_malloc_failure: |
| 463 |
return -1; |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
/**************************************************************** |
| 471 |
* |
| 472 |
* FILE-BASED SEARCH FUNCTIONS |
| 473 |
*/ |
| 474 |
|
| 475 |
|
| 476 |
/* READ A BLOCK FROM THE DATABASE FILE */ |
| 477 |
int read_imdiv(FILE *ifile,IntervalMap imdiv[],int div,int i_div,int ntop) |
| 478 |
{ |
| 479 |
int block; |
| 480 |
PYGR_OFF_T ipos; |
| 481 |
ipos=div*i_div; /* CALCULATE POSITION IN RECORDS */ |
| 482 |
if (ipos+div<=ntop) /* GET A WHOLE BLOCK */ |
| 483 |
block=div; |
| 484 |
else /* JUST READ PARTIAL BLOCK AT END */ |
| 485 |
block=ntop%div; |
| 486 |
ipos *= sizeof(IntervalMap); /* CALCULATE FILE POSITION IN BYTES */ |
| 487 |
PYGR_FSEEK(ifile,ipos,SEEK_SET); |
| 488 |
fread(imdiv,sizeof(IntervalMap),block,ifile); |
| 489 |
return block; |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
/* READ A SUBLIST FROM DATABASE FILE */ |
| 494 |
IntervalMap *read_sublist(FILE *ifile,SublistHeader *subheader, |
| 495 |
IntervalMap *im) |
| 496 |
{ |
| 497 |
PYGR_OFF_T ipos; |
| 498 |
if (im==NULL) { |
| 499 |
CALLOC(im,subheader->len,IntervalMap); |
| 500 |
} |
| 501 |
ipos=subheader->start; /* CALCULATE POSITION IN RECORDS */ |
| 502 |
ipos*=sizeof(IntervalMap); /* CALCULATE FILE POSITION IN BYTES */ |
| 503 |
PYGR_FSEEK(ifile,ipos,SEEK_SET); |
| 504 |
fread(im,sizeof(IntervalMap),subheader->len,ifile); |
| 505 |
return im; |
| 506 |
handle_malloc_failure: |
| 507 |
return NULL; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
/* READ A BLOCK OF THE SUBLIST HEADER FILE */ |
| 512 |
int read_subheader_block(SublistHeader subheader[],int isub,int nblock, |
| 513 |
int nsubheader,FILE *ifile) |
| 514 |
{ |
| 515 |
PYGR_OFF_T ipos; |
| 516 |
long start; |
| 517 |
start=isub-(isub%nblock); /* GET BLOCK START */ |
| 518 |
if (start+nblock>nsubheader) |
| 519 |
nblock=nsubheader-start; /* TRUNCATE TO FIT MAX FILE LENGTH */ |
| 520 |
ipos=start; /* CONVERT TO off_t TYPE */ |
| 521 |
ipos *= sizeof(SublistHeader); /* CALCULATE ACTUAL BYTE OFFSET */ |
| 522 |
PYGR_FSEEK(ifile,ipos,SEEK_SET); |
| 523 |
fread(subheader,sizeof(SublistHeader),nblock,ifile); |
| 524 |
return start; |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
int find_file_start(IntervalIterator *it,int start,int end,int isub, |
| 531 |
IntervalIndex ii[],int nii, |
| 532 |
SublistHeader *subheader,int nlists, |
| 533 |
SubheaderFile *subheader_file, |
| 534 |
int ntop,int div,FILE *ifile) |
| 535 |
{ |
| 536 |
int i_div= -1,offset=0,offset_div=0; |
| 537 |
if (isub<0) /* TOP-LEVEL SEARCH: USE THE INDEX */ |
| 538 |
i_div=find_index_start(start,end,ii,nii); |
| 539 |
else { /* GET PTR TO subheader[isub] */ |
| 540 |
#ifdef ON_DEMAND_SUBLIST_HEADER |
| 541 |
if (isub<subheader_file->start /* isub OUTSIDE OUR CURRENT BLOCK */ |
| 542 |
|| isub>=subheader_file->start+subheader_file->nblock) |
| 543 |
subheader_file->start= /* LOAD NEW BLOCK FROM DISK */ |
| 544 |
read_subheader_block(subheader_file->subheader,isub, |
| 545 |
subheader_file->nblock,nlists, |
| 546 |
subheader_file->ifile); |
| 547 |
subheader=subheader_file->subheader + (isub-subheader_file->start); |
| 548 |
#else |
| 549 |
subheader += isub; /* POINT TO OUR SUBHEADER */ |
| 550 |
#endif |
| 551 |
if (subheader->len>div) { /* BIG SUBLIST, SO USE THE INDEX */ |
| 552 |
offset=subheader->start; |
| 553 |
offset_div=offset/div;/* offset GUARANTEED TO BE MULTIPLE OF div */ |
| 554 |
ntop=subheader->len; |
| 555 |
nii=ntop/div; /* CALCULATE SUBLIST INDEX SIZE */ |
| 556 |
if (ntop%div) /* ONE EXTRA ENTRY FOR PARTIAL BLOCK */ |
| 557 |
nii++; |
| 558 |
i_div=find_index_start(start,end,ii+offset_div,nii); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
if (!it->im) { /* NO ALLOCATION? ALLOCATE OUR BLOCK SIZE div */ |
| 563 |
CALLOC(it->im,div,IntervalMap); /* ALWAYS ALLOCATE div BUFFERSIZE */ |
| 564 |
} |
| 565 |
if (i_div>=0) { /* READ A SPECIFIC BLOCK OF SIZE div */ |
| 566 |
it->n=read_imdiv(ifile,it->im,div,i_div+offset_div,ntop+offset); |
| 567 |
it->ntop=ntop+offset; /* END OF THIS LIST IN THE BINARY FILE */ |
| 568 |
it->nii=nii+offset_div; /* SAVE INFORMATION FOR READING SUBSEQUENT BLOCKS */ |
| 569 |
it->i_div=i_div+offset_div; /* INDEX OF THIS BLOCK IN THE BINARY FILE */ |
| 570 |
} |
| 571 |
else { /* A SMALL SUBLIST: READ THE WHOLE LIST INTO MEMORY */ |
| 572 |
read_sublist(ifile,subheader,it->im); /* GUARANTEED TO BE <=div ITEMS */ |
| 573 |
it->n=subheader->len; |
| 574 |
it->nii=1; |
| 575 |
it->i_div=0; /* INDICATE THAT THERE ARE NO ADDITIONAL BLOCKS TO READ*/ |
| 576 |
} |
| 577 |
|
| 578 |
it->i=find_overlap_start(start,end,it->im,it->n); |
| 579 |
return it->i; |
| 580 |
handle_malloc_failure: |
| 581 |
return -2; /* SIGNAL THAT MEMORY ERROR OCCURRED */ |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
int find_file_intervals(IntervalIterator *it0,int start,int end, |
| 586 |
IntervalIndex ii[],int nii, |
| 587 |
SublistHeader subheader[],int nlists, |
| 588 |
SubheaderFile *subheader_file, |
| 589 |
int ntop,int div,FILE *ifile, |
| 590 |
IntervalMap buf[],int nbuf, |
| 591 |
int *p_nreturn,IntervalIterator **it_return) |
| 592 |
{ |
| 593 |
IntervalIterator *it=NULL,*it2=NULL; |
| 594 |
int k,ibuf=0,ori_sign=1,ov=0; |
| 595 |
if (!it0) { /* ALLOCATE AN ITERATOR IF NOT SUPPLIED*/ |
| 596 |
CALLOC(it,1,IntervalIterator); |
| 597 |
} |
| 598 |
else |
| 599 |
it=it0; |
| 600 |
|
| 601 |
#if defined(ALL_POSITIVE_ORIENTATION) || defined(MERGE_INTERVAL_ORIENTATIONS) |
| 602 |
if (start<0) { /* NEED TO CONVERT TO POSITIVE ORIENTATION */ |
| 603 |
k=start; |
| 604 |
start= -end; |
| 605 |
end= -k; |
| 606 |
ori_sign = -1; |
| 607 |
} |
| 608 |
#endif |
| 609 |
|
| 610 |
if (it->n == 0) /* DEFAULT: SEARCH THE TOP NESTED LIST */ |
| 611 |
if (find_file_start(it,start,end,-1,ii,nii,subheader,nlists, |
| 612 |
subheader_file,ntop,div,ifile) == FIND_FILE_MALLOC_ERR) |
| 613 |
goto handle_malloc_failure; |
| 614 |
|
| 615 |
do { /* ITERATOR STACK LOOP */ |
| 616 |
while (it->i_div < it->nii) { /* BLOCK ITERATION LOOP */ |
| 617 |
while (it->i>=0 && it->i<it->n /* INDIVIDUAL INTERVAL ITERATION LOOP */ |
| 618 |
&& HAS_OVERLAP_POSITIVE(it->im[it->i],start,end)) { /*OVERLAPS!*/ |
| 619 |
memcpy(buf+ibuf,it->im + it->i,sizeof(IntervalMap)); /*SAVE THIS HIT */ |
| 620 |
ibuf++; |
| 621 |
k=it->im[it->i].sublist; /* GET SUBLIST OF i IF ANY */ |
| 622 |
it->i++; /* ADVANCE TO NEXT INTERVAL */ |
| 623 |
PUSH_ITERATOR_STACK(it,it2,IntervalIterator); /* RECURSE TO SUBLIST */ |
| 624 |
if (k>=0 && (ov=find_file_start(it2,start,end,k,ii,nii,subheader,nlists, |
| 625 |
subheader_file,ntop,div,ifile))>=0) |
| 626 |
it=it2; /* PUSH THE ITERATOR STACK */ |
| 627 |
if (FIND_FILE_MALLOC_ERR == ov) |
| 628 |
goto handle_malloc_failure; |
| 629 |
|
| 630 |
if (ibuf>=nbuf) /* FILLED THE BUFFER, RETURN THE RESULTS SO FAR */ |
| 631 |
goto finally_return_result; |
| 632 |
} |
| 633 |
it->i_div++; /* TRY GOING TO NEXT BLOCK */ |
| 634 |
if (it->i == it->n /* USED WHOLE BLOCK, SO THERE MIGHT BE MORE */ |
| 635 |
&& it->i_div < it->nii) { /* CONTINUE TO NEXT BLOCK */ |
| 636 |
it->n=read_imdiv(ifile,it->im,div,it->i_div,it->ntop); /*READ NEXT BLOCK*/ |
| 637 |
it->i=0; /* PROCESS IT FROM ITS START */ |
| 638 |
} |
| 639 |
} |
| 640 |
} while (POP_ITERATOR_STACK(it)); /* IF STACK EXHAUSTED, EXIT */ |
| 641 |
if (!it0) /* FREE THE ITERATOR WE CREATED. NO NEED TO RETURN IT TO USER */ |
| 642 |
free_interval_iterator(it); |
| 643 |
it=NULL; /* ITERATOR IS EXHAUSTED */ |
| 644 |
|
| 645 |
finally_return_result: |
| 646 |
#if defined(ALL_POSITIVE_ORIENTATION) || defined(MERGE_INTERVAL_ORIENTATIONS) |
| 647 |
reorient_intervals(ibuf,buf,ori_sign); /* REORIENT INTERVALS TO MATCH QUERY ORI */ |
| 648 |
#endif |
| 649 |
*p_nreturn=ibuf; /* #INTERVALS FOUND IN THIS PASS */ |
| 650 |
*it_return=it; /* HAND BACK ITERATOR FOR CONTINUING THE SEARCH, IF ANY */ |
| 651 |
return 0; /* SIGNAL THAT NO ERROR OCCURRED */ |
| 652 |
handle_malloc_failure: |
| 653 |
return -1; |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
/* FUNCTIONS FOR READING AND WRITING OF THE BINARY DATABASE FILES */ |
| 662 |
|
| 663 |
int write_padded_binary(IntervalMap im[],int n,int div,FILE *ifile) |
| 664 |
{ |
| 665 |
int i,npad; |
| 666 |
fwrite(im,sizeof(IntervalMap),n,ifile); /* SAVE THE ACTUAL DATA */ |
| 667 |
npad=n%div; |
| 668 |
if (npad) { |
| 669 |
npad=div-npad; /* #ITEMS NEEDED TO PAD TO AN EXACT MULTIPLE OF div */ |
| 670 |
for (i=0;i<npad;i++) /* GUARANTEED im HAS AT LEAST ONE RECORD */ |
| 671 |
fwrite(im,sizeof(IntervalMap),1,ifile); /*THIS IS JUST PADDING */ |
| 672 |
} |
| 673 |
return n+npad; /* #RECORDS SAVED */ |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
int repack_subheaders(IntervalMap im[],int n,int div, |
| 678 |
SublistHeader *subheader,int nlists) |
| 679 |
{ |
| 680 |
int i,j,*sub_map=NULL; |
| 681 |
SublistHeader *sub_pack=NULL; |
| 682 |
|
| 683 |
CALLOC(sub_map,nlists,int); |
| 684 |
CALLOC(sub_pack,nlists,SublistHeader); |
| 685 |
for (i=j=0;i<nlists;i++) { /* PLACE SUBLISTS W/ len>div AT FRONT */ |
| 686 |
if (subheader[i].len>div) { |
| 687 |
memcpy(sub_pack+j,subheader+i,sizeof(SublistHeader)); |
| 688 |
sub_map[i]=j; |
| 689 |
j++; |
| 690 |
} |
| 691 |
} |
| 692 |
for (i=0;i<nlists;i++) { /* PLACE SUBLISTS W/ len<=div AFTERWARDS */ |
| 693 |
if (subheader[i].len<=div) { |
| 694 |
memcpy(sub_pack+j,subheader+i,sizeof(SublistHeader)); |
| 695 |
sub_map[i]=j; |
| 696 |
j++; |
| 697 |
} |
| 698 |
} |
| 699 |
for (i=0;i<n;i++) /* ADJUST im[].sublist TO THE NEW LOCATIONS */ |
| 700 |
if (im[i].sublist>=0) |
| 701 |
im[i].sublist=sub_map[im[i].sublist]; |
| 702 |
memcpy(subheader,sub_pack,nlists*sizeof(SublistHeader)); /* SAVE REORDERED LIST*/ |
| 703 |
|
| 704 |
FREE(sub_map); |
| 705 |
FREE(sub_pack); |
| 706 |
return 0; |
| 707 |
handle_malloc_failure: |
| 708 |
return -1; |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
int write_binary_index(IntervalMap im[],int n,int div,FILE *ifile) |
| 713 |
{ |
| 714 |
int i,j,nsave=0; |
| 715 |
for (i=0;i<n;i+=div) { |
| 716 |
#ifdef MERGE_INTERVAL_ORIENTATIONS |
| 717 |
if (im[i].start>=0) /* FORWARD ORI */ |
| 718 |
#endif |
| 719 |
fwrite(&(im[i].start),sizeof(int),1,ifile); /*SAVE start */ |
| 720 |
#ifdef MERGE_INTERVAL_ORIENTATIONS |
| 721 |
else { /* REVERSE ORI */ |
| 722 |
j= - im[i].end; |
| 723 |
fwrite(&j,sizeof(int),1,ifile); /*SAVE start */ |
| 724 |
} |
| 725 |
#endif |
| 726 |
j=i+div-1; |
| 727 |
if (j>=n) |
| 728 |
j=n-1; |
| 729 |
#ifdef MERGE_INTERVAL_ORIENTATIONS |
| 730 |
if (im[j].start>=0) /* FORWARD ORI */ |
| 731 |
#endif |
| 732 |
fwrite(&(im[j].end),sizeof(int),1,ifile); /*SAVE end */ |
| 733 |
#ifdef MERGE_INTERVAL_ORIENTATIONS |
| 734 |
else { /* REVERSE ORI */ |
| 735 |
j= - im[j].start; |
| 736 |
fwrite(&j,sizeof(int),1,ifile); /*SAVE end */ |
| 737 |
} |
| 738 |
#endif |
| 739 |
nsave++; |
| 740 |
} |
| 741 |
return nsave; |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
char *write_binary_files(IntervalMap im[],int n,int ntop,int div, |
| 747 |
SublistHeader *subheader,int nlists,char filestem[]) |
| 748 |
{ |
| 749 |
int i,npad=0,nii; |
| 750 |
char path[2048]; |
| 751 |
FILE *ifile=NULL,*ifile_subheader=NULL; |
| 752 |
SublistHeader sh_tmp; |
| 753 |
static char err_msg[1024]; |
| 754 |
|
| 755 |
if (nlists>0 /* REPACK SMALL SUBLISTS TO END */ |
| 756 |
&& repack_subheaders(im,n,div,subheader,nlists) |
| 757 |
== FIND_FILE_MALLOC_ERR) { |
| 758 |
sprintf(err_msg,"unable to malloc %d subheaders",nlists); |
| 759 |
return err_msg; |
| 760 |
} |
| 761 |
sprintf(path,"%s.subhead",filestem); /* SAVE THE SUBHEADER LIST */ |
| 762 |
ifile_subheader=fopen(path,"wb"); /* binary file */ |
| 763 |
if (!ifile_subheader) { |
| 764 |
sprintf(err_msg,"unable to open file %s for writing",path); |
| 765 |
return err_msg; |
| 766 |
} |
| 767 |
sprintf(path,"%s.idb",filestem); /* SAVE THE DATABASE */ |
| 768 |
ifile=fopen(path,"wb"); /* binary file */ |
| 769 |
if (!ifile) { |
| 770 |
sprintf(err_msg,"unable to open file %s for writing",path); |
| 771 |
return err_msg; |
| 772 |
} |
| 773 |
npad=write_padded_binary(im,ntop,div,ifile); /* WRITE THE TOP LEVEL LIST */ |
| 774 |
for (i=0;i<nlists;i++) { |
| 775 |
sh_tmp.start=npad; /* FILE LOCATION WHERE THIS SUBLIST STORED */ |
| 776 |
sh_tmp.len=subheader[i].len; /* SAVE THE TRUE SUBLIST LENGTH, UNPADDED */ |
| 777 |
fwrite(&sh_tmp,sizeof(SublistHeader),1,ifile_subheader); |
| 778 |
if (subheader[i].len>div) /* BIG LIST: PAD TO EXACT MULTIPLE OF div */ |
| 779 |
npad+=write_padded_binary(im+subheader[i].start,subheader[i].len,div,ifile); |
| 780 |
else { /* SMALL LIST: SAVE W/O PADDING */ |
| 781 |
fwrite(im+subheader[i].start,sizeof(IntervalMap),subheader[i].len,ifile); |
| 782 |
npad+=subheader[i].len; |
| 783 |
} |
| 784 |
} |
| 785 |
fclose(ifile); |
| 786 |
fclose(ifile_subheader); |
| 787 |
|
| 788 |
sprintf(path,"%s.index",filestem); /* SAVE THE COMPACTED INDEX */ |
| 789 |
ifile=fopen(path,"wb"); /* binary file */ |
| 790 |
if (!ifile) { |
| 791 |
sprintf(err_msg,"unable to open file %s for writing",path); |
| 792 |
return err_msg; |
| 793 |
} |
| 794 |
nii=write_binary_index(im,ntop,div,ifile); |
| 795 |
for (i=0;i<nlists;i++) /* ALSO STORE INDEX DATA FOR BIG SUBLISTS */ |
| 796 |
if (subheader[i].len>div) |
| 797 |
nii+=write_binary_index(im+subheader[i].start,subheader[i].len,div,ifile); |
| 798 |
fclose(ifile); |
| 799 |
|
| 800 |
sprintf(path,"%s.size",filestem); /* SAVE BASIC SIZE INFO*/ |
| 801 |
ifile=fopen(path,"w"); /* text file */ |
| 802 |
if (!ifile) { |
| 803 |
sprintf(err_msg,"unable to open file %s for writing",path); |
| 804 |
return err_msg; |
| 805 |
} |
| 806 |
fprintf(ifile,"%d %d %d %d %d\n",n,ntop,div,nlists,nii); |
| 807 |
fclose(ifile); |
| 808 |
|
| 809 |
return NULL; /* RETURN CODE SIGNALS SUCCESS!! */ |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
IntervalDBFile *read_binary_files(char filestem[],char err_msg[], |
| 815 |
int subheader_nblock) |
| 816 |
{ |
| 817 |
int n,ntop,div,nlists,nii; |
| 818 |
char path[2048]; |
| 819 |
IntervalIndex *ii=NULL; |
| 820 |
SublistHeader *subheader=NULL; |
| 821 |
IntervalDBFile *idb_file=NULL; |
| 822 |
FILE *ifile=NULL; |
| 823 |
|
| 824 |
sprintf(path,"%s.size",filestem); /* READ BASIC SIZE INFO*/ |
| 825 |
ifile=fopen(path,"r"); /* text file */ |
| 826 |
if (!ifile) { |
| 827 |
if (err_msg) |
| 828 |
sprintf(err_msg,"unable to open file %s",path); |
| 829 |
return NULL; |
| 830 |
} |
| 831 |
fscanf(ifile,"%d %d %d %d %d",&n,&ntop,&div,&nlists,&nii); |
| 832 |
fclose(ifile); |
| 833 |
|
| 834 |
CALLOC(ii,nii+1,IntervalIndex); |
| 835 |
if (nii>0) { |
| 836 |
sprintf(path,"%s.index",filestem); /* READ THE COMPACTED INDEX */ |
| 837 |
ifile=fopen(path,"rb"); /* binary file */ |
| 838 |
if (!ifile) { |
| 839 |
if (err_msg) |
| 840 |
sprintf(err_msg,"unable to open file %s",path); |
| 841 |
return NULL; |
| 842 |
} |
| 843 |
fread(ii,sizeof(IntervalIndex),nii,ifile); |
| 844 |
fclose(ifile); |
| 845 |
} |
| 846 |
|
| 847 |
CALLOC(idb_file,1,IntervalDBFile); |
| 848 |
if(nlists>0){ |
| 849 |
sprintf(path,"%s.subhead",filestem); /* SAVE THE SUBHEADER LIST */ |
| 850 |
ifile=fopen(path,"rb"); /* binary file */ |
| 851 |
if (!ifile) { |
| 852 |
if (err_msg) |
| 853 |
sprintf(err_msg,"unable to open file %s",path); |
| 854 |
return NULL; |
| 855 |
} |
| 856 |
#ifdef ON_DEMAND_SUBLIST_HEADER |
| 857 |
CALLOC(subheader,subheader_nblock,SublistHeader); |
| 858 |
idb_file->subheader_file.subheader=subheader; |
| 859 |
idb_file->subheader_file.nblock=subheader_nblock; |
| 860 |
idb_file->subheader_file.start = -subheader_nblock; /* NO BLOCK LOADED */ |
| 861 |
idb_file->subheader_file.ifile=ifile; |
| 862 |
#else |
| 863 |
CALLOC(subheader,nlists,SublistHeader); /* LOAD THE ENTIRE SUBHEADER */ |
| 864 |
fread(subheader,sizeof(SublistHeader),nlists,ifile); /*SAVE LIST */ |
| 865 |
fclose(ifile); |
| 866 |
#endif |
| 867 |
} |
| 868 |
|
| 869 |
idb_file->n=n; |
| 870 |
idb_file->ntop=ntop; |
| 871 |
idb_file->nlists=nlists; |
| 872 |
idb_file->div=div; |
| 873 |
idb_file->nii=ntop/div; |
| 874 |
if (ntop%div) /* INDEX IS PADDED TO EXACT MULTIPLE OF div */ |
| 875 |
idb_file->nii++; /* ONE EXTRA ENTRY FOR PARTIAL BLOCK */ |
| 876 |
idb_file->ii=ii; |
| 877 |
idb_file->subheader=subheader; |
| 878 |
sprintf(path,"%s.idb",filestem); /* OPEN THE DATABASE */ |
| 879 |
idb_file->ifile_idb=fopen(path,"rb"); /* binary file */ |
| 880 |
if (!idb_file->ifile_idb) { |
| 881 |
if (err_msg) |
| 882 |
sprintf(err_msg,"unable to open file %s",path); |
| 883 |
free(idb_file); |
| 884 |
return NULL; |
| 885 |
} |
| 886 |
return idb_file; |
| 887 |
handle_malloc_failure: |
| 888 |
FREE(ii); /* DUMP OUR MEMORY */ |
| 889 |
FREE(subheader); |
| 890 |
FREE(idb_file); |
| 891 |
return NULL; |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
int free_interval_dbfile(IntervalDBFile *db_file) |
| 897 |
{ |
| 898 |
if (db_file->ifile_idb) |
| 899 |
fclose(db_file->ifile_idb); |
| 900 |
#ifdef ON_DEMAND_SUBLIST_HEADER |
| 901 |
if (db_file->subheader_file.ifile) |
| 902 |
fclose(db_file->subheader_file.ifile); |
| 903 |
#endif |
| 904 |
FREE(db_file->ii); |
| 905 |
FREE(db_file->subheader); |
| 906 |
free(db_file); |
| 907 |
return 0; |
| 908 |
} |
| 909 |
|
| 910 |
|
| 911 |
|