KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > util > BitField


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.util;
6
7
8 /**
9  * @author Thomas
10  */

11
12 public class BitField {
13     
14     private long[] data = new long[10];
15     private static final int ADDRESS_BITS = 6;
16     private static final int BITS = 64;
17     private static final int ADDRESS_MASK = BITS - 1;
18     
19     public int getLastSetBit() {
20         int i = (data.length << ADDRESS_BITS) - 1;
21         while(i >= 0) {
22             if(get(i)) {
23                 return i;
24             }
25             i--;
26         }
27         return -1;
28     }
29     
30     public int nextSetBit(int fromIndex) {
31         int i = fromIndex >> ADDRESS_BITS;
32         int max = data.length;
33         int maxAddress = data.length << ADDRESS_BITS;
34         for(; i<max; i++) {
35             if(data[i] == 0) {
36                 continue;
37             }
38             for(int j=Math.max(fromIndex, i << ADDRESS_BITS); j<maxAddress; j++) {
39                 if(get(j)) {
40                     return j;
41                 }
42             }
43         }
44         return -1;
45     }
46     
47     public int nextClearBit(int fromIndex) {
48         int i = fromIndex >> ADDRESS_BITS;
49         int max = data.length;
50         for(; i<max; i++) {
51             if(data[i] == -1) {
52                 continue;
53             }
54             for(int j=Math.max(fromIndex, i << ADDRESS_BITS); ; j++) {
55                 if(!get(j)) {
56                     return j;
57                 }
58             }
59         }
60         return fromIndex;
61     }
62     
63     public long getLong(int i) {
64         int addr = getAddress(i);
65         if(addr >= data.length) {
66             return 0;
67         }
68         return data[addr];
69     }
70
71     public boolean get(int i) {
72         int addr = getAddress(i);
73         if(addr >= data.length) {
74             return false;
75         }
76         return (data[addr] & getBitMask(i)) != 0;
77     }
78     
79     public void set(int i) {
80         int addr = getAddress(i);
81         checkCapacity(addr);
82         data[addr] |= getBitMask(i);
83     }
84     
85     public void clear(int i) {
86         int addr = getAddress(i);
87         if(addr >= data.length) {
88             return;
89         }
90         data[addr] &= ~getBitMask(i);
91     }
92     
93     private int getAddress(int i) {
94         return i >> ADDRESS_BITS;
95     }
96     
97     private long getBitMask(int i) {
98         return 1L << (i & ADDRESS_MASK);
99     }
100     
101     private void checkCapacity(int size) {
102         while (size >= data.length) {
103             int newSize = data.length == 0 ? 1 : data.length * 2;
104             long[] d = new long[newSize];
105             System.arraycopy(data, 0, d, 0, data.length);
106             data = d;
107         }
108     }
109
110     /*
111     private BitSet data = new BitSet();
112     
113     public boolean get(int i) {
114         return data.get(i);
115     }
116     
117     public void set(int i) {
118         data.set(i);
119     }
120     
121     public void clear(int i) {
122         data.clear(i);
123     }
124     */

125     
126     public void setRange(int start, int len, boolean value) {
127         for(int end = start + len; start < end; start++) {
128             set(start, value);
129         }
130     }
131     
132     private void set(int i, boolean value) {
133         if(value) {
134             set(i);
135         } else {
136             clear(i);
137         }
138     }
139
140 }
141
Popular Tags