1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors definitions generic generic.single kernel
4 namespaces words math math.order combinators sequences
5 generic.single.private quotations kernel.private
6 assocs arrays layouts ;
9 TUPLE: standard-combination < single-combination # ;
11 : <standard-combination> ( n -- standard-combination )
12 dup 0 2 between? [ "Bad dispatch position" throw ] unless
13 standard-combination boa ;
15 PREDICATE: standard-generic < generic
16 "combination" word-prop standard-combination? ;
18 PREDICATE: simple-generic < standard-generic
19 "combination" word-prop #>> 0 = ;
21 CONSTANT: simple-combination T{ standard-combination f 0 }
23 : define-simple-generic ( word effect -- )
24 [ simple-combination ] dip define-generic ;
26 : (picker) ( n -- quot )
31 [ 1 - (picker) [ dip swap ] curry ]
34 M: standard-combination picker
35 combination get #>> (picker) ;
37 M: standard-combination dispatch# #>> ;
39 M: standard-generic effective-method
40 [ datastack ] dip [ "combination" word-prop #>> swap <reversed> nth ] keep
43 M: standard-combination inline-cache-quot ( word methods -- )
44 #! Direct calls to the generic word (not tail calls or indirect calls)
45 #! will jump to the inline cache entry point instead of the megamorphic
46 #! dispatch entry point.
47 combination get #>> [ { } inline-cache-miss ] 3curry [ ] like ;
49 : make-empty-cache ( -- array )
50 mega-cache-size get f <array> ;
52 M: standard-combination mega-cache-quot
53 combination get #>> make-empty-cache [ mega-cache-lookup ] 3curry [ ] like ;
55 M: standard-generic definer drop \ GENERIC# f ;
57 M: simple-generic definer drop \ GENERIC: f ;