public override int Select1(int rank)
{
if (rank < 1) {
return -1;
}
int blockIndex = GenericSearch.FindFirst<int> (rank, this.AbsRank);
if (this.AbsRank[blockIndex] == rank) {
blockIndex--;
}
int classIndex = blockIndex * this.BlockSize;
rank -= this.AbsRank[blockIndex];
int pos = classIndex * 15;
int offset = this.AbsOffset[blockIndex];
int klass;
var ctx = new CtxCache (-1);
while (rank > 0) {
klass = this.DecodeClass (classIndex, ctx);
if (rank - klass <= 0) {
break;
}
classIndex++;
pos += 15;
rank -= klass;
int numbits = NumBits[klass];
offset += numbits;
}
if (rank > 0) {
klass = this.DecodeClass (classIndex, ctx);
uint block = (uint)this.ReadBlock (klass, offset);
// Console.WriteLine ("rank: {0}, pos: {1}, block: {2}, klass: {3}, offset: {4}",
// rank, pos, block, klass, offset);
pos += BitAccess.Select1 (block, rank);
}
return pos;
}