1 ! Copyright (C) 2008 Alex Chapman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays kernel math sequences sequences.private shuffle ;
8 GENERIC: modified-nth ( n seq -- elt )
9 M: modified nth modified-nth ;
10 M: modified nth-unsafe modified-nth ;
12 GENERIC: modified-set-nth ( elt n seq -- )
13 M: modified set-nth modified-set-nth ;
14 M: modified set-nth-unsafe modified-set-nth ;
16 INSTANCE: modified virtual-sequence
18 TUPLE: 1modified < modified seq ;
20 M: modified length seq>> length ;
21 M: modified set-length seq>> set-length ;
23 M: 1modified virtual-seq seq>> ;
25 TUPLE: scaled < 1modified c ;
28 : scale ( seq c -- new-seq )
29 dupd <scaled> swap like ;
31 M: scaled modified-nth ( n seq -- elt )
32 [ seq>> nth ] [ c>> * ] bi ;
34 M: scaled modified-set-nth ( elt n seq -- elt )
36 tuck [ c>> / ] 2dip seq>> set-nth ;
38 TUPLE: offset < 1modified n ;
41 : seq-offset ( seq n -- new-seq )
42 dupd <offset> swap like ;
44 M: offset modified-nth ( n seq -- elt )
45 [ seq>> nth ] [ n>> + ] bi ;
47 M: offset modified-set-nth ( elt n seq -- )
48 tuck [ n>> - ] 2dip seq>> set-nth ;
50 TUPLE: summed < modified seqs ;
53 M: summed length seqs>> [ length ] map supremum ;
56 : ?+ ( x/f y/f -- sum )
57 #! addition that treats f as 0
65 M: summed modified-nth ( n seq -- )
66 seqs>> [ ?nth ?+ ] with 0 swap reduce ;
68 M: summed modified-set-nth ( elt n seq -- ) immutable ;
70 M: summed set-length ( n seq -- )
71 seqs>> [ set-length ] with each ;
73 M: summed virtual-seq ( summed -- seq ) [ ] { } map-as ;
75 : <2summed> ( seq seq -- summed-seq ) 2array <summed> ;
76 : <3summed> ( seq seq seq -- summed-seq ) 3array <summed> ;