Mercurial > hg > blitz_condensed
diff src/org/dancres/struct/BitVisitorImpl.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/dancres/struct/BitVisitorImpl.java Sat Mar 21 11:00:06 2009 +0000 @@ -0,0 +1,67 @@ +package org.dancres.struct; + +/** + A class to take a bit array and iterate over them reporting offset of + each set bit. +*/ +public class BitVisitorImpl implements BitVisitor { + private Object[] theLocks; + private long[] theBits; + + private int theBitGroupIndex = 0; + private int theBitGroupPtr = 0; + + private int theBase = 0; + + private long theMask = 1L; + + private boolean testZero; + + public BitVisitorImpl(long[] aBits, Object[] aLocks, boolean findZero) { + theBits = aBits; + theLocks = aLocks; + testZero = findZero; + } + + /** + @return -1 when no bits are left + */ + public int getNext() { + while (theBitGroupIndex < theBits.length) { + long myLong; + + synchronized(theLocks[theBitGroupIndex]) { + myLong = theBits[theBitGroupIndex]; + } + + if (myLong != 0) { + while (theBitGroupPtr < 64) { + ++theBitGroupPtr; + + if (testZero) { + if ((myLong & theMask) == 0) { + theMask = theMask << 1; + return (theBase + theBitGroupPtr - 1); + } else + theMask = theMask << 1; + } else { + if ((myLong & theMask) != 0) { + theMask = theMask << 1; + return (theBase + theBitGroupPtr - 1); + } else + theMask = theMask << 1; + } + } + + // Get ready for next block + theBitGroupPtr = 0; + } + + theBase += 64; + ++theBitGroupIndex; + theMask = 1L; + } + + return -1; + } +} \ No newline at end of file