Int Vector (from java-objects-database) : Auto Growth Array « Collections Data Structure « Java






Int Vector (from java-objects-database)

         

/*
Copyright (C) 2007  Mobixess Inc. http://www.java-objects-database.com

This file is part of the JODB (Java Objects Database) open source project.

JODB is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation.

JODB 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 General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
*/

import java.io.Serializable;

public class IntVector implements Serializable {

    

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    public final static int DEFAULT_INCREMENT = 256;

    private int _increment;

    private int _data[];

    private int _size = 0;

    public IntVector() {
        _increment = DEFAULT_INCREMENT;
        _data = new int[_increment];
    }

    public IntVector(int increment) {
        _increment = increment;
        _data = new int[increment];
    }

    public IntVector(int initialSize, int increment) {

        _increment = increment;
        _data = new int[initialSize];
        _size = initialSize;
    }


    public IntVector(int[]  data) {
        _data = new int[data.length];
        _size = data.length;
        _increment = DEFAULT_INCREMENT;
        System.arraycopy(data, 0, _data, 0, _size);
    }

    /**
     * Get the length of the list.
     * 
     * @return length of the list
     */
    public final int size() {
        return _size;
    }

    /**
     * Get the length of the list.
     * 
     * @return length of the list
     */
    public final void setSize(int sz) {
        _size = sz;
    }

    /**
     * Append a int onto the vector.
     * 
     * @param value
     *            Int to add to the list
     */
    public final void addElement(int value) {
        ensureCapacity(_size+1);
        _data[_size] = value;
        _size++;
    }
    
    private void ensureCapacity(int size){
        if (size >= _data.length) {
            int newData[] = new int[size + _increment];

            System.arraycopy(_data, 0, newData, 0, _data.length);

            _data = newData;
        }
    }

    /**
     * Append several int values onto the vector.
     * 
     * @param value
     *            Int to add to the list
     */
    public final void addElements(int value, int numberOfElements) {
        ensureCapacity(_size+numberOfElements);

        for (int i = 0; i < numberOfElements; i++) {
            _data[_size] = value;
            _size++;
        }
    }


    /**
     * Inserts the specified node in this vector at the specified index.
     * 
     * @param value
     *            Int to insert
     * @param at
     *            Index of where to insert
     */
    public final void insertElementAt(int value, int at) {

        ensureCapacity(_size+1);

        if (at <= (_size - 1)) {
            System.arraycopy(_data, at, _data, at + 1, _size - at);
        }

        _data[at] = value;

        _size++;
    }

    public final void removeAllElements() {

        for (int i = 0; i < _size; i++) {
            _data[i] = java.lang.Integer.MIN_VALUE;
        }

        _size = 0;
    }

    /**
     * Removes the first occurrence of the argument from this vector. If the
     * object is found in this vector, each component in the vector with an
     * index greater or equal to the object's index is shifted downward to have
     * an index one smaller than the value it had previously.
     * 
     * @param s
     *            Int to remove from array
     * 
     * @return True if the int was removed, false if it was not found
     */
    public final boolean removeElement(int s) {

        for (int i = 0; i < _size; i++) {
            if (_data[i] == s) {
                if ((i + 1) < _size)
                    System.arraycopy(_data, i + 1, _data, i - 1, _size - i);
                else
                    _data[i] = java.lang.Integer.MIN_VALUE;

                _size--;

                return true;
            }
        }

        return false;
    }

    /**
     * Deletes the component at the specified index. Each component in this
     * vector with an index greater or equal to the specified index is shifted
     * downward to have an index one smaller than the value it had previously.
     * 
     * @param i
     *            index of where to remove and int
     */
    public final void removeElementAt(int i) {

        if (i > _size)
            System.arraycopy(_data, i + 1, _data, i, _size);
        else
            _data[i] = java.lang.Integer.MIN_VALUE;

        _size--;
    }

    /**
     * Sets the component at the specified index of this vector to be the
     * specified object. The previous component at that position is discarded.
     * 
     * The index must be a value greater than or equal to 0 and less than the
     * current size of the vector.
     * 
     * @param node
     *            object to set
     * @param index
     *            Index of where to set the object
     */
    public final void setElementAt(int value, int index) {
        _data[index] = value;
    }

    /**
     * Get the nth element.
     * 
     * @param i
     *            index of object to get
     * 
     * @return object at given index
     */
    public final int elementAt(int i) {
        return _data[i];
    }

    /**
     * Tell if the table contains the given node.
     * 
     * @param s
     *            object to look for
     * 
     * @return true if the object is in the list
     */
    public final boolean contains(int s) {

        for (int i = 0; i < _size; i++) {
            if (_data[i] == s) return true;
        }

        return false;
    }

    /**
     * Searches for the first occurence of the given argument, beginning the
     * search at index, and testing for equality using the equals method.
     * 
     * @param elem
     *            object to look for
     * @param index
     *            Index of where to begin search
     * @return the index of the first occurrence of the object argument in this
     *         vector at position index or later in the vector; returns -1 if
     *         the object is not found.
     */
    public final int indexOf(int elem, int index) {

        for (int i = index; i < _size; i++) {
            if (_data[i] == elem) return i;
        }

        return java.lang.Integer.MIN_VALUE;
    }

    /**
     * Searches for the first occurence of the given argument, beginning the
     * search at index, and testing for equality using the equals method.
     * 
     * @param elem
     *            object to look for
     * @return the index of the first occurrence of the object argument in this
     *         vector at position index or later in the vector; returns -1 if
     *         the object is not found.
     */
    public final int indexOf(int elem) {

        for (int i = 0; i < _size; i++) {
            if (_data[i] == elem) return i;
        }

        return java.lang.Integer.MIN_VALUE;
    }

    /**
     * Searches for the first occurence of the given argument, beginning the
     * search at index, and testing for equality using the equals method.
     * 
     * @param elem
     *            Object to look for
     * @return the index of the first occurrence of the object argument in this
     *         vector at position index or later in the vector; returns -1 if
     *         the object is not found.
     */
    public final int lastIndexOf(int elem) {

        for (int i = (_size - 1); i >= 0; i--) {
            if (_data[i] == elem) return i;
        }

        return java.lang.Integer.MIN_VALUE;
    }
    
    
    public int[] getDataAsArray() {
        return _data;
    }
    
    public final int binarySearch(int key){
        int low = 0;
        int high = _size;

        while (low <= high) {
            int mid = (low + high) >> 1;
            int midVal = _data[mid];

            if (midVal < key)
            low = mid + 1;
            else if (midVal > key)
            high = mid - 1;
            else
            return mid; // key found
        }
        return -(low + 1);  // key not found.
    }

}

   
    
    
    
    
    
    
    
    
  








Related examples in the same category

1.Growable int[]
2.Your own auto-growth Array
3.Long Vector
4.ArrayList of int primitives
5.ArrayList of long primitives
6.ArrayList of short primitives
7.ArrayList of double primitives
8.ArrayList of boolean primitives
9.ArrayList of char primitives
10.ArrayList of byte primitives
11.Growable String array with type specific access methods.
12.Auto Size Array
13.Dynamic Int Array
14.Dynamic Long Array
15.Int 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