File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\util\HashBase.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.util;
8
9
10 /**
11 * The base for other hash classes.
12 */
13 public abstract class HashBase {
14
15 private static final int MAX_LOAD = 90;
16
17 /**
18 * The bit mask to get the index from the hash code.
19 */
20 protected int mask;
21
22 /**
23 * The number of slots in the table.
24 */
25 protected int len;
26
27 /**
28 * The number of occupied slots, excluding the zero key (if any).
29 */
30 protected int size;
31
32 /**
33 * The number of deleted slots.
34 */
35 protected int deletedCount;
36
37 /**
38 * The level. The number of slots is 2 ^ level.
39 */
40 protected int level;
41
42 /**
43 * Whether the zero key is used.
44 */
45 protected boolean zeroKey;
46
47 private int maxSize, minSize, maxDeleted;
48
49 public HashBase() {
50 reset(2);
51 }
52
53 /**
54 * Increase the size of the underlying table and re-distribute the elements.
55 *
56 * @param newLevel the new level
57 */
58 protected abstract void rehash(int newLevel);
59
60 /**
61 * Get the size of the map.
62 *
63 * @return the size
64 */
65 public int size() {
66 return size + (zeroKey ? 1 : 0);
67 }
68
69 /**
70 * Check the size before adding an entry. This method resizes the map if
71 * required.
72 */
73 void checkSizePut() {
74 if (deletedCount > size) {
75 rehash(level);
76 }
77 if (size + deletedCount >= maxSize) {
78 rehash(level + 1);
79 }
80 }
81
82 /**
83 * Check the size before removing an entry. This method resizes the map if
84 * required.
85 */
86 protected void checkSizeRemove() {
87 if (size < minSize && level > 0) {
88 rehash(level - 1);
89 } else if (deletedCount > maxDeleted) {
90 rehash(level);
91 }
92 }
93
94 /**
95 * Clear the map and reset the level to the specified value.
96 *
97 * @param newLevel the new level
98 */
99 protected void reset(int newLevel) {
100 minSize = size * 3 / 4;
101 size = 0;
102 level = newLevel;
103 len = 2 << level;
104 mask = len - 1;
105 maxSize = (int) (len * MAX_LOAD / 100L);
106 deletedCount = 0;
107 maxDeleted = 20 + len / 2;
108 }
109
110 /**
111 * Calculate the index for this hash code.
112 *
113 * @param hash the hash code
114 * @return the index
115 */
116 protected int getIndex(int hash) {
117 return hash & mask;
118 }
119
120 }
121