1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.util;
21
22
23
24
25
26
27
28
29
30
31
32
33 public class ResizableDoubleList {
34
35 private double[] bufferArray;
36 private double[] compactArray;
37
38
39
40
41 private int foi;
42 private int fei;
43
44
45
46
47 public ResizableDoubleList() {
48 this(10);
49 }
50
51
52
53
54 public ResizableDoubleList(int initialBufferSize) {
55 super();
56 bufferArray = new double[initialBufferSize];
57 foi = 0;
58 fei = 0;
59 }
60
61
62
63
64
65 public void add(double d){
66
67 assureAddition(1);
68 bufferArray[fei]=d;
69 fei++;
70
71 }
72
73
74
75
76
77 public void add(double[] da){
78
79 assureAddition(da.length);
80 System.arraycopy(da,0,bufferArray,fei,da.length);
81 fei=fei+da.length;
82
83 }
84
85
86
87
88
89
90
91 public void add(double d, int index){
92
93 assureAddition(1);
94 if (checkIndex(index)) {
95
96 double[] tempArray = new double[fei-index];
97 System.arraycopy(bufferArray, index, tempArray, 0, tempArray.length);
98 bufferArray[index] = d;
99 System.arraycopy(tempArray,0,bufferArray, index+1, tempArray.length);
100 fei++;
101
102 } else {
103 throw new IndexOutOfBoundsException("Cannot add to specified index. Index: "+index+", Size: "+size()+".");
104 }
105 }
106
107
108
109
110
111
112
113
114 public void add(double[] da, int index){
115
116 assureAddition(da.length);
117 if (checkIndex(index)) {
118 double[] tempArray = new double[fei-index];
119
120 System.arraycopy(bufferArray, index, tempArray, 0, tempArray.length);
121
122 System.arraycopy(da,0,bufferArray,index, da.length);
123
124 System.arraycopy(tempArray,0,bufferArray,index+da.length,tempArray.length);
125 fei = fei + da.length;
126 } else {
127 throw new IndexOutOfBoundsException("Cannot add to specified index. Index: "+index+", Size: "+size()+".");
128 }
129 }
130
131
132
133
134
135
136 public void remove(int index, int lenght){
137 if (lenght == 0) return;
138 if (checkIndex(index) && checkIndex(index+lenght-1)) {
139 if (index == 0){
140 foi = foi + lenght;
141 } else if (index+lenght == fei) {
142 fei = index;
143 } else {
144 double[] tempArray = new double[fei-(foi+index+lenght)];
145
146 System.arraycopy(bufferArray, foi+index+lenght, tempArray, 0, tempArray.length);
147
148 System.arraycopy(tempArray, 0, bufferArray, foi+index, tempArray.length);
149 fei = fei - lenght;
150 }
151 downSize();
152 } else throw new IndexOutOfBoundsException("Cannot remove from index "+index+" to index "+(index+lenght-1) +". Size: "+size()+".");
153 }
154
155
156
157
158
159 private void downSize() {
160 if (size()<0.25*bufferArray.length && bufferArray.length > 20) {
161 double[] tempArray = new double[(int)(0.5*bufferArray.length+1)];
162 System.arraycopy(bufferArray, foi, tempArray,0,size());
163 bufferArray = tempArray;
164 fei = fei - foi;
165 foi = 0;
166 }
167 }
168
169
170
171
172
173
174
175 public int size(){
176
177 return fei-foi;
178 }
179
180
181
182
183
184 public double[] getArray(){
185
186 if ((compactArray==null) || (compactArray.length!=(fei-foi))) compactArray = new double[fei-foi];
187 System.arraycopy(bufferArray,foi, compactArray, 0, compactArray.length);
188
189
190 return compactArray;
191 }
192
193
194
195
196
197
198
199 public double[] getArray(int index, int lenght){
200 if (lenght == 0) return new double[0];
201 if (checkIndex(index) && checkIndex(index+lenght-1)) {
202 if ((compactArray==null) || (compactArray.length!=(lenght))) compactArray = new double[lenght];
203 System.arraycopy(bufferArray,foi+index, compactArray, 0, compactArray.length);
204 return compactArray;
205 }
206 else throw new IndexOutOfBoundsException("Cannot get sub-array from index "+index+" to index "+(index+lenght-1) +". Size: "+size()+".");
207 }
208
209
210
211
212
213
214 public double[] getArray(int index){
215 if (checkIndex(index)) {
216 if ((compactArray==null) || (compactArray.length!=(fei-index))) compactArray = new double[fei-index];
217 System.arraycopy(bufferArray, index, compactArray, 0, compactArray.length);
218 return compactArray;
219 }
220 else throw new IndexOutOfBoundsException("Cannot get sub-array from index "+index+" onwards. Size: "+size()+".");
221 }
222
223
224
225
226
227
228 public double get(int index){
229 if (checkIndex(index)){
230 return bufferArray[foi+index];
231 }
232 else throw new IndexOutOfBoundsException("Cannot get value from index "+index+". Size: "+size());
233 }
234
235
236
237
238
239
240 public void set(int index, double value){
241 if (checkIndex(index)){
242 bufferArray[foi+index] = value;
243 }
244 else throw new IndexOutOfBoundsException("Cannot set value at index "+index+". Size: "+size()+".");
245 }
246
247
248
249
250
251 private void assureAddition(int i) {
252 if (i < 0) return;
253 int freeSpacesEnd = bufferArray.length - 1 - fei;
254 if (i < freeSpacesEnd) return;
255 else {
256 double[] tempArray;
257 if (2*size()+i > bufferArray.length) tempArray = new double[2*size()+i];
258 else tempArray = bufferArray;
259 System.arraycopy(bufferArray, foi, tempArray,0,size());
260 bufferArray = tempArray;
261 fei = fei - foi;
262 foi = 0;
263 }
264 }
265
266
267
268
269
270
271 private boolean checkIndex(int index) {
272 if (foi+index<fei
273 else return false;
274 }
275
276
277
278
279
280
281
282 public double[] getBufferArray(){
283 return bufferArray;
284 }
285
286
287
288
289
290 public static void main(String[] args){
291
292
293
294 ResizableDoubleList list = new ResizableDoubleList(10);
295 list.displayIndexes();
296 list.displayRelevant();
297
298 list.add(0);
299 list.add(1);
300 list.add(2);
301 list.add(3);
302 list.add(4);
303 list.add(5);
304 list.add(6);
305 list.add(7);
306 list.add(8);
307 list.add(9);
308 list.displayRelevant();
309
310 list.remove(0,3);
311 list.displayRelevant();
312 list.remove(0,3);
313 list.displayRelevant();
314 list.remove(0,3);
315 list.displayRelevant();
316 list.remove(0,3);
317 list.displayRelevant();
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373 }
374
375
376
377
378 public void displayRelevant(){
379
380 System.out.print("\n[");
381 for (int i = 0; i < foi; i++){
382 System.out.print("... ");
383 }
384 for (int i = foi; i < fei; i++){
385 System.out.print(get(i-foi)+" ");
386
387 }
388 for (int i = fei; i < bufferArray.length; i++){
389 System.out.print("... ");
390 }
391 System.out.print("] FOI: "+foi+" FEI: "+fei);
392 }
393
394
395
396 public void displayIndexes(){
397
398 System.out.print("\n ");
399 for (int i = 0; i < bufferArray.length; i++) {
400 if (i < 10) System.out.print(" "+i+" ");
401 else if (i<100) System.out.print(" "+i+" ");
402 else System.out.print(""+i+" ");
403 }
404
405 System.out.print(" ");
406 }
407
408
409
410 }