2009-04-24 21:43:01 -04:00
|
|
|
! Copyright (C) 2009 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-04-24 21:43:01 -04:00
|
|
|
USING: accessors definitions generic generic.single kernel
|
2009-04-28 19:17:28 -04:00
|
|
|
namespaces words math math.order combinators sequences
|
2009-04-30 04:37:07 -04:00
|
|
|
generic.single.private quotations kernel.private
|
2009-04-30 05:14:14 -04:00
|
|
|
assocs arrays layouts ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: generic.standard
|
|
|
|
|
2009-04-24 21:43:01 -04:00
|
|
|
TUPLE: standard-combination < single-combination # ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-04-28 18:56:15 -04:00
|
|
|
: <standard-combination> ( n -- standard-combination )
|
|
|
|
dup 0 2 between? [ "Bad dispatch position" throw ] unless
|
|
|
|
standard-combination boa ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 03:44:10 -04:00
|
|
|
PREDICATE: standard-generic < generic
|
|
|
|
"combination" word-prop standard-combination? ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 03:44:10 -04:00
|
|
|
PREDICATE: simple-generic < standard-generic
|
2009-04-24 21:43:01 -04:00
|
|
|
"combination" word-prop #>> 0 = ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-03-22 19:00:26 -04:00
|
|
|
CONSTANT: simple-combination T{ standard-combination f 0 }
|
|
|
|
|
|
|
|
: define-simple-generic ( word effect -- )
|
|
|
|
[ simple-combination ] dip define-generic ;
|
2008-04-02 03:44:10 -04:00
|
|
|
|
2009-04-24 21:43:01 -04:00
|
|
|
: (picker) ( n -- quot )
|
|
|
|
{
|
|
|
|
{ 0 [ [ dup ] ] }
|
|
|
|
{ 1 [ [ over ] ] }
|
|
|
|
{ 2 [ [ pick ] ] }
|
2009-04-29 22:32:05 -04:00
|
|
|
[ 1- (picker) [ dip swap ] curry ]
|
2009-04-24 21:43:01 -04:00
|
|
|
} case ;
|
2008-04-02 03:44:10 -04:00
|
|
|
|
2009-04-24 21:43:01 -04:00
|
|
|
M: standard-combination picker
|
|
|
|
combination get #>> (picker) ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-04-02 22:27:49 -04:00
|
|
|
M: standard-combination dispatch# #>> ;
|
|
|
|
|
2009-04-25 20:41:27 -04:00
|
|
|
M: standard-generic effective-method
|
|
|
|
[ datastack ] dip [ "combination" word-prop #>> swap <reversed> nth ] keep
|
|
|
|
(effective-method) ;
|
|
|
|
|
2009-04-30 04:37:07 -04:00
|
|
|
M: standard-combination inline-cache-quot ( word methods -- )
|
2009-04-28 19:17:28 -04:00
|
|
|
#! Direct calls to the generic word (not tail calls or indirect calls)
|
|
|
|
#! will jump to the inline cache entry point instead of the megamorphic
|
|
|
|
#! dispatch entry point.
|
2009-04-29 22:32:05 -04:00
|
|
|
combination get #>> [ f inline-cache-miss ] 3curry [ ] like ;
|
2009-04-28 19:17:28 -04:00
|
|
|
|
2009-04-30 04:37:07 -04:00
|
|
|
: make-empty-cache ( -- array )
|
2009-04-30 05:14:14 -04:00
|
|
|
mega-cache-size get f <array> ;
|
2009-04-30 04:37:07 -04:00
|
|
|
|
|
|
|
M: standard-combination mega-cache-quot
|
|
|
|
combination get #>> make-empty-cache [ mega-cache-lookup ] 3curry [ ] like ;
|
|
|
|
|
2009-04-25 20:41:27 -04:00
|
|
|
M: standard-generic definer drop \ GENERIC# f ;
|
2008-01-06 11:13:44 -05:00
|
|
|
|
2009-04-25 20:41:27 -04:00
|
|
|
M: simple-generic definer drop \ GENERIC: f ;
|