Source code: mrsoft/util/IntVector.java
1 /**
2 // mrsoft.IntVector: Simple implementation of a vector for integers
3 // Copyright (C) 2001 Michiel de Roo
4 //
5 // date: 03/01/01
6 // author: Michiel de Roo
7 // e-mail: michiel@belangrijk.nl
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Library General Public
11 // License as published by the Free Software Foundation; either
12 // version 2 of the License, or (at your option) any later version.
13 //
14 // You should have received a copy of the GNU Library General Public
15 // License along with this library; if not, write to the
16 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 // Boston, MA 02111-1307, USA.
18 */
19
20 package mrsoft.util;
21 import mrsoft.util.*;
22
23 import java.util.Vector; //for main(..) only
24
25 public class IntVector
26 {
27 int increment;
28 int size1;
29 int pos1=0, pos2=-1;
30 int[][] elements;
31
32 public IntVector()
33 {
34 this(25);
35 }
36
37 public IntVector(int increment)
38 {
39 this.increment = increment;
40 size1 = increment;
41 elements = new int[increment][];
42 elements[0] = new int[increment];
43 }
44
45 public int getElement(int position) throws IndexOutOfBoundsException
46 {
47 int p1 = position/increment;
48 int p2 = position%increment;
49 if(p1*increment+p2 > pos1*increment+pos2)
50 throw new IndexOutOfBoundsException(position + " >= " + (size()-1));
51 return elements[p1][p2];
52 }
53
54 /** Adds an element and returns the position in the vector */
55 public int addElement(int value)
56 {
57 pos2++;
58 if (pos2==increment) { incrSize2(); }
59 elements[pos1][pos2] = value;
60
61 return pos1*increment+pos2;
62 }
63
64 public int size()
65 {
66 return pos1*increment+pos2+1;
67 }
68
69 protected void incrSize2()
70 {
71 pos2=0; pos1++;
72 if (pos1==increment) { incrSize1(); }
73 elements[pos1] = new int[increment];
74 }
75
76 protected void incrSize1()
77 {
78 int[][] tmp = new int[size1+increment][];
79 for(int i=0; i<size1; i++) tmp[i] = elements[i];
80 elements = tmp;
81 size1 += increment;
82 }
83
84 public static void main(String[] args)
85 {
86 int n=3500, cycles = 10;
87 long t1, t2, t3, tt;
88
89 //
90 // start up "the machinerie" first. It appears the first cycles of anything are always slower.
91 tt=0;
92 for(int j=0; j<5; j++) {
93 IntVector v = new IntVector(n/10);
94
95 for(int i=0; i<n; i++) { v.addElement(i-6); }
96 for(int i=0; i<n; i++) { System.out.print(v.getElement(i) + "\r");}
97
98 }
99
100 //
101 tt=0;
102 for(int j=0; j<cycles; j++) {
103 IntVector v = new IntVector(n/10);
104
105 t1 = System.currentTimeMillis();
106 for(int i=0; i<n; i++) { v.addElement(i-6); }
107 for(int i=0; i<n; i++) { System.out.print(v.getElement(i) + "\r");}
108 t2 = System.currentTimeMillis();
109
110 tt += (t2-t1);
111 System.out.println("IntVector time: " + (t2-t1));
112 }
113 System.out.println("IntVector mean time: " + (tt/(cycles)));
114 //
115 tt=0;
116 for(int j=0; j<cycles; j++) {
117
118 Vector v2 = new Vector();
119 Integer tmp;
120
121 t1 = System.currentTimeMillis();
122 for(int i=0; i<n; i++) { v2.addElement(new Integer(i-6)); }
123 for(int i=0; i<n; i++) {
124 tmp = (Integer)(v2.elementAt(i));
125 System.out.print( tmp.intValue() + "\r");
126 }
127 t2 = System.currentTimeMillis();
128
129 tt += (t2-t1);
130 System.out.println("Vector time: " + (t2-t1));
131 }
132 System.out.println("Vector mean time: " + (tt/(cycles)));
133
134 }
135
136 }//EC