Add vectors.functor for generating vector types from arrays; re-implement bit-vectors and specialized-vectors using this. Add DEFERS directive to functors
parent
e2044602aa
commit
c171527b8d
|
@ -22,11 +22,11 @@ HELP: bit-vector
|
||||||
{ $description "The class of resizable bit vectors. See " { $link "bit-vectors" } " for information." } ;
|
{ $description "The class of resizable bit vectors. See " { $link "bit-vectors" } " for information." } ;
|
||||||
|
|
||||||
HELP: <bit-vector>
|
HELP: <bit-vector>
|
||||||
{ $values { "n" "a positive integer specifying initial capacity" } { "bit-vector" bit-vector } }
|
{ $values { "capacity" "a positive integer specifying initial capacity" } { "vector" bit-vector } }
|
||||||
{ $description "Creates a new bit vector that can hold " { $snippet "n" } " bits before resizing." } ;
|
{ $description "Creates a new bit vector that can hold " { $snippet "n" } " bits before resizing." } ;
|
||||||
|
|
||||||
HELP: >bit-vector
|
HELP: >bit-vector
|
||||||
{ $values { "seq" "a sequence" } { "bit-vector" bit-vector } }
|
{ $values { "seq" "a sequence" } { "vector" bit-vector } }
|
||||||
{ $description "Outputs a freshly-allocated bit vector with the same elements as a given sequence." } ;
|
{ $description "Outputs a freshly-allocated bit vector with the same elements as a given sequence." } ;
|
||||||
|
|
||||||
HELP: ?V{
|
HELP: ?V{
|
||||||
|
|
|
@ -2,34 +2,10 @@
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: arrays kernel kernel.private math sequences
|
USING: arrays kernel kernel.private math sequences
|
||||||
sequences.private growable bit-arrays prettyprint.custom
|
sequences.private growable bit-arrays prettyprint.custom
|
||||||
parser accessors ;
|
parser accessors vectors.functor classes.parser ;
|
||||||
IN: bit-vectors
|
IN: bit-vectors
|
||||||
|
|
||||||
TUPLE: bit-vector
|
<< "bit-vector" create-class-in \ bit-array \ <bit-array> define-vector >>
|
||||||
{ underlying bit-array initial: ?{ } }
|
|
||||||
{ length array-capacity } ;
|
|
||||||
|
|
||||||
: <bit-vector> ( n -- bit-vector )
|
|
||||||
<bit-array> 0 bit-vector boa ; inline
|
|
||||||
|
|
||||||
: >bit-vector ( seq -- bit-vector )
|
|
||||||
T{ bit-vector f ?{ } 0 } clone-like ;
|
|
||||||
|
|
||||||
M: bit-vector like
|
|
||||||
drop dup bit-vector? [
|
|
||||||
dup bit-array?
|
|
||||||
[ dup length bit-vector boa ] [ >bit-vector ] if
|
|
||||||
] unless ;
|
|
||||||
|
|
||||||
M: bit-vector new-sequence
|
|
||||||
drop [ <bit-array> ] [ >fixnum ] bi bit-vector boa ;
|
|
||||||
|
|
||||||
M: bit-vector equal?
|
|
||||||
over bit-vector? [ sequence= ] [ 2drop f ] if ;
|
|
||||||
|
|
||||||
M: bit-array new-resizable drop <bit-vector> ;
|
|
||||||
|
|
||||||
INSTANCE: bit-vector growable
|
|
||||||
|
|
||||||
SYNTAX: ?V{ \ } [ >bit-vector ] parse-literal ;
|
SYNTAX: ?V{ \ } [ >bit-vector ] parse-literal ;
|
||||||
|
|
||||||
|
|
|
@ -121,6 +121,8 @@ PRIVATE>
|
||||||
|
|
||||||
SYNTAX: IS [ dup search [ ] [ no-word ] ?if ] (INTERPOLATE) ;
|
SYNTAX: IS [ dup search [ ] [ no-word ] ?if ] (INTERPOLATE) ;
|
||||||
|
|
||||||
|
SYNTAX: DEFERS [ current-vocab create ] (INTERPOLATE) ;
|
||||||
|
|
||||||
SYNTAX: DEFINES [ create-in ] (INTERPOLATE) ;
|
SYNTAX: DEFINES [ create-in ] (INTERPOLATE) ;
|
||||||
|
|
||||||
SYNTAX: DEFINES-CLASS [ create-class-in ] (INTERPOLATE) ;
|
SYNTAX: DEFINES-CLASS [ create-class-in ] (INTERPOLATE) ;
|
||||||
|
|
|
@ -1,37 +1,23 @@
|
||||||
! Copyright (C) 2008 Slava Pestov.
|
! Copyright (C) 2008, 2009 Slava Pestov.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: functors sequences sequences.private growable
|
USING: functors sequences sequences.private growable
|
||||||
prettyprint.custom kernel words classes math parser ;
|
prettyprint.custom kernel words classes math parser ;
|
||||||
|
QUALIFIED: vectors.functor
|
||||||
IN: specialized-vectors.functor
|
IN: specialized-vectors.functor
|
||||||
|
|
||||||
FUNCTOR: define-vector ( T -- )
|
FUNCTOR: define-vector ( T -- )
|
||||||
|
|
||||||
|
V DEFINES-CLASS ${T}-vector
|
||||||
|
|
||||||
A IS ${T}-array
|
A IS ${T}-array
|
||||||
<A> IS <${A}>
|
<A> IS <${A}>
|
||||||
|
|
||||||
V DEFINES-CLASS ${T}-vector
|
>V DEFERS >${V}
|
||||||
<V> DEFINES <${V}>
|
|
||||||
>V DEFINES >${V}
|
|
||||||
V{ DEFINES ${V}{
|
V{ DEFINES ${V}{
|
||||||
|
|
||||||
WHERE
|
WHERE
|
||||||
|
|
||||||
TUPLE: V { underlying A } { length array-capacity } ;
|
V A <A> vectors.functor:define-vector
|
||||||
|
|
||||||
: <V> ( capacity -- vector ) <A> 0 V boa ; inline
|
|
||||||
|
|
||||||
M: V like
|
|
||||||
drop dup V instance? [
|
|
||||||
dup A instance? [ dup length V boa ] [ >V ] if
|
|
||||||
] unless ;
|
|
||||||
|
|
||||||
M: V new-sequence drop [ <A> ] [ >fixnum ] bi V boa ;
|
|
||||||
|
|
||||||
M: A new-resizable drop <V> ;
|
|
||||||
|
|
||||||
M: V equal? over V instance? [ sequence= ] [ 2drop f ] if ;
|
|
||||||
|
|
||||||
: >V ( seq -- vector ) V new clone-like ; inline
|
|
||||||
|
|
||||||
M: V pprint-delims drop \ V{ \ } ;
|
M: V pprint-delims drop \ V{ \ } ;
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
! Copyright (C) 2009 Slava Pestov.
|
||||||
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
|
USING: functors sequences sequences.private growable
|
||||||
|
kernel words classes math parser ;
|
||||||
|
IN: vectors.functor
|
||||||
|
|
||||||
|
FUNCTOR: define-vector ( V A <A> -- )
|
||||||
|
|
||||||
|
<V> DEFINES <${V}>
|
||||||
|
>V DEFINES >${V}
|
||||||
|
|
||||||
|
WHERE
|
||||||
|
|
||||||
|
TUPLE: V { underlying A } { length array-capacity } ;
|
||||||
|
|
||||||
|
: <V> ( capacity -- vector ) <A> 0 V boa ; inline
|
||||||
|
|
||||||
|
M: V like
|
||||||
|
drop dup V instance? [
|
||||||
|
dup A instance? [ dup length V boa ] [ >V ] if
|
||||||
|
] unless ;
|
||||||
|
|
||||||
|
M: V new-sequence drop [ <A> ] [ >fixnum ] bi V boa ;
|
||||||
|
|
||||||
|
M: A new-resizable drop <V> ;
|
||||||
|
|
||||||
|
M: V equal? over V instance? [ sequence= ] [ 2drop f ] if ;
|
||||||
|
|
||||||
|
: >V ( seq -- vector ) V new clone-like ; inline
|
||||||
|
|
||||||
|
INSTANCE: V growable
|
||||||
|
|
||||||
|
;FUNCTOR
|
Loading…
Reference in New Issue