-///////////////////////////////////////////////////////////////////////////////\r
-// Copyright (c) 2001, Eric D. Friedman All Rights Reserved.\r
-// Copyright (c) 2009, Rob Eden All Rights Reserved.\r
-// Copyright (c) 2009, Jeff Randall All Rights Reserved.\r
-//\r
-// This library is free software; you can redistribute it and/or\r
-// modify it under the terms of the GNU Lesser General Public\r
-// License as published by the Free Software Foundation; either\r
-// version 2.1 of the License, or (at your option) any later version.\r
-//\r
-// This library is distributed in the hope that it will be useful,\r
-// but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-// GNU General Public License for more details.\r
-//\r
-// You should have received a copy of the GNU Lesser General Public\r
-// License along with this program; if not, write to the Free Software\r
-// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\r
-///////////////////////////////////////////////////////////////////////////////\r
-\r
-package org.simantics.db.impl.graph;\r
-\r
-import gnu.trove.TIntCollection;\r
-import gnu.trove.function.TIntFunction;\r
-import gnu.trove.impl.Constants;\r
-import gnu.trove.impl.HashFunctions;\r
-import gnu.trove.iterator.TIntIterator;\r
-import gnu.trove.procedure.TIntProcedure;\r
-\r
-import java.io.IOException;\r
-import java.io.ObjectInput;\r
-import java.io.ObjectOutput;\r
-import java.util.Arrays;\r
-import java.util.Collection;\r
-import java.util.ConcurrentModificationException;\r
-import java.util.NoSuchElementException;\r
-import java.util.Random;\r
-\r
-\r
-//////////////////////////////////////////////////\r
-// THIS IS A GENERATED CLASS. DO NOT HAND EDIT! //\r
-//////////////////////////////////////////////////\r
-\r
-\r
-/**\r
- * A resizable, array-backed list of int primitives.\r
- */\r
-public class TIntArrayListInternal {\r
-\r
- /** the data of the list */\r
- protected int[] _data;\r
-\r
- /** the index after the last entry in the list */\r
- protected int _pos;\r
-\r
- /** the default capacity for new lists */\r
- protected static final int DEFAULT_CAPACITY = Constants.DEFAULT_CAPACITY;\r
-\r
- /** the int value that represents null */\r
- protected int no_entry_value;\r
-\r
-\r
- /**\r
- * Creates a new <code>TIntArrayList</code> instance with the\r
- * default capacity.\r
- */\r
- @SuppressWarnings({"RedundantCast"})\r
- public TIntArrayListInternal() {\r
- this( DEFAULT_CAPACITY, ( int ) 0 );\r
- }\r
-\r
-\r
- /**\r
- * Creates a new <code>TIntArrayList</code> instance with the\r
- * specified capacity.\r
- *\r
- * @param capacity an <code>int</code> value\r
- */\r
- @SuppressWarnings({"RedundantCast"})\r
- public TIntArrayListInternal( int capacity ) {\r
- this( capacity, ( int ) 0 );\r
- }\r
-\r
-\r
- /**\r
- * Creates a new <code>TIntArrayList</code> instance with the\r
- * specified capacity.\r
- *\r
- * @param capacity an <code>int</code> value\r
- * @param no_entry_value an <code>int</code> value that represents null.\r
- */\r
- public TIntArrayListInternal( int capacity, int no_entry_value ) {\r
- _data = new int[ capacity ];\r
- _pos = 0;\r
- this.no_entry_value = no_entry_value;\r
- }\r
-\r
- /**\r
- * Creates a new <code>TIntArrayList</code> instance that contains\r
- * a copy of the collection passed to us.\r
- *\r
- * @param collection the collection to copy\r
- */\r
- public TIntArrayListInternal ( TIntCollection collection ) {\r
- this( collection.size() );\r
- addAll( collection ); \r
- }\r
-\r
-\r
- /**\r
- * Creates a new <code>TIntArrayList</code> instance whose\r
- * capacity is the length of <tt>values</tt> array and whose\r
- * initial contents are the specified values.\r
- * <p>\r
- * A defensive copy of the given values is held by the new instance.\r
- *\r
- * @param values an <code>int[]</code> value\r
- */\r
- public TIntArrayListInternal( int[] values ) {\r
- this( values.length );\r
- add( values );\r
- }\r
-\r
- protected TIntArrayListInternal(int[] values, int no_entry_value, boolean wrap) {\r
- if (!wrap)\r
- throw new IllegalStateException("Wrong call");\r
-\r
- if (values == null)\r
- throw new IllegalArgumentException("values can not be null");\r
-\r
- _data = values;\r
- _pos = values.length;\r
- this.no_entry_value = no_entry_value;\r
- }\r
-\r
- /**\r
- * Returns a primitive List implementation that wraps around the given primitive array.\r
- * <p/>\r
- * NOTE: mutating operation are allowed as long as the List does not grow. In that case\r
- * an IllegalStateException will be thrown\r
- *\r
- * @param values\r
- * @return\r
- */\r
- public static TIntArrayListInternal wrap(int[] values) {\r
- return wrap(values, ( int ) 0);\r
- }\r
-\r
- /**\r
- * Returns a primitive List implementation that wraps around the given primitive array.\r
- * <p/>\r
- * NOTE: mutating operation are allowed as long as the List does not grow. In that case\r
- * an IllegalStateException will be thrown\r
- *\r
- * @param values\r
- * @param no_entry_value\r
- * @return\r
- */\r
- public static TIntArrayListInternal wrap(int[] values, int no_entry_value) {\r
- return new TIntArrayListInternal(values, no_entry_value, true) {\r
- /**\r
- * Growing the wrapped external array is not allow\r
- */\r
- @Override\r
- public void ensureCapacity(int capacity) {\r
- if (capacity > _data.length)\r
- throw new IllegalStateException("Can not grow ArrayList wrapped external array");\r
- }\r
- };\r
- }\r
-\r
- /** {@inheritDoc} */\r
- public int getNoEntryValue() {\r
- return no_entry_value;\r
- }\r
-\r
-\r
- // sizing\r
-\r
- /**\r
- * Grow the internal array as needed to accommodate the specified number of elements.\r
- * The size of the array bytes on each resize unless capacity requires more than twice\r
- * the current capacity.\r
- */\r
- public void ensureCapacity( int capacity ) {\r
- if ( capacity > _data.length ) {\r
- int newCap = Math.max( _data.length << 1, capacity );\r
- int[] tmp = new int[ newCap ];\r
- System.arraycopy( _data, 0, tmp, 0, _data.length );\r
- _data = tmp;\r
- }\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public int sizeInternal() {\r
- return _pos;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public boolean isEmpty() {\r
- return _pos == 0;\r
- }\r
-\r
-\r
- /**\r
- * Sheds any excess capacity above and beyond the current size of the list.\r
- */\r
- public void trimToSize() {\r
- if ( _data.length > sizeInternal() ) {\r
- int[] tmp = new int[ sizeInternal() ];\r
- toArray( tmp, 0, tmp.length );\r
- _data = tmp;\r
- }\r
- }\r
-\r
-\r
- // modifying\r
-\r
- /** {@inheritDoc} */\r
- public boolean add( int val ) {\r
- ensureCapacity( _pos + 1 );\r
- _data[ _pos++ ] = val;\r
- return true;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public void add( int[] vals ) {\r
- add( vals, 0, vals.length );\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public void add( int[] vals, int offset, int length ) {\r
- ensureCapacity( _pos + length );\r
- System.arraycopy( vals, offset, _data, _pos, length );\r
- _pos += length;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public void insert( int offset, int value ) {\r
- if ( offset == _pos ) {\r
- add( value );\r
- return;\r
- }\r
- ensureCapacity( _pos + 1 );\r
- // shift right\r
- System.arraycopy( _data, offset, _data, offset + 1, _pos - offset );\r
- // insert\r
- _data[ offset ] = value;\r
- _pos++;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public void insert( int offset, int[] values ) {\r
- insert( offset, values, 0, values.length );\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public void insert( int offset, int[] values, int valOffset, int len ) {\r
- if ( offset == _pos ) {\r
- add( values, valOffset, len );\r
- return;\r
- }\r
-\r
- ensureCapacity( _pos + len );\r
- // shift right\r
- System.arraycopy( _data, offset, _data, offset + len, _pos - offset );\r
- // insert\r
- System.arraycopy( values, valOffset, _data, offset, len );\r
- _pos += len;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public int getAt( int offset ) {\r
- if ( offset >= _pos ) {\r
- throw new ArrayIndexOutOfBoundsException( offset );\r
- }\r
- return _data[ offset ];\r
- }\r
-\r
-\r
- /**\r
- * Returns the value at the specified offset without doing any bounds checking.\r
- */\r
- public int getQuick( int offset ) {\r
- return _data[ offset ];\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public int set( int offset, int val ) {\r
- if ( offset >= _pos ) {\r
- throw new ArrayIndexOutOfBoundsException( offset );\r
- }\r
-\r
- int prev_val = _data[ offset ];\r
- _data[ offset ] = val;\r
- return prev_val;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public int replace( int offset, int val ) {\r
- if ( offset >= _pos ) {\r
- throw new ArrayIndexOutOfBoundsException( offset );\r
- }\r
- int old = _data[ offset ];\r
- _data[ offset ] = val;\r
- return old;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public void set( int offset, int[] values ) {\r
- set( offset, values, 0, values.length );\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public void set( int offset, int[] values, int valOffset, int length ) {\r
- if ( offset < 0 || offset + length > _pos ) {\r
- throw new ArrayIndexOutOfBoundsException( offset );\r
- }\r
- System.arraycopy( values, valOffset, _data, offset, length );\r
- }\r
-\r
-\r
- /**\r
- * Sets the value at the specified offset without doing any bounds checking.\r
- */\r
- public void setQuick( int offset, int val ) {\r
- _data[ offset ] = val;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public void clear() {\r
- clear( DEFAULT_CAPACITY );\r
- }\r
-\r
-\r
- /**\r
- * Flushes the internal state of the list, setting the capacity of the empty list to\r
- * <tt>capacity</tt>.\r
- */\r
- public void clear( int capacity ) {\r
- _data = new int[ capacity ];\r
- _pos = 0;\r
- }\r
-\r
-\r
- /**\r
- * Sets the size of the list to 0, but does not change its capacity. This method can\r
- * be used as an alternative to the {@link #clear()} method if you want to recycle a\r
- * list without allocating new backing arrays.\r
- */\r
- public void reset() {\r
- _pos = 0;\r
- Arrays.fill( _data, no_entry_value );\r
- }\r
-\r
-\r
- /**\r
- * Sets the size of the list to 0, but does not change its capacity. This method can\r
- * be used as an alternative to the {@link #clear()} method if you want to recycle a\r
- * list without allocating new backing arrays. This method differs from\r
- * {@link #reset()} in that it does not clear the old values in the backing array.\r
- * Thus, it is possible for getQuick to return stale data if this method is used and\r
- * the caller is careless about bounds checking.\r
- */\r
- public void resetQuick() {\r
- _pos = 0;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public boolean removeValue( int value ) {\r
- for ( int index = 0; index < _pos; index++ ) {\r
- if ( value == _data[index] ) {\r
- remove( index, 1 );\r
- return true;\r
- }\r
- }\r
- return false;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public int removeAt( int offset ) {\r
- int old = getAt( offset );\r
- remove( offset, 1 );\r
- return old;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public void remove( int offset, int length ) {\r
- if ( length == 0 ) return;\r
- if ( offset < 0 || offset >= _pos ) {\r
- throw new ArrayIndexOutOfBoundsException(offset);\r
- }\r
-\r
- if ( offset == 0 ) {\r
- // data at the front\r
- System.arraycopy( _data, length, _data, 0, _pos - length );\r
- }\r
- else if ( _pos - length == offset ) {\r
- // no copy to make, decrementing pos "deletes" values at\r
- // the end\r
- }\r
- else {\r
- // data in the middle\r
- System.arraycopy( _data, offset + length, _data, offset,\r
- _pos - ( offset + length ) );\r
- }\r
- _pos -= length;\r
- // no need to clear old values beyond _pos, because this is a\r
- // primitive collection and 0 takes as much room as any other\r
- // value\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public TIntIterator iteratorInternal() {\r
- return new TIntArrayIterator( 0 );\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public boolean containsAll( Collection<?> collection ) {\r
- for ( Object element : collection ) {\r
- if ( element instanceof Integer ) {\r
- int c = ( ( Integer ) element ).intValue();\r
- if ( ! contains( c ) ) {\r
- return false;\r
- }\r
- } else {\r
- return false;\r
- }\r
-\r
- }\r
- return true;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public boolean containsAll( TIntCollection collection ) {\r
- if ( this == collection ) {\r
- return true;\r
- }\r
- TIntIterator iter = collection.iterator();\r
- while ( iter.hasNext() ) {\r
- int element = iter.next();\r
- if ( ! contains( element ) ) {\r
- return false;\r
- }\r
- }\r
- return true;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public boolean containsAll( int[] array ) {\r
- for ( int i = array.length; i-- > 0; ) {\r
- if ( ! contains( array[i] ) ) {\r
- return false;\r
- }\r
- }\r
- return true;\r
- }\r
-\r
-\r
-// /** {@inheritDoc} */\r
-// public boolean addAll( Collection<? extends Integer> collection ) {\r
-// boolean changed = false;\r
-// for ( Integer element : collection ) {\r
-// int e = element.intValue();\r
-// if ( add( e ) ) {\r
-// changed = true;\r
-// }\r
-// }\r
-// return changed;\r
-// }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public boolean addAll( TIntCollection collection ) {\r
- boolean changed = false;\r
- TIntIterator iter = collection.iterator();\r
- while ( iter.hasNext() ) {\r
- int element = iter.next();\r
- if ( add( element ) ) {\r
- changed = true;\r
- }\r
- }\r
- return changed;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public boolean addAll( int[] array ) {\r
- boolean changed = false;\r
- for ( int element : array ) {\r
- if ( add( element ) ) {\r
- changed = true;\r
- }\r
- }\r
- return changed;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- @SuppressWarnings({"SuspiciousMethodCalls"})\r
- public boolean retainAll( Collection<?> collection ) {\r
- boolean modified = false;\r
- TIntIterator iter = iteratorInternal();\r
- while ( iter.hasNext() ) {\r
- if ( ! collection.contains( Integer.valueOf ( iter.next() ) ) ) {\r
- iter.remove();\r
- modified = true;\r
- }\r
- }\r
- return modified;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public boolean retainAll( TIntCollection collection ) {\r
- if ( this == collection ) {\r
- return false;\r
- }\r
- boolean modified = false;\r
- TIntIterator iter = iteratorInternal();\r
- while ( iter.hasNext() ) {\r
- if ( ! collection.contains( iter.next() ) ) {\r
- iter.remove();\r
- modified = true;\r
- }\r
- }\r
- return modified;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public boolean retainAll( int[] array ) {\r
- boolean changed = false;\r
- Arrays.sort( array );\r
- int[] data = _data;\r
-\r
- for ( int i = _pos; i-- > 0; ) {\r
- if ( Arrays.binarySearch( array, data[i] ) < 0 ) {\r
- remove( i, 1 );\r
- changed = true;\r
- }\r
- }\r
- return changed;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public boolean removeAll( Collection<?> collection ) {\r
- boolean changed = false;\r
- for ( Object element : collection ) {\r
- if ( element instanceof Integer ) {\r
- int c = ( ( Integer ) element ).intValue();\r
- if ( removeValue( c ) ) {\r
- changed = true;\r
- }\r
- }\r
- }\r
- return changed;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public boolean removeAll( TIntCollection collection ) {\r
- if ( collection == this ) {\r
- clear();\r
- return true;\r
- }\r
- boolean changed = false;\r
- TIntIterator iter = collection.iterator();\r
- while ( iter.hasNext() ) {\r
- int element = iter.next();\r
- if ( removeValue( element ) ) {\r
- changed = true;\r
- }\r
- }\r
- return changed;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public boolean removeAll( int[] array ) {\r
- boolean changed = false;\r
- for ( int i = array.length; i-- > 0; ) {\r
- if ( removeValue(array[i]) ) {\r
- changed = true;\r
- }\r
- }\r
- return changed;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public void transformValues( TIntFunction function ) {\r
- for ( int i = _pos; i-- > 0; ) {\r
- _data[ i ] = function.execute( _data[ i ] );\r
- }\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public void reverse() {\r
- reverse( 0, _pos );\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public void reverse( int from, int to ) {\r
- if ( from == to ) {\r
- return; // nothing to do\r
- }\r
- if ( from > to ) {\r
- throw new IllegalArgumentException( "from cannot be greater than to" );\r
- }\r
- for ( int i = from, j = to - 1; i < j; i++, j-- ) {\r
- swap( i, j );\r
- }\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public void shuffle( Random rand ) {\r
- for ( int i = _pos; i-- > 1; ) {\r
- swap( i, rand.nextInt( i ) );\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Swap the values at offsets <tt>i</tt> and <tt>j</tt>.\r
- *\r
- * @param i an offset into the data array\r
- * @param j an offset into the data array\r
- */\r
- private void swap( int i, int j ) {\r
- int tmp = _data[ i ];\r
- _data[ i ] = _data[ j ];\r
- _data[ j ] = tmp;\r
- }\r
-\r
-\r
- // copying\r
-\r
-// /** {@inheritDoc} */\r
-// public TIntList subList( int begin, int end ) {\r
-// if ( end < begin ) {\r
-// throw new IllegalArgumentException( "end index " + end +\r
-// " greater than begin index " + begin );\r
-// }\r
-// if ( begin < 0 ) {\r
-// throw new IndexOutOfBoundsException( "begin index can not be < 0" );\r
-// }\r
-// if ( end > _data.length ) {\r
-// throw new IndexOutOfBoundsException( "end index < " + _data.length );\r
-// }\r
-// TIntArrayListInternal list = new TIntArrayListInternal( end - begin );\r
-// for ( int i = begin; i < end; i++ ) {\r
-// list.add( _data[ i ] );\r
-// }\r
-// return list;\r
-// }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public int[] toArrayInternal() {\r
- return toArray( 0, _pos );\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public int[] toArray( int offset, int len ) {\r
- int[] rv = new int[ len ];\r
- toArray( rv, offset, len );\r
- return rv;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public int[] toArray( int[] dest ) {\r
- int len = dest.length;\r
- if ( dest.length > _pos ) {\r
- len = _pos;\r
- dest[len] = no_entry_value;\r
- }\r
- toArray( dest, 0, len );\r
- return dest;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public int[] toArray( int[] dest, int offset, int len ) {\r
- if ( len == 0 ) {\r
- return dest; // nothing to copy\r
- }\r
- if ( offset < 0 || offset >= _pos ) {\r
- throw new ArrayIndexOutOfBoundsException( offset );\r
- }\r
- System.arraycopy( _data, offset, dest, 0, len );\r
- return dest;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public int[] toArray( int[] dest, int source_pos, int dest_pos, int len ) {\r
- if ( len == 0 ) {\r
- return dest; // nothing to copy\r
- }\r
- if ( source_pos < 0 || source_pos >= _pos ) {\r
- throw new ArrayIndexOutOfBoundsException( source_pos );\r
- }\r
- System.arraycopy( _data, source_pos, dest, dest_pos, len );\r
- return dest;\r
- }\r
-\r
-\r
- // comparing\r
-\r
- /** {@inheritDoc} */\r
- @Override\r
- public boolean equals( Object other ) {\r
- if ( other == this ) {\r
- return true;\r
- }\r
- else if ( other instanceof TIntArrayListInternal ) {\r
- TIntArrayListInternal that = ( TIntArrayListInternal )other;\r
- if ( that.sizeInternal() != this.sizeInternal() ) return false;\r
- else {\r
- for ( int i = _pos; i-- > 0; ) {\r
- if ( this._data[ i ] != that._data[ i ] ) {\r
- return false;\r
- }\r
- }\r
- return true;\r
- }\r
- }\r
- else return false;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- @Override\r
- public int hashCode() {\r
- int h = 0;\r
- for ( int i = _pos; i-- > 0; ) {\r
- h += HashFunctions.hash( _data[ i ] );\r
- }\r
- return h;\r
- }\r
-\r
-\r
- // procedures\r
-\r
- /** {@inheritDoc} */\r
- public boolean forEach( TIntProcedure procedure ) {\r
- for ( int i = 0; i < _pos; i++ ) {\r
- if ( !procedure.execute( _data[ i ] ) ) {\r
- return false;\r
- }\r
- }\r
- return true;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public boolean forEachDescending( TIntProcedure procedure ) {\r
- for ( int i = _pos; i-- > 0; ) {\r
- if ( !procedure.execute( _data[ i ] ) ) {\r
- return false;\r
- }\r
- }\r
- return true;\r
- }\r
-\r
-\r
- // sorting\r
-\r
- /** {@inheritDoc} */\r
- public void sort() {\r
- Arrays.sort( _data, 0, _pos );\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public void sort( int fromIndex, int toIndex ) {\r
- Arrays.sort( _data, fromIndex, toIndex );\r
- }\r
-\r
-\r
- // filling\r
-\r
- /** {@inheritDoc} */\r
- public void fill( int val ) {\r
- Arrays.fill( _data, 0, _pos, val );\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public void fill( int fromIndex, int toIndex, int val ) {\r
- if ( toIndex > _pos ) {\r
- ensureCapacity( toIndex );\r
- _pos = toIndex;\r
- }\r
- Arrays.fill( _data, fromIndex, toIndex, val );\r
- }\r
-\r
-\r
- // searching\r
-\r
- /** {@inheritDoc} */\r
- public int binarySearch( int value ) {\r
- return binarySearch( value, 0, _pos );\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public int binarySearch(int value, int fromIndex, int toIndex) {\r
- if ( fromIndex < 0 ) {\r
- throw new ArrayIndexOutOfBoundsException( fromIndex );\r
- }\r
- if ( toIndex > _pos ) {\r
- throw new ArrayIndexOutOfBoundsException( toIndex );\r
- }\r
-\r
- int low = fromIndex;\r
- int high = toIndex - 1;\r
-\r
- while ( low <= high ) {\r
- int mid = ( low + high ) >>> 1;\r
- int midVal = _data[ mid ];\r
-\r
- if ( midVal < value ) {\r
- low = mid + 1;\r
- }\r
- else if ( midVal > value ) {\r
- high = mid - 1;\r
- }\r
- else {\r
- return mid; // value found\r
- }\r
- }\r
- return -( low + 1 ); // value not found.\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public int indexOf( int value ) {\r
- return indexOf( 0, value );\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public int indexOf( int offset, int value ) {\r
- for ( int i = offset; i < _pos; i++ ) {\r
- if ( _data[ i ] == value ) {\r
- return i;\r
- }\r
- }\r
- return -1;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public int lastIndexOf( int value ) {\r
- return lastIndexOf( _pos, value );\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public int lastIndexOf( int offset, int value ) {\r
- for ( int i = offset; i-- > 0; ) {\r
- if ( _data[ i ] == value ) {\r
- return i;\r
- }\r
- }\r
- return -1;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public boolean contains( int value ) {\r
- return lastIndexOf( value ) >= 0;\r
- }\r
-\r
-\r
-// /** {@inheritDoc} */\r
-// public TIntList grep( TIntProcedure condition ) {\r
-// TIntArrayListInternal list = new TIntArrayListInternal();\r
-// for ( int i = 0; i < _pos; i++ ) {\r
-// if ( condition.execute( _data[ i ] ) ) {\r
-// list.add( _data[ i ] );\r
-// }\r
-// }\r
-// return list;\r
-// }\r
-\r
-\r
-// /** {@inheritDoc} */\r
-// public TIntList inverseGrep( TIntProcedure condition ) {\r
-// TIntArrayListInternal list = new TIntArrayListInternal();\r
-// for ( int i = 0; i < _pos; i++ ) {\r
-// if ( !condition.execute( _data[ i ] ) ) {\r
-// list.add( _data[ i ] );\r
-// }\r
-// }\r
-// return list;\r
-// }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public int max() {\r
- if ( sizeInternal() == 0 ) {\r
- throw new IllegalStateException("cannot find maximum of an empty list");\r
- }\r
- int max = Integer.MIN_VALUE;\r
- for ( int i = 0; i < _pos; i++ ) {\r
- if ( _data[ i ] > max ) {\r
- max = _data[ i ];\r
- }\r
- }\r
- return max;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public int min() {\r
- if ( sizeInternal() == 0 ) {\r
- throw new IllegalStateException( "cannot find minimum of an empty list" );\r
- }\r
- int min = Integer.MAX_VALUE;\r
- for ( int i = 0; i < _pos; i++ ) {\r
- if ( _data[i] < min ) {\r
- min = _data[i];\r
- }\r
- }\r
- return min;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public int sum() {\r
- int sum = 0;\r
- for ( int i = 0; i < _pos; i++ ) {\r
- sum += _data[ i ];\r
- }\r
- return sum;\r
- }\r
-\r
-\r
- // stringification\r
-\r
- /** {@inheritDoc} */\r
- @Override\r
- public String toString() {\r
- final StringBuilder buf = new StringBuilder( "{" );\r
- for ( int i = 0, end = _pos - 1; i < end; i++ ) {\r
- buf.append( _data[ i ] );\r
- buf.append( ", " );\r
- }\r
- if ( sizeInternal() > 0 ) {\r
- buf.append( _data[ _pos - 1 ] );\r
- }\r
- buf.append( "}" );\r
- return buf.toString();\r
- }\r
-\r
-\r
- /** TIntArrayList iterator */\r
- class TIntArrayIterator implements TIntIterator {\r
-\r
- /** Index of element to be returned by subsequent call to next. */\r
- private int cursor = 0;\r
-\r
- /**\r
- * Index of element returned by most recent call to next or\r
- * previous. Reset to -1 if this element is deleted by a call\r
- * to remove.\r
- */\r
- int lastRet = -1;\r
-\r
-\r
- TIntArrayIterator( int index ) {\r
- cursor = index;\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public boolean hasNext() {\r
- return cursor < sizeInternal();\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public int next() {\r
- try {\r
- int next = getAt( cursor );\r
- lastRet = cursor++;\r
- return next;\r
- } catch ( IndexOutOfBoundsException e ) {\r
- throw new NoSuchElementException();\r
- }\r
- }\r
-\r
-\r
- /** {@inheritDoc} */\r
- public void remove() {\r
- if ( lastRet == -1 )\r
- throw new IllegalStateException();\r
-\r
- try {\r
- TIntArrayListInternal.this.remove( lastRet, 1);\r
- if ( lastRet < cursor )\r
- cursor--;\r
- lastRet = -1;\r
- } catch ( IndexOutOfBoundsException e ) {\r
- throw new ConcurrentModificationException();\r
- }\r
- }\r
- }\r
-\r
-\r
- public void writeExternal( ObjectOutput out ) throws IOException {\r
- // VERSION\r
- out.writeByte( 0 );\r
-\r
- // POSITION\r
- out.writeInt( _pos );\r
-\r
- // NO_ENTRY_VALUE\r
- out.writeInt( no_entry_value );\r
-\r
- // ENTRIES\r
- int len = _data.length;\r
- out.writeInt( len );\r
- for( int i = 0; i < len; i++ ) {\r
- out.writeInt( _data[ i ] );\r
- }\r
- }\r
-\r
-\r
- public void readExternal( ObjectInput in )\r
- throws IOException, ClassNotFoundException {\r
-\r
- // VERSION\r
- in.readByte();\r
-\r
- // POSITION\r
- _pos = in.readInt();\r
-\r
- // NO_ENTRY_VALUE\r
- no_entry_value = in.readInt();\r
-\r
- // ENTRIES\r
- int len = in.readInt();\r
- _data = new int[ len ];\r
- for( int i = 0; i < len; i++ ) {\r
- _data[ i ] = in.readInt();\r
- }\r
- }\r
-} // TIntArrayList\r
+///////////////////////////////////////////////////////////////////////////////
+// Copyright (c) 2001, Eric D. Friedman All Rights Reserved.
+// Copyright (c) 2009, Rob Eden All Rights Reserved.
+// Copyright (c) 2009, Jeff Randall All Rights Reserved.
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library 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 Lesser 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.
+///////////////////////////////////////////////////////////////////////////////
+
+package org.simantics.db.impl.graph;
+
+import gnu.trove.TIntCollection;
+import gnu.trove.function.TIntFunction;
+import gnu.trove.impl.Constants;
+import gnu.trove.impl.HashFunctions;
+import gnu.trove.iterator.TIntIterator;
+import gnu.trove.procedure.TIntProcedure;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.ConcurrentModificationException;
+import java.util.NoSuchElementException;
+import java.util.Random;
+
+
+//////////////////////////////////////////////////
+// THIS IS A GENERATED CLASS. DO NOT HAND EDIT! //
+//////////////////////////////////////////////////
+
+
+/**
+ * A resizable, array-backed list of int primitives.
+ */
+public class TIntArrayListInternal {
+
+ /** the data of the list */
+ protected int[] _data;
+
+ /** the index after the last entry in the list */
+ protected int _pos;
+
+ /** the default capacity for new lists */
+ protected static final int DEFAULT_CAPACITY = Constants.DEFAULT_CAPACITY;
+
+ /** the int value that represents null */
+ protected int no_entry_value;
+
+
+ /**
+ * Creates a new <code>TIntArrayList</code> instance with the
+ * default capacity.
+ */
+ @SuppressWarnings({"RedundantCast"})
+ public TIntArrayListInternal() {
+ this( DEFAULT_CAPACITY, ( int ) 0 );
+ }
+
+
+ /**
+ * Creates a new <code>TIntArrayList</code> instance with the
+ * specified capacity.
+ *
+ * @param capacity an <code>int</code> value
+ */
+ @SuppressWarnings({"RedundantCast"})
+ public TIntArrayListInternal( int capacity ) {
+ this( capacity, ( int ) 0 );
+ }
+
+
+ /**
+ * Creates a new <code>TIntArrayList</code> instance with the
+ * specified capacity.
+ *
+ * @param capacity an <code>int</code> value
+ * @param no_entry_value an <code>int</code> value that represents null.
+ */
+ public TIntArrayListInternal( int capacity, int no_entry_value ) {
+ _data = new int[ capacity ];
+ _pos = 0;
+ this.no_entry_value = no_entry_value;
+ }
+
+ /**
+ * Creates a new <code>TIntArrayList</code> instance that contains
+ * a copy of the collection passed to us.
+ *
+ * @param collection the collection to copy
+ */
+ public TIntArrayListInternal ( TIntCollection collection ) {
+ this( collection.size() );
+ addAll( collection );
+ }
+
+
+ /**
+ * Creates a new <code>TIntArrayList</code> instance whose
+ * capacity is the length of <tt>values</tt> array and whose
+ * initial contents are the specified values.
+ * <p>
+ * A defensive copy of the given values is held by the new instance.
+ *
+ * @param values an <code>int[]</code> value
+ */
+ public TIntArrayListInternal( int[] values ) {
+ this( values.length );
+ add( values );
+ }
+
+ protected TIntArrayListInternal(int[] values, int no_entry_value, boolean wrap) {
+ if (!wrap)
+ throw new IllegalStateException("Wrong call");
+
+ if (values == null)
+ throw new IllegalArgumentException("values can not be null");
+
+ _data = values;
+ _pos = values.length;
+ this.no_entry_value = no_entry_value;
+ }
+
+ /**
+ * Returns a primitive List implementation that wraps around the given primitive array.
+ * <p/>
+ * NOTE: mutating operation are allowed as long as the List does not grow. In that case
+ * an IllegalStateException will be thrown
+ *
+ * @param values
+ * @return
+ */
+ public static TIntArrayListInternal wrap(int[] values) {
+ return wrap(values, ( int ) 0);
+ }
+
+ /**
+ * Returns a primitive List implementation that wraps around the given primitive array.
+ * <p/>
+ * NOTE: mutating operation are allowed as long as the List does not grow. In that case
+ * an IllegalStateException will be thrown
+ *
+ * @param values
+ * @param no_entry_value
+ * @return
+ */
+ public static TIntArrayListInternal wrap(int[] values, int no_entry_value) {
+ return new TIntArrayListInternal(values, no_entry_value, true) {
+ /**
+ * Growing the wrapped external array is not allow
+ */
+ @Override
+ public void ensureCapacity(int capacity) {
+ if (capacity > _data.length)
+ throw new IllegalStateException("Can not grow ArrayList wrapped external array");
+ }
+ };
+ }
+
+ /** {@inheritDoc} */
+ public int getNoEntryValue() {
+ return no_entry_value;
+ }
+
+
+ // sizing
+
+ /**
+ * Grow the internal array as needed to accommodate the specified number of elements.
+ * The size of the array bytes on each resize unless capacity requires more than twice
+ * the current capacity.
+ */
+ public void ensureCapacity( int capacity ) {
+ if ( capacity > _data.length ) {
+ int newCap = Math.max( _data.length << 1, capacity );
+ int[] tmp = new int[ newCap ];
+ System.arraycopy( _data, 0, tmp, 0, _data.length );
+ _data = tmp;
+ }
+ }
+
+
+ /** {@inheritDoc} */
+ public int sizeInternal() {
+ return _pos;
+ }
+
+
+ /** {@inheritDoc} */
+ public boolean isEmpty() {
+ return _pos == 0;
+ }
+
+
+ /**
+ * Sheds any excess capacity above and beyond the current size of the list.
+ */
+ public void trimToSize() {
+ if ( _data.length > sizeInternal() ) {
+ int[] tmp = new int[ sizeInternal() ];
+ toArray( tmp, 0, tmp.length );
+ _data = tmp;
+ }
+ }
+
+
+ // modifying
+
+ /** {@inheritDoc} */
+ public boolean add( int val ) {
+ ensureCapacity( _pos + 1 );
+ _data[ _pos++ ] = val;
+ return true;
+ }
+
+
+ /** {@inheritDoc} */
+ public void add( int[] vals ) {
+ add( vals, 0, vals.length );
+ }
+
+
+ /** {@inheritDoc} */
+ public void add( int[] vals, int offset, int length ) {
+ ensureCapacity( _pos + length );
+ System.arraycopy( vals, offset, _data, _pos, length );
+ _pos += length;
+ }
+
+
+ /** {@inheritDoc} */
+ public void insert( int offset, int value ) {
+ if ( offset == _pos ) {
+ add( value );
+ return;
+ }
+ ensureCapacity( _pos + 1 );
+ // shift right
+ System.arraycopy( _data, offset, _data, offset + 1, _pos - offset );
+ // insert
+ _data[ offset ] = value;
+ _pos++;
+ }
+
+
+ /** {@inheritDoc} */
+ public void insert( int offset, int[] values ) {
+ insert( offset, values, 0, values.length );
+ }
+
+
+ /** {@inheritDoc} */
+ public void insert( int offset, int[] values, int valOffset, int len ) {
+ if ( offset == _pos ) {
+ add( values, valOffset, len );
+ return;
+ }
+
+ ensureCapacity( _pos + len );
+ // shift right
+ System.arraycopy( _data, offset, _data, offset + len, _pos - offset );
+ // insert
+ System.arraycopy( values, valOffset, _data, offset, len );
+ _pos += len;
+ }
+
+
+ /** {@inheritDoc} */
+ public int getAt( int offset ) {
+ if ( offset >= _pos ) {
+ throw new ArrayIndexOutOfBoundsException( offset );
+ }
+ return _data[ offset ];
+ }
+
+
+ /**
+ * Returns the value at the specified offset without doing any bounds checking.
+ */
+ public int getQuick( int offset ) {
+ return _data[ offset ];
+ }
+
+
+ /** {@inheritDoc} */
+ public int set( int offset, int val ) {
+ if ( offset >= _pos ) {
+ throw new ArrayIndexOutOfBoundsException( offset );
+ }
+
+ int prev_val = _data[ offset ];
+ _data[ offset ] = val;
+ return prev_val;
+ }
+
+
+ /** {@inheritDoc} */
+ public int replace( int offset, int val ) {
+ if ( offset >= _pos ) {
+ throw new ArrayIndexOutOfBoundsException( offset );
+ }
+ int old = _data[ offset ];
+ _data[ offset ] = val;
+ return old;
+ }
+
+
+ /** {@inheritDoc} */
+ public void set( int offset, int[] values ) {
+ set( offset, values, 0, values.length );
+ }
+
+
+ /** {@inheritDoc} */
+ public void set( int offset, int[] values, int valOffset, int length ) {
+ if ( offset < 0 || offset + length > _pos ) {
+ throw new ArrayIndexOutOfBoundsException( offset );
+ }
+ System.arraycopy( values, valOffset, _data, offset, length );
+ }
+
+
+ /**
+ * Sets the value at the specified offset without doing any bounds checking.
+ */
+ public void setQuick( int offset, int val ) {
+ _data[ offset ] = val;
+ }
+
+
+ /** {@inheritDoc} */
+ public void clear() {
+ clear( DEFAULT_CAPACITY );
+ }
+
+
+ /**
+ * Flushes the internal state of the list, setting the capacity of the empty list to
+ * <tt>capacity</tt>.
+ */
+ public void clear( int capacity ) {
+ _data = new int[ capacity ];
+ _pos = 0;
+ }
+
+
+ /**
+ * Sets the size of the list to 0, but does not change its capacity. This method can
+ * be used as an alternative to the {@link #clear()} method if you want to recycle a
+ * list without allocating new backing arrays.
+ */
+ public void reset() {
+ _pos = 0;
+ Arrays.fill( _data, no_entry_value );
+ }
+
+
+ /**
+ * Sets the size of the list to 0, but does not change its capacity. This method can
+ * be used as an alternative to the {@link #clear()} method if you want to recycle a
+ * list without allocating new backing arrays. This method differs from
+ * {@link #reset()} in that it does not clear the old values in the backing array.
+ * Thus, it is possible for getQuick to return stale data if this method is used and
+ * the caller is careless about bounds checking.
+ */
+ public void resetQuick() {
+ _pos = 0;
+ }
+
+
+ /** {@inheritDoc} */
+ public boolean removeValue( int value ) {
+ for ( int index = 0; index < _pos; index++ ) {
+ if ( value == _data[index] ) {
+ remove( index, 1 );
+ return true;
+ }
+ }
+ return false;
+ }
+
+
+ /** {@inheritDoc} */
+ public int removeAt( int offset ) {
+ int old = getAt( offset );
+ remove( offset, 1 );
+ return old;
+ }
+
+
+ /** {@inheritDoc} */
+ public void remove( int offset, int length ) {
+ if ( length == 0 ) return;
+ if ( offset < 0 || offset >= _pos ) {
+ throw new ArrayIndexOutOfBoundsException(offset);
+ }
+
+ if ( offset == 0 ) {
+ // data at the front
+ System.arraycopy( _data, length, _data, 0, _pos - length );
+ }
+ else if ( _pos - length == offset ) {
+ // no copy to make, decrementing pos "deletes" values at
+ // the end
+ }
+ else {
+ // data in the middle
+ System.arraycopy( _data, offset + length, _data, offset,
+ _pos - ( offset + length ) );
+ }
+ _pos -= length;
+ // no need to clear old values beyond _pos, because this is a
+ // primitive collection and 0 takes as much room as any other
+ // value
+ }
+
+
+ /** {@inheritDoc} */
+ public TIntIterator iteratorInternal() {
+ return new TIntArrayIterator( 0 );
+ }
+
+
+ /** {@inheritDoc} */
+ public boolean containsAll( Collection<?> collection ) {
+ for ( Object element : collection ) {
+ if ( element instanceof Integer ) {
+ int c = ( ( Integer ) element ).intValue();
+ if ( ! contains( c ) ) {
+ return false;
+ }
+ } else {
+ return false;
+ }
+
+ }
+ return true;
+ }
+
+
+ /** {@inheritDoc} */
+ public boolean containsAll( TIntCollection collection ) {
+ if ( this == collection ) {
+ return true;
+ }
+ TIntIterator iter = collection.iterator();
+ while ( iter.hasNext() ) {
+ int element = iter.next();
+ if ( ! contains( element ) ) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+
+ /** {@inheritDoc} */
+ public boolean containsAll( int[] array ) {
+ for ( int i = array.length; i-- > 0; ) {
+ if ( ! contains( array[i] ) ) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+
+// /** {@inheritDoc} */
+// public boolean addAll( Collection<? extends Integer> collection ) {
+// boolean changed = false;
+// for ( Integer element : collection ) {
+// int e = element.intValue();
+// if ( add( e ) ) {
+// changed = true;
+// }
+// }
+// return changed;
+// }
+
+
+ /** {@inheritDoc} */
+ public boolean addAll( TIntCollection collection ) {
+ boolean changed = false;
+ TIntIterator iter = collection.iterator();
+ while ( iter.hasNext() ) {
+ int element = iter.next();
+ if ( add( element ) ) {
+ changed = true;
+ }
+ }
+ return changed;
+ }
+
+
+ /** {@inheritDoc} */
+ public boolean addAll( int[] array ) {
+ boolean changed = false;
+ for ( int element : array ) {
+ if ( add( element ) ) {
+ changed = true;
+ }
+ }
+ return changed;
+ }
+
+
+ /** {@inheritDoc} */
+ @SuppressWarnings({"SuspiciousMethodCalls"})
+ public boolean retainAll( Collection<?> collection ) {
+ boolean modified = false;
+ TIntIterator iter = iteratorInternal();
+ while ( iter.hasNext() ) {
+ if ( ! collection.contains( Integer.valueOf ( iter.next() ) ) ) {
+ iter.remove();
+ modified = true;
+ }
+ }
+ return modified;
+ }
+
+
+ /** {@inheritDoc} */
+ public boolean retainAll( TIntCollection collection ) {
+ if ( this == collection ) {
+ return false;
+ }
+ boolean modified = false;
+ TIntIterator iter = iteratorInternal();
+ while ( iter.hasNext() ) {
+ if ( ! collection.contains( iter.next() ) ) {
+ iter.remove();
+ modified = true;
+ }
+ }
+ return modified;
+ }
+
+
+ /** {@inheritDoc} */
+ public boolean retainAll( int[] array ) {
+ boolean changed = false;
+ Arrays.sort( array );
+ int[] data = _data;
+
+ for ( int i = _pos; i-- > 0; ) {
+ if ( Arrays.binarySearch( array, data[i] ) < 0 ) {
+ remove( i, 1 );
+ changed = true;
+ }
+ }
+ return changed;
+ }
+
+
+ /** {@inheritDoc} */
+ public boolean removeAll( Collection<?> collection ) {
+ boolean changed = false;
+ for ( Object element : collection ) {
+ if ( element instanceof Integer ) {
+ int c = ( ( Integer ) element ).intValue();
+ if ( removeValue( c ) ) {
+ changed = true;
+ }
+ }
+ }
+ return changed;
+ }
+
+
+ /** {@inheritDoc} */
+ public boolean removeAll( TIntCollection collection ) {
+ if ( collection == this ) {
+ clear();
+ return true;
+ }
+ boolean changed = false;
+ TIntIterator iter = collection.iterator();
+ while ( iter.hasNext() ) {
+ int element = iter.next();
+ if ( removeValue( element ) ) {
+ changed = true;
+ }
+ }
+ return changed;
+ }
+
+
+ /** {@inheritDoc} */
+ public boolean removeAll( int[] array ) {
+ boolean changed = false;
+ for ( int i = array.length; i-- > 0; ) {
+ if ( removeValue(array[i]) ) {
+ changed = true;
+ }
+ }
+ return changed;
+ }
+
+
+ /** {@inheritDoc} */
+ public void transformValues( TIntFunction function ) {
+ for ( int i = _pos; i-- > 0; ) {
+ _data[ i ] = function.execute( _data[ i ] );
+ }
+ }
+
+
+ /** {@inheritDoc} */
+ public void reverse() {
+ reverse( 0, _pos );
+ }
+
+
+ /** {@inheritDoc} */
+ public void reverse( int from, int to ) {
+ if ( from == to ) {
+ return; // nothing to do
+ }
+ if ( from > to ) {
+ throw new IllegalArgumentException( "from cannot be greater than to" );
+ }
+ for ( int i = from, j = to - 1; i < j; i++, j-- ) {
+ swap( i, j );
+ }
+ }
+
+
+ /** {@inheritDoc} */
+ public void shuffle( Random rand ) {
+ for ( int i = _pos; i-- > 1; ) {
+ swap( i, rand.nextInt( i ) );
+ }
+ }
+
+
+ /**
+ * Swap the values at offsets <tt>i</tt> and <tt>j</tt>.
+ *
+ * @param i an offset into the data array
+ * @param j an offset into the data array
+ */
+ private void swap( int i, int j ) {
+ int tmp = _data[ i ];
+ _data[ i ] = _data[ j ];
+ _data[ j ] = tmp;
+ }
+
+
+ // copying
+
+// /** {@inheritDoc} */
+// public TIntList subList( int begin, int end ) {
+// if ( end < begin ) {
+// throw new IllegalArgumentException( "end index " + end +
+// " greater than begin index " + begin );
+// }
+// if ( begin < 0 ) {
+// throw new IndexOutOfBoundsException( "begin index can not be < 0" );
+// }
+// if ( end > _data.length ) {
+// throw new IndexOutOfBoundsException( "end index < " + _data.length );
+// }
+// TIntArrayListInternal list = new TIntArrayListInternal( end - begin );
+// for ( int i = begin; i < end; i++ ) {
+// list.add( _data[ i ] );
+// }
+// return list;
+// }
+
+
+ /** {@inheritDoc} */
+ public int[] toArrayInternal() {
+ return toArray( 0, _pos );
+ }
+
+
+ /** {@inheritDoc} */
+ public int[] toArray( int offset, int len ) {
+ int[] rv = new int[ len ];
+ toArray( rv, offset, len );
+ return rv;
+ }
+
+
+ /** {@inheritDoc} */
+ public int[] toArray( int[] dest ) {
+ int len = dest.length;
+ if ( dest.length > _pos ) {
+ len = _pos;
+ dest[len] = no_entry_value;
+ }
+ toArray( dest, 0, len );
+ return dest;
+ }
+
+
+ /** {@inheritDoc} */
+ public int[] toArray( int[] dest, int offset, int len ) {
+ if ( len == 0 ) {
+ return dest; // nothing to copy
+ }
+ if ( offset < 0 || offset >= _pos ) {
+ throw new ArrayIndexOutOfBoundsException( offset );
+ }
+ System.arraycopy( _data, offset, dest, 0, len );
+ return dest;
+ }
+
+
+ /** {@inheritDoc} */
+ public int[] toArray( int[] dest, int source_pos, int dest_pos, int len ) {
+ if ( len == 0 ) {
+ return dest; // nothing to copy
+ }
+ if ( source_pos < 0 || source_pos >= _pos ) {
+ throw new ArrayIndexOutOfBoundsException( source_pos );
+ }
+ System.arraycopy( _data, source_pos, dest, dest_pos, len );
+ return dest;
+ }
+
+
+ // comparing
+
+ /** {@inheritDoc} */
+ @Override
+ public boolean equals( Object other ) {
+ if ( other == this ) {
+ return true;
+ }
+ else if ( other instanceof TIntArrayListInternal ) {
+ TIntArrayListInternal that = ( TIntArrayListInternal )other;
+ if ( that.sizeInternal() != this.sizeInternal() ) return false;
+ else {
+ for ( int i = _pos; i-- > 0; ) {
+ if ( this._data[ i ] != that._data[ i ] ) {
+ return false;
+ }
+ }
+ return true;
+ }
+ }
+ else return false;
+ }
+
+
+ /** {@inheritDoc} */
+ @Override
+ public int hashCode() {
+ int h = 0;
+ for ( int i = _pos; i-- > 0; ) {
+ h += HashFunctions.hash( _data[ i ] );
+ }
+ return h;
+ }
+
+
+ // procedures
+
+ /** {@inheritDoc} */
+ public boolean forEach( TIntProcedure procedure ) {
+ for ( int i = 0; i < _pos; i++ ) {
+ if ( !procedure.execute( _data[ i ] ) ) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+
+ /** {@inheritDoc} */
+ public boolean forEachDescending( TIntProcedure procedure ) {
+ for ( int i = _pos; i-- > 0; ) {
+ if ( !procedure.execute( _data[ i ] ) ) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+
+ // sorting
+
+ /** {@inheritDoc} */
+ public void sort() {
+ Arrays.sort( _data, 0, _pos );
+ }
+
+
+ /** {@inheritDoc} */
+ public void sort( int fromIndex, int toIndex ) {
+ Arrays.sort( _data, fromIndex, toIndex );
+ }
+
+
+ // filling
+
+ /** {@inheritDoc} */
+ public void fill( int val ) {
+ Arrays.fill( _data, 0, _pos, val );
+ }
+
+
+ /** {@inheritDoc} */
+ public void fill( int fromIndex, int toIndex, int val ) {
+ if ( toIndex > _pos ) {
+ ensureCapacity( toIndex );
+ _pos = toIndex;
+ }
+ Arrays.fill( _data, fromIndex, toIndex, val );
+ }
+
+
+ // searching
+
+ /** {@inheritDoc} */
+ public int binarySearch( int value ) {
+ return binarySearch( value, 0, _pos );
+ }
+
+
+ /** {@inheritDoc} */
+ public int binarySearch(int value, int fromIndex, int toIndex) {
+ if ( fromIndex < 0 ) {
+ throw new ArrayIndexOutOfBoundsException( fromIndex );
+ }
+ if ( toIndex > _pos ) {
+ throw new ArrayIndexOutOfBoundsException( toIndex );
+ }
+
+ int low = fromIndex;
+ int high = toIndex - 1;
+
+ while ( low <= high ) {
+ int mid = ( low + high ) >>> 1;
+ int midVal = _data[ mid ];
+
+ if ( midVal < value ) {
+ low = mid + 1;
+ }
+ else if ( midVal > value ) {
+ high = mid - 1;
+ }
+ else {
+ return mid; // value found
+ }
+ }
+ return -( low + 1 ); // value not found.
+ }
+
+
+ /** {@inheritDoc} */
+ public int indexOf( int value ) {
+ return indexOf( 0, value );
+ }
+
+
+ /** {@inheritDoc} */
+ public int indexOf( int offset, int value ) {
+ for ( int i = offset; i < _pos; i++ ) {
+ if ( _data[ i ] == value ) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+
+ /** {@inheritDoc} */
+ public int lastIndexOf( int value ) {
+ return lastIndexOf( _pos, value );
+ }
+
+
+ /** {@inheritDoc} */
+ public int lastIndexOf( int offset, int value ) {
+ for ( int i = offset; i-- > 0; ) {
+ if ( _data[ i ] == value ) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+
+ /** {@inheritDoc} */
+ public boolean contains( int value ) {
+ return lastIndexOf( value ) >= 0;
+ }
+
+
+// /** {@inheritDoc} */
+// public TIntList grep( TIntProcedure condition ) {
+// TIntArrayListInternal list = new TIntArrayListInternal();
+// for ( int i = 0; i < _pos; i++ ) {
+// if ( condition.execute( _data[ i ] ) ) {
+// list.add( _data[ i ] );
+// }
+// }
+// return list;
+// }
+
+
+// /** {@inheritDoc} */
+// public TIntList inverseGrep( TIntProcedure condition ) {
+// TIntArrayListInternal list = new TIntArrayListInternal();
+// for ( int i = 0; i < _pos; i++ ) {
+// if ( !condition.execute( _data[ i ] ) ) {
+// list.add( _data[ i ] );
+// }
+// }
+// return list;
+// }
+
+
+ /** {@inheritDoc} */
+ public int max() {
+ if ( sizeInternal() == 0 ) {
+ throw new IllegalStateException("cannot find maximum of an empty list");
+ }
+ int max = Integer.MIN_VALUE;
+ for ( int i = 0; i < _pos; i++ ) {
+ if ( _data[ i ] > max ) {
+ max = _data[ i ];
+ }
+ }
+ return max;
+ }
+
+
+ /** {@inheritDoc} */
+ public int min() {
+ if ( sizeInternal() == 0 ) {
+ throw new IllegalStateException( "cannot find minimum of an empty list" );
+ }
+ int min = Integer.MAX_VALUE;
+ for ( int i = 0; i < _pos; i++ ) {
+ if ( _data[i] < min ) {
+ min = _data[i];
+ }
+ }
+ return min;
+ }
+
+
+ /** {@inheritDoc} */
+ public int sum() {
+ int sum = 0;
+ for ( int i = 0; i < _pos; i++ ) {
+ sum += _data[ i ];
+ }
+ return sum;
+ }
+
+
+ // stringification
+
+ /** {@inheritDoc} */
+ @Override
+ public String toString() {
+ final StringBuilder buf = new StringBuilder( "{" );
+ for ( int i = 0, end = _pos - 1; i < end; i++ ) {
+ buf.append( _data[ i ] );
+ buf.append( ", " );
+ }
+ if ( sizeInternal() > 0 ) {
+ buf.append( _data[ _pos - 1 ] );
+ }
+ buf.append( "}" );
+ return buf.toString();
+ }
+
+
+ /** TIntArrayList iterator */
+ class TIntArrayIterator implements TIntIterator {
+
+ /** Index of element to be returned by subsequent call to next. */
+ private int cursor = 0;
+
+ /**
+ * Index of element returned by most recent call to next or
+ * previous. Reset to -1 if this element is deleted by a call
+ * to remove.
+ */
+ int lastRet = -1;
+
+
+ TIntArrayIterator( int index ) {
+ cursor = index;
+ }
+
+
+ /** {@inheritDoc} */
+ public boolean hasNext() {
+ return cursor < sizeInternal();
+ }
+
+
+ /** {@inheritDoc} */
+ public int next() {
+ try {
+ int next = getAt( cursor );
+ lastRet = cursor++;
+ return next;
+ } catch ( IndexOutOfBoundsException e ) {
+ throw new NoSuchElementException();
+ }
+ }
+
+
+ /** {@inheritDoc} */
+ public void remove() {
+ if ( lastRet == -1 )
+ throw new IllegalStateException();
+
+ try {
+ TIntArrayListInternal.this.remove( lastRet, 1);
+ if ( lastRet < cursor )
+ cursor--;
+ lastRet = -1;
+ } catch ( IndexOutOfBoundsException e ) {
+ throw new ConcurrentModificationException();
+ }
+ }
+ }
+
+
+ public void writeExternal( ObjectOutput out ) throws IOException {
+ // VERSION
+ out.writeByte( 0 );
+
+ // POSITION
+ out.writeInt( _pos );
+
+ // NO_ENTRY_VALUE
+ out.writeInt( no_entry_value );
+
+ // ENTRIES
+ int len = _data.length;
+ out.writeInt( len );
+ for( int i = 0; i < len; i++ ) {
+ out.writeInt( _data[ i ] );
+ }
+ }
+
+
+ public void readExternal( ObjectInput in )
+ throws IOException, ClassNotFoundException {
+
+ // VERSION
+ in.readByte();
+
+ // POSITION
+ _pos = in.readInt();
+
+ // NO_ENTRY_VALUE
+ no_entry_value = in.readInt();
+
+ // ENTRIES
+ int len = in.readInt();
+ _data = new int[ len ];
+ for( int i = 0; i < len; i++ ) {
+ _data[ i ] = in.readInt();
+ }
+ }
+} // TIntArrayList