2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or (at
5
* your option) any later version.
7
* This program is distributed in the hope that it will be useful, but
8
* WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10
* General Public License for more details.
12
* You should have received a copy of the GNU General Public License
13
* along with this program; if not, write to the Free Software
14
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18
* Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
22
package weka.core.matrix;
24
import java.util.Arrays;
27
* A vector specialized on integers.
30
* @version $Revision: 1.3 $
32
public class IntVector implements Cloneable {
34
/** Array for internal storage of elements. */
37
/** size of the vector */
38
private int sizeOfVector;
41
/* ------------------------
43
* ------------------------ */
45
/** Constructs a null vector.
52
/** Constructs an n-vector of zeros.
55
public IntVector( int n ){
60
/** Constructs an n-vector of a constant
63
public IntVector( int n, int s ){
68
/** Constructs a vector given an int array
69
* @param v the int array
71
public IntVector( int v[] ){
77
V = new int[ v.length ];
79
set(0, size() - 1, v, 0);
83
/* ------------------------
85
* ------------------------ */
87
/** Gets the size of the vector.
94
* Sets the size of the vector. The provided size can't be greater than
95
* the capacity of the vector.
96
* @param size the new Size.
98
public void setSize( int size ){
99
if( size > capacity() )
100
throw new IllegalArgumentException("insufficient capacity");
104
/** Sets the value of an element.
105
* @param s the value for the element */
106
public void set( int s ) {
107
for( int i = 0; i < size(); i++ )
111
/** Sets the values of elements from an int array.
112
* @param i0 the index of the first element
113
* @param i1 the index of the last element
114
* @param v the int array that stores the values
115
* @param j0 the index of the first element in the int array */
116
public void set( int i0, int i1, int [] v, int j0){
117
for(int i = i0; i<= i1; i++)
118
set( i, v[j0 + i - i0] );
121
/** Sets the values of elements from another IntVector.
122
* @param i0 the index of the first element
123
* @param i1 the index of the last element
124
* @param v the IntVector that stores the values
125
* @param j0 the index of the first element in the IntVector */
126
public void set( int i0, int i1, IntVector v, int j0){
127
for(int i = i0; i<= i1; i++)
128
set( i, v.get(j0 + i - i0) );
131
/** Sets the values of elements from another IntVector.
132
* @param v the IntVector that stores the values
134
public void set( IntVector v ){
135
set( 0, v.size() - 1, v, 0);
138
/** Generates an IntVector that stores all integers inclusively between
140
* @param i0 the first integer
141
* @param i1 the second integer
143
public static IntVector seq( int i0, int i1 ) {
144
if( i1 < i0 ) throw new IllegalArgumentException("i1 < i0 ");
145
IntVector v = new IntVector( i1 - i0 + 1 );
146
for( int i = 0; i < i1 - i0 + 1; i++ ) {
152
/** Access the internal one-dimensional array.
153
@return Pointer to the one-dimensional array of vector elements. */
154
public int [] getArray() {
158
/** Sets the internal one-dimensional array.
159
@param a Pointer to the one-dimensional array of vector elements. */
160
protected void setArray( int [] a ) {
164
/** Sorts the elements in place
167
Arrays.sort( V, 0, size() );
170
/** Returns a copy of the internal one-dimensional array.
171
@return One-dimensional array copy of vector elements. */
172
public int[] getArrayCopy() {
173
int [] b = new int[ size() ];
174
for( int i = 0; i <= size() - 1; i++ ) {
180
/** Returns the capacity of the vector
182
public int capacity() {
186
/** Sets the capacity of the vector
187
* @param capacity the new capacity of the vector
189
public void setCapacity( int capacity ) {
190
if( capacity == capacity() ) return;
192
int m = Math.min( capacity, size() );
193
V = new int[ capacity ];
195
set(0, m-1, old_V, 0);
198
/** Sets a single element.
199
* @param i the index of the element
200
* @param s the new value
202
public void set( int i, int s ) {
206
/** Gets the value of an element.
207
* @param i the index of the element
208
* @return the value of the element
210
public int get( int i ) {
214
/** Makes a deep copy of the vector
216
public IntVector copy() {
217
return (IntVector) clone();
220
/** Clones the IntVector object.
222
public Object clone() {
223
IntVector u = new IntVector( size() );
224
for( int i = 0; i < size(); i++)
229
/** Returns a subvector.
230
* @param i0 the index of the first element
231
* @param i1 the index of the last element
232
* @return the subvector
234
public IntVector subvector( int i0, int i1 )
236
IntVector v = new IntVector( i1-i0+1 );
237
v.set(0, i1 - i0, this, i0);
241
/** Returns a subvector as indexed by an IntVector.
242
* @param index the index
243
* @return the subvector
245
public IntVector subvector( IntVector index ) {
246
IntVector v = new IntVector( index.size() );
247
for( int i = 0; i < index.size(); i++ )
248
v.V[i] = V[index.V[i]];
253
* Swaps the values stored at i and j
254
* @param i the index i
255
* @param j the index j
257
public void swap( int i, int j ){
265
* Shifts an element to another position. Elements between them are
266
* shifted one position left.
267
* @param i the index of the element
268
* @param j the index of the new position */
269
public void shift( int i, int j ){
273
for( int k = i; k <= j-1; k++ )
281
* Shifts an element to the end of the vector. Elements between them are
282
* shifted one position left.
283
* @param j the index of the element
285
public void shiftToEnd( int j ){
286
shift( j, size()-1 );
290
* Returns true if the vector is empty
292
public boolean isEmpty() {
293
if( size() == 0 ) return true;
297
/** Converts the IntVecor to a string
299
public String toString() {
300
return toString( 5, false );
303
/** Convert the IntVecor to a string
304
* @param digits number of digits to be shown
305
* @param trailing true if trailing zeros are to be shown
307
public String toString( int digits, boolean trailing ) {
308
if( isEmpty() ) return "null vector";
310
StringBuffer text = new StringBuffer();
311
FlexibleDecimalFormat nf = new FlexibleDecimalFormat( digits,
314
for( int i = 0; i < size(); i ++ ) nf.update( get(i) );
318
for( int i = 0; i < size(); i++ ) {
319
number = nf.format(get(i));
320
count += 1 + number.length();
321
if( count > width-1 ) {
323
count = 1 + number.length();
325
text.append( " " + number );
328
return text.toString();
332
* Tests the IntVector class
334
public static void main( String args[] ) {
336
IntVector u = new IntVector();
337
System.out.println( u );
339
IntVector v = IntVector.seq(10, 25);
340
System.out.println( v );
342
IntVector w = IntVector.seq(25, 10);
343
System.out.println( w );