X-Git-Url: http://git.nguyen.vg/gitweb/?p=SXSI%2Flibbp.git;a=blobdiff_plain;f=bp.h;h=e0eb4c9ad241c1a90055ea2a4af7674fefd19078;hp=a72888108b820a89a1f08c7dd5f33c939e924b42;hb=HEAD;hpb=89acd756a2eca5e47911e24b212f68cf7e3528bf diff --git a/bp.h b/bp.h index a728881..e0eb4c9 100644 --- a/bp.h +++ b/bp.h @@ -86,6 +86,14 @@ static inline int bp_root_node(bp *b) return 0; } +/////////////////////////////////////////// +// inspect(bp *b, int s) +// returns OP (==1) or CP (==0) at s-th bit (0 <= s < n) +/////////////////////////////////////////// +static inline int bp_inspect(bp *b, int s) +{ + return bp_getbit(b->B, s); +} /////////////////////////////////////////// // find_close(bp *b,int s) @@ -93,7 +101,11 @@ static inline int bp_root_node(bp *b) /////////////////////////////////////////// static inline int bp_find_close(bp *b,int s) { - return bp_fwd_excess(b, s, -1); + int s1 = s + 1; + if (bp_inspect(b, s1) == CP) + return s1; + else + return bp_fwd_excess(b, s, -1); } /////////////////////////////////////////// @@ -114,7 +126,9 @@ static inline int bp_find_open(bp *b,int s) /////////////////////////////////////////// static inline int bp_parent(bp *b, int s) { - int r = bp_bwd_excess(b,s,-2); + int r; + if (bp_getbit(b->B, s - 1) == OP) return s - 1; + r = bp_bwd_excess(b,s,-2); return (r >= -1) ? r+1 : -1; } @@ -185,15 +199,6 @@ static inline int bp_preorder_select(bp *b,int s) int bp_postorder_rank(bp *b,int s); -/////////////////////////////////////////// -// inspect(bp *b, int s) -// returns OP (==1) or CP (==0) at s-th bit (0 <= s < n) -/////////////////////////////////////////// -static inline int bp_inspect(bp *b, int s) -{ - return bp_getbit(b->B, s); -} - static inline int bp_isleaf(bp *b, int s) { return (bp_inspect(b, s+1) == CP); @@ -216,7 +221,8 @@ static inline int bp_subtree_size(bp *b, int s) static inline int bp_first_child(bp *b, int s) { - return (bp_inspect(b, s+1) == CP) ? -1 : s+1; + int s1 = s + 1; + return bp_inspect(b, s1) ? s1 : -1; } @@ -228,8 +234,8 @@ static inline int bp_first_child(bp *b, int s) static inline int bp_next_sibling(bp *b, int s) { int t; - t = bp_find_close(b,s) + 1; - return (bp_inspect(b, t) == CP) ? -1 : t; + t = bp_find_close(b, s) + 1; + return bp_inspect(b, t) ? t : -1; } @@ -288,6 +294,13 @@ int bp_child(bp *b, int s, int d); // new functions for persistence purposes, added by Diego Arroyuelo void saveTree(bp *b, FILE *fp); bp * loadTree(FILE *fp); + +//0: success 1: failure (errno) +int bp_save(bp *b, int fd); + +//non-null: sucess, null: failure (errno) + +bp * bp_load(int fd); void bp_delete(bp *b); @@ -306,8 +319,6 @@ static inline int blog(int x) extern int *matchtbl,*parenttbl; -void make_naivetbl(pb *B,int n); - extern int fwdtbl[(2*ETW+1)*(1<