KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > util > DynamicObjectArray


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.util;
20
21 /**
22  * simplistic Object array that differentiates from ArrayList by
23  * using chunks instead of exponential growth, thus efficiently dealing
24  * with huge, potentially sparse arrays
25  *
26  * the motivation for this class is memory optimization, i.e. space efficient
27  * storage of potentially huge arrays without good a-priori size guesses
28  *
29  * this class is awefully lifted from DynamicIntArray (same motivation, but
30  * primitive types - not much factorizable functionality w/o excessive
31  * casting/boxing)
32  *
33  * the API of this class is between a primitive array and a AbstractList. Since
34  * it handles Objects, we could turn this into a Collection (and probably should)
35  *
36  * NOTE: like standard Collection implementations/arrays, this class is not
37  * synchronized
38  */

39
40 public class DynamicObjectArray {
41   final static int DEFAULT_CHUNKSIZE = 256;
42   final static int INIT_CHUNKS = 16;
43
44   int chunkSize; /** our allocation sizes */
45   Object JavaDoc[][] data; /** the real data */
46   int length; /** max set element index +1 */
47   
48   public DynamicObjectArray () {
49     this( DEFAULT_CHUNKSIZE);
50   }
51   
52   public DynamicObjectArray (int chunkSize) {
53     this.chunkSize = chunkSize;
54     
55     data = new Object JavaDoc[INIT_CHUNKS][];
56   }
57   
58   public boolean isEmpty () {
59     return (length == 0);
60   }
61   
62   public Object JavaDoc get (int index) {
63     int i = index / chunkSize;
64     int j = index % chunkSize;
65     
66     if (data[i] == null) {
67       return null;
68     } else {
69       return data[i][j];
70     }
71   }
72   
73   public void set (int index, Object JavaDoc value) {
74     int i = index / chunkSize;
75     int j = index % chunkSize;
76     
77     if (i >= data.length) {
78       Object JavaDoc[][] newChunk = new Object JavaDoc[i+1][];
79       System.arraycopy(data, 0, newChunk, 0, data.length);
80       data = newChunk;
81     }
82     if (data[i] == null) {
83       data[i] = new Object JavaDoc[chunkSize];
84     }
85     
86     data[i][j] = value;
87     
88     if (index >= length) {
89       length = index+1;
90     }
91   }
92
93   public int size() {
94     return length;
95   }
96   
97   public String JavaDoc toString() {
98     int i;
99     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(length*4);
100     
101     sb.append('{');
102     int l = length-1;
103     for (i=0; i<l; i++) {
104       sb.append(get(i));
105       sb.append(',');
106     }
107     sb.append(get(i));
108     sb.append('}');
109     
110     return sb.toString();
111   }
112     
113   public Object JavaDoc[] toArray (Object JavaDoc[] buf) {
114     if (buf.length < length) {
115       buf = new Object JavaDoc[length];
116     }
117     for (int i=0; i<length; i++) {
118       buf[i] = get(i);
119     }
120     
121     return buf;
122   }
123
124 }
125
126
Popular Tags