factor/library/collections/vectors.factor

46 lines
1.2 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2005-05-05 22:30:58 -04:00
IN: vectors
2005-04-02 02:39:33 -05:00
USING: errors generic kernel kernel-internals lists math
math-internals sequences sequences-internals ;
M: vector set-length ( len vec -- ) grow-length ;
M: vector nth-unsafe ( n vec -- obj ) underlying array-nth ;
M: vector nth ( n vec -- obj ) bounds-check nth-unsafe ;
M: vector set-nth-unsafe ( obj n vec -- )
underlying set-array-nth ;
2004-12-24 02:52:02 -05:00
2005-05-05 22:30:58 -04:00
M: vector set-nth ( obj n vec -- )
growable-check 2dup ensure set-nth-unsafe ;
2004-12-24 02:52:02 -05:00
2005-04-06 21:41:49 -04:00
M: vector hashcode ( vec -- n )
dup length 0 number= [ drop 0 ] [ first hashcode ] ifte ;
: >vector ( list -- vector )
dup length <vector> [ swap nappend ] keep ; inline
M: object thaw >vector ;
M: vector clone ( vector -- vector ) clone-growable ;
M: general-list like drop >list ;
2005-09-07 17:21:11 -04:00
M: vector like drop dup vector? [ >vector ] unless ;
: 1vector ( x -- { x } )
1 empty-vector [ 0 swap set-nth ] keep ; flushable
2005-09-07 17:21:11 -04:00
: 2vector ( x y -- { x y } )
2 empty-vector
[ 1 swap set-nth ] keep
[ 0 swap set-nth ] keep ; flushable
2005-09-07 17:21:11 -04:00
: 3vector ( x y z -- { x y z } )
3 empty-vector
[ 2 swap set-nth ] keep
[ 1 swap set-nth ] keep
[ 0 swap set-nth ] keep ; flushable