~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/core/matrix/IntVector.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
6
 *
 
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.
 
11
 *
 
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.  */
 
15
 
 
16
/*
 
17
 *    IntVector.java
 
18
 *    Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
 
19
 *
 
20
 */
 
21
 
 
22
package weka.core.matrix;
 
23
 
 
24
import java.util.Arrays;
 
25
 
 
26
/**
 
27
 * A vector specialized on integers.
 
28
 * 
 
29
 * @author Yong Wang
 
30
 * @version $Revision: 1.3 $
 
31
 */
 
32
public class  IntVector implements Cloneable {
 
33
 
 
34
  /** Array for internal storage of elements. */
 
35
  int[]  V;
 
36
 
 
37
  /** size of the vector */
 
38
  private int  sizeOfVector;
 
39
 
 
40
 
 
41
  /* ------------------------
 
42
     Constructors
 
43
     * ------------------------ */
 
44
 
 
45
  /** Constructs a null vector.
 
46
   */
 
47
  public IntVector(){
 
48
    V = new int[ 0 ];
 
49
    setSize( 0 );
 
50
  }
 
51
    
 
52
  /** Constructs an n-vector of zeros. 
 
53
   *  @param n    Length.
 
54
  */
 
55
  public IntVector( int n ){
 
56
    V = new int[ n ];
 
57
    setSize( n );
 
58
  }
 
59
    
 
60
  /** Constructs an n-vector of a constant
 
61
   *  @param n    Length.
 
62
  */
 
63
  public IntVector( int n, int s ){
 
64
    this(n);
 
65
    set( s );
 
66
  }
 
67
    
 
68
  /** Constructs a vector given an int array
 
69
   *  @param v the int array
 
70
  */
 
71
  public IntVector( int v[] ){
 
72
    if( v == null ) {
 
73
      V = new int[ 0 ];
 
74
      setSize( 0 );
 
75
    }
 
76
    else {
 
77
      V = new int[ v.length ];
 
78
      setSize( v.length );
 
79
      set(0, size() - 1, v, 0);
 
80
    }
 
81
  }
 
82
    
 
83
  /* ------------------------
 
84
     Public Methods
 
85
     * ------------------------ */
 
86
    
 
87
  /** Gets the size of the vector.
 
88
   *  @return Size.  */
 
89
  public int  size(){
 
90
    return sizeOfVector;
 
91
  }
 
92
    
 
93
  /** 
 
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.
 
97
   */
 
98
  public void  setSize( int size ){
 
99
    if( size > capacity() ) 
 
100
      throw new IllegalArgumentException("insufficient capacity");
 
101
    sizeOfVector = size;
 
102
  }
 
103
  
 
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++ )
 
108
      set(i, s);
 
109
  }
 
110
 
 
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] );
 
119
  }
 
120
 
 
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) );
 
129
  }
 
130
 
 
131
  /** Sets the values of elements from another IntVector.
 
132
   *  @param v the IntVector that stores the values 
 
133
   */
 
134
  public void  set( IntVector v ){
 
135
    set( 0, v.size() - 1, v, 0);
 
136
  }
 
137
 
 
138
  /** Generates an IntVector that stores all integers inclusively between
 
139
   *  two integers.
 
140
   *  @param i0 the first integer
 
141
   *  @param i1 the second integer 
 
142
   */
 
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++ ) {
 
147
      v.set(i, i + i0);
 
148
    }
 
149
    return v; 
 
150
  } 
 
151
  
 
152
  /** Access the internal one-dimensional array.
 
153
      @return Pointer to the one-dimensional array of vector elements. */
 
154
  public int []  getArray() {
 
155
    return V;
 
156
  }
 
157
    
 
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 ) {
 
161
    V = a;
 
162
  }
 
163
    
 
164
  /** Sorts the elements in place 
 
165
   */
 
166
  public void  sort() {
 
167
    Arrays.sort( V, 0, size() );
 
168
  }
 
169
 
 
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++ ) {
 
175
      b[i] = V[i];
 
176
    }
 
177
    return b;
 
178
  }
 
179
 
 
180
  /** Returns the capacity of the vector 
 
181
   */
 
182
  public int capacity() {
 
183
    return V.length;
 
184
  }
 
185
 
 
186
  /** Sets the capacity of the vector 
 
187
   *  @param capacity the new capacity of the vector
 
188
   */
 
189
  public void  setCapacity( int capacity ) {
 
190
    if( capacity == capacity() ) return;
 
191
    int [] old_V = V;
 
192
    int m = Math.min( capacity, size() );
 
193
    V = new int[ capacity ];
 
194
    setSize( capacity );
 
195
    set(0, m-1, old_V, 0);
 
196
  }
 
197
 
 
198
  /** Sets a single element.
 
199
   *  @param i    the index of the element
 
200
   *  @param s    the new value
 
201
  */
 
202
  public void  set( int i, int s ) {
 
203
    V[i] = s;
 
204
  }
 
205
    
 
206
  /** Gets the value of an element.
 
207
   *  @param i    the index of the element
 
208
   *  @return     the value of the element
 
209
  */
 
210
  public int  get( int i ) {
 
211
    return V[i];
 
212
  }
 
213
  
 
214
  /** Makes a deep copy of the vector
 
215
   */
 
216
  public IntVector  copy() { 
 
217
    return (IntVector) clone();
 
218
  }
 
219
    
 
220
  /** Clones the IntVector object.
 
221
   */
 
222
  public Object  clone() { 
 
223
    IntVector u = new IntVector( size() );
 
224
    for( int i = 0; i < size(); i++) 
 
225
      u.V[i] = V[i];
 
226
    return u;
 
227
  }
 
228
  
 
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
 
233
  */
 
234
  public IntVector  subvector( int i0, int i1 ) 
 
235
  {
 
236
    IntVector v = new IntVector( i1-i0+1 );
 
237
    v.set(0, i1 - i0, this, i0);
 
238
    return v;
 
239
  }
 
240
 
 
241
  /** Returns a subvector as indexed by an IntVector.
 
242
   *  @param index   the index
 
243
   *  @return the subvector
 
244
  */
 
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]];
 
249
    return v;
 
250
  }
 
251
 
 
252
  /**
 
253
   *  Swaps the values stored at i and j
 
254
   *  @param i the index i
 
255
   *  @param j the index j
 
256
   */
 
257
  public void  swap( int i, int j ){
 
258
    if( i == j ) return;
 
259
    int t = get( i );
 
260
    set( i, get(j) );
 
261
    set( j, t );
 
262
  }
 
263
  
 
264
  /** 
 
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 ){
 
270
    if( i == j ) return;
 
271
    if( i < j ) {
 
272
      int t = V[i];
 
273
      for( int k = i; k <= j-1; k++ )
 
274
        V[k] = V[k+1];
 
275
      V[j] = t;
 
276
    }
 
277
    else shift( j, i );
 
278
  }
 
279
  
 
280
  /** 
 
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
 
284
   */
 
285
  public void  shiftToEnd( int j ){
 
286
    shift( j, size()-1 );
 
287
  }
 
288
  
 
289
  /** 
 
290
   * Returns true if the vector is empty
 
291
   */
 
292
  public boolean  isEmpty() {
 
293
    if( size() == 0 ) return true;
 
294
    return false;
 
295
  }
 
296
 
 
297
  /** Converts the IntVecor to a string
 
298
   */ 
 
299
  public String  toString() {
 
300
    return toString( 5, false );
 
301
  }
 
302
    
 
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
 
306
   */ 
 
307
  public String  toString( int digits, boolean trailing ) {
 
308
    if( isEmpty() ) return "null vector";
 
309
 
 
310
    StringBuffer text = new StringBuffer();
 
311
    FlexibleDecimalFormat nf = new FlexibleDecimalFormat( digits, 
 
312
                                                          trailing );
 
313
    nf.grouping( true );
 
314
    for( int i = 0; i < size(); i ++ ) nf.update( get(i) );
 
315
    int count = 0;
 
316
    int width = 80;
 
317
    String number;
 
318
    for( int i = 0; i < size(); i++ ) {
 
319
      number = nf.format(get(i));
 
320
      count += 1 + number.length();
 
321
      if( count > width-1 ) { 
 
322
        text.append('\n'); 
 
323
        count = 1 + number.length();
 
324
      }
 
325
      text.append( " " + number );
 
326
    }
 
327
        
 
328
    return text.toString();
 
329
  }
 
330
 
 
331
  /** 
 
332
   *  Tests the IntVector class
 
333
   */
 
334
  public static void  main( String args[] ) {
 
335
    
 
336
    IntVector u = new IntVector();
 
337
    System.out.println( u );
 
338
 
 
339
    IntVector v = IntVector.seq(10, 25);
 
340
    System.out.println( v );
 
341
 
 
342
    IntVector w = IntVector.seq(25, 10);
 
343
    System.out.println( w );
 
344
 
 
345
  }
 
346
}