2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.util;
20 import java.util.Iterator;
23 * simplistic Object array that differentiates from ArrayList by
24 * using chunks instead of exponential growth, thus efficiently dealing
25 * with huge, potentially sparse arrays
27 * the motivation for this class is memory optimization, i.e. space efficient
28 * storage of potentially huge arrays without good a-priori size guesses
30 * this class is awfully lifted from DynamicIntArray (same motivation, but
31 * primitive types - not much factorizable functionality w/o excessive
34 * the API of this class is between a primitive array and a AbstractList. Since
35 * it handles Objects, we could turn this into a Collection (and probably should)
37 * NOTE: like standard Collection implementations/arrays, this class is not
41 public final class DynamicObjectArray<E> implements Iterable<E> {
42 final static int DEFAULT_CHUNKBITS = 8;
43 final static int INIT_CHUNKS = 16;
45 /** growth strategy */
48 /** our allocation sizes */
50 int nPerChunk; // just a cache for (1<<chunkBits)
52 /** mask for index within chunk */
55 /** the real data. limitations in generics prevent use of E[][] */
58 /** the maximum index set so far */
61 class DynIterator implements Iterator<E> {
65 public boolean hasNext() {
75 public void remove() {
76 throw new UnsupportedOperationException();
80 public DynamicObjectArray () {
81 this(Growth.defaultGrowth, DEFAULT_CHUNKBITS, INIT_CHUNKS);
85 * Creates a DynamicObjectArray in which each chunk has 2**chunkBits elements
86 * and initChunks chunks are initially allocated.
88 public DynamicObjectArray (int chunkBits, int initChunks) {
89 this(Growth.defaultGrowth, chunkBits, initChunks);
92 public DynamicObjectArray (Growth strategy, int chunkBits, int initChunks) {
93 if (chunkBits > 20) throw new IllegalArgumentException();
94 this.chunkBits = chunkBits;
95 nPerChunk = 1<<chunkBits;
96 this.chunkMask = nPerChunk - 1;
97 data = new Object[initChunks][];
102 public Iterator<E> iterator() {
103 return new DynIterator();
106 @SuppressWarnings("unchecked")
107 public E get (int index) {
108 int i = index >> chunkBits;
109 if (i < data.length && data[i] != null) {
110 int j = index & chunkMask;
111 return (E) data[i][j];
117 // this is only the max size, not the max index that was accessed/set
119 return data.length * nPerChunk;
122 public int getMaxIndex() {
126 public void set (int index, E value) {
127 if (index > maxIndex) {
131 int i = index >> chunkBits;
132 int j = index & chunkMask;
134 if (i >= data.length) {
135 int nChunks = growth.grow(data.length, i+1);
136 Object[][] newChunks = new Object[nChunks][];
137 System.arraycopy(data, 0, newChunks, 0, data.length);
140 if (data[i] == null) {
141 data[i] = new Object[1 << chunkBits];
148 public String toString() {
149 int length = data.length * (1 << chunkBits);
150 while (length > 1 && get(length-1) == null) length--;
152 StringBuilder sb = new StringBuilder(length);
156 for (int i=0; i<l; i++) {
163 return sb.toString();
166 // removed toArray method, which is confusing for 1.5