Debian Codegraph @

ncls/src/intervaldb32.c

raw
1
2
3 #include "intervaldb32.h"
4
5
6 int imstart_qsort_cmp(const void *void_a,const void *void_b)
7 { /* STRAIGHTFORWARD COMPARISON OF SIGNED start VALUES, LONGER INTERVALS 1ST */
8 IntervalMap *a=(IntervalMap *)void_a,*b=(IntervalMap *)void_b;
9 if (a->start<b->start)
10 return -1;
11 else if (a->start>b->start)
12 return 1;
13 else if (a->end>b->end) /* SAME START: PUT LONGER INTERVAL 1ST */
14 return -1;
15 else if (a->end<b->end) /* CONTAINED INTERVAL SHOULD FOLLOW LARGER INTERVAL*/
16 return 1;
17 else
18 return 0;
19 }
20
21
22
23
24 #ifdef MERGE_INTERVAL_ORIENTATIONS
25 int im_qsort_cmp(const void *void_a,const void *void_b)
26 { /* MERGE FORWARD AND REVERSE INTERVALS AS IF THEY WERE ALL IN FORWARD ORI */
27 int a_start,a_end,b_start,b_end;
28 IntervalMap *a=(IntervalMap *)void_a,*b=(IntervalMap *)void_b;
29 SET_INTERVAL_POSITIVE(*a,a_start,a_end);
30 SET_INTERVAL_POSITIVE(*b,b_start,b_end);
31 if (a_start<b_start)
32 return -1;
33 else if (a_start>b_start)
34 return 1;
35 else if (a_end>b_end) /* SAME START: PUT LONGER INTERVAL 1ST */
36 return -1;
37 else if (a_end<b_end) /* CONTAINED INTERVAL SHOULD FOLLOW LARGER INTERVAL*/
38 return 1;
39 else
40 return 0;
41 }
42 #endif
43
44 int sublist_qsort_cmp(const void *void_a,const void *void_b)
45 { /* SORT IN SUBLIST ORDER, SECONDARILY BY start */
46 IntervalMap *a=(IntervalMap *)void_a,*b=(IntervalMap *)void_b;
47 if (a->sublist<b->sublist)
48 return -1;
49 else if (a->sublist>b->sublist)
50 return 1;
51 else if (START_POSITIVE(*a) < START_POSITIVE(*b))
52 return -1;
53 else if (START_POSITIVE(*a) > START_POSITIVE(*b))
54 return 1;
55 else
56 return 0;
57 }
58
59
60 SublistHeader *build_nested_list(IntervalMap im[],int n,
61 int *p_n,int *p_nlists)
62 {
63 int i=0,j,k,parent,nsub=0,nlists=0;
64 IntervalMap *imsub=NULL;
65 SublistHeader *subheader=NULL;
66
67 /* #ifdef ALL_POSITIVE_ORIENTATION */
68 /* reorient_intervals(n,im,1); /\* FORCE ALL INTERVALS INTO POSITIVE ORI *\/ */
69 /* #endif */
70 #ifdef MERGE_INTERVAL_ORIENTATIONS
71 qsort(im,n,sizeof(IntervalMap),im_qsort_cmp); /* SORT BY start, CONTAINMENT */
72 #else
73 qsort(im,n,sizeof(IntervalMap),imstart_qsort_cmp); /* SORT BY start, CONTAINMENT */
74 #endif
75 while (i<n) { /* TOP LEVEL LIST SCAN */
76 parent=i;
77 i=parent+1;
78 while (i<n && parent>=0) { /* RECURSIVE ALGORITHM OF ALEX ALEKSEYENKO */
79 if (END_POSITIVE(im[i])>END_POSITIVE(im[parent]) /* i NOT CONTAINED */
80 || (END_POSITIVE(im[i])==END_POSITIVE(im[parent]) /* SAME INTERVAL! */
81 && START_POSITIVE(im[i])==START_POSITIVE(im[parent])))
82 parent=im[parent].sublist; /* POP RECURSIVE STACK*/
83 else { /* i CONTAINED IN parent*/
84 im[i].sublist=parent; /* MARK AS CONTAINED IN parent */
85 nsub++; /* COUNT TOTAL #SUBLIST ENTRIES */
86 parent=i; /* AND PUSH ONTO RECURSIVE STACK */
87 i++; /* ADVANCE TO NEXT INTERVAL */
88 }
89 }
90 } /* AT THIS POINT sublist IS EITHER -1 IF NOT IN SUBLIST, OR INDICATES parent*/
91
92 if (nsub>0) { /* WE HAVE SUBLISTS TO PROCESS */
93 CALLOC(imsub,nsub,IntervalMap); /* TEMPORARY ARRAY FOR REPACKING SUBLISTS */
94 for (i=j=0;i<n;i++) { /* GENERATE LIST FOR SORTING; ASSIGN HEADER INDEXES*/
95 parent=im[i].sublist;
96 /* printf("Interval %i has parent %d\n", i, parent); */
97 if (parent>=0) {/* IN A SUBLIST */
98 imsub[j].start=i;
99 imsub[j].sublist=parent;
100 j++;
101 if (im[parent].sublist<0){ /* A NEW PARENT! SET HIS SUBLIST HEADER INDEX */
102 /* printf("Setting parent %d to sublist %d\n", parent, nlists += 1); */
103 im[parent].sublist=nlists++;
104 }
105 }
106 im[i].sublist= -1; /* RESET TO DEFAULT VALUE: NO SUBLIST */
107 }
108 qsort(imsub,nsub,sizeof(IntervalMap),sublist_qsort_cmp);
109 /* AT THIS POINT SUBLISTS ARE GROUPED TOGETHER, READY TO PACK */
110
111 CALLOC(subheader,nlists,SublistHeader); /* SUBLIST HEADER INDEX */
112 for (i=0;i<nsub;i++) { /* COPY SUBLIST ENTRIES TO imsub */
113 j=imsub[i].start;
114 /* printf("j: %d\n", j); */
115 parent=imsub[i].sublist;
116 memcpy(imsub+i,im+j,sizeof(IntervalMap)); /* COPY INTERVAL */
117 k=im[parent].sublist;
118 /* printf("k is %d\n", k); */
119 if (subheader[k].len==0) /* START A NEW SUBLIST */
120 subheader[k].start=i;
121 subheader[k].len++; /* COUNT THE SUBLIST ENTRIES */
122 im[j].start=im[j].end= -1; /* MARK FOR DELETION */
123 } /* DONE COPYING ALL SUBLISTS TO imsub */
124
125 for (i=j=0;i<n;i++) /* COMPRESS THE LIST TO REMOVE SUBLISTS */
126 if (im[i].start!= -1 || im[i].end!= -1) { /* NOT IN A SUBLIST, SO KEEP */
127 if (j<i) /* COPY TO NEW COMPACTED LOCATION */
128 memcpy(im+j,im+i,sizeof(IntervalMap));
129 j++;
130 }
131
132 memcpy(im+j,imsub,nsub*sizeof(IntervalMap)); /* COPY THE SUBLISTS */
133 for (i=0;i<nlists;i++) /* ADJUST start ADDRESSES FOR SHIFT*/
134 subheader[i].start += j;
135 FREE(imsub);
136 *p_n = j; /* COPY THE COMPRESSED LIST SIZES BACK TO CALLER*/
137 }
138 else { /* NO SUBLISTS: HANDLE THIS CASE CAREFULLY */
139 *p_n = n;
140 CALLOC(subheader,1,SublistHeader); /* RETURN A DUMMY ARRAY, SINCE NULL RETURN IS ERROR CODE */
141 }
142 *p_nlists=nlists; /* RETURN COUNT OF NUMBER OF SUBLISTS */
143 return subheader;
144 handle_malloc_failure:
145 FREE(imsub); /* FREE ANY MALLOCS WE PERFORMED*/
146 FREE(subheader);
147 return NULL;
148 }
149
150 int *alloc_array(int n){
151 /* var = (int*) malloc(n * sizeof(int)); */
152 int *arr = NULL;
153 CALLOC(arr,n,int);
154 handle_malloc_failure:
155 return NULL;
156 };
157
158
159 IntervalMap *interval_map_alloc(int n)
160 {
161 IntervalMap *im=NULL;
162 CALLOC(im,n,IntervalMap);
163 return im;
164 handle_malloc_failure:
165 return NULL;
166 }
167
168
169
170 inline int find_overlap_start(int start,int end,IntervalMap im[],int n)
171 {
172 int l=0,mid,r;
173
174 r=n-1;
175 while (l<r) {
176 mid=(l+r)/2;
177 if (END_POSITIVE(im[mid])<=start)
178 l=mid+1;
179 else
180 r=mid;
181 }
182 if (l<n && HAS_OVERLAP_POSITIVE(im[l],start,end))
183 return l; /* l IS START OF OVERLAP */
184 else
185 return -1; /* NO OVERLAP FOUND */
186 }
187
188
189
190
191 int find_index_start(int start,int end,IntervalIndex im[],int n)
192 {
193 int l=0,mid,r;
194
195 r=n-1;
196 while (l<r) {
197 mid=(l+r)/2;
198 if (END_POSITIVE(im[mid])<=start)
199 l=mid+1;
200 else
201 r=mid;
202 }
203 return l; /* l IS START OF POSSIBLE OVERLAP */
204 }
205
206
207
208 inline int find_suboverlap_start(int start,int end,int isub,IntervalMap im[],
209 SublistHeader subheader[])
210 {
211 int i;
212
213 if (isub>=0) {
214 i=find_overlap_start(start,end,im+subheader[isub].start,subheader[isub].len);
215 if (i>=0)
216 return i+subheader[isub].start;
217 }
218 return -1;
219 }
220
221
222 IntervalIterator *interval_iterator_alloc(void)
223 {
224 IntervalIterator *it=NULL;
225 CALLOC(it,1,IntervalIterator);
226 return it;
227 handle_malloc_failure:
228 return NULL;
229 }
230
231 int free_interval_iterator(IntervalIterator *it)
232 {
233 IntervalIterator *it2,*it_next;
234 if (!it)
235 return 0;
236 FREE_ITERATOR_STACK(it,it2,it_next);
237 return 0;
238 }
239
240
241 int find_intervals(IntervalIterator *it0, int start, int end,
242 IntervalMap im[],int n,
243 SublistHeader subheader[], int nlists,
244 IntervalMap buf[], int nbuf,
245 int *p_nreturn, IntervalIterator **it_return)
246 {
247 IntervalIterator *it=NULL,*it2=NULL;
248 int ibuf=0,j,k,ori_sign=1;
249 if (!it0) { /* ALLOCATE AN ITERATOR IF NOT SUPPLIED*/
250 CALLOC(it,1,IntervalIterator);
251 }
252 else
253 it=it0;
254
255 #if defined(ALL_POSITIVE_ORIENTATION) || defined(MERGE_INTERVAL_ORIENTATIONS)
256 if (start<0) { /* NEED TO CONVERT TO POSITIVE ORIENTATION */
257 j=start;
258 start= -end;
259 end= -j;
260 ori_sign = -1;
261 }
262 #endif
263 if (it->n == 0) { /* DEFAULT: SEARCH THE TOP NESTED LIST */
264 it->n=n;
265 it->i=find_overlap_start(start,end,im,n);
266 }
267
268 do {
269 while (it->i>=0 && it->i<it->n && HAS_OVERLAP_POSITIVE(im[it->i],start,end)) {
270 memcpy(buf+ibuf,im + it->i,sizeof(IntervalMap)); /*SAVE THIS HIT TO BUFFER */
271 ibuf++;
272 k=im[it->i].sublist; /* GET SUBLIST OF i IF ANY */
273 it->i++; /* ADVANCE TO NEXT INTERVAL */
274 if (k>=0 && (j=find_suboverlap_start(start,end,k,im,subheader))>=0) {
275 PUSH_ITERATOR_STACK(it,it2,IntervalIterator); /* RECURSE TO SUBLIST */
276 it2->i = j; /* START OF OVERLAPPING HITS IN THIS SUBLIST */
277 it2->n = subheader[k].start+subheader[k].len; /* END OF SUBLIST */
278 it=it2; /* PUSH THE ITERATOR STACK */
279 }
280 if (ibuf>=nbuf){ /* FILLED THE BUFFER, RETURN THE RESULTS SO FAR */
281 goto finally_return_result;
282 }
283 }
284 } while (POP_ITERATOR_STACK(it)); /* IF STACK EXHAUSTED, EXIT */
285 if (!it0) /* FREE THE ITERATOR WE CREATED. NO NEED TO RETURN IT TO USER */
286 free_interval_iterator(it);
287 it=NULL; /* ITERATOR IS EXHAUSTED */
288
289 finally_return_result:
290 /* #if defined(ALL_POSITIVE_ORIENTATION) || defined(MERGE_INTERVAL_ORIENTATIONS) */
291 /* reorient_intervals(ibuf,buf,ori_sign); /\* REORIENT INTERVALS TO MATCH QUERY ORI *\/ */
292 /* #endif */
293
294 *p_nreturn=ibuf; /* #INTERVALS FOUND IN THIS PASS */
295 *it_return=it; /* HAND BACK ITERATOR FOR CONTINUING THE SEARCH, IF ANY */
296 return 0; /* SIGNAL THAT NO ERROR OCCURRED */
297 handle_malloc_failure:
298 return -1;
299 }
300
301
302
303 void reorient_intervals(int n,IntervalMap im[],int ori_sign)
304 {
305 int i,tmp;
306 for (i=0;i<n;i++) {
307 if ((im[i].start>=0 ? 1:-1)!=ori_sign) { /* ORIENTATION MISMATCH */
308 tmp=im[i].start; /* SO REVERSE THIS INTERVAL MAPPING */
309 im[i].start= -im[i].end;
310 im[i].end = -tmp;
311 /* tmp=im[i].target_start; */
312 /* im[i].target_start= -im[i].target_end; */
313 /* im[i].target_end = -tmp; */
314 }
315 }
316 }
317
318 IntervalIterator *reset_interval_iterator(IntervalIterator *it)
319 {
320 ITERATOR_STACK_TOP(it);
321 it->n=0;
322 return it;
323 }