Int Array : Auto Growth Array « Collections Data Structure « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Collections Data Structure » Auto Growth Array 




Int Array
        
/*
 * Copyright (c) 2002-2008 "Neo Technology,"
 *     Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.HashSet;
import java.util.Set;

public class IntArray
{
    private int[] rels;
    private int arrayCount = 0;

    public IntArray() 
    {
        rels = new int[2];
    }
    
    public IntArrayint initialCapacity )
    {
        rels = new intinitialCapacity ];
    }
    
    public IntArrayint[] array )
    {
        rels = array;
        arrayCount = array.length;
    }
    
    public void addint id )
    {
        if arrayCount == rels.length )
        {
            int newRels[] new int[rels.length * 2];
            System.arraycopyrels, 0, newRels, 0, rels.length );
            rels = newRels;
        }
        rels[arrayCount++= id;
    }
    
    public void addAllIntArray array )
    {
        if array == null )
        {
            return;
        }
        if array.length() + arrayCount > rels.length )
        {
            int newSize = rels.length * 2;
            while array.length() + arrayCount > newSize )
            {
                newSize = newSize * 2;
            }
            int newRels[] new int[newSize];
            System.arraycopyrels, 0, newRels, 0, arrayCount );
            rels = newRels;
        }
        System.arraycopyarray.getArray()0, rels, arrayCount, 
            array.length() );
        arrayCount += array.length();
    }
    
    public int length()
    {
        return arrayCount;
    }
    
    public int[] getArray()
    {
        return rels;
    }
    
    public int getint )
    {
        assert i >= && i < arrayCount;
        return rels[i];
    }
    
    public static IntArray composeNewIntArray src, IntArray add, 
        IntArray remove )
    {
        if remove == null )
        {
            if src == null )
            {
                return add;
            }
            if add != null )
            {
                IntArray newArray = new IntArrayadd.length() + src.length() );
                newArray.addAllsrc );
                newArray.addAlladd );
                return newArray;
            }
            return src;
        }
        else
        {
            if src == null && add == null )
            {
                return null;
            }
            int newLength = 0;
            if add != null )
            {
                newLength += add.length();
            }
            if src != null )
            {
                newLength += src.length();
            }
            IntArray newArray = new IntArraynewLength );
            Set<Integer> set = new HashSet<Integer>remove.length() 1
                1.0f );
            for int i = 0; i < remove.length(); i++ )
            {
                set.addremove.get) );
            }
            newArray.addAllsrc );
            for int i = 0; i < newArray.length(); i++ )
            {
                int value = newArray.get);
                if set.containsvalue ) )
                {
                    boolean swapSuccessful = false;
                    for int j = newArray.length() 1; j >= i + 1; j--)
                    {
                        int backValue = newArray.get);
                        newArray.arrayCount--;
                        if !set.containsbackValue) )
                        {
                            newArray.getArray()[i= backValue;
                            swapSuccessful = true;
                            break;
                        }
                    }
                    if !swapSuccessful // all elements from pos in remove
                    {
                        newArray.arrayCount--;
                    }
                }
            }
            if add != null )
            {
                for int i = 0; i < add.length(); i++ )
                {
                    int value = add.get);
                    if !set.containsvalue ) )
                    {
                        newArray.addvalue );
                    }
                }
            }
           return newArray;
        }
    }

/*    public static IntArray composeNew( IntArray src, IntArray add, 
        IntArray remove )
    {
        if ( remove == null )
        {
            if ( src == null )
            {
                return add;
            }
            if ( add != null )
            {
                IntArray newArray = new IntArray( add.length() + src.length() );
                newArray.addAll( src );
                newArray.addAll( add );
                return newArray;
            }
            return src;
        }
        else
        {
            if ( src == null )
            {
                return null;
            }
            // TODO: merge add and remove array then append add array to result
            int[] newArray = new int[src.length()];
            int[] srcArray = src.getArray();
            int[] removeArray = remove.getArray();
            assert removeArray.length <= srcArray.length;
            System.arraycopy( srcArray, 0, newArray, 0, src.length() );
            Arrays.sort( newArray );
            Arrays.sort( removeArray );
            int newArraySize = newArray.length;
            int startSearchFrom = 0;
            int checkTo = removeArray.length; // can decrease if we swap
            for ( int i = 0; i < checkTo; i++ )
            {
                int index = binarySearch( newArray, startSearchFrom, 
                    newArraySize, removeArray[i] );
                if ( index >= 0 )
                {
                    // next search can start from here
                    startSearchFrom = index + 1;
                    
                    // find element we can swap with
                    for ( int j = newArraySize - 1; j >= startSearchFrom; j-- )
                    {
                        int swapValue = newArray[j];
                        int rIndex = binarySearch( removeArray, i, checkTo, 
                            swapValue );

                        newArraySize--;
                        
                        // ok last element in newArray == 
                        // last element in removeArray
                        if ( rIndex > 0 ) 
                        {
                            checkTo--;
                            // continue with second last to see if that is 
                            // swapable
                        }
                        else // we swap with this element
                        {
                            newArray[index] = newArray[j];
                            break;
                        }
                    }
                }
            }
            IntArray newIntArray = new IntArray( newArray );
            newIntArray.arrayCount = newArraySize;
            return newIntArray;
        }
    }
    
    private static int binarySearch( int[] array, int startOffset, 
        int endOffset, int value )
    {
        int pIndex = startOffset + ( endOffset - startOffset ) / 2;

        int valueFound = array[pIndex];
        if ( valueFound == value )
        {
            return pIndex;
        }
        if ( pIndex == startOffset ) // search exhausted
        {
            return -1;
        }
        
        if ( value < valueFound )
        {
            return binarySearch( array, startOffset, pIndex, value );
        }
        else
        {
            return binarySearch( array, pIndex, endOffset, value );
        }
    }*/
}

   
    
    
    
    
    
    
    
  














Related examples in the same category
1.Growable int[]
2.Your own auto-growth Array
3.Long Vector
4.Int Vector (from java-objects-database)
5.ArrayList of int primitives
6.ArrayList of long primitives
7.ArrayList of short primitives
8.ArrayList of double primitives
9.ArrayList of boolean primitives
10.ArrayList of char primitives
11.ArrayList of byte primitives
12.Growable String array with type specific access methods.
13.Auto Size Array
14.Dynamic Int Array
15.Dynamic Long Array
16.Int Array List
17.ArrayList of float primitives
18.Fast Array
19.Extensible vector of bytes
20.Int Vector
21.A two dimensional Vector
22.Lazy List creation based on ArrayList
23.Append the given Object to the given array
24.Adds all the elements of the given arrays into a new array.
25.Simple object pool
26.A variable length Double Array: expanding and contracting its internal storage array as elements are added and removed.
27.Append item to array
28.A growable array of bytes
29.Doubles the size of an array
30.Adds the object to the array.
31.Concatenate arrays
32.Double List
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.