factor/library/collections/sequences.factor

109 lines
3.0 KiB
Factor
Raw Normal View History

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.
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 )
GENERIC: thaw ( seq -- mutable-seq )
2005-05-18 16:26:22 -04:00
GENERIC: like ( seq seq -- seq )
GENERIC: reverse ( seq -- seq )
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 )
GENERIC: concat ( seq -- seq )
2005-06-10 16:08:00 -04:00
GENERIC: resize ( n seq -- seq )
G: each ( seq quot -- | quot: elt -- )
[ over ] [ type ] ; inline
: each-with ( obj seq quot -- | quot: obj elt -- )
swap [ with ] each 2drop ; inline
: reduce ( list identity quot -- value | quot: x y -- z )
swapd each ; inline
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
: accumulate ( list identity quot -- values | quot: x y -- z )
rot [ pick >r swap call r> ] map-with nip ; inline
G: 2map ( seq seq quot -- seq | quot: elt elt -- elt )
[ over ] [ type ] ; inline
2005-05-16 01:15:48 -04:00
DEFER: <range>
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
: first 0 swap nth ; inline
: second 1 swap nth ; inline
: third 2 swap nth ; inline
: fourth 3 swap nth ; inline
: 2unseq ( { x y } -- x y )
dup first swap second ;
: 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 ;