aa
AryInt.java
Go to the documentation of this file.
1 package com.cliffc.aa.util;
2 
3 import java.util.Arrays;
4 import java.util.Collection;
5 import java.util.function.IntUnaryOperator;
6 
7 // ArrayList with saner syntax
8 public class AryInt {
9  public int[] _es;
10  public int _len;
11  public AryInt(int[] es) { this(es,es.length); }
12  public AryInt(int[] es, int len) { _es=es; _len=len; }
13  public AryInt() { this(new int[1],0); }
14 
16  public boolean isEmpty() { return _len==0; }
18  public int len() { return _len; }
21  public int at( int i ) {
22  range_check(i);
23  return _es[i];
24  }
27  public int atX( int i ) {
28  return i < _len ? _es[i] : 0;
29  }
31  public int last( ) {
32  range_check(0);
33  return _es[_len-1];
34  }
35 
37  public int pop( ) {
38  range_check(0);
39  return _es[--_len];
40  }
41 
45  public AryInt push( int e ) {
46  if( _len >= _es.length ) _es = Arrays.copyOf(_es,Math.max(1,_es.length<<1));
47  _es[_len++] = e;
48  return this;
49  }
50 
55  public void insert( int i, int e ) {
56  if( i < 0 || i>_len )
57  throw new ArrayIndexOutOfBoundsException(""+i+" >= "+_len);
58  if( _len >= _es.length ) _es = Arrays.copyOf(_es,Math.max(1,_es.length<<1));
59  System.arraycopy(_es,i,_es,i+1,(_len++)-i);
60  _es[i] = e;
61  }
62 
66  public int del( int i ) {
67  range_check(i);
68  int tmp = _es[i];
69  _es[i]=_es[--_len];
70  return tmp;
71  }
72 
76  public int remove( int i ) {
77  range_check(i);
78  int e = _es[i];
79  System.arraycopy(_es,i+1,_es,i,(--_len)-i);
80  return e;
81  }
82 
84  public void clear( ) { Arrays.fill(_es,0,_len,0); _len=0; }
85 
86  // Extend and set
87  public int setX( int i, int e ) {
88  while( i>= _es.length ) _es = Arrays.copyOf(_es,_es.length<<1);
89  if( i >= _len ) _len = i+1;
90  return (_es[i] = e);
91  }
92 
93  public int set( int i, int e ) {
94  range_check(i);
95  return (_es[i] = e);
96  }
97 
98  public AryInt set_as( int e ) { _es[0] = e; _len=1; return this; }
99  public AryInt set_len( int len ) {
100  if( len > _len )
101  while( len>= _es.length ) _es = Arrays.copyOf(_es,_es.length<<1);
102  _len = len;
103  while( _es.length > (len<<1) ) // Shrink if hugely too large
104  _es = Arrays.copyOf(_es,_es.length>>1);
105  return this;
106  }
107 
109  public AryInt addAll( Collection<? extends Integer> c ) { for( int e : c ) push(e); return this; }
110 
112  public AryInt addAll( int[] es ) {
113  if( es.length==0 ) return this;
114  while( _len+es.length > _es.length ) _es = Arrays.copyOf(_es,_es.length<<1);
115  System.arraycopy(es,0,_es,_len,es.length);
116  _len += es.length;
117  return this;
118  }
119 
120  public AryInt map_update( IntUnaryOperator f ) { for( int i = 0; i<_len; i++ ) _es[i] = f.applyAsInt(_es[i]); return this; }
121 
123  public int[] asAry() { return _len==_es.length ? _es : Arrays.copyOf(_es,_len); }
124 
126  public void sort_update() { Arrays.sort(_es, 0, _len); }
131  public int find( int e ) {
132  for( int i=0; i<_len; i++ ) if( _es[i]==e ) return i;
133  return -1;
134  }
135 
136  @Override public String toString() {
137  SB sb = new SB().p('[');
138  for( int i=0; i<_len; i++ )
139  sb.p(_es[i]).p(',');
140  return sb.unchar().p(']').toString();
141  }
142 
143  private void range_check( int i ) {
144  if( i < 0 || i>=_len )
145  throw new ArrayIndexOutOfBoundsException(""+i+" >= "+_len);
146  }
147 
148  // Binary search sorted _es. Returns insertion point.
149  // Undefined results if _es is not sorted.
150  public int binary_search( int e ) {
151  int lo=0, hi=_len-1;
152  while( lo <= hi ) {
153  int mid = (hi + lo) >>> 1; // midpoint, rounded down
154  int mval = _es[mid];
155  if( e==mval ) {
156  // If dups, get to the first.
157  while( mid>0 && e==_es[mid-1] ) mid--;
158  return mid;
159  }
160  if( e >mval ) lo = mid+1;
161  else hi = mid-1;
162  }
163  return lo;
164  }
165 
166  // Note that the hashCode() and equals() are not invariant to changes in the
167  // underlying array. If the hashCode() is used (e.g., inserting into a
168  // HashMap) and the then the array changes, the hashCode() will change also.
169  @Override public boolean equals( Object o ) {
170  if( this==o ) return true;
171  if( !(o instanceof AryInt) ) return false;
172  AryInt ary = (AryInt)o;
173  if( _len != ary._len ) return false;
174  if( _es == ary._es ) return true;
175  for( int i=0; i<_len; i++ )
176  if( _es[i] != ary._es[i] )
177  return false;
178  return true;
179  }
180  @Override public int hashCode( ) {
181  int sum=_len;
182  for( int i=0; i<_len; i++ )
183  sum += _es[i];
184  return sum;
185  }
186 }
com.cliffc.aa.util.AryInt.atX
int atX(int i)
Definition: AryInt.java:27
com.cliffc.aa.util.AryInt.set_len
AryInt set_len(int len)
Definition: AryInt.java:99
com.cliffc.aa.util.AryInt.insert
void insert(int i, int e)
Slow, linear-time, element insert.
Definition: AryInt.java:55
com.cliffc.aa.util.AryInt.isEmpty
boolean isEmpty()
Definition: AryInt.java:16
com.cliffc.aa.util.AryInt.len
int len()
Definition: AryInt.java:18
com.cliffc.aa.util.AryInt.equals
boolean equals(Object o)
Definition: AryInt.java:169
com.cliffc.aa.util.AryInt.sort_update
void sort_update()
Sorts in-place.
Definition: AryInt.java:126
com.cliffc.aa.util.SB.unchar
SB unchar()
Definition: SB.java:58
com.cliffc.aa.util.AryInt.range_check
void range_check(int i)
Definition: AryInt.java:143
com.cliffc.aa.util.AryInt.AryInt
AryInt()
Definition: AryInt.java:13
com.cliffc.aa.util.AryInt.toString
String toString()
Definition: AryInt.java:136
com.cliffc.aa.util.AryInt.find
int find(int e)
Find the first matching element using ==, or -1 if none.
Definition: AryInt.java:131
com.cliffc.aa.util.AryInt.addAll
AryInt addAll(int[] es)
Definition: AryInt.java:112
com.cliffc.aa.util.AryInt.pop
int pop()
Definition: AryInt.java:37
com.cliffc.aa.util.AryInt._es
int[] _es
Definition: AryInt.java:9
com.cliffc.aa.util.AryInt.push
AryInt push(int e)
Add element in amortized constant time.
Definition: AryInt.java:45
com.cliffc.aa.util.AryInt.binary_search
int binary_search(int e)
Definition: AryInt.java:150
com.cliffc.aa.util.SB
Tight/tiny StringBuilder wrapper.
Definition: SB.java:8
com.cliffc.aa.util.AryInt.hashCode
int hashCode()
Definition: AryInt.java:180
com.cliffc.aa.util.AryInt.at
int at(int i)
Definition: AryInt.java:21
com.cliffc.aa.util.AryInt.AryInt
AryInt(int[] es, int len)
Definition: AryInt.java:12
com.cliffc.aa.util.AryInt.last
int last()
Definition: AryInt.java:31
com.cliffc.aa.util.SB.p
SB p(String s)
Definition: SB.java:13
com.cliffc.aa.util.AryInt.addAll
AryInt addAll(Collection<? extends Integer > c)
Definition: AryInt.java:109
com.cliffc.aa.util.AryInt.clear
void clear()
Remove all elements.
Definition: AryInt.java:84
com.cliffc.aa.util.AryInt.del
int del(int i)
Fast, constant-time, element removal.
Definition: AryInt.java:66
com.cliffc.aa.util.AryInt.map_update
AryInt map_update(IntUnaryOperator f)
Definition: AryInt.java:120
com.cliffc.aa.util.AryInt.AryInt
AryInt(int[] es)
Definition: AryInt.java:11
com.cliffc.aa.util.AryInt.setX
int setX(int i, int e)
Definition: AryInt.java:87
com.cliffc.aa.util.AryInt
Definition: AryInt.java:8
com.cliffc.aa.util.AryInt.asAry
int[] asAry()
Definition: AryInt.java:123
com.cliffc.aa.util.AryInt._len
int _len
Definition: AryInt.java:10
com.cliffc.aa.util.AryInt.set_as
AryInt set_as(int e)
Definition: AryInt.java:98
com.cliffc.aa.util.SB.toString
String toString()
Definition: SB.java:62