aa
TypeFlds.java
Go to the documentation of this file.
1 package com.cliffc.aa.type;
2 
3 import com.cliffc.aa.util.Ary;
4 import com.cliffc.aa.util.IHashMap;
5 
6 // Class to make hashcons Type[].
7 // Bug to change after interning.
8 public class TypeFlds {
9  // Lazy expanding list of TypeAry customed to handle various Type[] lengths.
10  private static final Ary<TypeFlds> TYPEARY = new Ary<>(new TypeFlds[1],0);
11 
12  // Make a TypeAry to handle Type[] of length 'len'
13  private static TypeFlds tary( int len) {
14  TypeFlds tary = TYPEARY.atX(len);
15  return tary==null ? TYPEARY.setX(len,new TypeFlds(len)) : tary;
16  }
17 
18  private static final Key K = new Key(null,0);
19 
20  // Wrapper to customize array.equals
21  private static class Key {
23  int _hash;
24  private Key(TypeFld[] ts, int hash) { _ts=ts; _hash = hash; }
25  private static int hash( Type[] ts ) {
26  int hash = 0;
27  for( Type t : ts ) hash += t._hash;
28  return hash;
29  }
30  @Override public int hashCode() { return _hash; }
31  @Override public boolean equals(Object o) {
32  if( !(o instanceof Key) ) return false;
33  Type[] ts = ((Key)o)._ts;
34  // This series of tests is NOT the same as Arrays.equals(), since it
35  // bottoms out in a pointer-equality test instead of 'equals'.
36  if( _ts==ts ) return true;
37  if( _ts.length != ts.length ) return false;
38  for( int i=0; i<ts.length; i++ )
39  if( _ts[i]!=ts[i] )
40  return false;
41  return true;
42  }
43  }
44 
45  private final int _len; // Length of arrays being handled
46  private final IHashMap _intern = new IHashMap();
47  private final Ary<TypeFld[]> _free = new Ary<>(new TypeFld[1][],0);
48  private TypeFlds( int len ) { _len=len; }
49 
50  private boolean check_(TypeFld[] ts) {
51  K._ts=ts;
52  K._hash = Key.hash(ts);
53  Key k2 = _intern.get(K);
54  return k2._ts==ts;
55  }
56 
57 
58  // Return a free Type[]
59  private TypeFld[] get() {
60  if( _free.isEmpty() )
61  _free.push(new TypeFld[_len]);
62  return _free.pop();
63  }
64 
65  private TypeFld[] hash_cons_(TypeFld[] ts) {
66  K._ts=ts;
67  K._hash = Key.hash(ts);
68  Key k2 = _intern.get(K);
69  if( k2 != null ) {
70  if( k2._ts!=ts ) _free.push(ts);
71  return k2._ts;
72  }
73  _intern.put(new Key(ts,K._hash));
74  return ts;
75  }
76 
77 
78 
79  public static TypeFld[] get(int len) { return tary(len).get(); }
80  public static void free(TypeFld[] ts) { tary(ts.length)._free.push(ts); }
81  public static TypeFld[] hash_cons(TypeFld[] ts) { return tary(ts.length).hash_cons_(ts); }
82  public static TypeFld[] ts(TypeFld t0) {
83  TypeFlds t1 = tary(1);
84  TypeFld[] ts = t1.get();
85  ts[0] = t0;
86  return ts;
87  }
88  public static TypeFld[] ts(TypeFld t0, TypeFld t1) {
89  TypeFlds t2 = tary(2);
90  TypeFld[] ts = t2.get();
91  ts[0] = t0;
92  ts[1] = t1;
93  return ts;
94  }
95  public static TypeFld[] ts(TypeFld t0, TypeFld t1, TypeFld t2) {
96  TypeFlds t3 = tary(3);
97  TypeFld[] ts = t3.get();
98  ts[0] = t0;
99  ts[1] = t1;
100  ts[2] = t2;
101  return ts;
102  }
103  public static TypeFld[] ts(TypeFld t0, TypeFld t1, TypeFld t2, TypeFld t3) {
104  TypeFlds t4 = tary(4);
105  TypeFld[] ts = t4.get();
106  ts[0] = t0;
107  ts[1] = t1;
108  ts[2] = t2;
109  ts[3] = t3;
110  return ts;
111  }
112 
113  // Result not interned; suitable for direct hacking.
114  // All TypeFlds are cloned new, not-interned and suitable for direct hacking.
115  // Original assumed in-use, not freed.
116  public static TypeFld[] clone(TypeFld[] ts) {
117  TypeFld[] ts2 = tary(ts.length).get();
118  for( int i=0; i<ts.length; i++ )
119  ts2[i] = TypeFld.malloc(ts[i]._fld,ts[i]._t,ts[i]._access,ts[i]._order);
120  return ts2;
121  }
122 
123  // Result not interned; suitable for direct hacking.
124  public static TypeFld[] copyOf(TypeFld[] flds, int len) {
125  TypeFld[] flds2 = tary(len).get();
126  System.arraycopy(flds,0,flds2,0,Math.min(flds.length,len));
127  return flds2;
128  }
129 }
com.cliffc.aa.type.TypeFlds.get
TypeFld[] get()
Definition: TypeFlds.java:59
com.cliffc.aa.util.IHashMap.get
public< T > T get(T key)
Definition: IHashMap.java:11
com.cliffc.aa.type.TypeFlds.TypeFlds
TypeFlds(int len)
Definition: TypeFlds.java:48
com.cliffc.aa.type.TypeFld._order
int _order
Definition: TypeFld.java:18
com.cliffc
com.cliffc.aa.type.TypeFlds._free
final Ary< TypeFld[]> _free
Definition: TypeFlds.java:47
com.cliffc.aa.type.TypeFld
Definition: TypeFld.java:12
com.cliffc.aa.util
Definition: AbstractEntry.java:1
com.cliffc.aa.type.Type
an implementation of language AA
Definition: Type.java:94
com.cliffc.aa.type.TypeFlds.K
static final Key K
Definition: TypeFlds.java:18
com.cliffc.aa.util.Ary
Definition: Ary.java:11
com.cliffc.aa.util.IHashMap
Definition: IHashMap.java:7
com.cliffc.aa.type.TypeFlds.Key._ts
TypeFld[] _ts
Definition: TypeFlds.java:22
com.cliffc.aa.type.TypeFlds.copyOf
static TypeFld[] copyOf(TypeFld[] flds, int len)
Definition: TypeFlds.java:124
com.cliffc.aa.type.TypeFlds.Key._hash
int _hash
Definition: TypeFlds.java:23
com.cliffc.aa.type.TypeFlds
Definition: TypeFlds.java:8
com.cliffc.aa.type.TypeFlds.ts
static TypeFld[] ts(TypeFld t0, TypeFld t1)
Definition: TypeFlds.java:88
com.cliffc.aa.type.TypeFlds.TYPEARY
static final Ary< TypeFlds > TYPEARY
Definition: TypeFlds.java:10
com.cliffc.aa.type.TypeFlds.hash_cons_
TypeFld[] hash_cons_(TypeFld[] ts)
Definition: TypeFlds.java:65
com.cliffc.aa.type.TypeFld._access
Access _access
Definition: TypeFld.java:17
com.cliffc.aa.type.TypeFlds.ts
static TypeFld[] ts(TypeFld t0, TypeFld t1, TypeFld t2)
Definition: TypeFlds.java:95
com.cliffc.aa.type.TypeFlds.check_
boolean check_(TypeFld[] ts)
Definition: TypeFlds.java:50
com.cliffc.aa.type.TypeFld._fld
String _fld
Definition: TypeFld.java:15
com.cliffc.aa.type.TypeFlds.ts
static TypeFld[] ts(TypeFld t0, TypeFld t1, TypeFld t2, TypeFld t3)
Definition: TypeFlds.java:103
com.cliffc.aa.type.TypeFld.malloc
static TypeFld malloc(String fld, Type t, Access access, int order)
Definition: TypeFld.java:53
com.cliffc.aa.type.TypeFlds.tary
static TypeFlds tary(int len)
Definition: TypeFlds.java:13
com.cliffc.aa
Definition: AA.java:1
com.cliffc.aa.type.TypeFlds._intern
final IHashMap _intern
Definition: TypeFlds.java:46
com.cliffc.aa.type.TypeFlds.Key.hashCode
int hashCode()
Definition: TypeFlds.java:30
com.cliffc.aa.type.TypeFlds.Key.Key
Key(TypeFld[] ts, int hash)
Definition: TypeFlds.java:24
com.cliffc.aa.type.TypeFlds.clone
static TypeFld[] clone(TypeFld[] ts)
Definition: TypeFlds.java:116
com.cliffc.aa.type.TypeFlds.Key.equals
boolean equals(Object o)
Definition: TypeFlds.java:31
com.cliffc.aa.type.TypeFld._t
Type _t
Definition: TypeFld.java:16
com.cliffc.aa.type.TypeFlds.Key
Definition: TypeFlds.java:21
com.cliffc.aa.type.TypeFlds.hash_cons
static TypeFld[] hash_cons(TypeFld[] ts)
Definition: TypeFlds.java:81
com.cliffc.aa.type.TypeFlds.Key.hash
static int hash(Type[] ts)
Definition: TypeFlds.java:25
com.cliffc.aa.type.TypeFlds._len
final int _len
Definition: TypeFlds.java:45
com.cliffc.aa.type.TypeFlds.free
static void free(TypeFld[] ts)
Definition: TypeFlds.java:80
com
com.cliffc.aa.util.IHashMap.put
public< T > T put(T kv)
Definition: IHashMap.java:9
com.cliffc.aa.type.TypeFlds.ts
static TypeFld[] ts(TypeFld t0)
Definition: TypeFlds.java:82