factor/library/collections/sequences-epilogue.factor

163 lines
3.8 KiB
Factor
Raw Normal View History

2006-01-09 01:34:23 -05:00
! Copyright (C) 2005, 2006 Slava Pestov.
2005-12-31 20:51:58 -05:00
! See http://factorcode.org/license.txt for BSD license.
2005-04-02 02:39:33 -05:00
IN: sequences
USING: arrays errors generic kernel kernel-internals math
2006-01-09 01:34:23 -05:00
sequences-internals strings vectors words ;
2005-04-02 02:39:33 -05:00
: first2 ( { x y } -- x y )
1 swap bounds-check nip first2-unsafe ;
: first3 ( { x y z } -- x y z )
2 swap bounds-check nip first3-unsafe ;
: first4 ( { x y z w } -- x y z w )
3 swap bounds-check nip first4-unsafe ;
2005-07-16 22:16:18 -04:00
M: object like drop ;
2006-03-27 22:20:42 -05:00
: index ( obj seq -- n )
[ = ] find-with drop ;
2006-03-27 22:20:42 -05:00
: index* ( obj i seq -- n )
[ = ] find-with* drop ;
2006-03-27 22:20:42 -05:00
: last-index ( obj seq -- n )
[ = ] find-last-with drop ;
2006-03-27 22:20:42 -05:00
: last-index* ( obj i seq -- n )
[ = ] find-last-with* drop ;
2006-03-27 22:20:42 -05:00
: member? ( obj seq -- ? )
[ = ] contains-with? ;
2006-03-27 22:20:42 -05:00
: memq? ( obj seq -- ? )
[ eq? ] contains-with? ;
2006-03-27 22:20:42 -05:00
: remove ( obj list -- list )
[ = not ] subset-with ;
2005-04-06 21:41:49 -04:00
2005-09-17 04:15:05 -04:00
: (subst) ( newseq oldseq elt -- new/elt )
[ swap index ] keep
2005-09-24 15:21:17 -04:00
over -1 > [ drop swap nth ] [ 2nip ] if ;
2005-09-17 04:15:05 -04:00
: subst ( newseq oldseq seq -- )
[ >r 2dup r> (subst) ] inject 2drop ;
2005-09-14 00:37:50 -04:00
: move ( to from seq -- )
pick pick number=
2005-09-24 15:21:17 -04:00
[ 3drop ] [ [ nth swap ] keep set-nth ] if ; inline
2005-09-14 00:37:50 -04:00
: (delete) ( elt store scan seq -- )
2dup length < [
3dup move
>r pick over r> dup >r nth = r> swap
2005-09-16 22:47:28 -04:00
[ >r >r 1+ r> r> ] unless >r 1+ r> (delete)
2005-09-14 00:37:50 -04:00
] when ;
: delete ( elt seq -- ) 0 0 rot (delete) nip set-length drop ;
2005-09-14 00:37:50 -04:00
2006-05-25 23:45:19 -04:00
: push-new ( elt seq -- ) [ delete ] 2keep push ;
2006-06-20 23:26:41 -04:00
: prune ( seq -- seq )
[ V{ } clone swap [ over push-new ] each ] keep like ;
2005-08-04 12:58:07 -04:00
: nappend ( to from -- )
2006-01-02 00:51:03 -05:00
>r [ length ] keep r> copy-into ; inline
: >resizable ( seq -- seq ) [ thaw dup ] keep nappend ;
2005-04-06 21:41:49 -04:00
2005-12-31 20:51:58 -05:00
: immutable ( seq quot -- seq | quot: seq -- )
2006-01-02 00:51:03 -05:00
swap [ >resizable [ swap call ] keep ] keep like ; inline
2005-12-31 20:51:58 -05:00
: append ( s1 s2 -- s1+s2 )
swap [ swap nappend ] immutable ;
: add ( seq elt -- seq )
swap [ push ] immutable ;
2005-04-17 21:59:11 -04:00
: add* ( seq elt -- seq )
over >r
over thaw [ push ] keep [ swap nappend ] keep
r> like ;
2005-12-04 22:06:12 -05:00
: diff ( seq1 seq2 -- seq2-seq1 )
[ swap member? not ] subset-with ;
2005-12-04 22:06:12 -05:00
: append3 ( s1 s2 s3 -- s1+s2+s3 )
rot [ [ rot nappend ] keep swap nappend ] immutable ;
2005-04-17 21:59:11 -04:00
: peek ( sequence -- element ) dup length 1- swap nth ;
2005-04-06 21:41:49 -04:00
2005-09-14 00:37:50 -04:00
: pop* ( sequence -- )
[ length 1- ] keep
2005-11-19 04:09:30 -05:00
[ 0 -rot set-nth ] 2keep
2006-05-02 06:05:58 -04:00
set-length ;
2005-09-14 00:37:50 -04:00
2005-04-06 21:41:49 -04:00
: pop ( sequence -- element )
2006-05-02 06:05:58 -04:00
dup peek swap pop* ;
2005-04-06 21:41:49 -04:00
2005-09-18 01:37:28 -04:00
: all-equal? ( seq -- ? ) [ = ] monotonic? ;
: all-eq? ( seq -- ? ) [ eq? ] monotonic? ;
2005-10-09 21:27:14 -04:00
: mismatch ( seq1 seq2 -- i )
2dup min-length
[ >r 2dup r> 2nth-unsafe = not ] find
swap >r 3drop r> ;
2005-10-09 21:27:14 -04:00
2005-07-30 02:08:59 -04:00
: flip ( seq -- seq )
2005-07-31 23:38:33 -04:00
dup empty? [
dup first [ length ] keep like
[ swap [ nth ] map-with ] map-with
] unless ;
: unpair ( seq -- firsts seconds )
flip dup empty? [ drop { } { } ] [ first2 ] if ;
2006-03-14 16:51:09 -05:00
: exchange ( n n seq -- )
pick over bounds-check 2drop 2dup bounds-check 2drop
exchange-unsafe ;
: assoc ( key assoc -- value )
[ first = ] find-with nip second ;
2006-05-20 16:42:33 -04:00
: rassoc ( value assoc -- key )
[ second = ] find-with nip first ;
: last/first ( seq -- pair ) dup peek swap first 2array ;
: sequence= ( seq seq -- ? )
2dup [ length ] 2apply = [
dup length [ >r 2dup r> 2nth-unsafe = ] all? 2nip
] [
2drop f
] if ;
UNION: sequence array string sbuf vector quotation ;
M: sequence = ( obj seq -- ? )
2dup eq? [
2drop t
] [
over type over type eq? [ sequence= ] [ 2drop f ] if
] if ;
M: sequence hashcode ( seq -- n )
#! Poor
length ;
2005-04-06 21:41:49 -04:00
IN: kernel
2006-01-09 01:34:23 -05:00
M: object <=>
2dup mismatch dup -1 =
[ drop [ length ] 2apply - ] [ 2nth-unsafe <=> ] if ;
2005-12-31 20:51:58 -05:00
: depth ( -- n ) datastack length ;
2005-08-08 15:21:14 -04:00
2005-09-17 22:25:18 -04:00
: no-cond "cond fall-through" throw ;
2005-08-08 15:21:14 -04:00
: cond ( conditions -- )
2006-06-04 03:46:06 -04:00
[ first call ] find nip dup [ second call ] [ no-cond ] if ;
2005-08-08 15:21:14 -04:00
2006-03-20 00:05:04 -05:00
: unix? os { "freebsd" "linux" "macosx" "solaris" } member? ;