1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2001, Eric D. Friedman All Rights Reserved.
3 // Copyright (c) 2009, Rob Eden All Rights Reserved.
4 // Copyright (c) 2009, Jeff Randall All Rights Reserved.
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 ///////////////////////////////////////////////////////////////////////////////
21 package org.simantics.db.impl.graph;
23 import gnu.trove.TIntCollection;
24 import gnu.trove.function.TIntFunction;
25 import gnu.trove.impl.Constants;
26 import gnu.trove.impl.HashFunctions;
27 import gnu.trove.iterator.TIntIterator;
28 import gnu.trove.procedure.TIntProcedure;
30 import java.io.IOException;
31 import java.io.ObjectInput;
32 import java.io.ObjectOutput;
33 import java.util.Arrays;
34 import java.util.Collection;
35 import java.util.ConcurrentModificationException;
36 import java.util.NoSuchElementException;
37 import java.util.Random;
40 //////////////////////////////////////////////////
41 // THIS IS A GENERATED CLASS. DO NOT HAND EDIT! //
42 //////////////////////////////////////////////////
46 * A resizable, array-backed list of int primitives.
48 public class TIntArrayListInternal {
50 /** the data of the list */
51 protected int[] _data;
53 /** the index after the last entry in the list */
56 /** the default capacity for new lists */
57 protected static final int DEFAULT_CAPACITY = Constants.DEFAULT_CAPACITY;
59 /** the int value that represents null */
60 protected int no_entry_value;
64 * Creates a new <code>TIntArrayList</code> instance with the
67 @SuppressWarnings({"RedundantCast"})
68 public TIntArrayListInternal() {
69 this( DEFAULT_CAPACITY, ( int ) 0 );
74 * Creates a new <code>TIntArrayList</code> instance with the
77 * @param capacity an <code>int</code> value
79 @SuppressWarnings({"RedundantCast"})
80 public TIntArrayListInternal( int capacity ) {
81 this( capacity, ( int ) 0 );
86 * Creates a new <code>TIntArrayList</code> instance with the
89 * @param capacity an <code>int</code> value
90 * @param no_entry_value an <code>int</code> value that represents null.
92 public TIntArrayListInternal( int capacity, int no_entry_value ) {
93 _data = new int[ capacity ];
95 this.no_entry_value = no_entry_value;
99 * Creates a new <code>TIntArrayList</code> instance that contains
100 * a copy of the collection passed to us.
102 * @param collection the collection to copy
104 public TIntArrayListInternal ( TIntCollection collection ) {
105 this( collection.size() );
106 addAll( collection );
111 * Creates a new <code>TIntArrayList</code> instance whose
112 * capacity is the length of <tt>values</tt> array and whose
113 * initial contents are the specified values.
115 * A defensive copy of the given values is held by the new instance.
117 * @param values an <code>int[]</code> value
119 public TIntArrayListInternal( int[] values ) {
120 this( values.length );
124 protected TIntArrayListInternal(int[] values, int no_entry_value, boolean wrap) {
126 throw new IllegalStateException("Wrong call");
129 throw new IllegalArgumentException("values can not be null");
132 _pos = values.length;
133 this.no_entry_value = no_entry_value;
137 * Returns a primitive List implementation that wraps around the given primitive array.
139 * NOTE: mutating operation are allowed as long as the List does not grow. In that case
140 * an IllegalStateException will be thrown
145 public static TIntArrayListInternal wrap(int[] values) {
146 return wrap(values, ( int ) 0);
150 * Returns a primitive List implementation that wraps around the given primitive array.
152 * NOTE: mutating operation are allowed as long as the List does not grow. In that case
153 * an IllegalStateException will be thrown
156 * @param no_entry_value
159 public static TIntArrayListInternal wrap(int[] values, int no_entry_value) {
160 return new TIntArrayListInternal(values, no_entry_value, true) {
162 * Growing the wrapped external array is not allow
165 public void ensureCapacity(int capacity) {
166 if (capacity > _data.length)
167 throw new IllegalStateException("Can not grow ArrayList wrapped external array");
173 public int getNoEntryValue() {
174 return no_entry_value;
181 * Grow the internal array as needed to accommodate the specified number of elements.
182 * The size of the array bytes on each resize unless capacity requires more than twice
183 * the current capacity.
185 public void ensureCapacity( int capacity ) {
186 if ( capacity > _data.length ) {
187 int newCap = Math.max( _data.length << 1, capacity );
188 int[] tmp = new int[ newCap ];
189 System.arraycopy( _data, 0, tmp, 0, _data.length );
196 public int sizeInternal() {
202 public boolean isEmpty() {
208 * Sheds any excess capacity above and beyond the current size of the list.
210 public void trimToSize() {
211 if ( _data.length > sizeInternal() ) {
212 int[] tmp = new int[ sizeInternal() ];
213 toArray( tmp, 0, tmp.length );
222 public boolean add( int val ) {
223 ensureCapacity( _pos + 1 );
224 _data[ _pos++ ] = val;
230 public void add( int[] vals ) {
231 add( vals, 0, vals.length );
236 public void add( int[] vals, int offset, int length ) {
237 ensureCapacity( _pos + length );
238 System.arraycopy( vals, offset, _data, _pos, length );
244 public void insert( int offset, int value ) {
245 if ( offset == _pos ) {
249 ensureCapacity( _pos + 1 );
251 System.arraycopy( _data, offset, _data, offset + 1, _pos - offset );
253 _data[ offset ] = value;
259 public void insert( int offset, int[] values ) {
260 insert( offset, values, 0, values.length );
265 public void insert( int offset, int[] values, int valOffset, int len ) {
266 if ( offset == _pos ) {
267 add( values, valOffset, len );
271 ensureCapacity( _pos + len );
273 System.arraycopy( _data, offset, _data, offset + len, _pos - offset );
275 System.arraycopy( values, valOffset, _data, offset, len );
281 public int getAt( int offset ) {
282 if ( offset >= _pos ) {
283 throw new ArrayIndexOutOfBoundsException( offset );
285 return _data[ offset ];
290 * Returns the value at the specified offset without doing any bounds checking.
292 public int getQuick( int offset ) {
293 return _data[ offset ];
298 public int set( int offset, int val ) {
299 if ( offset >= _pos ) {
300 throw new ArrayIndexOutOfBoundsException( offset );
303 int prev_val = _data[ offset ];
304 _data[ offset ] = val;
310 public int replace( int offset, int val ) {
311 if ( offset >= _pos ) {
312 throw new ArrayIndexOutOfBoundsException( offset );
314 int old = _data[ offset ];
315 _data[ offset ] = val;
321 public void set( int offset, int[] values ) {
322 set( offset, values, 0, values.length );
327 public void set( int offset, int[] values, int valOffset, int length ) {
328 if ( offset < 0 || offset + length > _pos ) {
329 throw new ArrayIndexOutOfBoundsException( offset );
331 System.arraycopy( values, valOffset, _data, offset, length );
336 * Sets the value at the specified offset without doing any bounds checking.
338 public void setQuick( int offset, int val ) {
339 _data[ offset ] = val;
344 public void clear() {
345 clear( DEFAULT_CAPACITY );
350 * Flushes the internal state of the list, setting the capacity of the empty list to
353 public void clear( int capacity ) {
354 _data = new int[ capacity ];
360 * Sets the size of the list to 0, but does not change its capacity. This method can
361 * be used as an alternative to the {@link #clear()} method if you want to recycle a
362 * list without allocating new backing arrays.
364 public void reset() {
366 Arrays.fill( _data, no_entry_value );
371 * Sets the size of the list to 0, but does not change its capacity. This method can
372 * be used as an alternative to the {@link #clear()} method if you want to recycle a
373 * list without allocating new backing arrays. This method differs from
374 * {@link #reset()} in that it does not clear the old values in the backing array.
375 * Thus, it is possible for getQuick to return stale data if this method is used and
376 * the caller is careless about bounds checking.
378 public void resetQuick() {
384 public boolean removeValue( int value ) {
385 for ( int index = 0; index < _pos; index++ ) {
386 if ( value == _data[index] ) {
396 public int removeAt( int offset ) {
397 int old = getAt( offset );
404 public void remove( int offset, int length ) {
405 if ( length == 0 ) return;
406 if ( offset < 0 || offset >= _pos ) {
407 throw new ArrayIndexOutOfBoundsException(offset);
412 System.arraycopy( _data, length, _data, 0, _pos - length );
414 else if ( _pos - length == offset ) {
415 // no copy to make, decrementing pos "deletes" values at
419 // data in the middle
420 System.arraycopy( _data, offset + length, _data, offset,
421 _pos - ( offset + length ) );
424 // no need to clear old values beyond _pos, because this is a
425 // primitive collection and 0 takes as much room as any other
431 public TIntIterator iteratorInternal() {
432 return new TIntArrayIterator( 0 );
437 public boolean containsAll( Collection<?> collection ) {
438 for ( Object element : collection ) {
439 if ( element instanceof Integer ) {
440 int c = ( ( Integer ) element ).intValue();
441 if ( ! contains( c ) ) {
454 public boolean containsAll( TIntCollection collection ) {
455 if ( this == collection ) {
458 TIntIterator iter = collection.iterator();
459 while ( iter.hasNext() ) {
460 int element = iter.next();
461 if ( ! contains( element ) ) {
470 public boolean containsAll( int[] array ) {
471 for ( int i = array.length; i-- > 0; ) {
472 if ( ! contains( array[i] ) ) {
480 // /** {@inheritDoc} */
481 // public boolean addAll( Collection<? extends Integer> collection ) {
482 // boolean changed = false;
483 // for ( Integer element : collection ) {
484 // int e = element.intValue();
494 public boolean addAll( TIntCollection collection ) {
495 boolean changed = false;
496 TIntIterator iter = collection.iterator();
497 while ( iter.hasNext() ) {
498 int element = iter.next();
499 if ( add( element ) ) {
508 public boolean addAll( int[] array ) {
509 boolean changed = false;
510 for ( int element : array ) {
511 if ( add( element ) ) {
520 @SuppressWarnings({"SuspiciousMethodCalls"})
521 public boolean retainAll( Collection<?> collection ) {
522 boolean modified = false;
523 TIntIterator iter = iteratorInternal();
524 while ( iter.hasNext() ) {
525 if ( ! collection.contains( Integer.valueOf ( iter.next() ) ) ) {
535 public boolean retainAll( TIntCollection collection ) {
536 if ( this == collection ) {
539 boolean modified = false;
540 TIntIterator iter = iteratorInternal();
541 while ( iter.hasNext() ) {
542 if ( ! collection.contains( iter.next() ) ) {
552 public boolean retainAll( int[] array ) {
553 boolean changed = false;
554 Arrays.sort( array );
557 for ( int i = _pos; i-- > 0; ) {
558 if ( Arrays.binarySearch( array, data[i] ) < 0 ) {
568 public boolean removeAll( Collection<?> collection ) {
569 boolean changed = false;
570 for ( Object element : collection ) {
571 if ( element instanceof Integer ) {
572 int c = ( ( Integer ) element ).intValue();
573 if ( removeValue( c ) ) {
583 public boolean removeAll( TIntCollection collection ) {
584 if ( collection == this ) {
588 boolean changed = false;
589 TIntIterator iter = collection.iterator();
590 while ( iter.hasNext() ) {
591 int element = iter.next();
592 if ( removeValue( element ) ) {
601 public boolean removeAll( int[] array ) {
602 boolean changed = false;
603 for ( int i = array.length; i-- > 0; ) {
604 if ( removeValue(array[i]) ) {
613 public void transformValues( TIntFunction function ) {
614 for ( int i = _pos; i-- > 0; ) {
615 _data[ i ] = function.execute( _data[ i ] );
621 public void reverse() {
627 public void reverse( int from, int to ) {
629 return; // nothing to do
632 throw new IllegalArgumentException( "from cannot be greater than to" );
634 for ( int i = from, j = to - 1; i < j; i++, j-- ) {
641 public void shuffle( Random rand ) {
642 for ( int i = _pos; i-- > 1; ) {
643 swap( i, rand.nextInt( i ) );
649 * Swap the values at offsets <tt>i</tt> and <tt>j</tt>.
651 * @param i an offset into the data array
652 * @param j an offset into the data array
654 private void swap( int i, int j ) {
655 int tmp = _data[ i ];
656 _data[ i ] = _data[ j ];
663 // /** {@inheritDoc} */
664 // public TIntList subList( int begin, int end ) {
665 // if ( end < begin ) {
666 // throw new IllegalArgumentException( "end index " + end +
667 // " greater than begin index " + begin );
669 // if ( begin < 0 ) {
670 // throw new IndexOutOfBoundsException( "begin index can not be < 0" );
672 // if ( end > _data.length ) {
673 // throw new IndexOutOfBoundsException( "end index < " + _data.length );
675 // TIntArrayListInternal list = new TIntArrayListInternal( end - begin );
676 // for ( int i = begin; i < end; i++ ) {
677 // list.add( _data[ i ] );
684 public int[] toArrayInternal() {
685 return toArray( 0, _pos );
690 public int[] toArray( int offset, int len ) {
691 int[] rv = new int[ len ];
692 toArray( rv, offset, len );
698 public int[] toArray( int[] dest ) {
699 int len = dest.length;
700 if ( dest.length > _pos ) {
702 dest[len] = no_entry_value;
704 toArray( dest, 0, len );
710 public int[] toArray( int[] dest, int offset, int len ) {
712 return dest; // nothing to copy
714 if ( offset < 0 || offset >= _pos ) {
715 throw new ArrayIndexOutOfBoundsException( offset );
717 System.arraycopy( _data, offset, dest, 0, len );
723 public int[] toArray( int[] dest, int source_pos, int dest_pos, int len ) {
725 return dest; // nothing to copy
727 if ( source_pos < 0 || source_pos >= _pos ) {
728 throw new ArrayIndexOutOfBoundsException( source_pos );
730 System.arraycopy( _data, source_pos, dest, dest_pos, len );
739 public boolean equals( Object other ) {
740 if ( other == this ) {
743 else if ( other instanceof TIntArrayListInternal ) {
744 TIntArrayListInternal that = ( TIntArrayListInternal )other;
745 if ( that.sizeInternal() != this.sizeInternal() ) return false;
747 for ( int i = _pos; i-- > 0; ) {
748 if ( this._data[ i ] != that._data[ i ] ) {
761 public int hashCode() {
763 for ( int i = _pos; i-- > 0; ) {
764 h += HashFunctions.hash( _data[ i ] );
773 public boolean forEach( TIntProcedure procedure ) {
774 for ( int i = 0; i < _pos; i++ ) {
775 if ( !procedure.execute( _data[ i ] ) ) {
784 public boolean forEachDescending( TIntProcedure procedure ) {
785 for ( int i = _pos; i-- > 0; ) {
786 if ( !procedure.execute( _data[ i ] ) ) {
798 Arrays.sort( _data, 0, _pos );
803 public void sort( int fromIndex, int toIndex ) {
804 Arrays.sort( _data, fromIndex, toIndex );
811 public void fill( int val ) {
812 Arrays.fill( _data, 0, _pos, val );
817 public void fill( int fromIndex, int toIndex, int val ) {
818 if ( toIndex > _pos ) {
819 ensureCapacity( toIndex );
822 Arrays.fill( _data, fromIndex, toIndex, val );
829 public int binarySearch( int value ) {
830 return binarySearch( value, 0, _pos );
835 public int binarySearch(int value, int fromIndex, int toIndex) {
836 if ( fromIndex < 0 ) {
837 throw new ArrayIndexOutOfBoundsException( fromIndex );
839 if ( toIndex > _pos ) {
840 throw new ArrayIndexOutOfBoundsException( toIndex );
844 int high = toIndex - 1;
846 while ( low <= high ) {
847 int mid = ( low + high ) >>> 1;
848 int midVal = _data[ mid ];
850 if ( midVal < value ) {
853 else if ( midVal > value ) {
857 return mid; // value found
860 return -( low + 1 ); // value not found.
865 public int indexOf( int value ) {
866 return indexOf( 0, value );
871 public int indexOf( int offset, int value ) {
872 for ( int i = offset; i < _pos; i++ ) {
873 if ( _data[ i ] == value ) {
882 public int lastIndexOf( int value ) {
883 return lastIndexOf( _pos, value );
888 public int lastIndexOf( int offset, int value ) {
889 for ( int i = offset; i-- > 0; ) {
890 if ( _data[ i ] == value ) {
899 public boolean contains( int value ) {
900 return lastIndexOf( value ) >= 0;
904 // /** {@inheritDoc} */
905 // public TIntList grep( TIntProcedure condition ) {
906 // TIntArrayListInternal list = new TIntArrayListInternal();
907 // for ( int i = 0; i < _pos; i++ ) {
908 // if ( condition.execute( _data[ i ] ) ) {
909 // list.add( _data[ i ] );
916 // /** {@inheritDoc} */
917 // public TIntList inverseGrep( TIntProcedure condition ) {
918 // TIntArrayListInternal list = new TIntArrayListInternal();
919 // for ( int i = 0; i < _pos; i++ ) {
920 // if ( !condition.execute( _data[ i ] ) ) {
921 // list.add( _data[ i ] );
930 if ( sizeInternal() == 0 ) {
931 throw new IllegalStateException("cannot find maximum of an empty list");
933 int max = Integer.MIN_VALUE;
934 for ( int i = 0; i < _pos; i++ ) {
935 if ( _data[ i ] > max ) {
945 if ( sizeInternal() == 0 ) {
946 throw new IllegalStateException( "cannot find minimum of an empty list" );
948 int min = Integer.MAX_VALUE;
949 for ( int i = 0; i < _pos; i++ ) {
950 if ( _data[i] < min ) {
961 for ( int i = 0; i < _pos; i++ ) {
972 public String toString() {
973 final StringBuilder buf = new StringBuilder( "{" );
974 for ( int i = 0, end = _pos - 1; i < end; i++ ) {
975 buf.append( _data[ i ] );
978 if ( sizeInternal() > 0 ) {
979 buf.append( _data[ _pos - 1 ] );
982 return buf.toString();
986 /** TIntArrayList iterator */
987 class TIntArrayIterator implements TIntIterator {
989 /** Index of element to be returned by subsequent call to next. */
990 private int cursor = 0;
993 * Index of element returned by most recent call to next or
994 * previous. Reset to -1 if this element is deleted by a call
1000 TIntArrayIterator( int index ) {
1005 /** {@inheritDoc} */
1006 public boolean hasNext() {
1007 return cursor < sizeInternal();
1011 /** {@inheritDoc} */
1014 int next = getAt( cursor );
1017 } catch ( IndexOutOfBoundsException e ) {
1018 throw new NoSuchElementException();
1023 /** {@inheritDoc} */
1024 public void remove() {
1025 if ( lastRet == -1 )
1026 throw new IllegalStateException();
1029 TIntArrayListInternal.this.remove( lastRet, 1);
1030 if ( lastRet < cursor )
1033 } catch ( IndexOutOfBoundsException e ) {
1034 throw new ConcurrentModificationException();
1040 public void writeExternal( ObjectOutput out ) throws IOException {
1045 out.writeInt( _pos );
1048 out.writeInt( no_entry_value );
1051 int len = _data.length;
1052 out.writeInt( len );
1053 for( int i = 0; i < len; i++ ) {
1054 out.writeInt( _data[ i ] );
1059 public void readExternal( ObjectInput in )
1060 throws IOException, ClassNotFoundException {
1066 _pos = in.readInt();
1069 no_entry_value = in.readInt();
1072 int len = in.readInt();
1073 _data = new int[ len ];
1074 for( int i = 0; i < len; i++ ) {
1075 _data[ i ] = in.readInt();