+
+static inline int bp_darray_rank(darray *da, int i)
+{
+ int r,j,i_rr, i_rrr;
+ int offset;
+ pb *p;
+ byte *buff;
+ i_rr = i >> logRR;
+ i_rrr = i >> logRRR;
+ r = da->rl[i>>logR] + da->rm[i_rr];
+
+ j = (i_rrr) & (RR/RRR-1);
+ offset = i_rr << (logRR-logRRR);
+ buff = &(da->rs[offset-1]);
+ while (j > 0) {
+ r += buff[j];
+ j--;
+ }
+
+ p = da->buf + ((i_rrr)<<(logRRR-logD));
+ j = i & (RRR-1);
+ while (j >= D) {
+ r += popcount(*p++);
+ j -= D;
+ }
+ r += popcount(*p >> (D-1-j));
+
+ return r;
+}
+