001    /*--------------------------------------------------------------------------+
002    $Id: ManagedIntArray.java 26283 2010-02-18 11:18:57Z juergens $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | You may obtain a copy of the License at                                  |
009    |                                                                          |
010    |    http://www.apache.org/licenses/LICENSE-2.0                            |
011    |                                                                          |
012    | Unless required by applicable law or agreed to in writing, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.commons.collections;
019    
020    /**
021     * A class containing an int array that is managed in the sense that it can grow
022     * dynamically (using exponential growth). It is useful for cases where an
023     * ArrayList<Integer> seems like overkill due to the high memory footprint of
024     * the Integer objects and the amount of work performed for auto (un)boxing.
025     * Note however, that a subclass has full access to the internals and thus might
026     * cause chaos.
027     * 
028     * @author hummelb
029     * @author $Author: juergens $
030     * @version $Rev: 26283 $
031     * @levd.rating GREEN Hash: 88A19EE6A6126AC7747DC3EC47C17C1D
032     */
033    public class ManagedIntArray {
034    
035            /** The current size of the array. */
036            protected int size = 0;
037    
038            /** The actual array. */
039            protected int[] array = new int[8];
040    
041            /** Add space for a single element to the end of the array. */
042            protected void addArrayElement() {
043                    addArrayElements(1);
044            }
045    
046            /** Add space for multiple elements to the end of the array. */
047            protected void addArrayElements(int count) {
048                    if (size + count >= array.length) {
049                            int newSize = 2 * array.length;
050                            while (newSize <= size + count) {
051                                    newSize *= 2;
052                            }
053    
054                            int[] oldArray = array;
055                            array = new int[newSize];
056                            System.arraycopy(oldArray, 0, array, 0, size);
057                    }
058                    size += count;
059            }
060    }