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-08-19 22:22:15 -04:00
|
|
|
GENERIC: empty? ( sequence -- ? ) flushable
|
|
|
|
|
GENERIC: length ( sequence -- n ) flushable
|
2005-08-22 02:06:32 -04:00
|
|
|
GENERIC: set-length ( n sequence -- )
|
2005-08-19 22:22:15 -04:00
|
|
|
GENERIC: nth ( n sequence -- obj ) flushable
|
2005-08-22 02:06:32 -04:00
|
|
|
GENERIC: set-nth ( value n sequence -- obj )
|
2005-08-19 22:22:15 -04:00
|
|
|
GENERIC: thaw ( seq -- mutable-seq ) flushable
|
|
|
|
|
GENERIC: like ( seq seq -- seq ) flushable
|
|
|
|
|
GENERIC: reverse ( seq -- seq ) flushable
|
|
|
|
|
GENERIC: reverse-slice ( seq -- seq ) flushable
|
|
|
|
|
GENERIC: peek ( seq -- elt ) flushable
|
|
|
|
|
GENERIC: head ( n seq -- seq ) flushable
|
|
|
|
|
GENERIC: tail ( n seq -- seq ) flushable
|
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-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-09-23 01:22:04 -04:00
|
|
|
: ?push ( elt seq/f -- seq )
|
|
|
|
|
[ 1 <vector> ] unless* [ push ] keep ;
|
|
|
|
|
|
2005-09-02 23:44:23 -04:00
|
|
|
: first2 ( { x y } -- x y )
|
2005-08-19 22:22:15 -04:00
|
|
|
dup first swap second ; inline
|
2005-07-12 20:30:05 -04:00
|
|
|
|
2005-09-02 23:44:23 -04:00
|
|
|
: first3 ( { x y z } -- x y z )
|
2005-08-19 22:22:15 -04:00
|
|
|
dup first over second rot third ; inline
|
2005-07-30 22:14:34 -04:00
|
|
|
|
2005-09-10 18:27:31 -04:00
|
|
|
IN: sequences-internals
|
|
|
|
|
|
|
|
|
|
! Unsafe sequence protocol for inner loops
|
|
|
|
|
GENERIC: nth-unsafe
|
|
|
|
|
GENERIC: set-nth-unsafe
|
|
|
|
|
|
|
|
|
|
M: object nth-unsafe nth ;
|
|
|
|
|
M: object set-nth-unsafe set-nth ;
|
|
|
|
|
|
|
|
|
|
: 2nth-unsafe ( s s n -- x x )
|
|
|
|
|
tuck swap nth-unsafe >r swap nth-unsafe r> ; inline
|
|
|
|
|
|
|
|
|
|
: change-nth-unsafe ( seq i quot -- )
|
|
|
|
|
pick pick >r >r >r swap nth-unsafe
|
|
|
|
|
r> call r> r> swap set-nth-unsafe ; inline
|
2005-09-24 16:34:10 -04:00
|
|
|
|
|
|
|
|
! Integers support the sequence protocol
|
|
|
|
|
M: integer length ;
|
|
|
|
|
M: integer nth drop ;
|
|
|
|
|
M: integer nth-unsafe drop ;
|