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