1 30 31 32 package org.hsqldb.store; 33 34 56 class HashIndex { 57 58 int[] hashTable; 59 int[] linkTable; 60 int newNodePointer; 61 int elementCount; 62 int reclaimedNodePointer = -1; 63 boolean fixedSize; 64 65 HashIndex(int hashTableSize, int capacity, boolean fixedSize) { 66 67 reset(hashTableSize, capacity); 68 69 this.fixedSize = fixedSize; 70 } 71 72 78 void reset(int hashTableSize, int capacity) { 79 80 int[] newHT = new int[hashTableSize]; 81 int[] newLT = new int[capacity]; 82 83 hashTable = newHT; 85 linkTable = newLT; 86 87 resetTables(); 88 } 89 90 void resetTables() { 91 92 int to = hashTable.length; 93 int[] intArray = hashTable; 94 95 while (--to >= 0) { 96 intArray[to] = -1; 97 } 98 99 newNodePointer = 0; 100 elementCount = 0; 101 reclaimedNodePointer = -1; 102 } 103 104 107 void clear() { 108 109 int to = linkTable.length; 110 int[] intArray = linkTable; 111 112 while (--to >= 0) { 113 intArray[to] = 0; 114 } 115 116 resetTables(); 117 } 118 119 122 int getHashIndex(int hash) { 123 return (hash & 0x7fffffff) % hashTable.length; 124 } 125 126 132 int getLookup(int hash) { 133 134 int index = (hash & 0x7fffffff) % hashTable.length; 135 136 return hashTable[index]; 137 } 138 139 145 int getNextLookup(int lookup) { 146 return linkTable[lookup]; 147 } 148 149 156 int linkNode(int index, int lastLookup) { 157 158 int lookup = reclaimedNodePointer; 160 161 if (lookup == -1) { 162 lookup = newNodePointer++; 163 } else { 164 165 reclaimedNodePointer = linkTable[lookup]; 167 } 168 169 if (lastLookup == -1) { 171 hashTable[index] = lookup; 172 } else { 173 linkTable[lastLookup] = lookup; 174 } 175 176 linkTable[lookup] = -1; 177 178 elementCount++; 179 180 return lookup; 181 } 182 183 190 void unlinkNode(int index, int lastLookup, int lookup) { 191 192 if (lastLookup == -1) { 194 hashTable[index] = linkTable[lookup]; 195 } else { 196 linkTable[lastLookup] = linkTable[lookup]; 197 } 198 199 linkTable[lookup] = reclaimedNodePointer; 201 reclaimedNodePointer = lookup; 202 203 elementCount--; 204 } 205 206 215 boolean removeEmptyNode(int lookup) { 216 217 boolean found = false; 218 int lastLookup = -1; 219 220 for (int i = reclaimedNodePointer; i >= 0; 221 lastLookup = i, i = linkTable[i]) { 222 if (i == lookup) { 223 if (lastLookup == -1) { 224 reclaimedNodePointer = linkTable[lookup]; 225 } else { 226 linkTable[lastLookup] = linkTable[lookup]; 227 } 228 229 found = true; 230 231 break; 232 } 233 } 234 235 if (!found) { 236 return false; 237 } 238 239 for (int i = 0; i < newNodePointer; i++) { 240 if (linkTable[i] > lookup) { 241 linkTable[i]--; 242 } 243 } 244 245 System.arraycopy(linkTable, lookup + 1, linkTable, lookup, 246 newNodePointer - lookup - 1); 247 248 linkTable[newNodePointer - 1] = 0; 249 250 newNodePointer--; 251 252 for (int i = 0; i < hashTable.length; i++) { 253 if (hashTable[i] > lookup) { 254 hashTable[i]--; 255 } 256 } 257 258 return true; 259 } 260 } 261 | Popular Tags |