8 ({ __typeof__ (a) _a = (a); \
9 __typeof__ (b) _b = (b); \
14 ({ __typeof__ (a) _a = (a); \
15 __typeof__ (b) _b = (b); \
16 _a <= _b ? _a : _b; })
20 static int postorder_select_bsearch(bp *b,int s);
22 static int naive_depth(bp *b, int s)
27 for (i=0; i<=s; i++) {
28 if (bp_getbit(b->B,i)==OP) {
37 void bp_print(bp *b, int s, int t)
41 for (i=s; i<=t; i++) {
42 if (bp_getbit(b->B,i)==OP) {
53 int *matchtbl,*parenttbl;
54 void make_naivetbl(pb *B,int n)
59 bp_xmalloc(matchtbl,n);
60 bp_xmalloc(parenttbl,n);
63 for (i=0; i<n; i++) matchtbl[i] = parenttbl[i] = -1;
69 if (bp_getbit(B,i)==OP) {
72 parenttbl[i] = stack[v-1];
77 matchtbl[stack[v]] = i; // close
78 matchtbl[i] = stack[v]; // open
88 int fwdtbl[(2*ETW+1)*(1<<ETW)];
89 int bwdtbl[(2*ETW+1)*(1<<ETW)];
91 int mintbl_li[1<<ETW], mintbl_lv[1<<ETW];
92 int mintbl_ri[1<<ETW], mintbl_rv[1<<ETW];
93 int maxtbl_li[1<<ETW], maxtbl_lv[1<<ETW];
94 int maxtbl_ri[1<<ETW], maxtbl_rv[1<<ETW];
96 int minmaxtbl_i[4][1<<ETW], minmaxtbl_v[4][1<<ETW];
98 int degtbl2[(2*ETW+1)*(1<<ETW)];
99 int childtbl[(ETW)*(1<<ETW)];
100 int childtbl2[2*ETW+1][ETW][(1<<ETW)];
101 int depthtbl[(2*ETW+1)*(1<<ETW)];
104 void make_matchtbl(void)
113 for (x = 0; x < (1<<ETW); x++) {
114 bp_setbits(buf,0,ETW,x);
115 for (r=-ETW; r<=ETW; r++) fwdtbl[((r+ETW)<<ETW)+x] = ETW;
116 for (r=-ETW; r<=ETW; r++) bwdtbl[((r+ETW)<<ETW)+x] = ETW;
117 for (r=-ETW; r<=ETW; r++) degtbl2[((r+ETW)<<ETW)+x] = 0;
118 for (r=-ETW; r<=ETW; r++) depthtbl[((r+ETW)<<ETW)+x] = 0;
121 for (i=0; i<ETW; i++) {
122 if (bp_getbit(buf,i)==OP) {
127 if (fwdtbl[((r+ETW)<<ETW)+x] == ETW) fwdtbl[((r+ETW)<<ETW)+x] = i;
131 for (i=ETW-1; i>=0; i--) {
132 if (bp_getbit(buf,i)==OP) {
137 if (bwdtbl[((r+ETW)<<ETW)+x] == ETW) bwdtbl[((r+ETW)<<ETW)+x] = ETW-1-i;
141 for (i=0; i<ETW; i++) {
142 if (bp_getbit(buf,i)==OP) {
147 depthtbl[((r+ETW)<<ETW)+x] += (1<<(ETW-1));
152 m = ETW+1; M = -ETW-1;
153 //maxtbl_lv[x] = -ETW-1;
154 //mintbl_lv[x] = ETW+1;
155 minmaxtbl_v[OPT_MAX | OPT_LEFT][x] = -ETW-1;
156 minmaxtbl_v[OPT_MIN | OPT_LEFT][x] = ETW+1;
158 for (i=0; i<ETW; i++) {
159 if (bp_getbit(buf,i)==OP) {
163 //maxtbl_li[x] = i; maxtbl_lv[x] = r;
164 minmaxtbl_i[OPT_MAX | OPT_LEFT][x] = i;
165 minmaxtbl_v[OPT_MAX | OPT_LEFT][x] = r;
171 childtbl[((deg-1)<<ETW) + x] = i;
175 //mintbl_li[x] = i; mintbl_lv[x] = r;
176 minmaxtbl_i[OPT_MIN | OPT_LEFT][x] = i;
177 minmaxtbl_v[OPT_MIN | OPT_LEFT][x] = r;
179 childtbl[((deg-1)<<ETW) + x] = i;
182 if (r <= m) degtbl2[((r+ETW)<<ETW)+x]++;
187 //maxtbl_rv[x] = -ETW-1;
188 //mintbl_rv[x] = ETW+1;
189 minmaxtbl_v[OPT_MAX | OPT_RIGHT][x] = -ETW-1;
190 minmaxtbl_v[OPT_MIN | OPT_RIGHT][x] = ETW+1;
191 for (i=0; i<ETW; i++) {
192 if (bp_getbit(buf,i)==OP) {
196 //maxtbl_ri[x] = i; maxtbl_rv[x] = r;
197 minmaxtbl_i[OPT_MAX | OPT_RIGHT][x] = i;
198 minmaxtbl_v[OPT_MAX | OPT_RIGHT][x] = r;
204 //mintbl_ri[x] = i; mintbl_rv[x] = r;
205 minmaxtbl_i[OPT_MIN | OPT_RIGHT][x] = i;
206 minmaxtbl_v[OPT_MIN | OPT_RIGHT][x] = r;
211 for (i = 0; i < ETW; i++) {
212 for (j = -ETW; j <= ETW; j++) {
213 childtbl2[j+ETW][i][x] = -1;
217 for (j=-ETW; j<=ETW; j++) {
221 for (i = 0; i < ETW; i++) {
222 if (bp_getbit(buf,i)==OP) {
229 childtbl2[j+ETW][ith-1][x] = i;
239 bp * bp_construct(int n, pb *B, int opt)
250 int r; // # of minimum values
264 b->da_inorder = NULL;
265 b->da_postorder = NULL;
266 b->da_dfuds_leaf = NULL;
267 b->da = bp_darray_construct(n,B, opt & OPT_FAST_PREORDER_SELECT);
268 b->idx_size += b->da->idx_size;
276 b->idx_size += ns * sizeof(*sm);
279 b->idx_size += ns * sizeof(*sM);
284 if (opt & OPT_DEGREE) {
286 b->idx_size += ns * sizeof(*sd);
290 for (i=0; i<n; i++) {
304 if (i % SB == SB-1 || i==n-1) {
305 ds = bp_depth(b,(i/SB)*SB-1);
306 if (m - ds + SB < 0 || m - ds + SB > 255) {
307 printf("error m=%d ds=%d\n",m,ds);
309 if (M - ds + 1 < 0 || M - ds + 1 > 255) {
310 printf("error M=%d ds=%d\n",M,ds);
312 sm[i/SB] = m - ds + SB;
313 sM[i/SB] = M - ds + 1;
314 if (opt & OPT_DEGREE) sd[i/SB] = r;
320 m_ofs = 1 << blog(nm-1);
323 bp_xmalloc(mm, nm + m_ofs);
324 b->idx_size += (nm+m_ofs) * sizeof(*mm);
326 bp_xmalloc(mM, nm + m_ofs);
327 b->idx_size += (nm+m_ofs) * sizeof(*mM);
331 if (opt & OPT_DEGREE) {
332 bp_xmalloc(md, nm + m_ofs);
333 b->idx_size += (nm+m_ofs) * sizeof(*md);
337 for (i=0; i<n; i++) {
350 if (i % MB == MB-1 || i==n-1) {
353 if (opt & OPT_DEGREE) md[m_ofs+ i/MB] = r;
357 for (j=m_ofs-1; j > 0; j--) {
359 if (j*2 < nm + m_ofs) m = mm[j*2];
360 if (j*2+1 < nm + m_ofs) m = min(m,mm[j*2+1]);
362 if (j*2 < nm + m_ofs) M = mM[j*2];
363 if (j*2+1 < nm + m_ofs) M = max(M,mM[j*2+1]);
364 mm[j] = m; mM[j] = M;
365 if (opt & OPT_DEGREE) {
367 if (j*2 < nm + m_ofs) d = md[j*2];
368 if (j*2+1 < nm + m_ofs) {
369 if (mm[j*2] == mm[j*2+1]) d += md[j*2+1];
370 if (mm[j*2] > mm[j*2+1]) d = md[j*2+1];
377 if (opt & OPT_DEGREE) {
382 if (opt & OPT_LEAF) {
383 b->da_leaf = bp_darray_pat_construct(n, B, 2, 0x2, opt & OPT_FAST_LEAF_SELECT);
384 b->idx_size += b->da_leaf->idx_size;
389 if (opt & OPT_INORDER) {
391 b->da_inorder = bp_darray_pat_construct(n, B, 2, 0x1, opt & OPT_FAST_INORDER_SELECT);
392 b->idx_size += b->da_inorder->idx_size;
395 b->da_inorder = NULL;
398 if (opt & OPT_FAST_POSTORDER_SELECT) {
400 b->da_postorder = bp_darray_pat_construct(n, B, 1, 0x0, (opt & OPT_FAST_POSTORDER_SELECT) | OPT_NO_RANK);
401 b->idx_size += b->da_postorder->idx_size;
404 b->da_postorder = NULL;
407 if (opt & OPT_DFUDS_LEAF) {
408 b->da_dfuds_leaf = bp_darray_pat_construct( n, B, 2, 0x0, opt & OPT_FAST_DFUDS_LEAF_SELECT);
409 b->idx_size += b->da_dfuds_leaf->idx_size;
412 b->da_dfuds_leaf = NULL;
418 void bp_delete(bp *b) {
419 if (!b) return; // nothing to free
421 bp_darray_free(b->da); // destroys da data structure
422 if (b->sm) free(b->sm);
423 if (b->sM) free(b->sM);
424 if (b->sd) free(b->sd);
425 if (b->mm) free(b->mm);
426 if (b->mM) free(b->mM);
427 if (b->md) free(b->md);
429 bp_darray_free(b->da_leaf);
431 bp_darray_free(b->da_inorder);
433 bp_darray_free(b->da_postorder);
435 bp_darray_free(b->da_dfuds_leaf);
441 // saveTree: saves parentheses data structure to file
442 // By Diego Arroyuelo
443 void saveTree(bp *b, FILE *fp) {
445 if (fwrite(&(b->n), sizeof(int), 1, fp) != 1) {
446 printf("Error: cannot save number of parentheses to file\n");
450 if (fwrite(b->B, sizeof(pb), (b->n+D-1)/D, fp) != ((b->n+D-1)/D)) {
451 printf("Error: cannot save parentheses sequence to file\n");
455 if (fwrite(&(b->opt), sizeof(int), 1, fp) != 1) {
456 printf("Error: cannot save opt in parentheses to file\n");
461 // loadTree: load parentheses data structure from file
462 // By Diego Arroyuelo
463 bp * loadTree(FILE *fp) {
468 if (fread(&n, sizeof(int), 1, fp) != 1) {
469 printf("Error: cannot read number of parentheses from file\n");
473 bp_xmalloc(B, (n+D-1)/D);
475 if (fread(B, sizeof(pb), (n+D-1)/D, fp) != ((n+D-1)/D)) {
476 printf("Error: cannot read parentheses sequence from file\n");
480 if (fread(&opt, sizeof(int), 1, fp) != 1) {
481 printf("Error: cannot read opt in parentheses from file\n");
485 return bp_construct(n, B, opt);
490 static int naive_fwd_excess(bp *b,int s, int rel)
496 for (i=s+1; i<n; i++) {
497 if (bp_getbit(B,i)==OP) {
502 if (v == rel) return i;
507 static int naive_bwd_excess(bp *b,int s, int rel)
513 for (i=s; i>=0; i--) {
514 if (bp_getbit(B,i)==OP) {
519 if (v == rel) return i-1;
524 static int naive_search_SB_l(bp *b, int i, int rel)
530 if (bp_getbit(b->B,i)==OP) {
535 if (rel == 0) return i-1;
537 if (i < 0) return -2;
541 int bp_naive_rmq(bp *b, int s, int t,int opt)
545 if (opt & OPT_RIGHT) {
546 d = dm = bp_depth(b,t); im = t;
549 if (bp_getbit(b->B,i+1)==CP) {
558 if (!(opt & OPT_MAX)) {
567 d = dm = bp_depth(b,s); im = s;
570 if (bp_getbit(b->B,i)==OP) {
579 if (!(opt & OPT_MAX)) {
593 int bp_rank_open(bp *b, int s)
595 return bp_darray_rank(b->da, s);
598 int bp_rank_close(bp *b, int s)
600 return s+1 - bp_darray_rank(b->da, s);
603 int bp_select_open(bp *b, int s)
605 if (b->opt & OPT_FAST_PREORDER_SELECT) {
606 return bp_darray_select(b->da,s,1);
608 return bp_darray_select_bsearch(b->da, s, getpat_preorder);
612 int bp_select_close(bp *b, int s)
614 if (b->opt & OPT_FAST_POSTORDER_SELECT) {
615 return bp_darray_pat_select(b->da_postorder, s, getpat_postorder);
617 return postorder_select_bsearch(b,s);
622 ///////////////////////////////////////////
623 // bp_postorder_rank(bp *b,int s)
624 // returns the postorder (>= 1) of node s (s >= 0)
625 // -1 if s-th bit is not OP
626 ///////////////////////////////////////////
627 int bp_postorder_rank(bp *b,int s)
630 if (bp_inspect(b,s) == CP) return -1;
631 t = bp_find_close(b,s);
632 // return t+1 - darray_rank(b->da,t);
633 return bp_rank_close(b,t);
636 static int postorder_select_bsearch(bp *b,int s)
640 if (s == 0) return -1;
642 if (s > b->da->n - b->da->m) {
645 l = 0; r = b->da->n - 1;
649 //printf("m=%d rank=%d s=%d\n",m,m+1 - darray_rank(b->da,m),s);
650 if (m+1 - bp_darray_rank(b->da,m) >= s) {
659 ///////////////////////////////////////////
660 // bp_postorder_select(bp *b,int s)
661 // returns the position of CP of the node with postorder s (>= 1)
662 ///////////////////////////////////////////
663 int bp_postorder_select(bp *b,int s)
666 if (b->opt & OPT_FAST_POSTORDER_SELECT) {
667 return bp_darray_pat_select(b->da_postorder,s,getpat_postorder);
669 return postorder_select_bsearch(b->da,s);
672 return bp_select_close(b,s);
676 ///////////////////////////////////////////
677 // bp_leaf_rank(bp *b,int s)
678 // returns the number of leaves to the left of s
679 ///////////////////////////////////////////
680 int bp_leaf_rank(bp *b,int s)
682 if ((b->opt & OPT_LEAF) == 0) {
683 printf("leaf_rank: error!!! not supported\n");
689 return bp_darray_pat_rank(b->da_leaf,s,getpat_leaf);
692 ///////////////////////////////////////////
693 // leaf_select(bp *b,int s)
694 // returns the position of s-th leaf
695 ///////////////////////////////////////////
696 int bp_leaf_select(bp *b,int s)
698 if ((b->opt & OPT_LEAF) == 0) {
699 printf("leaf_select: error!!! not supported\n");
702 if (s > b->da_leaf->m) return -1;
703 if (b->opt & OPT_FAST_LEAF_SELECT) {
704 return bp_darray_pat_select(b->da_leaf,s,getpat_leaf);
706 return bp_darray_select_bsearch(b->da_leaf,s,getpat_leaf);
711 ///////////////////////////////////////////
712 // bp_inorder_rank(bp *b,int s)
713 // returns the number of ")(" (s >= 0)
714 ///////////////////////////////////////////
715 int bp_inorder_rank(bp *b,int s)
717 if ((b->opt & OPT_INORDER) == 0) {
718 printf("inorder_rank: error!!! not supported\n");
724 return bp_darray_pat_rank(b->da_inorder,s,getpat_inorder);
727 ///////////////////////////////////////////
728 // bp_inorder_select(bp *b,int s)
729 // returns the s-th position of ")(" (s >= 1)
730 ///////////////////////////////////////////
731 int bp_inorder_select(bp *b,int s)
733 if ((b->opt & OPT_INORDER) == 0) {
734 printf("inorder_select: error!!! not supported\n");
737 if (b->opt & OPT_FAST_INORDER_SELECT) {
738 return bp_darray_pat_select(b->da_inorder,s,getpat_inorder);
740 return bp_darray_select_bsearch(b->da_inorder,s,getpat_inorder);
744 ///////////////////////////////////////////
745 // bp_leftmost_leaf(bp *b, int s)
746 ///////////////////////////////////////////
747 int bp_leftmost_leaf(bp *b, int s)
749 if ((b->opt & OPT_LEAF) == 0) {
750 printf("leftmost_leaf: error!!! not supported\n");
753 return bp_leaf_select(b, bp_leaf_rank(b,s)+1);
756 ///////////////////////////////////////////
757 // rightmost_leaf(bp *b, int s)
758 ///////////////////////////////////////////
759 int bp_rightmost_leaf(bp *b, int s)
762 if ((b->opt & OPT_LEAF) == 0) {
763 printf("leftmost_leaf: error!!! not supported\n");
766 t = bp_find_close(b,s);
767 return bp_leaf_select(b, bp_leaf_rank(b,t));
771 ///////////////////////////////////////////
772 // bp_prev_sibling(bp *b,int s)
773 // returns the previous sibling of parent(s)
774 // -1 if s is the first child
775 //////////////////////////////////////////
776 int bp_prev_sibling(bp *b, int s)
779 if (s == 0) return -1;
780 if (bp_inspect(b,s-1) == OP) return -1;
781 t = bp_find_open(b,s-1);
785 ///////////////////////////////////////////
786 // bp_deepest_node(bp *b,int s)
787 // returns the first node with the largest depth in the subtree of s
788 ///////////////////////////////////////////
789 int bp_deepest_node(bp *b,int s)
792 t = bp_find_close(b,s);
793 m = bp_rmq(b,s,t, OPT_MAX);
797 ///////////////////////////////////////////
798 // bp_subtree_height(bp *b,int s)
799 // returns the height of the subtree of s
801 ///////////////////////////////////////////
802 int bp_subtree_height(bp *b,int s)
805 t = bp_deepest_node(b,s);
806 return bp_depth(b,t) - bp_depth(b,s);
809 int bp_naive_degree(bp *b, int s)
813 t = bp_first_child(b,s);
816 t = bp_next_sibling(b,t);
821 ///////////////////////////////////////////
822 // bp_degree(bp *b, int s)
823 // returns the number of children of s
825 ///////////////////////////////////////////
826 int bp_degree(bp *b, int s)
828 if (b->opt & OPT_DEGREE) {
829 return bp_fast_degree(b,s,b->n,0);
831 return bp_naive_degree(b,s);
835 int bp_naive_child(bp *b, int s, int d)
838 t = bp_first_child(b,s);
839 for (i = 1; i < d; i++) {
841 t = bp_next_sibling(b,t);
846 ///////////////////////////////////////////
847 // bp_child(bp *b, int s, int d)
848 // returns the d-th child of s (1 <= d <= degree(s))
849 // -1 if no such node
850 ///////////////////////////////////////////
851 int bp_child(bp *b, int s, int d)
854 if (b->opt & OPT_DEGREE) {
855 //return find_open(b,fast_degree(b,s,b->n,d));
856 if (d==1) return bp_first_child(b,s);
857 r = bp_fast_degree(b,s,b->n,d-1)+1;
858 if (bp_inspect(b,r) == CP) return -1;
861 return bp_naive_child(b,s,d);
867 int bp_naive_child_rank(bp *b, int t)
873 t = bp_prev_sibling(b,t);
878 ///////////////////////////////////////////
879 // bp_child_rank(bp *b, int t)
880 // returns d if t is the d-th child of the parent of t (d >= 1)
881 // 1 if t is the root
882 ///////////////////////////////////////////
883 int bp_child_rank(bp *b, int t)
886 if (t == bp_root_node(b)) return 1;
887 if (b->opt & OPT_DEGREE) {
889 return bp_fast_degree(b,r,t,0)+1;
891 return bp_naive_child_rank(b,t);
896 ///////////////////////////////////////////
897 // distance(bp *b, int s, int t)
898 // returns the length of the shortest path from s to t in the tree
899 ///////////////////////////////////////////
900 int bp_distance(bp *b, int s, int t)
905 return (bp_depth(b,s) - d) + (bp_depth(b,t) - d);
908 ///////////////////////////////////////////
909 // level_next(bp *b, int d)
910 ///////////////////////////////////////////
911 int bp_level_next(bp *b,int s)
914 t = bp_fwd_excess(b,s,0);
918 ///////////////////////////////////////////
919 // level_prev(bp *b, int d)
920 ///////////////////////////////////////////
921 int bp_level_prev(bp *b,int s)
924 t = bp_bwd_excess(b,s,0);
928 ///////////////////////////////////////////
929 // bp_level_leftmost(bp *b, int d)
930 ///////////////////////////////////////////
931 int bp_level_leftmost(bp *b, int d)
934 if (d < 1) return -1;
935 if (d == 1) return 0;
936 t = bp_fwd_excess(b,0,d);
940 ///////////////////////////////////////////
941 // bp_level_rigthmost(bp *b, int d)
942 ///////////////////////////////////////////
943 int level_rigthmost(bp *b, int d)
946 if (d < 1) return -1;
947 if (d == 1) return 0;
948 t = bp_bwd_excess(b,0,d-1);
949 return bp_find_open(b,t);
952 ///////////////////////////////////////////
953 // leaf_size(bp *b, int s)
954 ///////////////////////////////////////////
955 int bp_leaf_size(bp *b, int s)
958 if ((b->opt & OPT_LEAF) == 0) {
959 printf("leaf_size: error!!! not supported\n");
962 t = bp_find_close(b,s);
963 return bp_leaf_rank(b,t) - bp_leaf_rank(b,s);