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