2005-04-02 02:39:33 -05:00
|
|
|
! Copyright (C) 2005 Slava Pestov.
|
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
|
|
|
|
IN: sequences
|
2005-05-05 22:30:58 -04:00
|
|
|
USING: errors generic kernel math math-internals strings vectors ;
|
2005-04-02 02:39:33 -05:00
|
|
|
|
|
|
|
|
! This file is needed very early in bootstrap.
|
|
|
|
|
|
|
|
|
|
! Sequences support the following protocol. Concrete examples
|
|
|
|
|
! are strings, string buffers, vectors, and arrays. Arrays are
|
2005-07-21 21:43:37 -04:00
|
|
|
! low level and no | quot: elt -- ? t bounds-checked; they are in the
|
2005-04-02 02:39:33 -05:00
|
|
|
! kernel-internals vocabulary, so don't use them unless you have
|
|
|
|
|
! a good reason.
|
|
|
|
|
|
2005-04-25 19:54:21 -04:00
|
|
|
GENERIC: empty? ( sequence -- ? )
|
2005-04-02 02:39:33 -05:00
|
|
|
GENERIC: length ( sequence -- n )
|
|
|
|
|
GENERIC: set-length ( n sequence -- )
|
|
|
|
|
GENERIC: nth ( n sequence -- obj )
|
|
|
|
|
GENERIC: set-nth ( value n sequence -- obj )
|
2005-04-25 19:54:21 -04:00
|
|
|
GENERIC: thaw ( seq -- mutable-seq )
|
2005-05-18 16:26:22 -04:00
|
|
|
GENERIC: like ( seq seq -- seq )
|
2005-04-25 19:54:21 -04:00
|
|
|
GENERIC: reverse ( seq -- seq )
|
2005-07-17 00:21:10 -04:00
|
|
|
GENERIC: reverse-slice ( seq -- seq )
|
2005-04-26 00:35:55 -04:00
|
|
|
GENERIC: peek ( seq -- elt )
|
2005-05-18 16:26:22 -04:00
|
|
|
GENERIC: head ( n seq -- seq )
|
|
|
|
|
GENERIC: tail ( n seq -- seq )
|
2005-05-24 19:59:21 -04:00
|
|
|
GENERIC: concat ( seq -- seq )
|
2005-06-10 16:08:00 -04:00
|
|
|
GENERIC: resize ( n seq -- seq )
|
2005-04-25 19:54:21 -04:00
|
|
|
|
2005-07-16 23:01:51 -04:00
|
|
|
: immutable ( seq quot -- seq | quot: seq -- )
|
|
|
|
|
swap [ thaw ] keep >r dup >r swap call r> r> like ; inline
|
|
|
|
|
|
2005-05-14 17:18:45 -04:00
|
|
|
G: each ( seq quot -- | quot: elt -- )
|
2005-08-15 03:25:39 -04:00
|
|
|
[ over ] [ standard-combination ] ; inline
|
2005-05-14 17:18:45 -04:00
|
|
|
|
|
|
|
|
: each-with ( obj seq quot -- | quot: obj elt -- )
|
|
|
|
|
swap [ with ] each 2drop ; inline
|
|
|
|
|
|
2005-07-21 03:04:19 -04:00
|
|
|
: reduce ( seq identity quot -- value | quot: x y -- z )
|
2005-06-25 20:39:53 -04:00
|
|
|
swapd each ; inline
|
|
|
|
|
|
2005-07-21 21:43:37 -04:00
|
|
|
G: find ( seq quot -- i elt | quot: elt -- ? )
|
2005-08-15 03:25:39 -04:00
|
|
|
[ over ] [ standard-combination ] ; inline
|
2005-07-16 22:16:18 -04:00
|
|
|
|
2005-07-21 21:43:37 -04:00
|
|
|
: find-with ( obj seq quot -- i elt | quot: elt -- ? )
|
2005-07-16 22:16:18 -04:00
|
|
|
swap [ with rot ] find 2swap 2drop ; inline
|
|
|
|
|
|
2005-05-28 20:52:23 -04:00
|
|
|
: first 0 swap nth ; inline
|
|
|
|
|
: second 1 swap nth ; inline
|
|
|
|
|
: third 2 swap nth ; inline
|
2005-06-17 02:40:25 -04:00
|
|
|
: fourth 3 swap nth ; inline
|
2005-05-28 20:52:23 -04:00
|
|
|
|
2005-07-16 22:16:18 -04:00
|
|
|
: push ( element sequence -- )
|
|
|
|
|
#! Push a value on the end of a sequence.
|
2005-08-07 00:00:57 -04:00
|
|
|
dup length swap set-nth ; inline
|
2005-07-16 22:16:18 -04:00
|
|
|
|
2005-08-02 06:32:48 -04:00
|
|
|
: 2nth ( s s n -- x x ) tuck swap nth >r swap nth r> ; inline
|
2005-07-16 22:16:18 -04:00
|
|
|
|
2005-07-12 20:30:05 -04:00
|
|
|
: 2unseq ( { x y } -- x y )
|
|
|
|
|
dup first swap second ;
|
|
|
|
|
|
2005-06-22 02:32:17 -04:00
|
|
|
: 3unseq ( { x y z } -- x y z )
|
|
|
|
|
dup first over second rot third ;
|
2005-07-30 22:14:34 -04:00
|
|
|
|
|
|
|
|
TUPLE: bounds-error index seq ;
|
|
|
|
|
: bounds-error <bounds-error> throw ;
|
|
|
|
|
|
|
|
|
|
: growable-check ( n seq -- fx seq )
|
|
|
|
|
>r >fixnum dup 0 fixnum<
|
|
|
|
|
[ r> 2dup bounds-error ] [ r> ] ifte ; inline
|
|
|
|
|
|
|
|
|
|
: bounds-check ( n seq -- fx seq )
|
|
|
|
|
growable-check 2dup length fixnum>=
|
|
|
|
|
[ 2dup bounds-error ] when ; inline
|