-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rank9.java
100 lines (83 loc) · 3.12 KB
/
Rank9.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Sux4J: Succinct data structures for Java
*
* Copyright (C) 2008-2013 Sebastiano Vigna
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*/
/** A <code>rank9</code> implementation.
*
* <p><code>rank9</code> is a
* ranking structure using just 25% additional space
* and providing exceptionally fast ranking (on an Opteron at 2800 MHz this class
* ranks a million-bit array in less than 8 nanoseconds). */
public class Rank9 {
//Commented by FC> private static final boolean ASSERTS = false;
//Commented by FC> private static final long serialVersionUID = 1L;
protected transient long[] bits;
final protected IntArray bitVector;
final protected long[] count;
final protected int numWords;
final protected long numOnes;
final protected long lastOne;
public Rank9( final IntArray bitVector ) {
this.bitVector = bitVector;
this.bits = bitVector.array;
final long length = bitVector.length();
numWords = (int)( ( length + Long.SIZE - 1 ) / Long.SIZE );
final int numCounts = (int)( ( length + 8 * Long.SIZE - 1 ) / ( 8 * Long.SIZE ) ) * 2;
// Init rank/select structure
count = new long[ numCounts + 1 ];
long c = 0, l = -1;
int pos = 0;
for( int i = 0; i < numWords; i += 8, pos += 2 ) {
count[ pos ] = c;
c += Long.bitCount( bits[ i ] );
if ( bits[ i ] != 0 ) l = i * 64L + Utils.mostSignificantBit( bits[ i ] );
for( int j = 1; j < 8; j++ ) {
count[ pos + 1 ] |= ( i + j <= numWords ? c - count[ pos ] : 0x1FFL ) << 9 * ( j - 1 );
if ( i + j < numWords ) {
c += Long.bitCount( bits[ i + j ] );
if ( bits[ i + j ] != 0 ) l = ( i + j ) * 64L + Utils.mostSignificantBit( bits[ i + j ] );
}
}
}
numOnes = c;
lastOne = l;
count[ numCounts ] = c;
}
public long rank( long pos ) {
//Commented by FC> if ( ASSERTS ) assert pos >= 0;
//Commented by FC> if ( ASSERTS ) assert pos <= bitVector.length();
// This test can be eliminated if there is always an additional word at the end of the bit array.
if ( pos > lastOne ) return numOnes;
final int word = (int)( pos / 64 );
final int block = word / 4 & ~1;
final int offset = word % 8 - 1;
return count[ block ] + ( count[ block + 1 ] >>> ( offset + ( offset >>> 32 - 4 & 0x8 ) ) * 9 & 0x1FF ) + Long.bitCount( bits[ word ] & ( ( 1L << pos % 64 ) - 1 ) );
}
public long numBits() {
return count.length * (long)Long.SIZE;
}
public long count() {
return numOnes;
}
public long rank( long from, long to ) {
return rank( to ) - rank( from );
}
public long lastOne() {
return lastOne;
}
}