2006-05-10 02:18:25 -04:00
|
|
|
! Copyright (C) 2005, 2006 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
IN: generic
|
2006-05-15 01:01:47 -04:00
|
|
|
USING: arrays errors hashtables kernel kernel-internals
|
2006-05-10 02:18:25 -04:00
|
|
|
math namespaces parser sequences sequences-internals strings
|
|
|
|
vectors words ;
|
|
|
|
|
2006-05-10 03:40:03 -04:00
|
|
|
IN: kernel-internals
|
2005-12-29 19:01:19 -05:00
|
|
|
|
2006-03-21 00:44:19 -05:00
|
|
|
: class-tuple ( object -- class )
|
|
|
|
dup tuple? [ 2 slot ] [ drop f ] if ; inline
|
|
|
|
|
2006-01-08 20:41:31 -05:00
|
|
|
: tuple= ( tuple tuple -- ? )
|
|
|
|
2dup [ array-capacity ] 2apply number= [
|
|
|
|
dup array-capacity
|
|
|
|
[ 2dup swap array-nth >r pick array-nth r> = ] all? 2nip
|
2005-12-29 19:01:19 -05:00
|
|
|
] [
|
|
|
|
2drop f
|
2006-05-12 17:07:56 -04:00
|
|
|
] if ;
|
2005-12-29 19:01:19 -05:00
|
|
|
|
2006-03-21 00:44:19 -05:00
|
|
|
: tuple-hashcode ( n tuple -- n )
|
2006-05-10 03:40:03 -04:00
|
|
|
dup class-tuple hashcode >r >r 1-
|
|
|
|
r> 4 slot hashcode* r> bitxor ;
|
2006-03-21 00:44:19 -05:00
|
|
|
|
2005-08-02 06:32:48 -04:00
|
|
|
IN: generic
|
2005-02-20 19:03:37 -05:00
|
|
|
|
2006-05-10 03:40:03 -04:00
|
|
|
: class ( object -- class )
|
|
|
|
dup tuple? [ 2 slot ] [ type type>class ] if ; inline
|
|
|
|
|
2005-01-29 16:39:30 -05:00
|
|
|
: tuple-predicate ( word -- )
|
2005-07-28 18:20:31 -04:00
|
|
|
dup predicate-word
|
2005-08-25 15:27:38 -04:00
|
|
|
[ \ class-tuple , over literalize , \ eq? , ] [ ] make
|
2005-08-03 18:47:32 -04:00
|
|
|
define-predicate ;
|
2005-01-29 16:39:30 -05:00
|
|
|
|
2005-05-15 21:17:56 -04:00
|
|
|
: forget-tuple ( class -- )
|
2006-05-10 20:32:04 -04:00
|
|
|
dup forget "predicate" word-prop first [ forget ] when* ;
|
2005-05-15 21:17:56 -04:00
|
|
|
|
2005-02-09 22:35:11 -05:00
|
|
|
: check-shape ( word slots -- )
|
2005-12-17 09:55:00 -05:00
|
|
|
>r in get lookup dup [
|
2005-03-08 22:54:59 -05:00
|
|
|
dup "tuple-size" word-prop r> length 2 + =
|
2005-09-24 15:21:17 -04:00
|
|
|
[ drop ] [ forget-tuple ] if
|
2005-02-09 22:35:11 -05:00
|
|
|
] [
|
|
|
|
r> 2drop
|
2005-09-24 15:21:17 -04:00
|
|
|
] if ;
|
2005-02-09 22:35:11 -05:00
|
|
|
|
2006-05-05 21:41:57 -04:00
|
|
|
: delegate-slots { { 3 object delegate set-delegate } } ;
|
2005-08-19 21:46:12 -04:00
|
|
|
|
2005-02-20 19:03:37 -05:00
|
|
|
: tuple-slots ( tuple slots -- )
|
2005-03-26 20:12:14 -05:00
|
|
|
2dup "slot-names" set-word-prop
|
2005-03-08 22:54:59 -05:00
|
|
|
2dup length 2 + "tuple-size" set-word-prop
|
2005-07-25 17:13:35 -04:00
|
|
|
dupd 4 simple-slots
|
2005-08-19 21:46:12 -04:00
|
|
|
2dup delegate-slots swap append "slots" set-word-prop
|
2005-07-25 17:13:35 -04:00
|
|
|
define-slots ;
|
2005-02-10 15:14:20 -05:00
|
|
|
|
2005-10-13 00:30:44 -04:00
|
|
|
PREDICATE: word tuple-class "tuple-size" word-prop ;
|
|
|
|
|
|
|
|
: check-tuple-class ( class -- )
|
|
|
|
tuple-class? [ "Not a tuple class" throw ] unless ;
|
|
|
|
|
2005-08-13 23:39:46 -04:00
|
|
|
: define-constructor ( word class def -- )
|
2005-10-13 00:30:44 -04:00
|
|
|
over check-tuple-class >r [
|
2005-08-03 23:56:28 -04:00
|
|
|
dup literalize , "tuple-size" word-prop , \ make-tuple ,
|
2005-08-25 15:27:38 -04:00
|
|
|
] [ ] make r> append define-compound ;
|
2005-02-10 15:14:20 -05:00
|
|
|
|
|
|
|
: default-constructor ( tuple -- )
|
2006-01-09 17:56:19 -05:00
|
|
|
[ create-constructor ] keep dup [
|
2006-05-14 23:25:34 -04:00
|
|
|
"slots" word-prop 1 swap tail-slice <reversed>
|
2005-07-25 17:13:35 -04:00
|
|
|
[ peek unit , \ keep , ] each
|
2005-08-25 15:27:38 -04:00
|
|
|
] [ ] make define-constructor ;
|
2005-02-09 22:35:11 -05:00
|
|
|
|
|
|
|
: define-tuple ( tuple slots -- )
|
|
|
|
2dup check-shape
|
2005-02-20 19:03:37 -05:00
|
|
|
>r create-in
|
2005-02-10 15:14:20 -05:00
|
|
|
dup intern-symbol
|
|
|
|
dup tuple-predicate
|
2005-11-24 19:02:20 -05:00
|
|
|
dup \ tuple bootstrap-word "superclass" set-word-prop
|
2005-09-16 02:39:33 -04:00
|
|
|
dup define-class
|
2005-02-20 19:03:37 -05:00
|
|
|
dup r> tuple-slots
|
2005-02-10 15:14:20 -05:00
|
|
|
default-constructor ;
|
2005-02-09 22:35:11 -05:00
|
|
|
|
2005-02-07 11:51:22 -05:00
|
|
|
M: tuple clone ( tuple -- tuple )
|
2005-09-10 18:27:31 -04:00
|
|
|
(clone) dup delegate clone over set-delegate ;
|
2005-02-07 11:51:22 -05:00
|
|
|
|
2006-03-21 00:44:19 -05:00
|
|
|
M: tuple hashcode* ( n tuple -- n )
|
|
|
|
{
|
|
|
|
{ [ over 0 <= ] [ 2drop 0 ] }
|
2006-05-10 02:18:25 -04:00
|
|
|
{ [ dup array-capacity 2 <= ] [ nip class hashcode ] }
|
2006-03-21 00:44:19 -05:00
|
|
|
{ [ t ] [ tuple-hashcode ] }
|
|
|
|
} cond ;
|
|
|
|
|
|
|
|
M: tuple hashcode ( tuple -- n ) 2 swap hashcode* ;
|
2005-01-31 14:02:09 -05:00
|
|
|
|
2005-04-12 13:35:27 -04:00
|
|
|
M: tuple = ( obj tuple -- ? )
|
2006-01-09 17:56:19 -05:00
|
|
|
2dup eq?
|
|
|
|
[ 2drop t ] [ over tuple? [ tuple= ] [ 2drop f ] if ] if ;
|
2005-04-12 13:35:27 -04:00
|
|
|
|
2005-07-13 18:08:54 -04:00
|
|
|
: is? ( obj pred -- ? | pred: obj -- ? )
|
2006-01-09 17:56:19 -05:00
|
|
|
over [
|
|
|
|
2dup >r >r call
|
|
|
|
[ r> r> 2drop t ] [ r> delegate r> is? ] if
|
2005-07-13 18:08:54 -04:00
|
|
|
] [
|
2006-01-09 17:56:19 -05:00
|
|
|
2drop f
|
2005-09-24 15:21:17 -04:00
|
|
|
] if ; inline
|
2005-09-24 16:34:10 -04:00
|
|
|
|
2006-01-09 17:56:19 -05:00
|
|
|
: >tuple ( seq -- tuple )
|
2005-09-24 16:34:10 -04:00
|
|
|
>vector dup first "tuple-size" word-prop over set-length
|
2006-01-09 17:56:19 -05:00
|
|
|
>array array>tuple ;
|