source: branches/MetisMQI/src/main/java/weka/core/matrix/IntVector.java

Last change on this file was 29, checked in by gnappo, 15 years ago

Taggata versione per la demo e aggiunto branch.

File size: 8.8 KB
Line 
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
22package weka.core.matrix;
23
24import weka.core.RevisionHandler;
25import weka.core.RevisionUtils;
26
27import java.util.Arrays;
28
29/**
30 * A vector specialized on integers.
31 *
32 * @author Yong Wang
33 * @version $Revision: 5953 $
34 */
35public class  IntVector
36  implements Cloneable, RevisionHandler {
37
38  /** Array for internal storage of elements. */
39  int[]  V;
40
41  /** size of the vector */
42  private int  sizeOfVector;
43
44
45  /* ------------------------
46     Constructors
47     * ------------------------ */
48
49  /** Constructs a null vector.
50   */
51  public IntVector(){
52    V = new int[ 0 ];
53    setSize( 0 );
54  }
55   
56  /** Constructs an n-vector of zeros.
57   *  @param n    Length.
58  */
59  public IntVector( int n ){
60    V = new int[ n ];
61    setSize( n );
62  }
63   
64  /** Constructs an n-vector of a constant
65   *  @param n    Length.
66  */
67  public IntVector( int n, int s ){
68    this(n);
69    set( s );
70  }
71   
72  /** Constructs a vector given an int array
73   *  @param v the int array
74  */
75  public IntVector( int v[] ){
76    if( v == null ) {
77      V = new int[ 0 ];
78      setSize( 0 );
79    }
80    else {
81      V = new int[ v.length ];
82      setSize( v.length );
83      set(0, size() - 1, v, 0);
84    }
85  }
86   
87  /* ------------------------
88     Public Methods
89     * ------------------------ */
90   
91  /** Gets the size of the vector.
92   *  @return Size.  */
93  public int  size(){
94    return sizeOfVector;
95  }
96   
97  /**
98   * Sets the size of the vector. The provided size can't be greater than
99   * the capacity of the vector.
100   * @param size the new Size.
101   */
102  public void  setSize( int size ){
103    if( size > capacity() ) 
104      throw new IllegalArgumentException("insufficient capacity");
105    sizeOfVector = size;
106  }
107 
108  /** Sets the value of an element.
109   *  @param s the value for the element */
110  public void  set( int s ) {
111    for( int i = 0; i < size(); i++ )
112      set(i, s);
113  }
114
115  /** Sets the values of elements from an int array.
116   *  @param i0 the index of the first element
117   *  @param i1 the index of the last element
118   *  @param v the int array that stores the values
119   *  @param j0 the index of the first element in the int array */
120  public void  set( int i0, int i1, int [] v, int j0){
121    for(int i = i0; i<= i1; i++)
122      set( i, v[j0 + i - i0] );
123  }
124
125  /** Sets the values of elements from another IntVector.
126   *  @param i0 the index of the first element
127   *  @param i1 the index of the last element
128   *  @param v the IntVector that stores the values
129   *  @param j0 the index of the first element in the IntVector */
130  public void  set( int i0, int i1, IntVector v, int j0){
131    for(int i = i0; i<= i1; i++)
132      set( i, v.get(j0 + i - i0) );
133  }
134
135  /** Sets the values of elements from another IntVector.
136   *  @param v the IntVector that stores the values
137   */
138  public void  set( IntVector v ){
139    set( 0, v.size() - 1, v, 0);
140  }
141
142  /** Generates an IntVector that stores all integers inclusively between
143   *  two integers.
144   *  @param i0 the first integer
145   *  @param i1 the second integer
146   */
147  public static IntVector  seq( int i0, int i1 ) {
148    if( i1 < i0 ) throw new IllegalArgumentException("i1 < i0 ");
149    IntVector v = new IntVector( i1 - i0 + 1 );
150    for( int i = 0; i < i1 - i0 + 1; i++ ) {
151      v.set(i, i + i0);
152    }
153    return v; 
154  } 
155 
156  /** Access the internal one-dimensional array.
157      @return Pointer to the one-dimensional array of vector elements. */
158  public int []  getArray() {
159    return V;
160  }
161   
162  /** Sets the internal one-dimensional array.
163      @param a Pointer to the one-dimensional array of vector elements. */
164  protected void  setArray( int [] a ) {
165    V = a;
166  }
167   
168  /** Sorts the elements in place
169   */
170  public void  sort() {
171    Arrays.sort( V, 0, size() );
172  }
173
174  /** Returns a copy of the internal one-dimensional array.
175      @return One-dimensional array copy of vector elements.  */
176  public int[]  getArrayCopy() {
177    int [] b = new int[ size() ];
178    for( int i = 0; i <= size() - 1; i++ ) {
179      b[i] = V[i];
180    }
181    return b;
182  }
183
184  /** Returns the capacity of the vector
185   */
186  public int capacity() {
187    return V.length;
188  }
189
190  /** Sets the capacity of the vector
191   *  @param capacity the new capacity of the vector
192   */
193  public void  setCapacity( int capacity ) {
194    if( capacity == capacity() ) return;
195    int [] old_V = V;
196    int m = Math.min( capacity, size() );
197    V = new int[ capacity ];
198    setSize( capacity );
199    set(0, m-1, old_V, 0);
200  }
201
202  /** Sets a single element.
203   *  @param i    the index of the element
204   *  @param s    the new value
205  */
206  public void  set( int i, int s ) {
207    V[i] = s;
208  }
209   
210  /** Gets the value of an element.
211   *  @param i    the index of the element
212   *  @return     the value of the element
213  */
214  public int  get( int i ) {
215    return V[i];
216  }
217 
218  /** Makes a deep copy of the vector
219   */
220  public IntVector  copy() { 
221    return (IntVector) clone();
222  }
223   
224  /** Clones the IntVector object.
225   */
226  public Object  clone() { 
227    IntVector u = new IntVector( size() );
228    for( int i = 0; i < size(); i++) 
229      u.V[i] = V[i];
230    return u;
231  }
232 
233  /** Returns a subvector.
234   *  @param i0   the index of the first element
235   *  @param i1   the index of the last element
236   *  @return the subvector
237  */
238  public IntVector  subvector( int i0, int i1 ) 
239  {
240    IntVector v = new IntVector( i1-i0+1 );
241    v.set(0, i1 - i0, this, i0);
242    return v;
243  }
244
245  /** Returns a subvector as indexed by an IntVector.
246   *  @param index   the index
247   *  @return the subvector
248  */
249  public IntVector  subvector( IntVector index ) {
250    IntVector v = new IntVector( index.size() );
251    for( int i = 0; i < index.size(); i++ )
252      v.V[i] = V[index.V[i]];
253    return v;
254  }
255
256  /**
257   *  Swaps the values stored at i and j
258   *  @param i the index i
259   *  @param j the index j
260   */
261  public void  swap( int i, int j ){
262    if( i == j ) return;
263    int t = get( i );
264    set( i, get(j) );
265    set( j, t );
266  }
267 
268  /**
269   *  Shifts an element to another position. Elements between them are
270   *  shifted one position left.
271   *  @param i the index of the element
272   *  @param j the index of the new position */
273  public void  shift( int i, int j ){
274    if( i == j ) return;
275    if( i < j ) {
276      int t = V[i];
277      for( int k = i; k <= j-1; k++ )
278        V[k] = V[k+1];
279      V[j] = t;
280    }
281    else shift( j, i );
282  }
283 
284  /**
285   *  Shifts an element to the end of the vector. Elements between them are
286   *  shifted one position left.
287   *  @param j the index of the element
288   */
289  public void  shiftToEnd( int j ){
290    shift( j, size()-1 );
291  }
292 
293  /**
294   * Returns true if the vector is empty
295   */
296  public boolean  isEmpty() {
297    if( size() == 0 ) return true;
298    return false;
299  }
300
301  /** Converts the IntVecor to a string
302   */ 
303  public String  toString() {
304    return toString( 5, false );
305  }
306   
307  /** Convert the IntVecor to a string
308   *  @param digits number of digits to be shown
309   *  @param trailing true if trailing zeros are to be shown
310   */ 
311  public String  toString( int digits, boolean trailing ) {
312    if( isEmpty() ) return "null vector";
313
314    StringBuffer text = new StringBuffer();
315    FlexibleDecimalFormat nf = new FlexibleDecimalFormat( digits, 
316                                                          trailing );
317    nf.grouping( true );
318    for( int i = 0; i < size(); i ++ ) nf.update( get(i) );
319    int count = 0;
320    int width = 80;
321    String number;
322    for( int i = 0; i < size(); i++ ) {
323      number = nf.format(get(i));
324      count += 1 + number.length();
325      if( count > width-1 ) { 
326        text.append('\n'); 
327        count = 1 + number.length();
328      }
329      text.append( " " + number );
330    }
331       
332    return text.toString();
333  }
334 
335  /**
336   * Returns the revision string.
337   *
338   * @return            the revision
339   */
340  public String getRevision() {
341    return RevisionUtils.extract("$Revision: 5953 $");
342  }
343
344  /**
345   *  Tests the IntVector class
346   */
347  public static void  main( String args[] ) {
348   
349    IntVector u = new IntVector();
350    System.out.println( u );
351
352    IntVector v = IntVector.seq(10, 25);
353    System.out.println( v );
354
355    IntVector w = IntVector.seq(25, 10);
356    System.out.println( w );
357
358  }
359}
Note: See TracBrowser for help on using the repository browser.