diff options
| author | Cliff Click <cliffc@acm.org> | 2025-02-07 08:58:36 -0800 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2025-02-07 08:58:36 -0800 |
| commit | 2aa8c5d1d0659bd1c828134f1a1f14d025a2d0f1 (patch) | |
| tree | 0532f3ba5d74de006fcaa906dad9b787ebb06e2e /src/main/java/com/seaofnodes/simple/Ary.java | |
| parent | 68dc63b9390bbe4ec50cc1b814d4e559e459c057 (diff) | |
| download | Simple-linear-chapter19.tar.gz Simple-linear-chapter19.zip | |
Ch# Chapter 19: Instruction Selection and Portable Compilationlinear-chapter19
Co-authored-by: hadha <helsgithub15@gmail.com>
Co-authored-by: Tamás Hadházy <85063808+Hels15@users.noreply.github.com>
Diffstat (limited to 'src/main/java/com/seaofnodes/simple/Ary.java')
| -rw-r--r-- | src/main/java/com/seaofnodes/simple/Ary.java | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/src/main/java/com/seaofnodes/simple/Ary.java b/src/main/java/com/seaofnodes/simple/Ary.java index b5184948..73ada8e0 100644 --- a/src/main/java/com/seaofnodes/simple/Ary.java +++ b/src/main/java/com/seaofnodes/simple/Ary.java @@ -42,8 +42,7 @@ public class Ary<E> extends AbstractList<E> implements List<E> { } /** @return remove and return last element */ public E pop( ) { - range_check(0); - return _es[--_len]; + return _len==0 ? null : _es[--_len]; } @Override public E removeLast() { return pop(); } @@ -93,6 +92,22 @@ public class Ary<E> extends AbstractList<E> implements List<E> { return old; } + /** Set existing element + * @param i element to set + * @param e value to set + * @return old value + * @exception AIOOBE if !(0 <= i < _len) + */ + public E setX( int i, E e ) { + if( i >= _len ) { + while( i >= _es.length ) _es = Arrays.copyOf( _es, _es.length << 1 ); + _len = i+1; + } + E old = _es[i]; + _es[i] = e; + return old; + } + public Ary<E> setLen( int len ) { if( len > _len ) while( len>= _es.length ) _es = Arrays.copyOf(_es,_es.length<<1); @@ -103,13 +118,14 @@ public class Ary<E> extends AbstractList<E> implements List<E> { return this; } - public void swap( int i, int j ) { + public E swap( int i, int j ) { range_check(i); range_check(j); - if( i==j ) return; E tmp = _es[i]; + if( i==j ) return tmp; _es[i] = _es[j]; _es[j] = tmp; + return tmp; } @@ -139,12 +155,12 @@ public class Ary<E> extends AbstractList<E> implements List<E> { return this; } - /** @return compact array version */ - public E[] asAry() { return Arrays.copyOf(_es,_len); } + /** @return compact array version */ + public E[] asAry() { return Arrays.copyOf(_es,_len); } - /** Sorts in-place - * @param c Comparator to sort by */ - public void sort_update(Comparator<? super E> c ) { Arrays.sort(_es, 0, _len, c); } + /** Sorts in-place + * @param c Comparator to sort by */ + public void sort_update(Comparator<? super E> c ) { Arrays.sort(_es, 0, _len, c); } /** Find the first matching element using ==, or -1 if none. Note that * most del calls shuffle the list, so the first element might be random. |
