Go to the documentation of this file. 1 package com.cliffc.aa.type;
6 import java.util.concurrent.ConcurrentHashMap;
7 import java.util.function.Predicate;
95 static private int CNT=1;
104 @SuppressWarnings(
"unchecked")
105 protected T
init(
byte type, String name) {
_type=type;
_name=name;
return (T)
this; }
112 @Override
public boolean equals( Object o ) {
113 if(
this == o )
return true;
114 if( !(o instanceof
Type) )
return false;
137 if( t0==t1 )
return true;
138 if( t0==
null || t1==
null )
return false;
139 if( t0.length != t1.length )
return false;
140 for(
int i=0; i<t0.length; i++ )
148 Pool P =
POOLS[type];
149 Type t1 = P.malloc();
150 return t1.
init(type,
"").hashcons_free();
152 @SuppressWarnings(
"unchecked")
155 return this==t2 ? t2 : (T)
POOLS[
_type].free(
this,t2);
162 private static final ConcurrentHashMap<Type,Type>
INTERN =
new ConcurrentHashMap<>();
164 @SuppressWarnings(
"unchecked")
167 T t2 = (T)
INTERN.get(
this);
169 assert t2._dual !=
null;
180 d._hash = d.compute_hash();
182 if(
this==d )
return d;
184 assert d._dual==
null;
185 assert
INTERN.get(d)==
null;
192 @SuppressWarnings(
"unchecked")
195 assert
INTERN.get(
this)==
this;
200 @SuppressWarnings(
"unchecked")
202 assert
_dual._dual ==
this;
204 assert
INTERN.get(
this)==
null;
206 assert
INTERN.get(
this)==
this;
216 if( !k.intern_check0(v) ) {
217 System.out.println(
"INTERN_CHECK FAIL: "+k._uid+
":"+k+
" vs "+v.
_uid+
":"+v);
262 private static final String[]
STRS =
new String[]{
"all",
"any",
"Ctrl",
"~Ctrl",
"Scalar",
"~Scalar",
"nScalar",
"~nScalar",
"Real",
"~Real",
"nReal",
"~nReal",
"nil",
"0"};
282 @SuppressWarnings(
"unchecked")
284 private int _malloc, _free,
_pool, _clone;
294 if( _frees.
isEmpty() ) { _malloc++; _clone--;
return (T)_gold.
clone(); }
295 else { _pool ++;
return (T)_frees.
pop(); }
303 @SuppressWarnings(
"unchecked")
315 catch( CloneNotSupportedException cns ) {
throw new RuntimeException(cns); }
355 private static final byte[]
ISA_SCALAR =
new byte[]{0,0,0,0,1,1,1,1,1,1,1,1,1,1,0, 1,1,1,0,0,0,0,0,0,0,1,1,0,0};
364 @SuppressWarnings(
"unchecked")
370 private static class Key {
390 int hash = k.hashCode();
391 Integer ii = hashs.
get(hash);
392 hashs.
put(hash,ii==
null ? 1 : ii+1);
394 int[] hist =
new int[16];
397 for(
long l : hashs.
keySet() ) {
398 int reps = hashs.
get(l);
399 if( reps > maxval ) { maxval=reps; maxkey=l; }
400 if( reps < hist.length ) hist[reps]++;
401 else System.out.println(
"hash "+l+
" repeats "+reps);
403 for(
int i=0; i<hist.length; i++ )
405 System.out.println(
"Number of hashes with "+i+
" repeats: "+hist[i]);
406 System.out.println(
"Max repeat key "+maxkey+
" repeats: "+maxval);
414 if( t ==
this )
return this;
416 Type mt = Key.get(
this,t);
417 if( mt !=
null )
return mt;
500 boolean that_oop = t.
is_ptr();
501 boolean that_num = t.
is_num();
502 assert !(that_oop&&that_num);
508 if( that_num || t==
NIL || t==
XNIL ) {
548 static boolean check_name( String n ) {
return n.isEmpty() || n.charAt(n.length()-1)==
':'; }
555 @SuppressWarnings(
"unchecked")
557 @SuppressWarnings(
"unchecked")
562 return t1.hashcons_free();
568 Type t0 =
this, t1 = t;
569 String s0 = t0.
_name, s1 = t1._name;
571 if(
Util.
eq(s0,s1) )
return s0;
573 if( s0.length() > s1.length() ) { t1=
this; t0=t; s0=t0.
_name; s1=t1._name; }
574 int x = 0, i;
char c;
576 for( i = 0; i < s0.length(); i++ ) {
577 if( (c=s0.charAt(i)) != s1.charAt(i) )
585 String s2 = i==s0.length() ? s0 : s0.substring(0, x).intern();
594 if( t==
this )
return true;
601 if( mt==nmt2 )
return true;
602 System.out.println(
"Meet not commutative: "+
this+
".meet("+t+
")="+mt+
",\n but "+t+
".meet("+
this+
")="+nmt2);
609 if( t==
this )
return true;
613 System.err.print(
"("+
this+
" & "+t+
")=="+mt+
" but \n("+mt.
_dual+
" & ");
614 if( ta!=t.
_dual ) System.err.println(t.
_dual+
")=="+ta+
" \nwhich is not "+t.
_dual);
615 else System.err.println(
_dual+
")=="+tb+
" \nwhich is not "+
_dual);
642 public static void init0( HashMap<String,Type> types ) {
643 types.put(
"real",
REAL);
644 types.put(
"scalar",
SCALAR);
671 for(
int i=0; i<ts.
_len; i++ )
672 for(
int j=i+1; j<ts.
_len; j++ )
677 for(
Type t1 : ts1 ) {
678 assert !t1.above_center();
680 if( t1!=t1.dual() ) ts.
push(t1.dual());
689 for(
Type t1 : ts ) {
692 assert t0.check_symmetric(t1,mt);
699 for(
Type t2 : ts ) {
704 if( t01_2 != t0_12 && errs++ < 10 )
705 System.err.println(
"("+t0+
"&"+t1+
") & "+t2+
" == "+t0+
" & ("+t1+
" & "+t2+
"); "+
706 "("+t01 +
") & "+t2+
" == "+t0+
" & ("+t12 +
"); "+
707 t01_2 +
" == "+t0_12);
709 assert errs==0 :
"Found "+errs+
" associative errors";
714 for(
Type t1 : ts ) {
716 for(
Type t2 : ts ) {
720 if( mt != t12 && errs++ < 10 ) {
721 System.err.println(
"("+t0+
" ^ "+t2+
") = "+t02+
"; "+
722 "("+t1+
" ^ "+t2+
") = "+t12+
"; "+
723 "their meet = "+mt+
" which is not "+t12);
728 assert errs==0 :
"Found "+errs+
" non-join-type errors";
755 default:
throw typerr(
null);
772 default:
throw typerr(
null);
793 default:
throw typerr(
null);
839 default:
throw typerr(
null);
861 default:
throw typerr(
null);
885 default:
throw typerr(
null);
901 default:
throw typerr(
null);
910 case TXNIL:
return nil;
921 default:
throw typerr(
null);
948 throw new RuntimeException(
"Should not reach here: internal type system error with "+
this+(t==
null?
"":(
" and "+t)));
static final Type[] TYPES
boolean intern_check0(Type v)
static final byte TMEMPTR
static void init1(HashMap< String, Type > types)
E push(E e)
Add element in amortized constant time.
A lock-free alternate implementation of java.util.concurrent.ConcurrentHashMap with better scaling pr...
< T extends Type > T free(T t1, T t2)
RuntimeException typerr(Type t)
Memory type; the state of all of memory; memory edges order memory ops.
static boolean eq(String s0, String s1)
static final Type XSCALAR
static final TypeRPC ALL_CALL
boolean check_commute(Type t, Type mt)
static final TypeFunPtr GENERIC_FUNPTR
static void init1(HashMap< String, Type > types)
an implementation of language AA
boolean contains(Type t, VBitSet bs)
static void intern_meet_quality_check()
TypeStruct repeats_in_cycles(TypeStruct head, VBitSet bs)
Type xmt_name(Type t, Type mt)
static NonBlockingHashMap< Key, Type > INTERN_MEET
static boolean check_name(String n)
static final TypeFunPtr[] TYPES
A memory-based collection of optionally named fields.
static RuntimeException unimpl()
SB str(SB sb, VBitSet dups, TypeMem mem, boolean debug)
static final byte TFUNPTR
static boolean intern_check()
static final TypeMem ANYMEM
static final byte[] ISA_SCALAR
boolean cycle_equals(Type t)
static Ary< Type > ALL_TYPES
Set< Long > keySet()
Returns a Set view of the keys contained in this map; with care the keys may be iterated over without...
static final byte TXNREAL
static final TypeInt INT64
final T set_name(String name)
final TypeV get(long key)
Returns the value to which the specified key is mapped, or.
static final byte TSTRUCT
static void init0(HashMap< String, Type > types)
static int RECURSIVE_MEET
static final Type XNSCALR
final boolean contains(Type t)
final Type cross_nil(Type t)
Type make_from(Type head, TypeMem map, VBitSet visit)
static final ConcurrentHashMap< Type, Type > INTERN
final String mtname(Type t, Type mt)
T init(byte type, String name)
boolean check_symmetric(Type t, Type mt)
static final byte TSCALAR
static final Type[] TYPES
static final byte TXSCALAR
static Type intern_find(int uid)
static void put(Type a, Type b, Type mt)
static boolean check_startup()
Tight/tiny StringBuilder wrapper.
static final byte TSIMPLE
an implementation of language AA
static Ary< Type > ALL_TYPES()
Type oop_deep_impl(Type t)
static Type[] SCALAR_PRIMS
TypeMemPtr oob(TypeMemPtr e)
static final TypeStruct[] TYPES
static Type make(byte type)
void walk(Predicate< Type > p)
static final TypeFunSig[] TYPES
final boolean isa_scalar()
static final String[] STRS
A lock-free alternate implementation of java.util.concurrent.ConcurrentHashMap with primitive long ke...
TypeStruct oob(TypeStruct e)
static final Pool[] POOLS
TypeV put(long key, TypeV val)
Maps the specified key to the specified value in the table.
static void concat(Ary< Type > ts, Type[] ts1)
static final byte TNSCALR
static final TypeMemPtr OOP0
< T extends Type > T malloc()
static boolean eq(Type[] t0, Type[] t1)
static void init1(HashMap< String, Type > types)
static final TypeInt BOOL
static final byte TXNSCALR
static final byte TFUNSIG
static final TypeFlt FLT64