public override int Rank1(int pos)
{
if (pos < 0 || this.Count1 == 0) {
return 0;
}
int rank0_prev = 1 + (pos >> this.GetNumLowerBits ());
int pos_prev = this.H.Select0 (rank0_prev);
// Remember that $pos = rank0 + rank1 - 1$, thus $rank1 = pos - rank0 + 1$
//int rank1_prevX = this.H.Rank1 (pos_prev); // prevcount = rank1
int rank1_prev = pos_prev - rank0_prev + 1;
//int rank1_nextX = this.H.Rank1 (this.H.Select0 (rank0_prev + 1));
int pos_next = this.H.Select0 (rank0_prev + 1);
int rank1_next = pos_next - rank0_prev;
uint pos_masked = (uint)(this.get_mask () & pos);
// Console.WriteLine ("xxxxx {0}", nextcount - prevcount);
int count = rank1_next - rank1_prev;
if (count < 16) {
// if (true) {
// sequential search
int rank = rank1_prev;
for (int i = 0; i < count; i++) {
var u = this.L [rank];
rank++;
if (u >= pos_masked) {
if (u > pos_masked) {
rank--;
}
break;
}
}
return rank;
} else {
// binary search
//return 1 + GenericSearch.FindLast<int> ((int)pos_masked, this.L, rank1_prev, rank1_next);
return 1 + Search.FindLast ((int)pos_masked, this.L, rank1_prev, rank1_next);
}
}