| 1 |
/////////////////////////////////////////////////////////////////////////////// |
| 2 |
// Copyright (c) 2001, Eric D. Friedman All Rights Reserved. |
| 3 |
// |
| 4 |
// This library is free software; you can redistribute it and/or |
| 5 |
// modify it under the terms of the GNU Lesser General Public |
| 6 |
// License as published by the Free Software Foundation; either |
| 7 |
// version 2.1 of the License, or (at your option) any later version. |
| 8 |
// |
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU Lesser General Public |
| 15 |
// License along with this program; if not, write to the Free Software |
| 16 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 |
/////////////////////////////////////////////////////////////////////////////// |
| 18 |
// THIS FILE IS AUTOGENERATED, PLEASE DO NOT EDIT OR ELSE |
| 19 |
|
| 20 |
package gnu.trove; |
| 21 |
|
| 22 |
import java.io.IOException; |
| 23 |
import java.io.ObjectInputStream; |
| 24 |
import java.io.ObjectOutputStream; |
| 25 |
import java.io.Serializable; |
| 26 |
import java.util.Arrays; |
| 27 |
import java.util.Random; |
| 28 |
|
| 29 |
/** |
| 30 |
* A resizable, array-backed list of #e# primitives. |
| 31 |
* |
| 32 |
* Created: Sat Dec 29 14:21:12 2001 |
| 33 |
* |
| 34 |
* @author Eric D. Friedman |
| 35 |
*/ |
| 36 |
public class T#E#ArrayList implements Serializable, Cloneable { |
| 37 |
|
| 38 |
/** the data of the list */ |
| 39 |
protected transient #e#[] _data; // null means the array list was just created |
| 40 |
|
| 41 |
/** the index after the last entry in the list */ |
| 42 |
protected transient int _pos; |
| 43 |
|
| 44 |
/** the default capacity for new lists */ |
| 45 |
protected static final int DEFAULT_CAPACITY = 4; |
| 46 |
|
| 47 |
/** |
| 48 |
* Creates a new <code>T#E#ArrayList</code> instance with the |
| 49 |
* default capacity. |
| 50 |
*/ |
| 51 |
public T#E#ArrayList() { |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Creates a new <code>T#E#ArrayList</code> instance with the |
| 56 |
* specified capacity. |
| 57 |
* |
| 58 |
* @param capacity an <code>int</code> value |
| 59 |
*/ |
| 60 |
public T#E#ArrayList(int capacity) { |
| 61 |
_data = new #e#[capacity]; |
| 62 |
_pos = 0; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Creates a new <code>T#E#ArrayList</code> instance whose |
| 67 |
* capacity is the greater of the length of <tt>values</tt> and |
| 68 |
* DEFAULT_CAPACITY and whose initial contents are the specified |
| 69 |
* values. |
| 70 |
* |
| 71 |
* @param values an <code>#e#[]</code> value |
| 72 |
*/ |
| 73 |
public T#E#ArrayList(#e#[] values) { |
| 74 |
this(Math.max(values.length, DEFAULT_CAPACITY)); |
| 75 |
add(values); |
| 76 |
} |
| 77 |
|
| 78 |
// sizing |
| 79 |
|
| 80 |
/** |
| 81 |
* Grow the internal array as needed to accommodate the specified |
| 82 |
* number of elements. The size of the array doubles on each |
| 83 |
* resize unless <tt>capacity</tt> requires more than twice the |
| 84 |
* current capacity. |
| 85 |
* |
| 86 |
* @param capacity an <code>int</code> value |
| 87 |
*/ |
| 88 |
public void ensureCapacity(int capacity) { |
| 89 |
if (_data == null) { |
| 90 |
_data = new #e#[Math.max(DEFAULT_CAPACITY, capacity)]; |
| 91 |
} |
| 92 |
if (capacity > _data.length) { |
| 93 |
int newCap = Math.max(_data.length << 1, capacity); |
| 94 |
#e#[] tmp = new #e#[newCap]; |
| 95 |
System.arraycopy(_data, 0, tmp, 0, _data.length); |
| 96 |
_data = tmp; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Returns the number of values in the list. |
| 102 |
* |
| 103 |
* @return the number of values in the list. |
| 104 |
*/ |
| 105 |
public int size() { |
| 106 |
return _pos; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Tests whether this list contains any values. |
| 111 |
* |
| 112 |
* @return true if the list is empty. |
| 113 |
*/ |
| 114 |
public boolean isEmpty() { |
| 115 |
return _pos == 0; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Sheds any excess capacity above and beyond the current size of |
| 120 |
* the list. |
| 121 |
*/ |
| 122 |
public void trimToSize() { |
| 123 |
if (_data != null && _data.length > size()) { |
| 124 |
#e#[] tmp = new #e#[size()]; |
| 125 |
toNativeArray(tmp, 0, tmp.length); |
| 126 |
_data = tmp; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// modifying |
| 131 |
|
| 132 |
/** |
| 133 |
* Adds <tt>val</tt> to the end of the list, growing as needed. |
| 134 |
* |
| 135 |
* @param val an <code>#e#</code> value |
| 136 |
*/ |
| 137 |
public void add(#e# val) { |
| 138 |
ensureCapacity(_pos + 1); |
| 139 |
_data[_pos++] = val; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Adds the values in the array <tt>vals</tt> to the end of the |
| 144 |
* list, in order. |
| 145 |
* |
| 146 |
* @param vals an <code>#e#[]</code> value |
| 147 |
*/ |
| 148 |
public void add(#e#[] vals) { |
| 149 |
add(vals, 0, vals.length); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Adds a subset of the values in the array <tt>vals</tt> to the |
| 154 |
* end of the list, in order. |
| 155 |
* |
| 156 |
* @param vals an <code>#e#[]</code> value |
| 157 |
* @param offset the offset at which to start copying |
| 158 |
* @param length the number of values to copy. |
| 159 |
*/ |
| 160 |
public void add(#e#[] vals, int offset, int length) { |
| 161 |
ensureCapacity(_pos + length); |
| 162 |
System.arraycopy(vals, offset, _data, _pos, length); |
| 163 |
_pos += length; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Inserts <tt>value</tt> into the list at <tt>offset</tt>. All |
| 168 |
* values including and to the right of <tt>offset</tt> are shifted |
| 169 |
* to the right. |
| 170 |
* |
| 171 |
* @param offset an <code>int</code> value |
| 172 |
* @param value an <code>#e#</code> value |
| 173 |
*/ |
| 174 |
public void insert(int offset, #e# value) { |
| 175 |
if (offset == _pos) { |
| 176 |
add(value); |
| 177 |
return; |
| 178 |
} |
| 179 |
ensureCapacity(_pos + 1); |
| 180 |
// shift right |
| 181 |
System.arraycopy(_data, offset, _data, offset + 1, _pos - offset); |
| 182 |
// insert |
| 183 |
_data[offset] = value; |
| 184 |
_pos++; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Inserts the array of <tt>values</tt> into the list at |
| 189 |
* <tt>offset</tt>. All values including and to the right of |
| 190 |
* <tt>offset</tt> are shifted to the right. |
| 191 |
* |
| 192 |
* @param offset an <code>int</code> value |
| 193 |
* @param values an <code>#e#[]</code> value |
| 194 |
*/ |
| 195 |
public void insert(int offset, #e#[] values) { |
| 196 |
insert(offset, values, 0, values.length); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Inserts a slice of the array of <tt>values</tt> into the list |
| 201 |
* at <tt>offset</tt>. All values including and to the right of |
| 202 |
* <tt>offset</tt> are shifted to the right. |
| 203 |
* |
| 204 |
* @param offset an <code>int</code> value |
| 205 |
* @param values an <code>#e#[]</code> value |
| 206 |
* @param valOffset the offset in the values array at which to |
| 207 |
* start copying. |
| 208 |
* @param len the number of values to copy from the values array |
| 209 |
*/ |
| 210 |
public void insert(int offset, #e#[] values, int valOffset, int len) { |
| 211 |
if (offset == _pos) { |
| 212 |
add(values, valOffset, len); |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
ensureCapacity(_pos + len); |
| 217 |
// shift right |
| 218 |
System.arraycopy(_data, offset, _data, offset + len, _pos - offset); |
| 219 |
// insert |
| 220 |
System.arraycopy(values, valOffset, _data, offset, len); |
| 221 |
_pos += len; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Returns the value at the specified offset. |
| 226 |
* |
| 227 |
* @param offset an <code>int</code> value |
| 228 |
* @return an <code>#e#</code> value |
| 229 |
*/ |
| 230 |
public #e# get(int offset) { |
| 231 |
if (offset >= _pos) { |
| 232 |
throw new ArrayIndexOutOfBoundsException(offset); |
| 233 |
} |
| 234 |
return _data[offset]; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Returns the value at the specified offset without doing any |
| 239 |
* bounds checking. |
| 240 |
* |
| 241 |
* @param offset an <code>int</code> value |
| 242 |
* @return an <code>#e#</code> value |
| 243 |
*/ |
| 244 |
public #e# getQuick(int offset) { |
| 245 |
return _data[offset]; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Sets the value at the specified offset. |
| 250 |
* |
| 251 |
* @param offset an <code>int</code> value |
| 252 |
* @param val an <code>#e#</code> value |
| 253 |
*/ |
| 254 |
public void set(int offset, #e# val) { |
| 255 |
if (offset < 0 || offset >= _pos) { |
| 256 |
throw new ArrayIndexOutOfBoundsException(offset); |
| 257 |
} |
| 258 |
_data[offset] = val; |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Sets the value at the specified offset and returns the |
| 263 |
* previously stored value. |
| 264 |
* |
| 265 |
* @param offset an <code>int</code> value |
| 266 |
* @param val an <code>#e#</code> value |
| 267 |
* @return the value previously stored at offset. |
| 268 |
*/ |
| 269 |
public #e# getSet(int offset, #e# val) { |
| 270 |
if (offset < 0 || offset >= _pos) { |
| 271 |
throw new ArrayIndexOutOfBoundsException(offset); |
| 272 |
} |
| 273 |
#e# old = _data[offset]; |
| 274 |
_data[offset] = val; |
| 275 |
return old; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Replace the values in the list starting at <tt>offset</tt> with |
| 280 |
* the contents of the <tt>values</tt> array. |
| 281 |
* |
| 282 |
* @param offset the first offset to replace |
| 283 |
* @param values the source of the new values |
| 284 |
*/ |
| 285 |
public void set(int offset, #e#[] values) { |
| 286 |
set(offset, values, 0, values.length); |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Replace the values in the list starting at <tt>offset</tt> with |
| 291 |
* <tt>length</tt> values from the <tt>values</tt> array, starting |
| 292 |
* at valOffset. |
| 293 |
* |
| 294 |
* @param offset the first offset to replace |
| 295 |
* @param values the source of the new values |
| 296 |
* @param valOffset the first value to copy from the values array |
| 297 |
* @param length the number of values to copy |
| 298 |
*/ |
| 299 |
public void set(int offset, #e#[] values, int valOffset, int length) { |
| 300 |
if (offset < 0 || offset + length > _pos) { |
| 301 |
throw new ArrayIndexOutOfBoundsException(offset); |
| 302 |
} |
| 303 |
System.arraycopy(_data, offset, values, valOffset, length); |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Sets the value at the specified offset without doing any bounds |
| 308 |
* checking. |
| 309 |
* |
| 310 |
* @param offset an <code>int</code> value |
| 311 |
* @param val an <code>#e#</code> value |
| 312 |
*/ |
| 313 |
public void setQuick(int offset, #e# val) { |
| 314 |
_data[offset] = val; |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Flushes the internal state of the list, resetting the capacity |
| 319 |
* to the default. |
| 320 |
*/ |
| 321 |
public void clear() { |
| 322 |
_data = null; |
| 323 |
_pos = 0; |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Flushes the internal state of the list, setting the capacity of |
| 328 |
* the empty list to <tt>capacity</tt>. |
| 329 |
* |
| 330 |
* @param capacity an <code>int</code> value |
| 331 |
*/ |
| 332 |
public void clear(int capacity) { |
| 333 |
_data = new #e#[capacity]; |
| 334 |
_pos = 0; |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* Sets the size of the list to 0, but does not change its |
| 339 |
* capacity. This method can be used as an alternative to the |
| 340 |
* {@link #clear clear} method if you want to recycle a list without |
| 341 |
* allocating new backing arrays. |
| 342 |
* |
| 343 |
* @see #clear |
| 344 |
*/ |
| 345 |
public void reset() { |
| 346 |
fill((#e#)0); |
| 347 |
_pos = 0; |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Sets the size of the list to 0, but does not change its |
| 352 |
* capacity. This method can be used as an alternative to the |
| 353 |
* {@link #clear clear} method if you want to recycle a list |
| 354 |
* without allocating new backing arrays. This method differs |
| 355 |
* from {@link #reset reset} in that it does not clear the old |
| 356 |
* values in the backing array. Thus, it is possible for {@link |
| 357 |
* #getQuick getQuick} to return stale data if this method is used |
| 358 |
* and the caller is careless about bounds checking. |
| 359 |
* |
| 360 |
* @see #reset |
| 361 |
* @see #clear |
| 362 |
* @see #getQuick |
| 363 |
*/ |
| 364 |
public void resetQuick() { |
| 365 |
_pos = 0; |
| 366 |
} |
| 367 |
|
| 368 |
/** |
| 369 |
* Removes the value at <tt>offset</tt> from the list. |
| 370 |
* |
| 371 |
* @param offset an <code>int</code> value |
| 372 |
* @return the value previously stored at offset. |
| 373 |
*/ |
| 374 |
public #e# remove(int offset) { |
| 375 |
#e# old = get(offset); |
| 376 |
remove(offset, 1); |
| 377 |
return old; |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* Removes <tt>length</tt> values from the list, starting at |
| 382 |
* <tt>offset</tt> |
| 383 |
* |
| 384 |
* @param offset an <code>int</code> value |
| 385 |
* @param length an <code>int</code> value |
| 386 |
*/ |
| 387 |
public void remove(int offset, int length) { |
| 388 |
if (offset < 0 || offset >= _pos) { |
| 389 |
throw new ArrayIndexOutOfBoundsException(offset); |
| 390 |
} |
| 391 |
|
| 392 |
if (offset == 0) { |
| 393 |
// data at the front |
| 394 |
System.arraycopy(_data, length, _data, 0, _pos - length); |
| 395 |
} else if (_pos - length == offset) { |
| 396 |
// no copy to make, decrementing pos "deletes" values at |
| 397 |
// the end |
| 398 |
} else { |
| 399 |
// data in the middle |
| 400 |
System.arraycopy(_data, offset + length, |
| 401 |
_data, offset, _pos - (offset + length)); |
| 402 |
} |
| 403 |
_pos -= length; |
| 404 |
// no need to clear old values beyond _pos, because this is a |
| 405 |
// primitive collection and 0 takes as much room as any other |
| 406 |
// value |
| 407 |
} |
| 408 |
|
| 409 |
/** |
| 410 |
* Transform each value in the list using the specified function. |
| 411 |
* |
| 412 |
* @param function a <code>T#E#Function</code> value |
| 413 |
*/ |
| 414 |
public void transformValues(T#E#Function function) { |
| 415 |
for (int i = _pos; i-- > 0;) { |
| 416 |
_data[i] = function.execute(_data[i]); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* Reverse the order of the elements in the list. |
| 422 |
*/ |
| 423 |
public void reverse() { |
| 424 |
reverse(0, _pos); |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* Reverse the order of the elements in the range of the list. |
| 429 |
* |
| 430 |
* @param from the inclusive index at which to start reversing |
| 431 |
* @param to the exclusive index at which to stop reversing |
| 432 |
*/ |
| 433 |
public void reverse(int from, int to) { |
| 434 |
if (from == to) { |
| 435 |
return; // nothing to do |
| 436 |
} |
| 437 |
if (from > to) { |
| 438 |
throw new IllegalArgumentException("from cannot be greater than to"); |
| 439 |
} |
| 440 |
for (int i = from, j = to - 1; i < j; i++, j--) { |
| 441 |
swap(i, j); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* Shuffle the elements of the list using the specified random |
| 447 |
* number generator. |
| 448 |
* |
| 449 |
* @param rand a <code>Random</code> value |
| 450 |
*/ |
| 451 |
public void shuffle(Random rand) { |
| 452 |
for (int i = _pos; i-- > 1;) { |
| 453 |
swap(i, rand.nextInt(i)); |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
/** |
| 458 |
* Swap the values at offsets <tt>i</tt> and <tt>j</tt>. |
| 459 |
* |
| 460 |
* @param i an offset into the data array |
| 461 |
* @param j an offset into the data array |
| 462 |
*/ |
| 463 |
private void swap(int i, int j) { |
| 464 |
#e# tmp = _data[i]; |
| 465 |
_data[i] = _data[j]; |
| 466 |
_data[j] = tmp; |
| 467 |
} |
| 468 |
|
| 469 |
// copying |
| 470 |
|
| 471 |
/** |
| 472 |
* Returns a clone of this list. Since this is a primitive |
| 473 |
* collection, this will be a deep clone. |
| 474 |
* |
| 475 |
* @return a deep clone of the list. |
| 476 |
*/ |
| 477 |
@Override |
| 478 |
public Object clone() { |
| 479 |
T#E#ArrayList clone = null; |
| 480 |
try { |
| 481 |
clone = (T#E#ArrayList)super.clone(); |
| 482 |
clone._data = _data == null ? null : _data.clone(); |
| 483 |
} |
| 484 |
catch (CloneNotSupportedException e) { |
| 485 |
// it's supported |
| 486 |
} // end of try-catch |
| 487 |
return clone; |
| 488 |
} |
| 489 |
|
| 490 |
/** |
| 491 |
* Copies the contents of the list into a native array. |
| 492 |
* |
| 493 |
* @return an <code>#e#[]</code> value |
| 494 |
*/ |
| 495 |
public #e#[] toNativeArray() { |
| 496 |
return toNativeArray(0, _pos); |
| 497 |
} |
| 498 |
|
| 499 |
/** |
| 500 |
* Copies a slice of the list into a native array. |
| 501 |
* |
| 502 |
* @param offset the offset at which to start copying |
| 503 |
* @param len the number of values to copy. |
| 504 |
* @return an <code>#e#[]</code> value |
| 505 |
*/ |
| 506 |
public #e#[] toNativeArray(int offset, int len) { |
| 507 |
#e#[] rv = new #e#[len]; |
| 508 |
toNativeArray(rv, offset, len); |
| 509 |
return rv; |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* Copies a slice of the list into a native array. |
| 514 |
* |
| 515 |
* @param dest the array to copy into. |
| 516 |
* @param offset the offset of the first value to copy |
| 517 |
* @param len the number of values to copy. |
| 518 |
*/ |
| 519 |
public void toNativeArray(#e#[] dest, int offset, int len) { |
| 520 |
if (len == 0) { |
| 521 |
return; // nothing to copy |
| 522 |
} |
| 523 |
if (offset < 0 || offset >= _pos) { |
| 524 |
throw new ArrayIndexOutOfBoundsException(offset); |
| 525 |
} |
| 526 |
System.arraycopy(_data, offset, dest, 0, len); |
| 527 |
} |
| 528 |
|
| 529 |
// comparing |
| 530 |
|
| 531 |
/** |
| 532 |
* Compares this list to another list, value by value. |
| 533 |
* |
| 534 |
* @param other the object to compare against |
| 535 |
* @return true if other is a T#E#ArrayList and has exactly the |
| 536 |
* same values. |
| 537 |
*/ |
| 538 |
@Override |
| 539 |
public boolean equals(Object other) { |
| 540 |
if (other == this) { |
| 541 |
return true; |
| 542 |
} |
| 543 |
else if (other instanceof T#E#ArrayList) { |
| 544 |
T#E#ArrayList that = (T#E#ArrayList)other; |
| 545 |
if (that.size() != size()) { |
| 546 |
return false; |
| 547 |
} else { |
| 548 |
for (int i = _pos; i-- > 0;) { |
| 549 |
if (_data[i] != that._data[i]) { |
| 550 |
return false; |
| 551 |
} |
| 552 |
} |
| 553 |
return true; |
| 554 |
} |
| 555 |
} else { |
| 556 |
return false; |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
@Override |
| 561 |
public int hashCode() { |
| 562 |
int h = 0; |
| 563 |
for (int i = _pos; i-- > 0;) { |
| 564 |
h += HashFunctions.hash(_data[i]); |
| 565 |
} |
| 566 |
return h; |
| 567 |
} |
| 568 |
|
| 569 |
// procedures |
| 570 |
|
| 571 |
/** |
| 572 |
* Applies the procedure to each value in the list in ascending |
| 573 |
* (front to back) order. |
| 574 |
* |
| 575 |
* @param procedure a <code>T#E#Procedure</code> value |
| 576 |
* @return true if the procedure did not terminate prematurely. |
| 577 |
*/ |
| 578 |
public boolean forEach(T#E#Procedure procedure) { |
| 579 |
for (int i = 0; i < _pos; i++) { |
| 580 |
if (! procedure.execute(_data[i])) { |
| 581 |
return false; |
| 582 |
} |
| 583 |
} |
| 584 |
return true; |
| 585 |
} |
| 586 |
|
| 587 |
/** |
| 588 |
* Applies the procedure to each value in the list in descending |
| 589 |
* (back to front) order. |
| 590 |
* |
| 591 |
* @param procedure a <code>T#E#Procedure</code> value |
| 592 |
* @return true if the procedure did not terminate prematurely. |
| 593 |
*/ |
| 594 |
public boolean forEachDescending(T#E#Procedure procedure) { |
| 595 |
for (int i = _pos; i-- > 0;) { |
| 596 |
if (! procedure.execute(_data[i])) { |
| 597 |
return false; |
| 598 |
} |
| 599 |
} |
| 600 |
return true; |
| 601 |
} |
| 602 |
|
| 603 |
// sorting |
| 604 |
|
| 605 |
/** |
| 606 |
* Sort the values in the list (ascending) using the Sun quicksort |
| 607 |
* implementation. |
| 608 |
* |
| 609 |
* @see java.util.Arrays#sort |
| 610 |
*/ |
| 611 |
public void sort() { |
| 612 |
if (!isEmpty()) Arrays.sort(_data, 0, _pos); |
| 613 |
} |
| 614 |
|
| 615 |
/** |
| 616 |
* Sort a slice of the list (ascending) using the Sun quicksort |
| 617 |
* implementation. |
| 618 |
* |
| 619 |
* @param fromIndex the index at which to start sorting (inclusive) |
| 620 |
* @param toIndex the index at which to stop sorting (exclusive) |
| 621 |
* @see java.util.Arrays#sort |
| 622 |
*/ |
| 623 |
public void sort(int fromIndex, int toIndex) { |
| 624 |
if (!isEmpty()) Arrays.sort(_data, fromIndex, toIndex); |
| 625 |
} |
| 626 |
|
| 627 |
// filling |
| 628 |
|
| 629 |
/** |
| 630 |
* Fills every slot in the list with the specified value. |
| 631 |
* |
| 632 |
* @param val the value to use when filling |
| 633 |
*/ |
| 634 |
public void fill(#e# val) { |
| 635 |
if (!isEmpty()) Arrays.fill(_data, 0, _pos, val); |
| 636 |
} |
| 637 |
|
| 638 |
/** |
| 639 |
* Fills a range in the list with the specified value. |
| 640 |
* |
| 641 |
* @param fromIndex the offset at which to start filling (inclusive) |
| 642 |
* @param toIndex the offset at which to stop filling (exclusive) |
| 643 |
* @param val the value to use when filling |
| 644 |
*/ |
| 645 |
public void fill(int fromIndex, int toIndex, #e# val) { |
| 646 |
if (toIndex > _pos) { |
| 647 |
ensureCapacity(toIndex); |
| 648 |
_pos = toIndex; |
| 649 |
} |
| 650 |
if (!isEmpty()) Arrays.fill(_data, fromIndex, toIndex, val); |
| 651 |
} |
| 652 |
|
| 653 |
// searching |
| 654 |
|
| 655 |
/** |
| 656 |
* Performs a binary search for <tt>value</tt> in the entire list. |
| 657 |
* Note that you <b>must</b> {@link #sort sort} the list before |
| 658 |
* doing a search. |
| 659 |
* |
| 660 |
* @param value the value to search for |
| 661 |
* @return the absolute offset in the list of the value, or its |
| 662 |
* negative insertion point into the sorted list. |
| 663 |
*/ |
| 664 |
public int binarySearch(#e# value) { |
| 665 |
return binarySearch(value, 0, _pos); |
| 666 |
} |
| 667 |
|
| 668 |
/** |
| 669 |
* Performs a binary search for <tt>value</tt> in the specified |
| 670 |
* range. Note that you <b>must</b> {@link #sort sort} the list |
| 671 |
* or the range before doing a search. |
| 672 |
* |
| 673 |
* @param value the value to search for |
| 674 |
* @param fromIndex the lower boundary of the range (inclusive) |
| 675 |
* @param toIndex the upper boundary of the range (exclusive) |
| 676 |
* @return the absolute offset in the list of the value, or its |
| 677 |
* negative insertion point into the sorted list. |
| 678 |
*/ |
| 679 |
public int binarySearch(#e# value, int fromIndex, int toIndex) { |
| 680 |
if (fromIndex < 0) { |
| 681 |
throw new ArrayIndexOutOfBoundsException(fromIndex); |
| 682 |
} |
| 683 |
if (toIndex > _pos) { |
| 684 |
throw new ArrayIndexOutOfBoundsException(toIndex); |
| 685 |
} |
| 686 |
|
| 687 |
int low = fromIndex; |
| 688 |
int high = toIndex - 1; |
| 689 |
|
| 690 |
while (low <= high) { |
| 691 |
int mid = (low + high) >> 1; |
| 692 |
#e# midVal = _data[mid]; |
| 693 |
|
| 694 |
if (midVal < value) { |
| 695 |
low = mid + 1; |
| 696 |
} else if (midVal > value) { |
| 697 |
high = mid - 1; |
| 698 |
} else { |
| 699 |
return mid; // value found |
| 700 |
} |
| 701 |
} |
| 702 |
return -(low + 1); // value not found. |
| 703 |
} |
| 704 |
|
| 705 |
/** |
| 706 |
* Searches the list front to back for the index of |
| 707 |
* <tt>value</tt>. |
| 708 |
* |
| 709 |
* @param value an <code>#e#</code> value |
| 710 |
* @return the first offset of the value, or -1 if it is not in |
| 711 |
* the list. |
| 712 |
* @see #binarySearch for faster searches on sorted lists |
| 713 |
*/ |
| 714 |
public int indexOf(#e# value) { |
| 715 |
return indexOf(0, value); |
| 716 |
} |
| 717 |
|
| 718 |
/** |
| 719 |
* Searches the list front to back for the index of |
| 720 |
* <tt>value</tt>, starting at <tt>offset</tt>. |
| 721 |
* |
| 722 |
* @param offset the offset at which to start the linear search |
| 723 |
* (inclusive) |
| 724 |
* @param value an <code>#e#</code> value |
| 725 |
* @return the first offset of the value, or -1 if it is not in |
| 726 |
* the list. |
| 727 |
* @see #binarySearch for faster searches on sorted lists |
| 728 |
*/ |
| 729 |
public int indexOf(int offset, #e# value) { |
| 730 |
for (int i = offset; i < _pos; i++) { |
| 731 |
if (_data[i] == value) { |
| 732 |
return i; |
| 733 |
} |
| 734 |
} |
| 735 |
return -1; |
| 736 |
} |
| 737 |
|
| 738 |
/** |
| 739 |
* Searches the list back to front for the last index of |
| 740 |
* <tt>value</tt>. |
| 741 |
* |
| 742 |
* @param value an <code>#e#</code> value |
| 743 |
* @return the last offset of the value, or -1 if it is not in |
| 744 |
* the list. |
| 745 |
* @see #binarySearch for faster searches on sorted lists |
| 746 |
*/ |
| 747 |
public int lastIndexOf(#e# value) { |
| 748 |
return lastIndexOf(_pos, value); |
| 749 |
} |
| 750 |
|
| 751 |
/** |
| 752 |
* Searches the list back to front for the last index of |
| 753 |
* <tt>value</tt>, starting at <tt>offset</tt>. |
| 754 |
* |
| 755 |
* @param offset the offset at which to start the linear search |
| 756 |
* (exclusive) |
| 757 |
* @param value an <code>#e#</code> value |
| 758 |
* @return the last offset of the value, or -1 if it is not in |
| 759 |
* the list. |
| 760 |
* @see #binarySearch for faster searches on sorted lists |
| 761 |
*/ |
| 762 |
public int lastIndexOf(int offset, #e# value) { |
| 763 |
for (int i = offset; i-- > 0;) { |
| 764 |
if (_data[i] == value) { |
| 765 |
return i; |
| 766 |
} |
| 767 |
} |
| 768 |
return -1; |
| 769 |
} |
| 770 |
|
| 771 |
/** |
| 772 |
* Searches the list for <tt>value</tt> |
| 773 |
* |
| 774 |
* @param value an <code>#e#</code> value |
| 775 |
* @return true if value is in the list. |
| 776 |
*/ |
| 777 |
public boolean contains(#e# value) { |
| 778 |
return lastIndexOf(value) >= 0; |
| 779 |
} |
| 780 |
|
| 781 |
/** |
| 782 |
* Searches the list for values satisfying <tt>condition</tt> in |
| 783 |
* the manner of the *nix <tt>grep</tt> utility. |
| 784 |
* |
| 785 |
* @param condition a condition to apply to each element in the list |
| 786 |
* @return a list of values which match the condition. |
| 787 |
*/ |
| 788 |
public T#E#ArrayList grep(T#E#Procedure condition) { |
| 789 |
T#E#ArrayList list = new T#E#ArrayList(); |
| 790 |
for (int i = 0; i < _pos; i++) { |
| 791 |
if (condition.execute(_data[i])) { |
| 792 |
list.add(_data[i]); |
| 793 |
} |
| 794 |
} |
| 795 |
return list; |
| 796 |
} |
| 797 |
|
| 798 |
/** |
| 799 |
* Searches the list for values which do <b>not</b> satisfy |
| 800 |
* <tt>condition</tt>. This is akin to *nix <code>grep -v</code>. |
| 801 |
* |
| 802 |
* @param condition a condition to apply to each element in the list |
| 803 |
* @return a list of values which do not match the condition. |
| 804 |
*/ |
| 805 |
public T#E#ArrayList inverseGrep(T#E#Procedure condition) { |
| 806 |
T#E#ArrayList list = new T#E#ArrayList(); |
| 807 |
for (int i = 0; i < _pos; i++) { |
| 808 |
if (! condition.execute(_data[i])) { |
| 809 |
list.add(_data[i]); |
| 810 |
} |
| 811 |
} |
| 812 |
return list; |
| 813 |
} |
| 814 |
|
| 815 |
/** |
| 816 |
* Finds the maximum value in the list. |
| 817 |
* |
| 818 |
* @return the largest value in the list. |
| 819 |
* @exception IllegalStateException if the list is empty |
| 820 |
*/ |
| 821 |
public #e# max() { |
| 822 |
if (size() == 0) { |
| 823 |
throw new IllegalStateException("cannot find maximum of an empty list"); |
| 824 |
} |
| 825 |
#e# max = _data[_pos - 1]; |
| 826 |
for (int i = _pos - 1; i-- > 0;) { |
| 827 |
max = (#e#) Math.max(max, _data[_pos]); |
| 828 |
} |
| 829 |
return max; |
| 830 |
} |
| 831 |
|
| 832 |
/** |
| 833 |
* Finds the minimum value in the list. |
| 834 |
* |
| 835 |
* @return the smallest value in the list. |
| 836 |
* @exception IllegalStateException if the list is empty |
| 837 |
*/ |
| 838 |
public #e# min() { |
| 839 |
if (size() == 0) { |
| 840 |
throw new IllegalStateException("cannot find minimum of an empty list"); |
| 841 |
} |
| 842 |
#e# min = _data[_pos - 1]; |
| 843 |
for (int i = _pos - 1; i-- > 0;) { |
| 844 |
min = (#e#) Math.min(min, _data[_pos]); |
| 845 |
} |
| 846 |
return min; |
| 847 |
} |
| 848 |
|
| 849 |
// stringification |
| 850 |
|
| 851 |
/** |
| 852 |
* Returns a String representation of the list, front to back. |
| 853 |
* |
| 854 |
* @return a <code>String</code> value |
| 855 |
*/ |
| 856 |
@Override |
| 857 |
public String toString() { |
| 858 |
final StringBuffer buf = new StringBuffer("{"); |
| 859 |
forEach(new T#E#Procedure() { |
| 860 |
public boolean execute(#e# val) { |
| 861 |
buf.append(val); |
| 862 |
buf.append(", "); |
| 863 |
return true; |
| 864 |
} |
| 865 |
}); |
| 866 |
buf.append("}"); |
| 867 |
return buf.toString(); |
| 868 |
} |
| 869 |
|
| 870 |
private void writeObject(ObjectOutputStream stream) |
| 871 |
throws IOException { |
| 872 |
stream.defaultWriteObject(); |
| 873 |
|
| 874 |
// number of entries |
| 875 |
stream.writeInt(size()); |
| 876 |
|
| 877 |
SerializationProcedure writeProcedure = new SerializationProcedure(stream); |
| 878 |
if (! forEach(writeProcedure)) { |
| 879 |
throw writeProcedure.exception; |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
private void readObject(ObjectInputStream stream) |
| 884 |
throws IOException, ClassNotFoundException { |
| 885 |
stream.defaultReadObject(); |
| 886 |
|
| 887 |
int size = stream.readInt(); |
| 888 |
_data = new #e#[size]; |
| 889 |
while (size-- > 0) { |
| 890 |
#e# val = stream.read#E#(); |
| 891 |
add(val); |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
} // T#E#ArrayList |