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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
|
/*
* Copyright (C) 2002-2014 Sebastiano Vigna
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package PACKAGE;
#if #keys(reference)
import it.unimi.dsi.fastutil.Stack;
#endif
import java.util.List;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Collection;
import java.util.NoSuchElementException;
/** An abstract class providing basic methods for lists implementing a type-specific list interface.
*
* <P>As an additional bonus, this class implements on top of the list operations a type-specific stack.
*/
public abstract class ABSTRACT_LIST KEY_GENERIC extends ABSTRACT_COLLECTION KEY_GENERIC implements LIST KEY_GENERIC, STACK KEY_GENERIC {
protected ABSTRACT_LIST() {}
/** Ensures that the given index is nonnegative and not greater than the list size.
*
* @param index an index.
* @throws IndexOutOfBoundsException if the given index is negative or greater than the list size.
*/
protected void ensureIndex( final int index ) {
if ( index < 0 ) throw new IndexOutOfBoundsException( "Index (" + index + ") is negative" );
if ( index > size() ) throw new IndexOutOfBoundsException( "Index (" + index + ") is greater than list size (" + ( size() ) + ")" );
}
/** Ensures that the given index is nonnegative and smaller than the list size.
*
* @param index an index.
* @throws IndexOutOfBoundsException if the given index is negative or not smaller than the list size.
*/
protected void ensureRestrictedIndex( final int index ) {
if ( index < 0 ) throw new IndexOutOfBoundsException( "Index (" + index + ") is negative" );
if ( index >= size() ) throw new IndexOutOfBoundsException( "Index (" + index + ") is greater than or equal to list size (" + ( size() ) + ")" );
}
public void add( final int index, final KEY_GENERIC_TYPE k ) {
throw new UnsupportedOperationException();
}
public boolean add( final KEY_GENERIC_TYPE k ) {
add( size(), k );
return true;
}
public KEY_GENERIC_TYPE REMOVE_KEY( int i ) {
throw new UnsupportedOperationException();
}
public KEY_GENERIC_TYPE set( final int index, final KEY_GENERIC_TYPE k ) {
throw new UnsupportedOperationException();
}
public boolean addAll( int index, final Collection<? extends KEY_GENERIC_CLASS> c ) {
ensureIndex( index );
int n = c.size();
if ( n == 0 ) return false;
Iterator<? extends KEY_GENERIC_CLASS> i = c.iterator();
while( n-- != 0 ) add( index++, i.next() );
return true;
}
/** Delegates to a more generic method. */
public boolean addAll( final Collection<? extends KEY_GENERIC_CLASS> c ) {
return addAll( size(), c );
}
/** Delegates to the new covariantly stronger generic method. */
@Deprecated
public KEY_LIST_ITERATOR KEY_GENERIC KEY_LIST_ITERATOR_METHOD() {
return listIterator();
}
/** Delegates to the new covariantly stronger generic method. */
@Deprecated
public KEY_LIST_ITERATOR KEY_GENERIC KEY_LIST_ITERATOR_METHOD( final int index ) {
return listIterator( index );
}
public KEY_LIST_ITERATOR KEY_GENERIC iterator() {
return listIterator();
}
public KEY_LIST_ITERATOR KEY_GENERIC listIterator() {
return listIterator( 0 );
}
public KEY_LIST_ITERATOR KEY_GENERIC listIterator( final int index ) {
return new KEY_ABSTRACT_LIST_ITERATOR KEY_GENERIC() {
int pos = index, last = -1;
public boolean hasNext() { return pos < ABSTRACT_LIST.this.size(); }
public boolean hasPrevious() { return pos > 0; }
public KEY_GENERIC_TYPE NEXT_KEY() { if ( ! hasNext() ) throw new NoSuchElementException(); return ABSTRACT_LIST.this.GET_KEY( last = pos++ ); }
public KEY_GENERIC_TYPE PREV_KEY() { if ( ! hasPrevious() ) throw new NoSuchElementException(); return ABSTRACT_LIST.this.GET_KEY( last = --pos ); }
public int nextIndex() { return pos; }
public int previousIndex() { return pos - 1; }
public void add( KEY_GENERIC_TYPE k ) {
if ( last == -1 ) throw new IllegalStateException();
ABSTRACT_LIST.this.add( pos++, k );
last = -1;
}
public void set( KEY_GENERIC_TYPE k ) {
if ( last == -1 ) throw new IllegalStateException();
ABSTRACT_LIST.this.set( last, k );
}
public void remove() {
if ( last == -1 ) throw new IllegalStateException();
ABSTRACT_LIST.this.REMOVE_KEY( last );
/* If the last operation was a next(), we are removing an element *before* us, and we must decrease pos correspondingly. */
if ( last < pos ) pos--;
last = -1;
}
};
}
public boolean contains( final KEY_TYPE k ) {
return indexOf( k ) >= 0;
}
public int indexOf( final KEY_TYPE k ) {
final KEY_LIST_ITERATOR KEY_GENERIC i = listIterator();
KEY_GENERIC_TYPE e;
while( i.hasNext() ) {
e = i.NEXT_KEY();
if ( KEY_EQUALS( k, e ) ) return i.previousIndex();
}
return -1;
}
public int lastIndexOf( final KEY_TYPE k ) {
KEY_LIST_ITERATOR KEY_GENERIC i = listIterator( size() );
KEY_GENERIC_TYPE e;
while( i.hasPrevious() ) {
e = i.PREV_KEY();
if ( KEY_EQUALS( k, e ) ) return i.nextIndex();
}
return -1;
}
public void size( final int size ) {
int i = size();
if ( size > i ) while( i++ < size ) add( KEY_NULL );
else while( i-- != size ) remove( i );
}
public LIST KEY_GENERIC subList( final int from, final int to ) {
ensureIndex( from );
ensureIndex( to );
if ( from > to ) throw new IndexOutOfBoundsException( "Start index (" + from + ") is greater than end index (" + to + ")" );
return new SUBLIST KEY_GENERIC( this, from, to );
}
/** Delegates to the new covariantly stronger generic method. */
@Deprecated
public LIST KEY_GENERIC SUBLIST_METHOD( final int from, final int to ) {
return subList( from, to );
}
/** Removes elements of this type-specific list one-by-one.
*
* <P>This is a trivial iterator-based implementation. It is expected that
* implementations will override this method with a more optimized version.
*
*
* @param from the start index (inclusive).
* @param to the end index (exclusive).
*/
public void removeElements( final int from, final int to ) {
ensureIndex( to );
KEY_LIST_ITERATOR KEY_GENERIC i = listIterator( from );
int n = to - from;
if ( n < 0 ) throw new IllegalArgumentException( "Start index (" + from + ") is greater than end index (" + to + ")" );
while( n-- != 0 ) {
i.NEXT_KEY();
i.remove();
}
}
/** Adds elements to this type-specific list one-by-one.
*
* <P>This is a trivial iterator-based implementation. It is expected that
* implementations will override this method with a more optimized version.
*
* @param index the index at which to add elements.
* @param a the array containing the elements.
* @param offset the offset of the first element to add.
* @param length the number of elements to add.
*/
public void addElements( int index, final KEY_GENERIC_TYPE a[], int offset, int length ) {
ensureIndex( index );
if ( offset < 0 ) throw new ArrayIndexOutOfBoundsException( "Offset (" + offset + ") is negative" );
if ( offset + length > a.length ) throw new ArrayIndexOutOfBoundsException( "End index (" + ( offset + length ) + ") is greater than array length (" + a.length + ")" );
while( length-- != 0 ) add( index++, a[ offset++ ] );
}
public void addElements( final int index, final KEY_GENERIC_TYPE a[] ) {
addElements( index, a, 0, a.length );
}
/** Copies element of this type-specific list into the given array one-by-one.
*
* <P>This is a trivial iterator-based implementation. It is expected that
* implementations will override this method with a more optimized version.
*
* @param from the start index (inclusive).
* @param a the destination array.
* @param offset the offset into the destination array where to store the first element copied.
* @param length the number of elements to be copied.
*/
public void getElements( final int from, final KEY_TYPE a[], int offset, int length ) {
KEY_LIST_ITERATOR KEY_GENERIC i = listIterator( from );
if ( offset < 0 ) throw new ArrayIndexOutOfBoundsException( "Offset (" + offset + ") is negative" );
if ( offset + length > a.length ) throw new ArrayIndexOutOfBoundsException( "End index (" + ( offset + length ) + ") is greater than array length (" + a.length + ")" );
if ( from + length > size() ) throw new IndexOutOfBoundsException( "End index (" + ( from + length ) + ") is greater than list size (" + size() + ")" );
while( length-- != 0 ) a[ offset++ ] = i.NEXT_KEY();
}
#if ! #keyclass(Reference)
private boolean valEquals( final Object a, final Object b ) {
return a == null ? b == null : a.equals( b );
}
#endif
public boolean equals( final Object o ) {
if ( o == this ) return true;
if ( ! ( o instanceof List ) ) return false;
final List<?> l = (List<?>)o;
int s = size();
if ( s != l.size() ) return false;
#if #keys(primitive)
if ( l instanceof LIST ) {
final KEY_LIST_ITERATOR KEY_GENERIC i1 = listIterator(), i2 = ((LIST KEY_GENERIC)l).listIterator();
while( s-- != 0 ) if ( i1.NEXT_KEY() != i2.NEXT_KEY() ) return false;
return true;
}
#endif
final ListIterator<?> i1 = listIterator(), i2 = l.listIterator();
#if #keyclass(Reference)
while( s-- != 0 ) if ( i1.next() != i2.next() ) return false;
#else
while( s-- != 0 ) if ( ! valEquals( i1.next(), i2.next() ) ) return false;
#endif
return true;
}
#if ! #keyclass(Reference)
/** Compares this list to another object. If the
* argument is a {@link java.util.List}, this method performs a lexicographical comparison; otherwise,
* it throws a <code>ClassCastException</code>.
*
* @param l a list.
* @return if the argument is a {@link java.util.List}, a negative integer,
* zero, or a positive integer as this list is lexicographically less than, equal
* to, or greater than the argument.
* @throws ClassCastException if the argument is not a list.
*/
@SuppressWarnings("unchecked")
public int compareTo( final List<? extends KEY_GENERIC_CLASS> l ) {
if ( l == this ) return 0;
if ( l instanceof LIST ) {
final KEY_LIST_ITERATOR KEY_GENERIC i1 = listIterator(), i2 = ((LIST KEY_GENERIC)l).listIterator();
int r;
KEY_GENERIC_TYPE e1, e2;
while( i1.hasNext() && i2.hasNext() ) {
e1 = i1.NEXT_KEY();
e2 = i2.NEXT_KEY();
if ( ( r = KEY_CMP( e1, e2 ) ) != 0 ) return r;
}
return i2.hasNext() ? -1 : ( i1.hasNext() ? 1 : 0 );
}
ListIterator<? extends KEY_GENERIC_CLASS> i1 = listIterator(), i2 = l.listIterator();
int r;
while( i1.hasNext() && i2.hasNext() ) {
if ( ( r = ((Comparable<? super KEY_GENERIC_CLASS>)i1.next()).compareTo( i2.next() ) ) != 0 ) return r;
}
return i2.hasNext() ? -1 : ( i1.hasNext() ? 1 : 0 );
}
#endif
/** Returns the hash code for this list, which is identical to {@link java.util.List#hashCode()}.
*
* @return the hash code for this list.
*/
public int hashCode() {
KEY_ITERATOR KEY_GENERIC i = iterator();
int h = 1, s = size();
while ( s-- != 0 ) {
KEY_GENERIC_TYPE k = i.NEXT_KEY();
h = 31 * h + KEY2JAVAHASH( k );
}
return h;
}
public void push( KEY_GENERIC_TYPE o ) {
add( o );
}
public KEY_GENERIC_TYPE POP() {
if ( isEmpty() ) throw new NoSuchElementException();
return REMOVE_KEY( size() - 1 );
}
public KEY_GENERIC_TYPE TOP() {
if ( isEmpty() ) throw new NoSuchElementException();
return GET_KEY( size() - 1 );
}
public KEY_GENERIC_TYPE PEEK( int i ) {
return GET_KEY( size() - 1 - i );
}
#if #keys(primitive)
public boolean rem( KEY_TYPE k ) {
int index = indexOf( k );
if ( index == -1 ) return false;
REMOVE_KEY( index );
return true;
}
/** Delegates to <code>rem()</code>. */
public boolean remove( final Object o ) {
return rem( KEY_OBJ2TYPE( o ) );
}
/** Delegates to a more generic method. */
public boolean addAll( final int index, final COLLECTION c ) {
return addAll( index, (Collection<? extends KEY_CLASS>)c );
}
/** Delegates to a more generic method. */
public boolean addAll( final int index, final LIST l ) {
return addAll( index, (COLLECTION)l );
}
public boolean addAll( final COLLECTION c ) {
return addAll( size(), c );
}
public boolean addAll( final LIST l ) {
return addAll( size(), l );
}
/** Delegates to the corresponding type-specific method. */
public void add( final int index, final KEY_CLASS ok ) {
add( index, ok.KEY_VALUE() );
}
/** Delegates to the corresponding type-specific method. */
public KEY_CLASS set( final int index, final KEY_CLASS ok ) {
return KEY2OBJ( set( index, ok.KEY_VALUE() ) );
}
/** Delegates to the corresponding type-specific method. */
public KEY_CLASS get( final int index ) {
return KEY2OBJ( GET_KEY( index ) );
}
/** Delegates to the corresponding type-specific method. */
public int indexOf( final Object ok) {
return indexOf( KEY_OBJ2TYPE( ok ) );
}
/** Delegates to the corresponding type-specific method. */
public int lastIndexOf( final Object ok ) {
return lastIndexOf( KEY_OBJ2TYPE( ok ) );
}
/** Delegates to the corresponding type-specific method. */
public KEY_CLASS remove( final int index ) {
return KEY2OBJ( REMOVE_KEY( index ) );
}
/** Delegates to the corresponding type-specific method. */
public void push( KEY_CLASS o ) {
push( o.KEY_VALUE() );
}
/** Delegates to the corresponding type-specific method. */
public KEY_CLASS pop() {
return KEY_CLASS.valueOf( POP() );
}
/** Delegates to the corresponding type-specific method. */
public KEY_CLASS top() {
return KEY_CLASS.valueOf( TOP() );
}
/** Delegates to the corresponding type-specific method. */
public KEY_CLASS peek( int i ) {
return KEY_CLASS.valueOf( PEEK( i ) );
}
#endif
public String toString() {
final StringBuilder s = new StringBuilder();
final KEY_ITERATOR KEY_GENERIC i = iterator();
int n = size();
KEY_GENERIC_TYPE k;
boolean first = true;
s.append("[");
while( n-- != 0 ) {
if (first) first = false;
else s.append(", ");
k = i.NEXT_KEY();
#if #keys(reference)
if (this == k) s.append("(this list)"); else
#endif
s.append( String.valueOf( k ) );
}
s.append("]");
return s.toString();
}
public static class SUBLIST KEY_GENERIC extends ABSTRACT_LIST KEY_GENERIC implements java.io.Serializable {
private static final long serialVersionUID = -7046029254386353129L;
/** The list this sublist restricts. */
protected final LIST KEY_GENERIC l;
/** Initial (inclusive) index of this sublist. */
protected final int from;
/** Final (exclusive) index of this sublist. */
protected int to;
private static final boolean ASSERTS = ASSERTS_VALUE;
public SUBLIST( final LIST KEY_GENERIC l, final int from, final int to ) {
this.l = l;
this.from = from;
this.to = to;
}
private void assertRange() {
if ( ASSERTS ) {
assert from <= l.size();
assert to <= l.size();
assert to >= from;
}
}
public boolean add( final KEY_GENERIC_TYPE k ) {
l.add( to, k );
to++;
if ( ASSERTS ) assertRange();
return true;
}
public void add( final int index, final KEY_GENERIC_TYPE k ) {
ensureIndex( index );
l.add( from + index, k );
to++;
if ( ASSERTS ) assertRange();
}
public boolean addAll( final int index, final Collection<? extends KEY_GENERIC_CLASS> c ) {
ensureIndex( index );
to += c.size();
if ( ASSERTS ) {
boolean retVal = l.addAll( from + index, c );
assertRange();
return retVal;
}
return l.addAll( from + index, c );
}
public KEY_GENERIC_TYPE GET_KEY( int index ) {
ensureRestrictedIndex( index );
return l.GET_KEY( from + index );
}
public KEY_GENERIC_TYPE REMOVE_KEY( int index ) {
ensureRestrictedIndex( index );
to--;
return l.REMOVE_KEY( from + index );
}
public KEY_GENERIC_TYPE set( int index, KEY_GENERIC_TYPE k ) {
ensureRestrictedIndex( index );
return l.set( from + index, k );
}
public void clear() {
removeElements( 0, size() );
if ( ASSERTS ) assertRange();
}
public int size() {
return to - from;
}
public void getElements( final int from, final KEY_TYPE[] a, final int offset, final int length ) {
ensureIndex( from );
if ( from + length > size() ) throw new IndexOutOfBoundsException( "End index (" + from + length + ") is greater than list size (" + size() + ")" );
l.getElements( this.from + from, a, offset, length );
}
public void removeElements( final int from, final int to ) {
ensureIndex( from );
ensureIndex( to );
l.removeElements( this.from + from, this.from + to );
this.to -= ( to - from );
if ( ASSERTS ) assertRange();
}
public void addElements( int index, final KEY_GENERIC_TYPE a[], int offset, int length ) {
ensureIndex( index );
l.addElements( this.from + index, a, offset, length );
this.to += length;
if ( ASSERTS ) assertRange();
}
public KEY_LIST_ITERATOR KEY_GENERIC listIterator( final int index ) {
ensureIndex( index );
return new KEY_ABSTRACT_LIST_ITERATOR KEY_GENERIC() {
int pos = index, last = -1;
public boolean hasNext() { return pos < size(); }
public boolean hasPrevious() { return pos > 0; }
public KEY_GENERIC_TYPE NEXT_KEY() { if ( ! hasNext() ) throw new NoSuchElementException(); return l.GET_KEY( from + ( last = pos++ ) ); }
public KEY_GENERIC_TYPE PREV_KEY() { if ( ! hasPrevious() ) throw new NoSuchElementException(); return l.GET_KEY( from + ( last = --pos ) ); }
public int nextIndex() { return pos; }
public int previousIndex() { return pos - 1; }
public void add( KEY_GENERIC_TYPE k ) {
if ( last == -1 ) throw new IllegalStateException();
SUBLIST.this.add( pos++, k );
last = -1;
if ( ASSERTS ) assertRange();
}
public void set( KEY_GENERIC_TYPE k ) {
if ( last == -1 ) throw new IllegalStateException();
SUBLIST.this.set( last, k );
}
public void remove() {
if ( last == -1 ) throw new IllegalStateException();
SUBLIST.this.REMOVE_KEY( last );
/* If the last operation was a next(), we are removing an element *before* us, and we must decrease pos correspondingly. */
if ( last < pos ) pos--;
last = -1;
if ( ASSERTS ) assertRange();
}
};
}
public LIST KEY_GENERIC subList( final int from, final int to ) {
ensureIndex( from );
ensureIndex( to );
if ( from > to ) throw new IllegalArgumentException( "Start index (" + from + ") is greater than end index (" + to + ")" );
return new SUBLIST KEY_GENERIC( this, from, to );
}
#if #keys(primitive)
public boolean rem( KEY_TYPE k ) {
int index = indexOf( k );
if ( index == -1 ) return false;
to--;
l.REMOVE_KEY( from + index );
if ( ASSERTS ) assertRange();
return true;
}
public boolean remove( final Object o ) {
return rem( KEY_OBJ2TYPE( o ) );
}
public boolean addAll( final int index, final COLLECTION c ) {
ensureIndex( index );
to += c.size();
if ( ASSERTS ) {
boolean retVal = l.addAll( from + index, c );
assertRange();
return retVal;
}
return l.addAll( from + index, c );
}
public boolean addAll( final int index, final LIST l ) {
ensureIndex( index );
to += l.size();
if ( ASSERTS ) {
boolean retVal = this.l.addAll( from + index, l );
assertRange();
return retVal;
}
return this.l.addAll( from + index, l );
}
#else
@SuppressWarnings("unchecked")
public boolean remove( final Object o ) {
int index = indexOf( o );
if ( index == -1 ) return false;
REMOVE_KEY( index );
return true;
}
#endif
}
}
|