Go to the documentation of this file. 1 package com.cliffc.aa.util;
3 import org.jetbrains.annotations.NotNull;
5 import java.lang.reflect.Array;
7 import java.util.function.Function;
8 import java.util.function.Predicate;
14 public Ary(E[] es) {
this(es,es.length); }
15 public Ary(E[] es,
int len) {
if( es.length==0 ) es=Arrays.copyOf(es,1);
_es=es;
_len=
len; }
16 @SuppressWarnings(
"unchecked")
17 public
Ary(Class<E> clazz) {
this((E[]) Array.newInstance(clazz, 1),0); }
25 public E
at(
int i ) {
31 public E
atX(
int i ) {
32 return i <
_len ?
_es[i] :
null;
69 throw new ArrayIndexOutOfBoundsException(
""+i+
" >= "+
_len);
78 public E
del(
int i ) {
89 for(
int i=0; i<
_len; i++ ) {
102 public E
remove(
int i ) {
117 while( i>=
_es.length )
_es = Arrays.copyOf(
_es,
_es.length<<1);
133 public E
set(
int i, E e ) {
144 while(
_es.length > (
len<<1) )
151 public Ary<E> addAll( Collection<? extends E> c ) {
if( c!=
null )
for( E e : c )
add(e);
return this; }
155 if( es==
null || es.length==0 )
return this;
156 while(
_len+es.length >
_es.length )
_es = Arrays.copyOf(
_es,
_es.length<<1);
157 System.arraycopy(es,0,
_es,
_len,es.length);
164 if( c==
null || c.
_len==0 )
return this;
180 for(
int i=0; i<
_len; i++ )
181 if( !P.test(
_es[i]) )
193 for(
int i=0; i<
_len; i++ )
if(
_es[i]==e )
return i;
200 public int find( Predicate<E> P ) {
201 for(
int i=0; i<
_len; i++ )
if( P.test(
_es[i]) )
return i;
209 for(
int i=0; i<
_len; i++ )
if(
_es[i]==old ) {
_es[i]=nnn;
return true; }
229 int cmp = x.compareTo(y);
230 if( cmp<0 ) { res.
add(x); i++; }
231 else if( cmp>0 ) { res.
add(y); j++; }
232 else { res.
add(x); i++; j++; }
254 X x = a0.
_es[i];
if( !filter.test(x) ) { i++;
continue; }
255 X y = a1.
_es[j];
if( !filter.test(y) ) { j++;
continue; }
256 int cmp = cmpr.compare(x,y);
257 if( cmp<0 ) { res.
add(x); i++; }
258 else if( cmp>0 ) { res.
add(y); j++; }
259 else { res.
add(x); i++; j++; }
261 while( i<a0.
_len )
if( filter.test(a0.
_es[i++]) ) res.
add(a0.
_es[i-1]);
262 while( j<a1.
_len )
if( filter.test(a1.
_es[j++]) ) res.
add(a1.
_es[j-1]);
281 int cmp = x.compareTo(y);
283 else if( cmp>0 ) { j++; }
284 else { res.
add(x); i++; j++; }
291 @Override
public Iterator<E>
iterator() {
return new Iter(); }
292 private class Iter implements Iterator<E> {
299 SB sb =
new SB().
p(
'{');
300 for(
int i=0; i<
_len; i++ ) {
308 if( i < 0 || i>=
_len )
309 throw new ArrayIndexOutOfBoundsException(
""+i+
" >= "+
_len);
315 @Override
public boolean equals( Object o ) {
316 if(
this==o )
return true;
317 if( !(o instanceof
Ary) )
return false;
319 if(
_len != ary.
_len )
return false;
320 if(
_es == ary.
_es )
return true;
321 for(
int i=0; i<
_len; i++ )
322 if( !(
_es[i]==
null ? (ary.
_es[i] ==
null) :
_es[i].equals(ary.
_es[i])) )
328 for(
int i=0; i<
_len; i++ )
E push(E e)
Add element in amortized constant time.
Ary< E > set_len(int len)
Ary< E > addAll(Ary<? extends E > c)
void insert(int i, E e)
Slow, linear-time, element insert.
Ary< E > addAll(Collection<? extends E > c)
E del(E e)
Element removal, using '=='.
void clear()
Remove all elements.
public< F extends E > Ary< E > addAll(F[] es)
Ary< E > filter_update(Predicate< E > P)
Ary< E > add(E e)
Add element in amortized constant time.
void clear(int i)
Clear element.
Tight/tiny StringBuilder wrapper.
static< X > Ary< X > merge_or(Ary< X > a0, Ary< X > a1, Comparator< X > cmpr, Predicate< X > filter)
Merge-Or.
static< X extends Comparable< X > Ary< X > merge_or(Ary< X > a0, Ary< X > a1)
Merge-Or.
void sort_update(Comparator<? super E > c)
Sorts in-place.
E del(int i)
Fast, constant-time, element removal.
int find(Predicate< E > P)
Find the first element matching predicate P, or -1 if none.
int find(E e)
Find the first matching element using ==, or -1 if none.
Ary< E > map_update(Function< E, E > f)
boolean replace(E old, E nnn)
Find and replace the first matching element using ==.
static< X extends Comparable< X > Ary< X > merge_and(Ary< X > a0, Ary< X > a1)
Merge-And.