diff src/clojure/lang/MethodImplCache.java @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/lang/MethodImplCache.java	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,66 @@
     1.4 +/**
     1.5 + *   Copyright (c) Rich Hickey. All rights reserved.
     1.6 + *   The use and distribution terms for this software are covered by the
     1.7 + *   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
     1.8 + *   which can be found in the file epl-v10.html at the root of this distribution.
     1.9 + *   By using this software in any fashion, you are agreeing to be bound by
    1.10 + * 	 the terms of this license.
    1.11 + *   You must not remove this notice, or any other, from this software.
    1.12 + **/
    1.13 +
    1.14 +/* rich Nov 8, 2009 */
    1.15 +
    1.16 +package clojure.lang;
    1.17 +
    1.18 +public final class MethodImplCache{
    1.19 +
    1.20 +static public class Entry{
    1.21 +	final public Class c;
    1.22 +	final public IFn fn;
    1.23 +
    1.24 +	public Entry(Class c, IFn fn){
    1.25 +		this.c = c;
    1.26 +		this.fn = fn;
    1.27 +	}
    1.28 +}
    1.29 +
    1.30 +public final IPersistentMap protocol;
    1.31 +public final Keyword methodk;
    1.32 +public final int shift;
    1.33 +public final int mask;
    1.34 +public final Object[] table;    //[class, entry. class, entry ...]
    1.35 +
    1.36 +volatile Entry mre = null;
    1.37 +
    1.38 +public MethodImplCache(IPersistentMap protocol, Keyword methodk){
    1.39 +	this(protocol, methodk, 0, 0, RT.EMPTY_ARRAY);
    1.40 +}
    1.41 +
    1.42 +public MethodImplCache(IPersistentMap protocol, Keyword methodk, int shift, int mask, Object[] table){
    1.43 +	this.protocol = protocol;
    1.44 +	this.methodk = methodk;
    1.45 +	this.shift = shift;
    1.46 +	this.mask = mask;
    1.47 +	this.table = table;
    1.48 +}
    1.49 +
    1.50 +public IFn fnFor(Class c){
    1.51 +	Entry last = mre;
    1.52 +	if(last != null && last.c == c)
    1.53 +		return last.fn;
    1.54 +	return findFnFor(c);
    1.55 +}
    1.56 +
    1.57 +IFn findFnFor(Class c){
    1.58 +	int idx = ((Util.hash(c) >> shift) & mask) << 1;
    1.59 +	if(idx < table.length && table[idx] == c)
    1.60 +		{
    1.61 +		Entry e = ((Entry) table[idx + 1]);
    1.62 +		mre = e;
    1.63 +		return  e != null ? e.fn : null;
    1.64 +		}
    1.65 +	return null;
    1.66 +}
    1.67 +
    1.68 +
    1.69 +}