KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > util > IntArray


1 /*****************************************************************************
2  * Copyright (C) Zephyr Business Solutions Corp. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8 /*
9  * Created on Jan 20, 2005
10  *
11  */

12 package jfun.util;
13
14 /**
15  * A simple, efficient and dynamic int array.
16  * <p>
17  * @author Ben Yu.
18  *
19  */

20 public final class IntArray {
21   private int[] buf;
22   private int len = 0;
23   /**
24    * Create a int[] object with all the elements stored in this IntArray.
25    * @return the int[] object.
26    */

27   public int[] toArray(){
28     final int[] ret = new int[len];
29     for(int i=0; i<len; i++){
30       ret[i] = buf[i];
31     }
32     return ret;
33   }
34   /**
35    * Create an IntArray object with an initial capacity.
36    * @param capacity the initial capacity.
37    */

38   public IntArray(int capacity){
39     this.buf = new int[capacity];
40   }
41   /**
42    * Create an IntArray object.
43    */

44   public IntArray(){
45     this(10);
46   }
47   /**
48    * Get the number of int values stored.
49    */

50   public int size(){
51     return len;
52   }
53   private void checkIndex(int i){
54     if(i<0 || i>=len)
55       throw new ArrayIndexOutOfBoundsException JavaDoc(i);
56   }
57   /**
58    * Get the int value at a certain index.
59    * @param i the 0-based index of the value.
60    * @return the int value.
61    * @throws ArrayIndexOutOfBoundsException if i is negative or >= size().
62    */

63   public int get(int i){
64     checkIndex(i);
65     return buf[i];
66   }
67   /**
68    * Set the value at a certain index.
69    * @param i the 0-based index.
70    * @param val the new value.
71    * @return the old value.
72    * @throws ArrayIndexOutOfBoundsException if i is negative or >= size().
73    */

74   public int set(int i, int val){
75     checkIndex(i);
76     final int old = buf[i];
77     buf[i] = val;
78     return old;
79   }
80   private static int calcSize(int l, int factor){
81     final int rem = l%factor;
82     return l/factor*factor + (rem>0?factor:0);
83   }
84   /**
85    * Ensure that the IntArray has at least "l" capacity.
86    * @param capacity the minimal capacity.
87    */

88   public void ensureCapacity(int capacity){
89     if(capacity > buf.length){
90       final int factor = buf.length / 2 + 1;
91       grow(calcSize(capacity-buf.length, factor));
92     }
93   }
94   private void grow(int l){
95     final int[] nbuf = new int[buf.length+l];
96     for(int i=0; i<buf.length; i++){
97       nbuf[i] = buf[i];
98     }
99     buf = nbuf;
100   }
101   /**
102    * Add a int into the array.
103    * @param i the int value.
104    * @return this IntArray object.
105    */

106   public IntArray add(int i){
107     ensureCapacity(len+1);
108     buf[len++] = i;
109     return this;
110   }
111 }
112
Popular Tags