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 static 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 static 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");
449 if (fwrite(b->B, sizeof(pb), (b->n+D-1)/D, fp) != ((b->n+D-1)/D)) {
450 printf("Error: cannot save parentheses sequence to file\n");
454 if (fwrite(&(b->opt), sizeof(int), 1, fp) != 1) {
455 printf("Error: cannot save opt in parentheses to file\n");
460 // loadTree: load parentheses data structure from file
461 // By Diego Arroyuelo
462 bp * loadTree(FILE *fp) {
467 if (fread(&n, sizeof(int), 1, fp) != 1) {
468 printf("Error: cannot read number of parentheses from file\n");
472 bp_xmalloc(B, (n+D-1)/D);
474 if (fread(B, sizeof(pb), (n+D-1)/D, fp) != ((n+D-1)/D)) {
475 printf("Error: cannot read parentheses sequence from file\n");
479 if (fread(&opt, sizeof(int), 1, fp) != 1) {
480 printf("Error: cannot read opt in parentheses from file\n");
484 return bp_construct(n, B, opt);
489 static int naive_fwd_excess(bp *b,int s, int rel)
495 for (i=s+1; i<n; i++) {
496 if (bp_getbit(B,i)==OP) {
501 if (v == rel) return i;
506 static int naive_bwd_excess(bp *b,int s, int rel)
512 for (i=s; i>=0; i--) {
513 if (bp_getbit(B,i)==OP) {
518 if (v == rel) return i-1;
523 static int naive_search_SB_l(bp *b, int i, int rel)
529 if (bp_getbit(b->B,i)==OP) {
534 if (rel == 0) return i-1;
536 if (i < 0) return -2;
540 int bp_naive_rmq(bp *b, int s, int t,int opt)
544 if (opt & OPT_RIGHT) {
545 d = dm = bp_depth(b,t); im = t;
548 if (bp_getbit(b->B,i+1)==CP) {
557 if (!(opt & OPT_MAX)) {
566 d = dm = bp_depth(b,s); im = s;
569 if (bp_getbit(b->B,i)==OP) {
578 if (!(opt & OPT_MAX)) {
592 int bp_rank_open(bp *b, int s)
594 return bp_darray_rank(b->da, s);
597 int bp_rank_close(bp *b, int s)
599 return s+1 - bp_darray_rank(b->da, s);
602 int bp_select_open(bp *b, int s)
604 if (b->opt & OPT_FAST_PREORDER_SELECT) {
605 return bp_darray_select(b->da,s,1);
607 return bp_darray_select_bsearch(b->da, s, getpat_preorder);
611 int bp_select_close(bp *b, int s)
613 if (b->opt & OPT_FAST_POSTORDER_SELECT) {
614 return bp_darray_pat_select(b->da_postorder, s, getpat_postorder);
616 return postorder_select_bsearch(b,s);
621 ///////////////////////////////////////////
622 // bp_postorder_rank(bp *b,int s)
623 // returns the postorder (>= 1) of node s (s >= 0)
624 // -1 if s-th bit is not OP
625 ///////////////////////////////////////////
626 int bp_postorder_rank(bp *b,int s)
629 if (bp_inspect(b,s) == CP) return -1;
630 t = bp_find_close(b,s);
631 // return t+1 - darray_rank(b->da,t);
632 return bp_rank_close(b,t);
635 static int postorder_select_bsearch(bp *b,int s)
639 if (s == 0) return -1;
641 if (s > b->da->n - b->da->m) {
644 l = 0; r = b->da->n - 1;
648 //printf("m=%d rank=%d s=%d\n",m,m+1 - darray_rank(b->da,m),s);
649 if (m+1 - bp_darray_rank(b->da,m) >= s) {
658 ///////////////////////////////////////////
659 // bp_postorder_select(bp *b,int s)
660 // returns the position of CP of the node with postorder s (>= 1)
661 ///////////////////////////////////////////
662 int bp_postorder_select(bp *b,int s)
665 if (b->opt & OPT_FAST_POSTORDER_SELECT) {
666 return bp_darray_pat_select(b->da_postorder,s,getpat_postorder);
668 return postorder_select_bsearch(b->da,s);
671 return bp_select_close(b,s);
675 ///////////////////////////////////////////
676 // bp_leaf_rank(bp *b,int s)
677 // returns the number of leaves to the left of s
678 ///////////////////////////////////////////
679 int bp_leaf_rank(bp *b,int s)
681 if ((b->opt & OPT_LEAF) == 0) {
682 printf("leaf_rank: error!!! not supported\n");
688 return bp_darray_pat_rank(b->da_leaf,s,getpat_leaf);
691 ///////////////////////////////////////////
692 // leaf_select(bp *b,int s)
693 // returns the position of s-th leaf
694 ///////////////////////////////////////////
695 int bp_leaf_select(bp *b,int s)
697 if ((b->opt & OPT_LEAF) == 0) {
698 printf("leaf_select: error!!! not supported\n");
701 if (s > b->da_leaf->m) return -1;
702 if (b->opt & OPT_FAST_LEAF_SELECT) {
703 return bp_darray_pat_select(b->da_leaf,s,getpat_leaf);
705 return bp_darray_select_bsearch(b->da_leaf,s,getpat_leaf);
710 ///////////////////////////////////////////
711 // bp_inorder_rank(bp *b,int s)
712 // returns the number of ")(" (s >= 0)
713 ///////////////////////////////////////////
714 int bp_inorder_rank(bp *b,int s)
716 if ((b->opt & OPT_INORDER) == 0) {
717 printf("inorder_rank: error!!! not supported\n");
723 return bp_darray_pat_rank(b->da_inorder,s,getpat_inorder);
726 ///////////////////////////////////////////
727 // bp_inorder_select(bp *b,int s)
728 // returns the s-th position of ")(" (s >= 1)
729 ///////////////////////////////////////////
730 int bp_inorder_select(bp *b,int s)
732 if ((b->opt & OPT_INORDER) == 0) {
733 printf("inorder_select: error!!! not supported\n");
736 if (b->opt & OPT_FAST_INORDER_SELECT) {
737 return bp_darray_pat_select(b->da_inorder,s,getpat_inorder);
739 return bp_darray_select_bsearch(b->da_inorder,s,getpat_inorder);
743 ///////////////////////////////////////////
744 // bp_leftmost_leaf(bp *b, int s)
745 ///////////////////////////////////////////
746 int bp_leftmost_leaf(bp *b, int s)
748 if ((b->opt & OPT_LEAF) == 0) {
749 printf("leftmost_leaf: error!!! not supported\n");
752 return bp_leaf_select(b, bp_leaf_rank(b,s)+1);
755 ///////////////////////////////////////////
756 // rightmost_leaf(bp *b, int s)
757 ///////////////////////////////////////////
758 int bp_rightmost_leaf(bp *b, int s)
761 if ((b->opt & OPT_LEAF) == 0) {
762 printf("leftmost_leaf: error!!! not supported\n");
765 t = bp_find_close(b,s);
766 return bp_leaf_select(b, bp_leaf_rank(b,t));
770 ///////////////////////////////////////////
771 // bp_prev_sibling(bp *b,int s)
772 // returns the previous sibling of parent(s)
773 // -1 if s is the first child
774 //////////////////////////////////////////
775 int bp_prev_sibling(bp *b, int s)
778 if (s == 0) return -1;
779 if (bp_inspect(b,s-1) == OP) return -1;
780 t = bp_find_open(b,s-1);
784 ///////////////////////////////////////////
785 // bp_deepest_node(bp *b,int s)
786 // returns the first node with the largest depth in the subtree of s
787 ///////////////////////////////////////////
788 int bp_deepest_node(bp *b,int s)
791 t = bp_find_close(b,s);
792 m = bp_rmq(b,s,t, OPT_MAX);
796 ///////////////////////////////////////////
797 // bp_subtree_height(bp *b,int s)
798 // returns the height of the subtree of s
800 ///////////////////////////////////////////
801 int bp_subtree_height(bp *b,int s)
804 t = bp_deepest_node(b,s);
805 return bp_depth(b,t) - bp_depth(b,s);
808 int bp_naive_degree(bp *b, int s)
812 t = bp_first_child(b,s);
815 t = bp_next_sibling(b,t);
820 ///////////////////////////////////////////
821 // bp_degree(bp *b, int s)
822 // returns the number of children of s
824 ///////////////////////////////////////////
825 int bp_degree(bp *b, int s)
827 if (b->opt & OPT_DEGREE) {
828 return bp_fast_degree(b,s,b->n,0);
830 return bp_naive_degree(b,s);
834 int bp_naive_child(bp *b, int s, int d)
837 t = bp_first_child(b,s);
838 for (i = 1; i < d; i++) {
840 t = bp_next_sibling(b,t);
845 ///////////////////////////////////////////
846 // bp_child(bp *b, int s, int d)
847 // returns the d-th child of s (1 <= d <= degree(s))
848 // -1 if no such node
849 ///////////////////////////////////////////
850 int bp_child(bp *b, int s, int d)
853 if (b->opt & OPT_DEGREE) {
854 //return find_open(b,fast_degree(b,s,b->n,d));
855 if (d==1) return bp_first_child(b,s);
856 r = bp_fast_degree(b,s,b->n,d-1)+1;
857 if (bp_inspect(b,r) == CP) return -1;
860 return bp_naive_child(b,s,d);
866 int bp_naive_child_rank(bp *b, int t)
872 t = bp_prev_sibling(b,t);
877 ///////////////////////////////////////////
878 // bp_child_rank(bp *b, int t)
879 // returns d if t is the d-th child of the parent of t (d >= 1)
880 // 1 if t is the root
881 ///////////////////////////////////////////
882 int bp_child_rank(bp *b, int t)
885 if (t == bp_root_node(b)) return 1;
886 if (b->opt & OPT_DEGREE) {
888 return bp_fast_degree(b,r,t,0)+1;
890 return bp_naive_child_rank(b,t);
895 ///////////////////////////////////////////
896 // distance(bp *b, int s, int t)
897 // returns the length of the shortest path from s to t in the tree
898 ///////////////////////////////////////////
899 int bp_distance(bp *b, int s, int t)
904 return (bp_depth(b,s) - d) + (bp_depth(b,t) - d);
907 ///////////////////////////////////////////
908 // level_next(bp *b, int d)
909 ///////////////////////////////////////////
910 int bp_level_next(bp *b,int s)
913 t = bp_fwd_excess(b,s,0);
917 ///////////////////////////////////////////
918 // level_prev(bp *b, int d)
919 ///////////////////////////////////////////
920 int bp_level_prev(bp *b,int s)
923 t = bp_bwd_excess(b,s,0);
927 ///////////////////////////////////////////
928 // bp_level_leftmost(bp *b, int d)
929 ///////////////////////////////////////////
930 int bp_level_leftmost(bp *b, int d)
933 if (d < 1) return -1;
934 if (d == 1) return 0;
935 t = bp_fwd_excess(b,0,d);
939 ///////////////////////////////////////////
940 // bp_level_rigthmost(bp *b, int d)
941 ///////////////////////////////////////////
942 int level_rigthmost(bp *b, int d)
945 if (d < 1) return -1;
946 if (d == 1) return 0;
947 t = bp_bwd_excess(b,0,d-1);
948 return bp_find_open(b,t);
951 ///////////////////////////////////////////
952 // leaf_size(bp *b, int s)
953 ///////////////////////////////////////////
954 int bp_leaf_size(bp *b, int s)
957 if ((b->opt & OPT_LEAF) == 0) {
958 printf("leaf_size: error!!! not supported\n");
961 t = bp_find_close(b,s);
962 return bp_leaf_rank(b,t) - bp_leaf_rank(b,s);