File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\util\ValueHashMap.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 import java.util.ArrayList;
10 import org.h2.message.DbException;
11 import org.h2.value.Value;
12 import org.h2.value.ValueNull;
13
14 /**
15 * This hash map supports keys of type Value.
16 *
17 * @param <V> the value type
18 */
19 public class ValueHashMap<V> extends HashBase {
20
21 private Value[] keys;
22 private V[] values;
23
24 /**
25 * Create a new value hash map.
26 *
27 * @return the object
28 */
29 public static <T> ValueHashMap<T> newInstance() {
30 return new ValueHashMap<T>();
31 }
32
33 @SuppressWarnings("unchecked")
34 protected void reset(int newLevel) {
35 super.reset(newLevel);
36 keys = new Value[len];
37 values = (V[]) new Object[len];
38 }
39
40 protected void rehash(int newLevel) {
41 Value[] oldKeys = keys;
42 V[] oldValues = values;
43 reset(newLevel);
44 int len = oldKeys.length;
45 for (int i = 0; i < len; i++) {
46 Value k = oldKeys[i];
47 if (k != null && k != ValueNull.DELETED) {
48 put(k, oldValues[i]);
49 }
50 }
51 }
52
53 private int getIndex(Value key) {
54 return key.hashCode() & mask;
55 }
56
57 /**
58 * Add or update a key value pair.
59 *
60 * @param key the key
61 * @param value the new value
62 */
63 public void put(Value key, V value) {
64 checkSizePut();
65 int index = getIndex(key);
66 int plus = 1;
67 int deleted = -1;
68 do {
69 Value k = keys[index];
70 if (k == null) {
71 // found an empty record
72 if (deleted >= 0) {
73 index = deleted;
74 deletedCount--;
75 }
76 size++;
77 keys[index] = key;
78 values[index] = value;
79 return;
80 } else if (k == ValueNull.DELETED) {
81 // found a deleted record
82 if (deleted < 0) {
83 deleted = index;
84 }
85 } else if (k.equals(key)) {
86 // update existing
87 values[index] = value;
88 return;
89 }
90 index = (index + plus++) & mask;
91 } while (plus <= len);
92 // no space
93 DbException.throwInternalError("hashmap is full");
94 }
95
96 /**
97 * Remove a key value pair.
98 *
99 * @param key the key
100 */
101 public void remove(Value key) {
102 checkSizeRemove();
103 int index = getIndex(key);
104 int plus = 1;
105 do {
106 Value k = keys[index];
107 if (k == null) {
108 // found an empty record
109 return;
110 } else if (k == ValueNull.DELETED) {
111 // found a deleted record
112 } else if (k.equals(key)) {
113 // found the record
114 keys[index] = ValueNull.DELETED;
115 values[index] = null;
116 deletedCount++;
117 size--;
118 return;
119 }
120 index = (index + plus++) & mask;
121 } while(plus <= len);
122 // not found
123 }
124
125 /**
126 * Get the value for this key. This method returns null if the key was not
127 * found.
128 *
129 * @param key the key
130 * @return the value for the given key
131 */
132 public V get(Value key) {
133 int index = getIndex(key);
134 int plus = 1;
135 do {
136 Value k = keys[index];
137 if (k == null) {
138 // found an empty record
139 return null;
140 } else if (k == ValueNull.DELETED) {
141 // found a deleted record
142 } else if (k.equals(key)) {
143 // found it
144 return values[index];
145 }
146 index = (index + plus++) & mask;
147 } while (plus <= len);
148 return null;
149 }
150
151 /**
152 * Get the list of keys.
153 *
154 * @return all keys
155 */
156 public ArrayList<Value> keys() {
157 ArrayList<Value> list = New.arrayList(size);
158 for (Value k : keys) {
159 if (k != null && k != ValueNull.DELETED) {
160 list.add(k);
161 }
162 }
163 return list;
164 }
165
166 /**
167 * Get the list of values.
168 *
169 * @return all values
170 */
171 public ArrayList<V> values() {
172 ArrayList<V> list = New.arrayList(size);
173 int len = keys.length;
174 for (int i = 0; i < len; i++) {
175 Value k = keys[i];
176 if (k != null && k != ValueNull.DELETED) {
177 list.add(values[i]);
178 }
179 }
180 return list;
181 }
182
183 }
184