1 30 31 32 package org.hsqldb.store; 33 34 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 63 public void reset() { 64 map = new int[defaultCapacity / 32]; 65 capacity = defaultCapacity; 66 } 67 68 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 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 |