KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > store > BitMap


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.store;
33
34 /**
35  * Implementation of a bit map of any size, together with static methods to
36  * manipulate int values as bit maps.
37  *
38 * @author fredt@users
39 * @version 1.8.0
40 * @since 1.8.0
41 */

42 public class BitMap {
43
44     int defaultCapacity;
45     int capacity;
46     int[] map;
47
48     public BitMap(int initialCapacity) {
49
50         int words = initialCapacity / 32;
51
52         if (initialCapacity % 32 != 0) {
53             words++;
54         }
55
56         defaultCapacity = capacity = words * 32;
57         map = new int[words];
58     }
59
60     /**
61      * Resets to blank with original capacity
62      */

63     public void reset() {
64         map = new int[defaultCapacity / 32];
65         capacity = defaultCapacity;
66     }
67
68     /**
69      * Sets pos and returns old value
70      */

71     public int set(int pos) {
72
73         while (pos >= capacity) {
74             doubleCapacity();
75         }
76
77         int windex = pos >> 5;
78         int mask = 0x80000000 >>> (pos & 0x1F);
79         int word = map[windex];
80         int result = (word & mask) == 0 ? 0
81                                         : 1;
82
83         map[windex] = (word | mask);
84
85         return result;
86     }
87
88     /**
89      * Unsets pos and returns old value
90      */

91     public int unset(int pos) {
92
93         if (pos >= capacity) {
94             return 0;
95         }
96
97         int windex = pos >> 5;
98         int mask = 0x80000000 >>> (pos & 0x1F);
99         int word = map[windex];
100         int result = (word & mask) == 0 ? 0
101                                         : 1;
102
103         mask = ~mask;
104         map[windex] = (word & mask);
105
106         return result;
107     }
108
109     public int get(int pos) {
110
111         while (pos >= capacity) {
112             doubleCapacity();
113         }
114
115         int windex = pos >> 5;
116         int mask = 0x80000000 >>> (pos & 0x1F);
117         int word = map[windex];
118
119         return (word & mask) == 0 ? 0
120                                   : 1;
121     }
122
123     public static int set(int map, int pos) {
124
125         int mask = 0x80000000 >>> pos;
126
127         return (map | mask);
128     }
129
130     public static int unset(int map, int pos) {
131
132         int mask = 0x80000000 >>> pos;
133
134         mask = ~mask;
135
136         return (map & mask);
137     }
138
139     public static boolean isSet(int map, int pos) {
140
141         int mask = 0x80000000 >>> pos;
142
143         return (map & mask) == 0 ? false
144                                  : true;
145     }
146
147     private void doubleCapacity() {
148
149         int[] newmap = new int[map.length * 2];
150
151         capacity *= 2;
152
153         System.arraycopy(map, 0, newmap, 0, map.length);
154
155         map = newmap;
156     }
157 }
158
Popular Tags