KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Util > IntVector


1
2 package SOFA.SOFAnode.Util;
3
4 /**
5  * A Vector-like class for handling arrays of primitive types (int)
6  *
7  * @author Vladimir Mencl
8  * @version 1.0.0
9  */

10     public class IntVector {
11       public int data[];
12       public int size;
13       public int used;
14
15       public int sz_initial = 16;
16
17       /**
18        * Create an empty IntVector
19        */

20       public IntVector() { data = null; size=0; used=0; };
21
22       /**
23        * Create an IntVector using an int[]
24        *
25        * @param iarr array with values to initialize the IntVector
26        * @param docopy whether a local copy of the array should be created.
27        * Otherwise, only a reference to the array is copied
28        */

29       public IntVector(int iarr[], boolean docopy) {
30     if (docopy) {
31       data=new int[iarr.length];
32       for (int i=0;i<iarr.length;i++) data[i]=iarr[i];
33     } else { data=iarr; };
34     size=used=iarr.length;
35       };
36
37
38       /**
39        * Create an IntVector by copying the used part of an IntVector
40        *
41        * @param copyfrom the IntVector to be used
42        */

43       public IntVector(IntVector copyfrom) {
44     data=new int[copyfrom.used];
45     for (int i=0;i<copyfrom.used;i++) data[i]=copyfrom.data[i];
46     size=used=copyfrom.used;
47       };
48
49       /**
50        * Return a string representation of this IntVector
51        *
52        * The string is embedded in square brackets, values are separated
53        * by commas, no spaces are included.
54        *
55        * Example: [0,1,4]
56        *
57        * @returns the string representation
58        */

59       public String JavaDoc dump() {
60         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
61     s.append("[");
62     for (int i=0;i<used;i++) {
63       s.append(data[i]); if (i+1<used) s.append(",");
64     };
65     s.append("]");
66     return s.toString();
67       };
68
69       /**
70        * Insert a value into a sorted IntVector, if it is not included yet.
71        *
72        * The IntVector must be sorted, then it is guaranteed
73        * to be sorted afterwards. If the value alread exists in the
74        * IntVector, no action is performed.
75        *
76        * @param i the value to insert
77        */

78       public void sortedAddIntIfNExists(int i) {
79     int j=0,t;
80
81     while ((j<used) && (data[j]<i)) j++;
82
83     if (j==used) addInt(i);
84     else {
85       if (data[j]==i) return; /* already exists */
86       while (j<used) {
87         t=data[j]; data[j]=i; i=t;
88         j++;
89       };
90       addInt(i);
91     };
92       };
93
94       /**
95        * Create an IntVector containing indexes of elements whose value is
96        * at least minValue
97        *
98        * The size of the vector is exactly the amount of elements copied
99        * (no further additions are expected)
100        *
101        * Note: An IntVector created this way is inherently sorted.
102        * (Of course, only untill the first modification).
103        *
104        * @param iarr the array to be used
105        * @param minvalue the minimal value of an element to be
106        * valid (so that it's position is used)
107        */

108       public IntVector(int iarr[], int minValue) {
109     int i,n=0;
110     for (i=0;i<iarr.length;i++) if (iarr[i]>=minValue) n++;
111     size=n; data = new int[size];
112     for (i=0;i<iarr.length;i++) if (iarr[i]>=minValue) data[used++]=i;
113       }
114
115       /**
116        * add an int to the IntVector
117        *
118        * checkSize is called to assure there is enough space
119        *
120        * @param i value to be added
121        * @returns position of the element inserted
122        * @see #checkSize
123        */

124       public int addInt(int i) {
125     checkSize(used+1); data[used++]=i; return used-1;
126       };
127
128       public void checkSize(int requested) {
129     if (requested>size) {
130       if (size==0) size=sz_initial; else size*=2;
131       int newdata[]=new int[size];
132       for (int i=0;i<used;i++) newdata[i]=data[i];
133       data=newdata;
134     };
135       };
136     };
137
138
139
Popular Tags