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 PrimitiveLongList {
34
35 public long[] primaryArray = null;
36 private int pStart;
37 private int pEnd;
38 private long[] secondaryArray = null;
39 private boolean growing = false;
40 private long[] compactArray = null;
41
42
43
44
45 public PrimitiveLongList(int initialSize, boolean grow) {
46 super();
47 growing = grow;
48 if (growing) {
49 primaryArray = new long[2 * initialSize];
50
51 pStart = 0;
52 pEnd = 0;
53 } else {
54 primaryArray = new long[initialSize + 1];
55 secondaryArray = new long[initialSize];
56
57 }
58
59 }
60
61 public int size() {
62 return pEnd - pStart;
63 }
64
65 public long[] 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 long[] compactGrowingArray(long[] array, int start, int end) {
107
108 compactArray = new long[end - start];
109 System.arraycopy(array, start, compactArray, 0, end - start);
110
111 return compactArray;
112 }
113
114 public synchronized void addElement(long 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 long[] tempArray;
159 if (2 * (pEnd - pStart + 1) > primaryArray.length) {
160 tempArray = new long[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
181 if (pStart < pEnd)
182 if (pStart + i < pEnd) {
183 pStart = pStart + i;
184
185
186 } else
187 pStart = pEnd;
188
189 }
190 public long get(int index){
191 return primaryArray[pStart+index];
192 }
193
194 public long[] fillLatest(long[] arrayToFill) {
195
196 int i = arrayToFill.length;
197
198 if (i == pEnd - pStart)
199 System.arraycopy(primaryArray, pStart, arrayToFill, 0, i);
200 else if (i < pEnd - pStart)
201 System.arraycopy(primaryArray, pEnd - i, arrayToFill, 0, i);
202 else if (i > pEnd - pStart)
203 System.arraycopy(
204 primaryArray,
205 pStart,
206 arrayToFill,
207 i - (pEnd - pStart),
208 pEnd - pStart);
209 return arrayToFill;
210 }
211
212
213
214
215
216
217 public static void main(String[] args) {
218
219
220
221 PrimitiveLongList pdl = new PrimitiveLongList(6, true);
222 ArrayList al = new ArrayList(6);
223
224
225
226
227
228 long aStart;
229 long aStop;
230 long bStart;
231 long bStop;
232 Double d = new Double(1);
233
234
235
236
237 aStart = System.currentTimeMillis();
238 for (int i = 0; i < 5000; i++) {
239 for (int j = 0; j < 8; j++) {
240 al.add(d);
241 }
242 for (int j = 0; j < 5; j++) {
243 al.remove(0);
244 }
245 }
246 aStop = System.currentTimeMillis();
247 System.out.println(
248 "ArrayList "
249 + (aStop - aStart)
250 + " milis, array lenght "
251 + al.size());
252 bStart = System.currentTimeMillis();
253
254
255
256
257
258
259 for (int i = 0; i < 5000; i++) {
260 for (int j = 0; j < 8; j++) {
261 pdl.addElement(j);
262 }
263 for (int j = 0; j < 1; j++) {
264 pdl.removeOldest(5);
265 }
266 }
267 bStop = System.currentTimeMillis();
268
269 System.out.println(
270 "PrimitiveDoubleList "
271 + (bStop - bStart)
272 + " milis, array lenght "
273 + pdl.getArray().length
274 + " actual: "
275 + pdl.primaryArray.length);
276
277 System.out.println(
278 "I am better for "
279 + 100
280 * ((aStop - aStart) - (bStop - bStart))
281 / ((bStop - bStart))
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 }