View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of Java-Common.
5    *
6    * Java-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Java-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Java-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.util;
21  
22  import java.util.ArrayList;
23  
24  /**
25   * @author <a href="mailto:gasper.pajor@cosylab.com">Gasper Pajor</a>
26   * @version $id$
27   *
28   * this is temporary version of a tool that performs some tasks
29   * with double[]; for the moment adding to array of fixed lenght 
30   * and retrieving from such array is implemented.
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  	 * Constructor for PrimitiveDoubleList.
44  	 */
45  	public PrimitiveLongList(int initialSize, boolean grow) {
46  		super();
47  		growing = grow;
48  		if (growing) {
49  			primaryArray = new long[2 * initialSize];
50  			//			secondaryArray = new long[2*initialSize];
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 	 * Method compactGrowingArray.
101 	 * @param array
102 	 * @param start
103 	 * @param end
104 	 * @return long[]
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 			//System.out.println("End set to "+pEnd);
124 			//System.out.println("element added - value "+ (pEnd-1));
125 
126 		} else {
127 
128 			if (pStart == 0 && pEnd == 0) {
129 				// array empty
130 				primaryArray[pEnd] = el;
131 				pEnd++;
132 			} else if (pStart == 0 && pEnd != primaryArray.length - 1) {
133 				// array not full yet, filling
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 	 * Method activeToSmall.
154 	 */
155 	private synchronized void activeTooSmall() {
156 		//System.out.println("active to small! activeLength="+primaryArray.length+" secLength="+secondaryArray.length+" pStart="+pStart+" pEnd="+pEnd);
157 
158 		long[] tempArray;
159 		if (2 * (pEnd - pStart + 1) > primaryArray.length) {
160 			tempArray = new long[2 * (pEnd - pStart + 1)];
161 			//System.out.println("new secondary (creation)! activeLength="+primaryArray.length+" tempLength="+tempArray.length+" pStart="+pStart+" pEnd="+pEnd);
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 		//System.out.println("---new startStop! activeLength="+primaryArray.length+" tempLength="+tempArray.length+" pStart="+pStart+" pEnd="+pEnd);
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 				//System.out.println("start set to "+pStart);
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 	 * Run test applet.
214 	 * @param args command line parameters
215 	 */
216 
217 	public static void main(String[] args) {
218 
219 		// this is demo.
220 
221 		PrimitiveLongList pdl = new PrimitiveLongList(6, true);
222 		ArrayList al = new ArrayList(6);
223 
224 		/////////////////////////////////////////
225 		/// ArrayList Vs. PrimitiveDoubleList ///
226 		/////////////////////////////////////////
227 
228 		long aStart;
229 		long aStop;
230 		long bStart;
231 		long bStop;
232 		Double d = new Double(1);
233 
234 		// Arraylist does it
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 		// Doublelist does it
255 		/////////////////////////////////
256 
257 		//pdl.addElement(-1);
258 		//pdl.addElement(0);
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 		//test 2
286 		////////////////////////
287 
288 		/*
289 		System.out.println("Inserting 10 longs");
290 		
291 		for (int i = 11; i < 60000; i++) {
292 			pdl.addElement(i);
293 			long[] tempA = pdl.getArray();
294 			for (int j = 0; j < tempA.length; j++) {
295 			//System.out.print(" "+tempA[j]);	
296 			}
297 			//System.out.println("");
298 		}
299 		System.out.println("\nremoving 3 oldest longs");	
300 		pdl.removeOldest(3);
301 				
302 			long[] tempA = pdl.getArray();
303 			for (int j = 0; j < tempA.length; j++) {
304 			System.out.print(" "+tempA[j]);	
305 			}
306 		System.out.println("\nretriving longs");
307 		tempA = new long[7];
308 		pdl.fillLatest(tempA);	
309 		
310 			for (int j = 0; j < tempA.length; j++) {
311 			System.out.print(" "+tempA[j]);	
312 			}
313 		*/
314 	}
315 
316 }