1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.hipparchus.util;
24
25 import java.io.IOException;
26 import java.io.ObjectInputStream;
27 import java.io.Serializable;
28 import java.util.Arrays;
29 import java.util.ConcurrentModificationException;
30 import java.util.NoSuchElementException;
31
32
33
34
35
36
37
38
39
40
41 public class OpenIntToDoubleHashMap extends AbstractOpenIntHashMap implements Serializable {
42
43
44 private static final long serialVersionUID = 20240326L;
45
46
47 private double[] values;
48
49
50 private final double missingEntries;
51
52
53
54
55 public OpenIntToDoubleHashMap() {
56 this(DEFAULT_EXPECTED_SIZE, Double.NaN);
57 }
58
59
60
61
62
63 public OpenIntToDoubleHashMap(final double missingEntries) {
64 this(DEFAULT_EXPECTED_SIZE, missingEntries);
65 }
66
67
68
69
70
71 public OpenIntToDoubleHashMap(final int expectedSize) {
72 this(expectedSize, Double.NaN);
73 }
74
75
76
77
78
79
80 public OpenIntToDoubleHashMap(final int expectedSize,
81 final double missingEntries) {
82 super(expectedSize);
83 values = new double[getCapacity()];
84 this.missingEntries = missingEntries;
85 }
86
87
88
89
90
91 public OpenIntToDoubleHashMap(final OpenIntToDoubleHashMap source) {
92 super(source);
93 values = new double[getCapacity()];
94 System.arraycopy(source.values, 0, values, 0, getCapacity());
95 missingEntries = source.missingEntries;
96 }
97
98
99
100
101
102
103 public double get(final int key) {
104 final int index = locate(key);
105 return index < 0 ? missingEntries : values[index];
106 }
107
108
109
110
111
112
113
114
115 public Iterator iterator() {
116 return new Iterator();
117 }
118
119
120 @Override
121 public boolean equals(final Object o) {
122 if (this == o) {
123 return true;
124 }
125 if (o == null || getClass() != o.getClass()) {
126 return false;
127 }
128 final OpenIntToDoubleHashMap that = (OpenIntToDoubleHashMap) o;
129 return equalKeys(that) && equalStates(that) && Arrays.equals(values, that.values);
130 }
131
132
133 @Override
134 public int hashCode() {
135 return keysStatesHashCode() + Arrays.hashCode(values);
136 }
137
138
139
140
141
142
143 public double remove(final int key) {
144 final int index = locate(key);
145 if (index < 0) {
146 return missingEntries;
147 } else {
148 final double previous = values[index];
149 doRemove(index);
150 values[index] = missingEntries;
151 return previous;
152 }
153 }
154
155
156
157
158
159
160
161 public double put(final int key, final double value) {
162 final InsertionHolder ih = put(key);
163 final double previous = ih.isExisting() ? values[ih.getIndex()] : missingEntries;
164 values[ih.getIndex()] = value;
165 return previous;
166 }
167
168
169 @Override
170 protected int growTable(final int oldIndex) {
171 final double[] newValues = new double[RESIZE_MULTIPLIER * values.length];
172 final int newIndex = doGrowTable(oldIndex, (src, dest) -> newValues[dest] = values[src]);
173 values = newValues;
174 return newIndex;
175 }
176
177
178 public class Iterator extends BaseIterator {
179
180
181
182
183
184
185 public double value() throws ConcurrentModificationException, NoSuchElementException {
186 return values[getCurrent()];
187 }
188
189 }
190
191
192
193
194
195
196
197
198 private void readObject(final ObjectInputStream stream)
199 throws IOException, ClassNotFoundException {
200 stream.defaultReadObject();
201 resetCount();
202 }
203
204
205
206
207
208 private Object writeReplace() {
209 return new DataTransferObject(missingEntries, getSize(), iterator());
210 }
211
212
213 private static class DataTransferObject implements Serializable {
214
215
216 private static final long serialVersionUID = 20240326L;
217
218
219 private final double missingEntries;
220
221
222 private final int[] keys;
223
224
225 private final double[] values;
226
227
228
229
230
231
232 DataTransferObject(final double missingEntries, final int size, final Iterator iterator) {
233 this.missingEntries = missingEntries;
234 this.keys = new int[size];
235 this.values = new double[size];
236 for (int i = 0; i < size; ++i) {
237 iterator.advance();
238 keys[i] = iterator.key();
239 values[i] = iterator.value();
240 }
241 }
242
243
244
245
246 private Object readResolve() {
247 final OpenIntToDoubleHashMap map = new OpenIntToDoubleHashMap(missingEntries);
248 for (int i = 0; i < keys.length; ++i) {
249 map.put(keys[i], values[i]);
250 }
251 return map;
252 }
253
254 }
255
256 }