1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
///////////////////////////////////////////////////////////////////////////////
// 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 gnu.trove.stack.array;
import gnu.trove.stack.T#E#Stack;
import gnu.trove.list.array.T#E#ArrayList;
import gnu.trove.impl.*;
import java.io.Externalizable;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.IOException;
//////////////////////////////////////////////////
// THIS IS A GENERATED CLASS. DO NOT HAND EDIT! //
//////////////////////////////////////////////////
/**
* A stack of #e# primitives, backed by a T#E#ArrayList
*/
public class T#E#ArrayStack implements T#E#Stack, Externalizable {
static final long serialVersionUID = 1L;
/** the list used to hold the stack values. */
protected T#E#ArrayList _list;
public static final int DEFAULT_CAPACITY = Constants.DEFAULT_CAPACITY;
/**
* Creates a new <code>T#E#ArrayStack</code> instance with the default
* capacity.
*/
public T#E#ArrayStack() {
this( DEFAULT_CAPACITY );
}
/**
* Creates a new <code>T#E#ArrayStack</code> instance with the
* specified capacity.
*
* @param capacity the initial depth of the stack
*/
public T#E#ArrayStack( int capacity ) {
_list = new T#E#ArrayList( capacity );
}
/**
* Creates a new <code>T#E#ArrayStack</code> instance with the
* specified capacity.
*
* @param capacity the initial depth of the stack
* @param no_entry_value value that represents null
*/
public T#E#ArrayStack( int capacity, #e# no_entry_value ) {
_list = new T#E#ArrayList( capacity, no_entry_value );
}
/**
* Creates a new <code>T#E#ArrayStack</code> instance that is
* a copy of the instanced passed to us.
*
* @param stack the instance to copy
*/
public T#E#ArrayStack( T#E#Stack stack ) {
if ( stack instanceof T#E#ArrayStack ) {
T#E#ArrayStack array_stack = ( T#E#ArrayStack ) stack;
this._list = new T#E#ArrayList( array_stack._list );
} else {
throw new UnsupportedOperationException( "Only support T#E#ArrayStack" );
}
}
/**
* Returns the value that is used to represent null. The default
* value is generally zero, but can be changed during construction
* of the collection.
*
* @return the value that represents null
*/
public #e# getNoEntryValue() {
return _list.getNoEntryValue();
}
/**
* Pushes the value onto the top of the stack.
*
* @param val an <code>#e#</code> value
*/
public void push( #e# val ) {
_list.add( val );
}
/**
* Removes and returns the value at the top of the stack.
*
* @return an <code>#e#</code> value
*/
public #e# pop() {
return _list.removeAt( _list.size() - 1 );
}
/**
* Returns the value at the top of the stack.
*
* @return an <code>#e#</code> value
*/
public #e# peek() {
return _list.get( _list.size() - 1 );
}
/**
* Returns the current depth of the stack.
*/
public int size() {
return _list.size();
}
/**
* Clears the stack.
*/
public void clear() {
_list.clear();
}
/**
* Copies the contents of the stack into a native array. Note that this will NOT
* pop them out of the stack. The front of the list will be the top of the stack.
*
* @return an <code>#e#[]</code> value
*/
public #e#[] toArray() {
#e#[] retval = _list.toArray();
reverse( retval, 0, size() );
return retval;
}
/**
* Copies a slice of the list into a native array. Note that this will NOT
* pop them out of the stack. The front of the list will be the top
* of the stack.
* <p>
* If the native array is smaller than the stack depth,
* the native array will be filled with the elements from the top
* of the array until it is full and exclude the remainder.
*
* @param dest the array to copy into.
*/
public void toArray( #e#[] dest ) {
int size = size();
int start = size - dest.length;
if ( start < 0 ) {
start = 0;
}
int length = Math.min( size, dest.length );
_list.toArray( dest, start, length );
reverse( dest, 0, length );
if ( dest.length > size ) {
dest[size] = _list.getNoEntryValue();
}
}
/**
* Reverse the order of the elements in the range of the list.
*
* @param dest the array of data
* @param from the inclusive index at which to start reversing
* @param to the exclusive index at which to stop reversing
*/
private void reverse( #e#[] dest, 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( dest, i, j );
}
}
/**
* Swap the values at offsets <tt>i</tt> and <tt>j</tt>.
*
* @param dest the array of data
* @param i an offset into the data array
* @param j an offset into the data array
*/
private void swap( #e#[] dest, int i, int j ) {
#e# tmp = dest[ i ];
dest[ i ] = dest[ j ];
dest[ j ] = tmp;
}
/**
* Returns a String representation of the list, top to bottom.
*
* @return a <code>String</code> value
*/
public String toString() {
final StringBuilder buf = new StringBuilder( "{" );
for ( int i = _list.size() - 1; i > 0; i-- ) {
buf.append( _list.get( i ) );
buf.append( ", " );
}
if ( size() > 0 ) {
buf.append( _list.get( 0 ) );
}
buf.append( "}" );
return buf.toString();
}
public boolean equals( Object o ) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
T#E#ArrayStack that = ( T#E#ArrayStack ) o;
return _list.equals( that._list );
}
public int hashCode() {
return _list.hashCode();
}
public void writeExternal( ObjectOutput out ) throws IOException {
// VERSION
out.writeByte( 0 );
// LIST
out.writeObject( _list );
}
public void readExternal( ObjectInput in )
throws IOException, ClassNotFoundException {
// VERSION
in.readByte();
// LIST
_list = ( T#E#ArrayList ) in.readObject();
}
} // T#E#ArrayStack
|