-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathMethodImplCache.java
More file actions
91 lines (76 loc) · 2.18 KB
/
MethodImplCache.java
File metadata and controls
91 lines (76 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Copyright (c) Rich Hickey. All rights reserved.
* The use and distribution terms for this software are covered by the
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
* which can be found in the file epl-v10.html at the root of this distribution.
* By using this software in any fashion, you are agreeing to be bound by
* the terms of this license.
* You must not remove this notice, or any other, from this software.
**/
/* rich Nov 8, 2009 */
package clojure.lang;
import java.util.Map;
public final class MethodImplCache{
static public class Entry{
final public Class c;
final public IFn fn;
public Entry(Class c, IFn fn){
this.c = c;
this.fn = fn;
}
}
public final IPersistentMap protocol;
public final Symbol sym;
public final Keyword methodk;
public final int shift;
public final int mask;
public final Object[] table; //[class, entry. class, entry ...]
public final Map map;
Entry mre = null;
public MethodImplCache(Symbol sym,IPersistentMap protocol, Keyword methodk){
this(sym, protocol, methodk, 0, 0, RT.EMPTY_ARRAY);
}
public MethodImplCache(Symbol sym, IPersistentMap protocol, Keyword methodk, int shift, int mask, Object[] table){
this.sym = sym;
this.protocol = protocol;
this.methodk = methodk;
this.shift = shift;
this.mask = mask;
this.table = table;
this.map = null;
}
public MethodImplCache(Symbol sym, IPersistentMap protocol, Keyword methodk, Map map){
this.sym = sym;
this.protocol = protocol;
this.methodk = methodk;
this.shift = 0;
this.mask = 0;
this.table = null;
this.map = map;
}
public IFn fnFor(Class c){
Entry last = mre;
if(last != null && last.c == c)
return last.fn;
return findFnFor(c);
}
IFn findFnFor(Class c){
if (map != null)
{
Entry e = (Entry) map.get(c);
mre = e;
return e != null ? e.fn : null;
}
else
{
int idx = ((Util.hash(c) >> shift) & mask) << 1;
if(idx < table.length && table[idx] == c)
{
Entry e = ((Entry) table[idx + 1]);
mre = e;
return e != null ? e.fn : null;
}
return null;
}
}
}