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 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  	 * Constructor for PrimitiveDoubleList.
44  	 */
45  	public PrimitiveDoubleList(int initialSize, boolean grow) {
46  		super();
47  		growing = grow;
48  		if (growing) {
49  			primaryArray = new double[2 * initialSize];
50  			//			secondaryArray = new double[2*initialSize];
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 	 * Method compactGrowingArray.
101 	 * @param array
102 	 * @param start
103 	 * @param end
104 	 * @return double[]
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 			//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 		double[] tempArray;
159 		if (2 * (pEnd - pStart + 1) > primaryArray.length) {
160 			tempArray = new double[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     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  * Run test applet.
216  * 
217  * @param args command line parameters
218  */
219 	public static void main(String[] args) {
220 
221 		// this is demo.
222 
223 		PrimitiveDoubleList pdl = new PrimitiveDoubleList(6, true);
224 		ArrayList al = new ArrayList(6);
225 
226 
227 		/////////////////////////////////////////
228 		/// ArrayList Vs. PrimitiveDoubleList ///
229 		/////////////////////////////////////////
230 
231 		long aStart;
232 		long aStop;
233 		long bStart;
234 		long bStop;
235 		Double d = new Double(1);
236 
237 		// Arraylist does it
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 		// PrimitiveDoublelist does it
258 		/////////////////////////////////
259 
260 		//pdl.addElement(-1);
261 		//pdl.addElement(0);
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         // New Doublelist does it
286         /////////////////////////////////
287 
288         //pdl.addElement(-1);
289         //pdl.addElement(0);
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 		//test 2
320 		////////////////////////
321 
322 		/*
323 		System.out.println("Inserting 10 doubles");
324 		
325 		for (int i = 11; i < 60000; i++) {
326 			pdl.addElement(i);
327 			double[] tempA = pdl.getArray();
328 			for (int j = 0; j < tempA.length; j++) {
329 			//System.out.print(" "+tempA[j]);	
330 			}
331 			//System.out.println("");
332 		}
333 		System.out.println("\nremoving 3 oldest doubles");	
334 		pdl.removeOldest(3);
335 				
336 			double[] tempA = pdl.getArray();
337 			for (int j = 0; j < tempA.length; j++) {
338 			System.out.print(" "+tempA[j]);	
339 			}
340 		System.out.println("\nretriving doubles");
341 		tempA = new double[7];
342 		pdl.fillLatest(tempA);	
343 		
344 			for (int j = 0; j < tempA.length; j++) {
345 			System.out.print(" "+tempA[j]);	
346 			}
347 		*/
348 	}
349 
350 }