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 import java.util.ArrayList;
23
24
25
26
27
28
29
30
31
32
33 public class PrimitiveDoubleList {
34
35 public double[] primaryArray = null;
36 private int pStart;
37 private int pEnd;
38 private double[] secondaryArray = null;
39 private boolean growing = false;
40 private double[] compactArray = null;
41
42
43
44
45 public PrimitiveDoubleList(int initialSize, boolean grow) {
46 super();
47 growing = grow;
48 if (growing) {
49 primaryArray = new double[2 * initialSize];
50
51 pStart = 0;
52 pEnd = 0;
53 } else {
54 primaryArray = new double[initialSize + 1];
55 secondaryArray = new double[initialSize];
56
57 }
58
59 }
60
61 public int size() {
62 return pEnd - pStart;
63 }
64
65 public double[] getArray() {
66
67 if (growing) {
68
69 return compactGrowingArray(primaryArray, pStart, pEnd);
70
71 } else {
72
73 if (pStart < pEnd) {
74 System.arraycopy(
75 primaryArray,
76 pStart,
77 secondaryArray,
78 0,
79 (pEnd - pStart));
80 } else {
81 System.arraycopy(
82 primaryArray,
83 pStart,
84 secondaryArray,
85 0,
86 (primaryArray.length - pStart));
87 System.arraycopy(
88 primaryArray,
89 0,
90 secondaryArray,
91 (primaryArray.length - pStart),
92 pEnd);
93 }
94 return secondaryArray;
95 }
96
97 }
98
99
100
101
102
103
104
105
106 private double[] compactGrowingArray(double[] array, int start, int end) {
107
108 if ((compactArray==null) || (compactArray.length!=(end-start))) compactArray = new double[end - start];
109 System.arraycopy(array, start, compactArray, 0, end - start);
110
111 return compactArray;
112 }
113
114 public synchronized void addElement(double el) {
115
116 if (growing) {
117
118 if ((pEnd >= primaryArray.length - 1))
119 activeTooSmall();
120
121 primaryArray[pEnd] = el;
122 pEnd = (pEnd + 1);
123
124
125
126 } else {
127
128 if (pStart == 0 && pEnd == 0) {
129
130 primaryArray[pEnd] = el;
131 pEnd++;
132 } else if (pStart == 0 && pEnd != primaryArray.length - 1) {
133
134 primaryArray[pEnd] = el;
135 pEnd++;
136 } else if (pEnd == primaryArray.length - 1) {
137 primaryArray[pEnd] = el;
138 pEnd = 0;
139 pStart++;
140 } else if (pStart == primaryArray.length - 1) {
141 primaryArray[pEnd] = el;
142 pEnd++;
143 pStart = 0;
144 } else if (pEnd + 1 == pStart) {
145 primaryArray[pEnd] = el;
146 pEnd++;
147 pStart++;
148 }
149 }
150 }
151
152
153
154
155 private synchronized void activeTooSmall() {
156
157
158 double[] tempArray;
159 if (2 * (pEnd - pStart + 1) > primaryArray.length) {
160 tempArray = new double[2 * (pEnd - pStart + 1)];
161
162 } else
163 tempArray = primaryArray;
164 System.arraycopy(primaryArray, pStart, tempArray, 0, pEnd - pStart);
165 primaryArray = tempArray;
166 pEnd = pEnd - pStart;
167 pStart = 0;
168
169
170 }
171
172 public synchronized void removeOldest() {
173
174 if (pStart < pEnd)
175 pStart = pStart + 1;
176
177 }
178
179 public synchronized void removeOldest(int i) {
180 if ((i<1) || (i>size()-1)) return;
181 if (pStart < pEnd)
182 if (pStart + i < pEnd) {
183 pStart = pStart + i;
184 } else
185 pStart = pEnd;
186
187 }
188 public double get(int index){
189 return primaryArray[pStart+index];
190 }
191 public void set(int index, double value){
192 if (pStart+index<pEnd)
193 primaryArray[pStart+index] = value;
194 }
195
196 public double[] fillLatest(double[] arrayToFill) {
197
198 int i = arrayToFill.length;
199
200 if (i == pEnd - pStart)
201 System.arraycopy(primaryArray, pStart, arrayToFill, 0, i);
202 else if (i < pEnd - pStart)
203 System.arraycopy(primaryArray, pEnd - i, arrayToFill, 0, i);
204 else if (i > pEnd - pStart)
205 System.arraycopy(
206 primaryArray,
207 pStart,
208 arrayToFill,
209 i - (pEnd - pStart),
210 pEnd - pStart);
211 return arrayToFill;
212 }
213
214
215
216
217
218
219 public static void main(String[] args) {
220
221
222
223 PrimitiveDoubleList pdl = new PrimitiveDoubleList(6, true);
224 ArrayList al = new ArrayList(6);
225
226
227
228
229
230
231 long aStart;
232 long aStop;
233 long bStart;
234 long bStop;
235 Double d = new Double(1);
236
237
238
239
240 aStart = System.currentTimeMillis();
241 for (int i = 0; i < 5000; i++) {
242 for (int j = 0; j < 8; j++) {
243 al.add(d);
244 }
245 for (int j = 0; j < 5; j++) {
246 al.remove(0);
247 }
248 }
249 aStop = System.currentTimeMillis();
250 System.out.println(
251 "ArrayList "
252 + (aStop - aStart)
253 + " milis, array lenght "
254 + al.size());
255 bStart = System.currentTimeMillis();
256
257
258
259
260
261
262 for (int i = 0; i < 5000; i++) {
263 for (int j = 0; j < 8; j++) {
264 pdl.addElement(j);
265 }
266 for (int j = 0; j < 1; j++) {
267 pdl.removeOldest(5);
268 }
269 }
270 bStop = System.currentTimeMillis();
271
272 System.out.println(
273 "PrimitiveDoubleList "
274 + (bStop - bStart)
275 + " milis, array lenght "
276 + pdl.getArray().length
277 + " actual: "
278 + pdl.primaryArray.length);
279
280
281
282
283 ResizableDoubleList dl = new ResizableDoubleList(6);
284
285
286
287
288
289
290 for (int i = 0; i < 5000; i++) {
291 for (int j = 0; j < 8; j++) {
292 dl.add(j);
293 }
294 for (int j = 0; j < 1; j++) {
295 dl.remove(0,5);
296 }
297 }
298 System.out.println(
299 "ResizableDoubleList "
300 + (bStop - bStart)
301 + " milis, array lenght "
302 + dl.getArray().length
303 + " actual: "
304 + dl.getBufferArray().length);
305
306
307
308
309
310
311 System.out.println(
312 "I am better for "
313 + 100
314 * ((aStop - aStart) - (bStop - bStart))
315 / ((bStop - bStart))
316 + " %");
317
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 }