-
Notifications
You must be signed in to change notification settings - Fork 57
/
ComplexList.java
325 lines (296 loc) · 10.9 KB
/
ComplexList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.numbers.complex.arrays;
import org.apache.commons.numbers.complex.Complex;
import java.util.AbstractList;
import java.util.Arrays;
import java.util.Collection;
/**
* Resizable-double array implementation of the List interface. Implements all optional list operations,
* and permits all elements. In addition to implementing the List interface,
* this class provides methods to manipulate the size of the array that is used internally to store the list.
*
* <p>Each ComplexList instance has a capacity. The capacity is half the size of the double array used to store the elements
* in the list. As elements are added to an ComplexList, its capacity grows automatically.
* The complex number is stored using an interleaved format and so the maximum number of elements that may be added is
* approximately 2^30. This is half the maximum capacity of java.util.ArrayList.
* The memory usage is more efficient than using a List of Complex objects as the underlying numbers are not stored
* using instances of Complex.</p>
*
* <p>An application can increase the capacity of an ComplexList instance before adding a large number of elements
* using the ensureCapacity operation. This may reduce the amount of incremental reallocation.</p>
*/
public class ComplexList extends AbstractList<Complex> {
/**
* The maximum size of array to allocate.
* Ensuring Max capacity is even with additional space for vm array headers.
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 9;
/**
* Max capacity for size of complex numbers in the list.
*/
private static final int MAX_CAPACITY = MAX_ARRAY_SIZE / 2;
/**
* error in case of allocation above max capacity.
*/
private static final String OOM_ERROR_STRING = "cannot allocate capacity %s greater than max " + MAX_CAPACITY;
/**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 8;
/**
* Size label message.
*/
private static final String SIZE_MSG = ", Size: ";
/**
* Index position label message.
*/
private static final String INDEX_MSG = "Index: ";
/**
* The double array buffer into which the elements of the ComplexList are stored.
*/
private double[] realAndImagParts;
/**
* Size of ComplexList.
*/
private int size;
/**
* Constructs an empty list up to the specified capacity without a memory reallocation.
*
* @param capacity Capacity of list.
* @throws IllegalArgumentException if the {@code capacity} is greater than {@code MAX_CAPACITY}.
*/
public ComplexList(int capacity) {
if (capacity > MAX_CAPACITY) {
throw new IllegalArgumentException(String.format(OOM_ERROR_STRING, capacity));
}
final int arrayLength = Math.max(DEFAULT_CAPACITY, capacity) * 2;
realAndImagParts = new double[arrayLength];
}
/**
* Constructs an empty list.
*/
public ComplexList() {
realAndImagParts = new double[DEFAULT_CAPACITY * 2];
}
/**
* {@inheritDoc}
*/
@Override
public int size() {
return size;
}
/**
* Checks if the given index is in range.
*
* @param index Index of the element to range check.
* @throws IndexOutOfBoundsException if index isn't within the range.
*/
private void rangeCheck(int index) {
if (index >= size) {
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
}
/**
* A version of rangeCheck used by add and addAll.
*
* @param index Index of the element to range check.
* @throws IndexOutOfBoundsException if index isn't within the range of list.
*/
private void rangeCheckForInsert(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
}
/**
* Gets the complex number \( (a + i b) \) at the indexed position of the list.
*
* {@inheritDoc}
* @return the complex number.
*/
@Override
public Complex get(int index) {
rangeCheck(index);
final int i = index << 1;
return Complex.ofCartesian(realAndImagParts[i], realAndImagParts[i + 1]);
}
/**
* Replaces the element at the specified position in this list with the specified element's
* real and imaginary parts. No range checks are done.
*
* @param index Index of the element to replace.
* @param real Real part \( a \) of the complex number \( (a +ib) \).
* @param imaginary Imaginary part \( b \) of the complex number \( (a +ib) \).
*/
private void setNoRangeCheck(int index, double real, double imaginary) {
final int i = index << 1;
realAndImagParts[i] = real;
realAndImagParts[i + 1] = imaginary;
}
/**
* {@inheritDoc}
*
* @param element Complex element to be set.
*/
@Override
public Complex set(int index, Complex element) {
rangeCheck(index);
final int i = index << 1;
final Complex oldValue = Complex.ofCartesian(realAndImagParts[i], realAndImagParts[i + 1]);
setNoRangeCheck(index, element.getReal(), element.getImaginary());
return oldValue;
}
/**
* Increases the capacity of this ComplexList instance, if necessary, to ensure that it can hold at
* least the number of elements specified by the minimum capacity argument.
*
* @param minCapacity Desired minimum capacity.
* @return the backing double array.
* @throws OutOfMemoryError if the {@code minCapacity} is greater than {@code MAX_ARRAY_SIZE}.
*/
private double[] ensureCapacityInternal(int minCapacity) {
modCount++;
final long minArrayCapacity = Integer.toUnsignedLong(minCapacity) << 1;
if (minArrayCapacity > MAX_ARRAY_SIZE) {
throw new OutOfMemoryError(String.format(OOM_ERROR_STRING, minArrayCapacity));
}
final long oldArrayCapacity = realAndImagParts.length;
if (minArrayCapacity > oldArrayCapacity) {
long newArrayCapacity = oldArrayCapacity + (oldArrayCapacity >> 1);
// Round-odd up to even
newArrayCapacity += newArrayCapacity & 1;
// Ensure minArrayCapacity <= newArrayCapacity <= MAX_ARRAY_SIZE
// Note: At this point minArrayCapacity <= MAX_ARRAY_SIZE
if (newArrayCapacity > MAX_ARRAY_SIZE) {
newArrayCapacity = MAX_ARRAY_SIZE;
} else if (newArrayCapacity < minArrayCapacity) {
newArrayCapacity = minArrayCapacity;
}
realAndImagParts = Arrays.copyOf(realAndImagParts, (int) newArrayCapacity);
}
return realAndImagParts;
}
/**
* Increases the capacity of this ComplexList instance, if necessary, to ensure that it can hold at
* least an additional number of elements specified by the capacity argument.
*
* @param capacity Desired capacity.
*/
private void expand(int capacity) {
ensureCapacityInternal(size + capacity);
}
/**
* {@inheritDoc}
*
* @param element Complex element to be appended to this list
*/
@Override
public boolean add(Complex element) {
double[] e = realAndImagParts;
if (size == (e.length >>> 1)) {
e = ensureCapacityInternal(size + 1);
}
final int i = size << 1;
e[i] = element.real();
e[i + 1] = element.imag();
size++;
return true;
}
/**
* {@inheritDoc}
*/
@Override
public void add(int index, Complex element) {
rangeCheckForInsert(index);
double[] e = realAndImagParts;
if (size == e.length >>> 1) {
e = ensureCapacityInternal(size + 1);
}
final int i = index << 1;
final int s = size << 1;
System.arraycopy(e, i, e, i + 2, s - i);
e[i] = element.real();
e[i + 1] = element.imag();
size++;
}
/**
* {@inheritDoc}
*/
@Override
public boolean addAll(Collection<? extends Complex> c) {
final int numNew = c.size();
expand(numNew);
double[] realAndImgData = new double[numNew * 2];
int i = 0;
for (final Complex val : c) {
realAndImgData[i++] = val.getReal();
realAndImgData[i++] = val.getImaginary();
}
final int s = size << 1;
System.arraycopy(realAndImgData, 0, realAndImagParts, s, realAndImgData.length);
size += numNew;
return numNew != 0;
}
/**
* {@inheritDoc}
*/
@Override
public boolean addAll(int index, Collection<? extends Complex> c) {
rangeCheckForInsert(index);
final int numNew = c.size();
final int numNew2 = numNew << 1;
expand(numNew);
final double[] realAndImgData = new double[numNew * 2];
int i = 0;
for (final Complex val : c) {
realAndImgData[i++] = val.getReal();
realAndImgData[i++] = val.getImaginary();
}
final int numMoved = (size - index) * 2;
final int index2 = index << 1;
System.arraycopy(realAndImagParts, index2, realAndImagParts, index2 + numNew2, numMoved);
System.arraycopy(realAndImgData, 0, realAndImagParts, index2, realAndImgData.length);
size += numNew;
return numNew != 0;
}
/**
* {@inheritDoc}
*/
@Override
public Complex remove(int index) {
rangeCheck(index);
modCount++;
final int i = index << 1;
final int s = size << 1;
final Complex oldValue = Complex.ofCartesian(realAndImagParts[i], realAndImagParts[i + 1]);
final int numMoved = s - i - 2;
if (numMoved > 0) {
System.arraycopy(realAndImagParts, i + 2, realAndImagParts, i, numMoved);
}
size--;
return oldValue;
}
/**
* Constructs an IndexOutOfBoundsException detail message.
*
* @param index Index of the element.
* @return message detailing the exception.
*/
private String outOfBoundsMsg(int index) {
return INDEX_MSG + index + SIZE_MSG + size;
}
}