aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/jogamp/common/util/Int32ArrayBitfield.java
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2015-08-02 01:12:21 +0200
committerSven Gothel <[email protected]>2015-08-02 01:12:21 +0200
commit047e9adaf2a5f51f7acfa194a744c99b6bfadaea (patch)
treec701ab77a9387cdec1a61c51b9ed289d5aa755e2 /src/java/jogamp/common/util/Int32ArrayBitfield.java
parent805800e9b02acf54a6cc5a04ce94c9b465c42f43 (diff)
Bitfield: Refine API method names, add clearField(boolean), fix put32(..) and bitCount(), add unit test (passed)
Diffstat (limited to 'src/java/jogamp/common/util/Int32ArrayBitfield.java')
-rw-r--r--src/java/jogamp/common/util/Int32ArrayBitfield.java39
1 files changed, 26 insertions, 13 deletions
diff --git a/src/java/jogamp/common/util/Int32ArrayBitfield.java b/src/java/jogamp/common/util/Int32ArrayBitfield.java
index dd18b3d..3dcb9ae 100644
--- a/src/java/jogamp/common/util/Int32ArrayBitfield.java
+++ b/src/java/jogamp/common/util/Int32ArrayBitfield.java
@@ -44,16 +44,29 @@ public class Int32ArrayBitfield implements Bitfield {
* @param storageBitSize
*/
public Int32ArrayBitfield(final int storageBitSize) {
- final int units = Math.max(1, ( storageBitSize + 7 ) >>> UNIT_SHIFT);
- this.storage = new int[units];
+ final int units = Math.max(1, ( storageBitSize + 31 ) >>> UNIT_SHIFT);
+ this.storage = new int[units]; // initialized w/ default '0'
this.bitSize = units << UNIT_SHIFT;
}
@Override
- public int getStorageBitSize() {
+ public int size() {
return bitSize;
}
+ @Override
+ public final void clearField(final boolean bit) {
+ final int v;
+ if( bit ) {
+ v = Bitfield.UNSIGNED_INT_MAX_VALUE;
+ } else {
+ v = 0;
+ }
+ for(int i=storage.length-1; i>=0; i--) {
+ storage[i] = v;
+ }
+ }
+
private static final void check(final int size, final int bitnum) throws IndexOutOfBoundsException {
if( 0 > bitnum || bitnum >= size ) {
throw new IndexOutOfBoundsException("Bitnum should be within [0.."+(size-1)+"], but is "+bitnum);
@@ -70,14 +83,14 @@ public class Int32ArrayBitfield implements Bitfield {
final int left = 32 - ( lowBitnum - ( u << UNIT_SHIFT ) ); // remaining bits of first chunk storage
if( 32 == left ) {
// fast path
- final int m = ( 1 << length ) - 1;
+ final int m = Util.getBitMask(length); // mask of chunk
return m & storage[u];
} else {
// slow path
- final int l = Math.min(length, left); // length of first chunk
+ final int l = Math.min(length, left); // length of first chunk < 32
final int m = ( 1 << l ) - 1; // mask of first chunk
final int d = m & ( storage[u] >>> lowBitnum );
- final int l2 = length - l; // length of last chunk
+ final int l2 = length - l; // length of last chunk < 32
if( l2 > 0 ) {
final int m2 = ( 1 << l2 ) - 1; // mask of last chunk
return d | ( ( m2 & storage[u+1] ) << l );
@@ -96,20 +109,20 @@ public class Int32ArrayBitfield implements Bitfield {
final int left = 32 - ( lowBitnum - ( u << UNIT_SHIFT ) ); // remaining bits of first chunk storage
if( 32 == left ) {
// fast path
- final int m = ( 1 << length ) - 1; // mask of chunk
+ final int m = Util.getBitMask(length); // mask of chunk
storage[u] = ( ( ~m ) & storage[u] ) // keep non-written storage bits
| ( m & data ); // overwrite storage w/ used data bits
} else {
// slow path
- final int l = Math.min(length, left); // length of first chunk
+ final int l = Math.min(length, left); // length of first chunk < 32
final int m = ( 1 << l ) - 1; // mask of first chunk
storage[u] = ( ( ~( m << lowBitnum ) ) & storage[u] ) // keep non-written storage bits
| ( ( m & data ) << lowBitnum ); // overwrite storage w/ used data bits
- final int l2 = length - l; // length of last chunk
+ final int l2 = length - l; // length of last chunk < 32
if( l2 > 0 ) {
final int m2 = ( 1 << l2 ) - 1; // mask of last chunk
- storage[u] = ( ( ~m2 ) & storage[u+1] ) // keep non-written storage bits
- | ( m2 & ( data >>> l ) ); // overwrite storage w/ used data bits
+ storage[u+1] = ( ( ~m2 ) & storage[u+1] ) // keep non-written storage bits
+ | ( m2 & ( data >>> l ) ); // overwrite storage w/ used data bits
}
}
}
@@ -180,10 +193,10 @@ public class Int32ArrayBitfield implements Bitfield {
}
@Override
- public int getBitCount() {
+ public int bitCount() {
int c = 0;
for(int i = storage.length-1; i>=0; i--) {
- c += Bitfield.Util.getBitCount(storage[i]);
+ c += Bitfield.Util.bitCount(storage[i]);
}
return c;
}