28import org.jau.util.BitMath;
29import org.jau.util.Bitfield;
38 private static final int UNIT_SHIFT = 5;
39 private final int[] storage;
40 private final int bitSize;
46 final int units = Math.max(1, ( storageBitSize + 31 ) >>> UNIT_SHIFT);
47 this.storage =
new int[units];
48 this.bitSize = units << UNIT_SHIFT;
64 for(
int i=storage.length-1; i>=0; i--) {
69 private static final void check(
final int size,
final int bitnum)
throws IndexOutOfBoundsException {
70 if( 0 > bitnum || bitnum >=
size ) {
71 throw new IndexOutOfBoundsException(
"Bitnum should be within [0.."+(
size-1)+
"], but is "+bitnum);
76 public final int get32(
final int lowBitnum,
final int length)
throws IndexOutOfBoundsException {
77 if( 0 > length || length > 32 ) {
78 throw new IndexOutOfBoundsException(
"length should be within [0..32], but is "+length);
80 check(bitSize-length+1, lowBitnum);
81 final int u = lowBitnum >>> UNIT_SHIFT;
82 final int left = 32 - ( lowBitnum - ( u << UNIT_SHIFT ) );
86 return m & storage[u];
89 final int l = Math.min(length, left);
90 final int m = ( 1 << l ) - 1;
91 final int d = m & ( storage[u] >>> lowBitnum );
92 final int l2 = length - l;
94 final int m2 = ( 1 << l2 ) - 1;
95 return d | ( ( m2 & storage[u+1] ) << l );
102 public final void put32(
final int lowBitnum,
final int length,
final int data)
throws IndexOutOfBoundsException {
103 if( 0 > length || length > 32 ) {
104 throw new IndexOutOfBoundsException(
"length should be within [0..32], but is "+length);
106 check(bitSize-length+1, lowBitnum);
107 final int u = lowBitnum >>> UNIT_SHIFT;
108 final int left = 32 - ( lowBitnum - ( u << UNIT_SHIFT ) );
112 storage[u] = ( ( ~m ) & storage[u] )
116 final int l = Math.min(length, left);
117 final int m = ( 1 << l ) - 1;
118 storage[u] = ( ( ~( m << lowBitnum ) ) & storage[u] )
119 | ( ( m & data ) << lowBitnum );
120 final int l2 = length - l;
122 final int m2 = ( 1 << l2 ) - 1;
123 storage[u+1] = ( ( ~m2 ) & storage[u+1] )
124 | ( m2 & ( data >>> l ) );
129 public final int copy32(
final int srcBitnum,
final int dstBitnum,
final int length)
throws IndexOutOfBoundsException {
130 final int data =
get32(srcBitnum, length);
131 put32(dstBitnum, length, data);
136 public final boolean get(
final int bitnum)
throws IndexOutOfBoundsException {
137 check(bitSize, bitnum);
138 final int u = bitnum >>> UNIT_SHIFT;
139 final int b = bitnum - ( u << UNIT_SHIFT );
140 return 0 != ( storage[u] & ( 1 << b ) ) ;
144 public final boolean put(
final int bitnum,
final boolean bit)
throws IndexOutOfBoundsException {
145 check(bitSize, bitnum);
146 final int u = bitnum >>> UNIT_SHIFT;
147 final int b = bitnum - ( u << UNIT_SHIFT );
148 final int m = 1 << b;
149 final boolean prev = 0 != ( storage[u] & m ) ;
160 public final void set(
final int bitnum)
throws IndexOutOfBoundsException {
161 check(bitSize, bitnum);
162 final int u = bitnum >>> UNIT_SHIFT;
163 final int b = bitnum - ( u << UNIT_SHIFT );
164 final int m = 1 << b;
168 public final void clear(
final int bitnum)
throws IndexOutOfBoundsException {
169 check(bitSize, bitnum);
170 final int u = bitnum >>> UNIT_SHIFT;
171 final int b = bitnum - ( u << UNIT_SHIFT );
172 final int m = 1 << b;
176 public final boolean copy(
final int srcBitnum,
final int dstBitnum)
throws IndexOutOfBoundsException {
177 check(bitSize, srcBitnum);
178 check(bitSize, dstBitnum);
182 final int u = srcBitnum >>> UNIT_SHIFT;
183 final int b = srcBitnum - ( u << UNIT_SHIFT );
184 bit = 0 != ( storage[u] & ( 1 << b ) ) ;
187 final int u = dstBitnum >>> UNIT_SHIFT;
188 final int b = dstBitnum - ( u << UNIT_SHIFT );
189 final int m = 1 << b;
201 for(
int i = storage.length-1; i>=0; i--) {
Simple bitfield interface for efficient storage access in O(1).
final void clear(final int bitnum)
Clear the bit at position bitnum according to bit.
int size()
Returns the storage size in bit units, e.g.
int bitCount()
Returns the number of one bits within this bitfield.
final boolean copy(final int srcBitnum, final int dstBitnum)
Copies the bit at position srcBitnum to position dstBitnum and returning true if the bit is set,...
final void put32(final int lowBitnum, final int length, final int data)
Puts length bits of given data into this storage, starting w/ the lowest bit to the storage position ...
final int copy32(final int srcBitnum, final int dstBitnum, final int length)
Copies length bits at position srcLowBitnum to position dstLowBitnum and returning the bits.
final void clearField(final boolean bit)
Set all bits of this bitfield to the given value bit.
final int get32(final int lowBitnum, final int length)
Returns length bits from this storage, starting with the lowest bit from the storage position lowBitn...
Int32ArrayBitfield(final int storageBitSize)
final boolean put(final int bitnum, final boolean bit)
Set or clear the bit at position bitnum according to bit and return the previous value.
static final int bitCount(int n)
Returns the number of set bits within given 32bit integer in O(1) using a HAKEM 169 Bit Count inspire...
static int getBitMask(final int n)
Returns the 32 bit mask of n-bits, i.e.
static final int UNSIGNED_INT_MAX_VALUE
Maximum 32 bit Unsigned Integer Value: 0xffffffff == {@value}.
Simple bitfield interface for efficient bit storage access in O(1).