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
|
|
|
|
|
! low level and not bounds-checked; they are in the
|
|
|
|
|
! 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-04-26 00:35:55 -04:00
|
|
|
GENERIC: peek ( seq -- elt )
|
2005-05-05 22:30:58 -04:00
|
|
|
GENERIC: contains? ( elt seq -- ? )
|
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-05-14 17:18:45 -04:00
|
|
|
G: each ( seq quot -- | quot: elt -- )
|
|
|
|
|
[ over ] [ type ] ; inline
|
|
|
|
|
|
|
|
|
|
: each-with ( obj seq quot -- | quot: obj elt -- )
|
|
|
|
|
swap [ with ] each 2drop ; inline
|
|
|
|
|
|
2005-06-25 20:39:53 -04:00
|
|
|
: reduce ( list identity quot -- value | quot: x y -- z )
|
|
|
|
|
swapd each ; inline
|
|
|
|
|
|
2005-05-14 17:18:45 -04:00
|
|
|
G: tree-each ( obj quot -- | quot: elt -- )
|
|
|
|
|
[ over ] [ type ] ; inline
|
|
|
|
|
|
|
|
|
|
: tree-each-with ( obj vector quot -- )
|
|
|
|
|
swap [ with ] tree-each 2drop ; inline
|
|
|
|
|
|
|
|
|
|
G: map ( seq quot -- seq | quot: elt -- elt )
|
|
|
|
|
[ over ] [ type ] ; inline
|
|
|
|
|
|
|
|
|
|
: map-with ( obj list quot -- list | quot: obj elt -- elt )
|
|
|
|
|
swap [ with rot ] map 2nip ; inline
|
|
|
|
|
|
2005-06-25 20:39:53 -04:00
|
|
|
: accumulate ( list identity quot -- values | quot: x y -- z )
|
|
|
|
|
rot [ pick >r swap call r> ] map-with nip ; inline
|
|
|
|
|
|
2005-05-14 17:18:45 -04:00
|
|
|
G: 2map ( seq seq quot -- seq | quot: elt elt -- elt )
|
|
|
|
|
[ over ] [ type ] ; inline
|
|
|
|
|
|
2005-05-16 01:15:48 -04:00
|
|
|
DEFER: <range>
|
2005-04-25 19:54:21 -04:00
|
|
|
DEFER: append ! remove this when sort is moved from lists to sequences
|
2005-05-18 16:26:22 -04:00
|
|
|
DEFER: subseq
|
2005-05-05 22:30:58 -04:00
|
|
|
|
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-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-05-05 22:30:58 -04:00
|
|
|
! Some low-level code used by vectors and string buffers.
|
|
|
|
|
IN: kernel-internals
|
|
|
|
|
|
|
|
|
|
: assert-positive ( fx -- )
|
|
|
|
|
0 fixnum<
|
|
|
|
|
[ "Sequence index must be positive" throw ] when ; inline
|
|
|
|
|
|
|
|
|
|
: assert-bounds ( fx seq -- )
|
|
|
|
|
over assert-positive
|
|
|
|
|
length fixnum>=
|
|
|
|
|
[ "Sequence index out of bounds" throw ] when ; inline
|
|
|
|
|
|
|
|
|
|
: bounds-check ( n seq -- fixnum seq )
|
|
|
|
|
>r >fixnum r> 2dup assert-bounds ; inline
|
|
|
|
|
|
|
|
|
|
: growable-check ( n seq -- fixnum seq )
|
|
|
|
|
>r >fixnum dup assert-positive r> ; inline
|
|
|
|
|
|
|
|
|
|
GENERIC: underlying
|
|
|
|
|
GENERIC: set-underlying
|
|
|
|
|
GENERIC: set-capacity
|
|
|
|
|
|
2005-06-10 16:08:00 -04:00
|
|
|
: expand ( len seq -- )
|
|
|
|
|
[ underlying resize ] keep set-underlying ;
|
2005-05-05 22:30:58 -04:00
|
|
|
|
|
|
|
|
: ensure ( n seq -- )
|
|
|
|
|
#! If n is beyond the sequence's length, increase the length,
|
|
|
|
|
#! growing the underlying storage if necessary, with an
|
|
|
|
|
#! optimistic doubling of its size.
|
|
|
|
|
2dup length fixnum>= [
|
|
|
|
|
>r 1 fixnum+ r>
|
|
|
|
|
2dup underlying length fixnum> [
|
2005-06-10 16:08:00 -04:00
|
|
|
over 2 fixnum* over expand
|
2005-05-05 22:30:58 -04:00
|
|
|
] when
|
|
|
|
|
set-capacity
|
|
|
|
|
] [
|
|
|
|
|
2drop
|
|
|
|
|
] ifte ;
|