comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package org.dancres.struct;
2
3 /**
4 A class to take a bit array and iterate over them reporting offset of
5 each set bit.
6 */
7 public class BitVisitorImpl implements BitVisitor {
8 private Object[] theLocks;
9 private long[] theBits;
10
11 private int theBitGroupIndex = 0;
12 private int theBitGroupPtr = 0;
13
14 private int theBase = 0;
15
16 private long theMask = 1L;
17
18 private boolean testZero;
19
20 public BitVisitorImpl(long[] aBits, Object[] aLocks, boolean findZero) {
21 theBits = aBits;
22 theLocks = aLocks;
23 testZero = findZero;
24 }
25
26 /**
27 @return -1 when no bits are left
28 */
29 public int getNext() {
30 while (theBitGroupIndex < theBits.length) {
31 long myLong;
32
33 synchronized(theLocks[theBitGroupIndex]) {
34 myLong = theBits[theBitGroupIndex];
35 }
36
37 if (myLong != 0) {
38 while (theBitGroupPtr < 64) {
39 ++theBitGroupPtr;
40
41 if (testZero) {
42 if ((myLong & theMask) == 0) {
43 theMask = theMask << 1;
44 return (theBase + theBitGroupPtr - 1);
45 } else
46 theMask = theMask << 1;
47 } else {
48 if ((myLong & theMask) != 0) {
49 theMask = theMask << 1;
50 return (theBase + theBitGroupPtr - 1);
51 } else
52 theMask = theMask << 1;
53 }
54 }
55
56 // Get ready for next block
57 theBitGroupPtr = 0;
58 }
59
60 theBase += 64;
61 ++theBitGroupIndex;
62 theMask = 1L;
63 }
64
65 return -1;
66 }
67 }