]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences.factor
sequences: add a copy-unsafe that can be used sometimes.
[factor.git] / core / sequences / sequences.factor
1 ! Copyright (C) 2005, 2011 Slava Pestov, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel kernel.private math math.order
4 math.private slots.private ;
5 IN: sequences
6
7 MIXIN: sequence
8
9 GENERIC: length ( seq -- n ) flushable
10 GENERIC: set-length ( n seq -- )
11 GENERIC: nth ( n seq -- elt ) flushable
12 GENERIC: set-nth ( elt n seq -- )
13 GENERIC: new-sequence ( len seq -- newseq ) flushable
14 GENERIC: new-resizable ( len seq -- newseq ) flushable
15 GENERIC: like ( seq exemplar -- newseq ) flushable
16 GENERIC: clone-like ( seq exemplar -- newseq ) flushable
17
18 : new-like ( len exemplar quot -- seq )
19     over [ [ new-sequence ] dip call ] dip like ; inline
20
21 M: sequence like drop ; inline
22
23 GENERIC: lengthen ( n seq -- )
24 GENERIC: shorten ( n seq -- )
25
26 M: sequence lengthen 2dup length > [ set-length ] [ 2drop ] if ; inline
27 M: sequence shorten 2dup length < [ set-length ] [ 2drop ] if ; inline
28
29 : empty? ( seq -- ? ) length 0 = ; inline
30
31 : if-empty ( ..a seq quot1: ( ..a -- ..b ) quot2: ( ..a seq -- ..b ) -- ..b )
32     [ dup empty? ] [ [ drop ] prepose ] [ ] tri* if ; inline
33
34 : when-empty ( seq quot -- ) [ ] if-empty ; inline
35
36 : unless-empty ( seq quot -- ) [ ] swap if-empty ; inline
37
38 : delete-all ( seq -- ) 0 swap set-length ;
39
40 : first ( seq -- first ) 0 swap nth ; inline
41 : second ( seq -- second ) 1 swap nth ; inline
42 : third ( seq -- third ) 2 swap nth ; inline
43 : fourth ( seq -- fourth ) 3 swap nth ; inline
44
45 : set-first ( first seq -- ) 0 swap set-nth ; inline
46 : set-second ( second seq -- ) 1 swap set-nth ; inline
47 : set-third ( third seq -- ) 2 swap set-nth ; inline
48 : set-fourth  ( fourth seq -- ) 3 swap set-nth ; inline
49
50 : push ( elt seq -- ) [ length ] [ set-nth ] bi ;
51
52 ERROR: bounds-error index seq ;
53
54 GENERIC# bounds-check? 1 ( n seq -- ? )
55
56 M: integer bounds-check? ( n seq -- ? )
57     dupd length < [ 0 >= ] [ drop f ] if ; inline
58
59 : bounds-check ( n seq -- n seq )
60     2dup bounds-check? [ bounds-error ] unless ; inline
61
62 MIXIN: immutable-sequence
63
64 ERROR: immutable seq ;
65
66 M: immutable-sequence set-nth immutable ;
67
68 INSTANCE: immutable-sequence sequence
69
70 <PRIVATE
71
72 : array-nth ( n array -- elt )
73     swap 2 fixnum+fast slot ; inline
74
75 : set-array-nth ( elt n array -- )
76     swap 2 fixnum+fast set-slot ; inline
77
78 : dispatch ( n array -- ) array-nth call ;
79
80 GENERIC: resize ( n seq -- newseq ) flushable
81
82 ! Unsafe sequence protocol for inner loops
83 GENERIC: nth-unsafe ( n seq -- elt ) flushable
84 GENERIC: set-nth-unsafe ( elt n seq -- )
85
86 M: sequence nth bounds-check nth-unsafe ; inline
87 M: sequence set-nth bounds-check set-nth-unsafe ; inline
88
89 M: sequence nth-unsafe nth ; inline
90 M: sequence set-nth-unsafe set-nth ; inline
91
92 : change-nth-unsafe ( i seq quot -- )
93     [ [ nth-unsafe ] dip call ] 3keep drop set-nth-unsafe ; inline
94
95 PRIVATE>
96
97 ! The f object supports the sequence protocol trivially
98 M: f length drop 0 ; inline
99 M: f nth-unsafe nip ; inline
100 M: f like drop [ f ] when-empty ; inline
101
102 INSTANCE: f immutable-sequence
103
104 ! Integer sequences
105 TUPLE: iota { n integer read-only } ;
106
107 : iota ( n -- iota ) \ iota boa ; inline
108
109 M: iota length n>> ; inline
110 M: iota nth-unsafe drop ; inline
111
112 INSTANCE: iota immutable-sequence
113
114 <PRIVATE
115
116 : first-unsafe ( seq -- first ) 0 swap nth-unsafe ; inline
117 : second-unsafe ( seq -- second ) 1 swap nth-unsafe ; inline
118 : third-unsafe ( seq -- third ) 2 swap nth-unsafe ; inline
119 : fourth-unsafe ( seq -- fourth ) 3 swap nth-unsafe ; inline
120
121 : first2-unsafe ( seq -- first second )
122     [ first-unsafe ] [ second-unsafe ] bi ; inline
123
124 : first3-unsafe ( seq -- first second third )
125     [ first2-unsafe ] [ third-unsafe ] bi ; inline
126
127 : first4-unsafe ( seq -- first second third fourth )
128     [ first3-unsafe ] [ fourth-unsafe ] bi ; inline
129
130 : exchange-unsafe ( m n seq -- )
131     [ [ nth-unsafe ] curry bi@ ]
132     [ [ set-nth-unsafe ] curry bi@ ] 3bi ; inline
133
134 : (head) ( seq n -- from to seq ) [ 0 ] 2dip swap ; inline
135
136 : (tail) ( seq n -- from to seq ) swap [ length ] keep ; inline
137
138 : from-end ( seq n -- seq n' ) [ dup length ] dip - ; inline
139
140 : (1sequence) ( obj seq -- seq )
141     [ 0 swap set-nth-unsafe ] keep ; inline
142
143 : (2sequence) ( obj1 obj2 seq -- seq )
144     [ 1 swap set-nth-unsafe ] keep (1sequence) ; inline
145
146 : (3sequence) ( obj1 obj2 obj3 seq -- seq )
147     [ 2 swap set-nth-unsafe ] keep (2sequence) ; inline
148
149 : (4sequence) ( obj1 obj2 obj3 obj4 seq -- seq )
150     [ 3 swap set-nth-unsafe ] keep (3sequence) ; inline
151
152 PRIVATE>
153
154 : 1sequence ( obj exemplar -- seq )
155     1 swap [ (1sequence) ] new-like ; inline
156
157 : 2sequence ( obj1 obj2 exemplar -- seq )
158     2 swap [ (2sequence) ] new-like ; inline
159
160 : 3sequence ( obj1 obj2 obj3 exemplar -- seq )
161     3 swap [ (3sequence) ] new-like ; inline
162
163 : 4sequence ( obj1 obj2 obj3 obj4 exemplar -- seq )
164     4 swap [ (4sequence) ] new-like ; inline
165
166 : first2 ( seq -- first second )
167     1 swap bounds-check nip first2-unsafe ; inline
168
169 : first3 ( seq -- first second third )
170     2 swap bounds-check nip first3-unsafe ; inline
171
172 : first4 ( seq -- first second third fourth )
173     3 swap bounds-check nip first4-unsafe ; inline
174
175 : ?nth ( n seq -- elt/f )
176     2dup bounds-check? [ nth-unsafe ] [ 2drop f ] if ; inline
177
178 : ?set-nth ( elt n seq -- )
179     2dup bounds-check? [ set-nth-unsafe ] [ 3drop ] if ; inline
180
181 : ?first ( seq -- elt/f ) 0 swap ?nth ; inline
182 : ?second ( seq -- elt/f ) 1 swap ?nth ; inline
183 : ?last ( seq -- elt/f )
184     [ length 1 - ] keep over 0 <
185     [ 2drop f ] [ nth-unsafe ] if ; inline
186
187 MIXIN: virtual-sequence
188 GENERIC: virtual-exemplar ( seq -- seq' )
189 GENERIC: virtual@ ( n seq -- n' seq' )
190
191 M: virtual-sequence nth virtual@ nth ; inline
192 M: virtual-sequence set-nth virtual@ set-nth ; inline
193 M: virtual-sequence nth-unsafe virtual@ nth-unsafe ; inline
194 M: virtual-sequence set-nth-unsafe virtual@ set-nth-unsafe ; inline
195 M: virtual-sequence like virtual-exemplar like ; inline
196 M: virtual-sequence new-sequence virtual-exemplar new-sequence ; inline
197
198 INSTANCE: virtual-sequence sequence
199
200 ! A reversal of an underlying sequence.
201 TUPLE: reversed { seq read-only } ;
202
203 C: <reversed> reversed
204
205 M: reversed virtual-exemplar seq>> ; inline
206 M: reversed virtual@ seq>> [ length swap - 1 - ] keep ; inline
207 M: reversed length seq>> length ; inline
208
209 INSTANCE: reversed virtual-sequence
210
211 ! A slice of another sequence.
212 TUPLE: slice
213 { from read-only }
214 { to read-only }
215 { seq read-only } ;
216
217 : collapse-slice ( m n slice -- m' n' seq )
218     [ from>> ] [ seq>> ] bi [ [ + ] curry bi@ ] dip ; inline
219
220 ERROR: slice-error from to seq reason ;
221
222 : check-slice-error ( from to seq ? string -- from to seq )
223     [ slice-error ] curry when ; inline
224
225 : check-slice ( from to seq -- from to seq )
226     3dup
227     [ 2drop 0 < "start < 0" check-slice-error ]
228     [ [ drop ] 2dip length > "end > sequence" check-slice-error ]
229     [ drop > "start > end" check-slice-error ]
230     3tri ; inline
231
232 <PRIVATE
233
234 : <slice-unsafe> ( from to seq -- slice )
235     dup slice? [ collapse-slice ] when slice boa ; inline
236
237 PRIVATE>
238
239 : <slice> ( from to seq -- slice )
240     check-slice <slice-unsafe> ; inline
241
242 M: slice virtual-exemplar seq>> ; inline
243
244 M: slice virtual@ [ from>> + ] [ seq>> ] bi ; inline
245
246 M: slice length [ to>> ] [ from>> ] bi - ; inline
247
248 : short ( seq n -- seq n' ) over length min ; inline
249
250 : head-slice ( seq n -- slice ) (head) <slice> ; inline
251
252 : tail-slice ( seq n -- slice ) (tail) <slice> ; inline
253
254 : rest-slice ( seq -- slice ) 1 tail-slice ; inline
255
256 : head-slice* ( seq n -- slice ) from-end head-slice ; inline
257
258 : tail-slice* ( seq n -- slice ) from-end tail-slice ; inline
259
260 : but-last-slice ( seq -- slice ) 1 head-slice* ; inline
261
262 INSTANCE: slice virtual-sequence
263
264 ! One element repeated many times
265 TUPLE: repetition { len read-only } { elt read-only } ;
266
267 C: <repetition> repetition
268
269 M: repetition length len>> ; inline
270 M: repetition nth-unsafe nip elt>> ; inline
271
272 INSTANCE: repetition immutable-sequence
273
274 <PRIVATE
275
276 ERROR: integer-length-expected obj ;
277
278 : check-length ( n -- n )
279     dup integer? [ integer-length-expected ] unless ; inline
280
281 TUPLE: copy-state
282     { src-i read-only }
283     { src read-only }
284     { dst-i read-only }
285     { dst read-only } ;
286
287 C: <copy> copy-state
288
289 : ((copy)) ( n copy -- )
290     [ [ src-i>> + ] [ src>> ] bi nth-unsafe ]
291     [ [ dst-i>> + ] [ dst>> ] bi set-nth-unsafe ] 2bi ; inline
292
293 : (copy) ( n copy -- dst )
294     over 0 <= [ nip dst>> ] [
295         [ 1 - ] dip [ ((copy)) ] [ (copy) ] 2bi
296     ] if ; inline recursive
297
298 : subseq>copy ( from to seq -- n copy )
299     [ over - check-length swap ] dip
300     3dup nip new-sequence 0 swap <copy> ; inline
301
302 : bounds-check-head ( n seq -- n seq )
303     over 0 < [ bounds-error ] when ; inline
304
305 : check-copy ( src n dst -- src n dst )
306     3dup bounds-check-head
307     [ swap length + ] dip lengthen ; inline
308
309 : copy-unsafe ( src i dst -- )
310     #! The check-length call forces partial dispatch
311     [ [ length check-length 0 ] keep ] 2dip <copy> (copy) drop ; inline
312
313 PRIVATE>
314
315 : subseq ( from to seq -- subseq )
316     [ check-slice subseq>copy (copy) ] keep like ;
317
318 : head ( seq n -- headseq ) (head) subseq ;
319
320 : tail ( seq n -- tailseq ) (tail) subseq ;
321
322 : rest ( seq -- tailseq ) 1 tail ;
323
324 : head* ( seq n -- headseq ) from-end head ;
325
326 : tail* ( seq n -- tailseq ) from-end tail ;
327
328 : but-last ( seq -- headseq ) 1 head* ;
329
330 : copy ( src i dst -- )
331     check-copy copy-unsafe ; inline
332
333 M: sequence clone-like
334     [ dup length ] dip new-sequence [ 0 swap copy-unsafe ] keep ; inline
335
336 M: immutable-sequence clone-like like ; inline
337
338 : push-all ( src dst -- ) [ length ] [ copy ] bi ; inline
339
340 <PRIVATE
341
342 : (append) ( seq1 seq2 accum -- accum )
343     [ [ over length ] dip copy-unsafe ]
344     [ 0 swap copy-unsafe ]
345     [ ] tri ; inline
346
347 PRIVATE>
348
349 : append-as ( seq1 seq2 exemplar -- newseq )
350     [ 2dup [ length ] bi@ + ] dip
351     [ (append) ] new-like ; inline
352
353 : 3append-as ( seq1 seq2 seq3 exemplar -- newseq )
354     [ 3dup [ length ] tri@ + + ] dip [
355         [ [ 2over [ length ] bi@ + ] dip copy-unsafe ]
356         [ (append) ] bi
357     ] new-like ; inline
358
359 : append ( seq1 seq2 -- newseq ) over append-as ;
360
361 : prepend-as ( seq1 seq2 exemplar -- newseq ) swapd append-as ; inline
362
363 : prepend ( seq1 seq2 -- newseq ) over prepend-as ;
364
365 : 3append ( seq1 seq2 seq3 -- newseq ) pick 3append-as ;
366
367 : surround ( seq1 seq2 seq3 -- newseq ) swapd 3append ; inline
368
369 : glue ( seq1 seq2 seq3 -- newseq ) swap 3append ; inline
370
371 : change-nth ( ..a i seq quot: ( ..a elt -- ..b newelt ) -- ..b )
372     [ [ nth ] dip call ] 3keep drop set-nth-unsafe ; inline
373
374 : min-length ( seq1 seq2 -- n ) [ length ] bi@ min ; inline
375
376 : max-length ( seq1 seq2 -- n ) [ length ] bi@ max ; inline
377
378 <PRIVATE
379
380 : ((each)) ( seq -- n quot )
381     [ length ] keep [ nth-unsafe ] curry ; inline
382
383 : (each) ( seq quot -- n quot' )
384     [ ((each)) ] dip compose ; inline
385
386 : (each-index) ( seq quot -- n quot' )
387     [ ((each)) [ keep ] curry ] dip compose ; inline
388
389 : (collect) ( quot into -- quot' )
390     [ [ keep ] dip set-nth-unsafe ] 2curry ; inline
391
392 : collect ( n quot into -- )
393     (collect) each-integer ; inline
394
395 : map-into ( seq quot into -- )
396     [ (each) ] dip collect ; inline
397
398 : 2nth-unsafe ( n seq1 seq2 -- elt1 elt2 )
399     [ nth-unsafe ] bi-curry@ bi ; inline
400
401 : ((2each)) ( seq1 seq2 -- n quot )
402     [ min-length ] 2keep [ 2nth-unsafe ] 2curry ; inline
403
404 : (2each) ( seq1 seq2 quot -- n quot' )
405     [ ((2each)) ] dip compose ; inline
406
407 : 3nth-unsafe ( n seq1 seq2 seq3 -- elt1 elt2 elt3 )
408     [ nth-unsafe ] tri-curry@ tri ; inline
409
410 : (3each) ( seq1 seq2 seq3 quot -- n quot' )
411     [
412         [ [ length ] tri@ min min ]
413         [ [ 3nth-unsafe ] 3curry ] 3bi
414     ] dip compose ; inline
415
416 : finish-find ( i seq -- i elt )
417     over [ dupd nth-unsafe ] [ drop f ] if ; inline
418
419 : (find) ( seq quot quot' -- i elt )
420     pick [ [ (each) ] dip call ] dip finish-find ; inline
421
422 : (find-from) ( n seq quot quot' -- i elt )
423     [ 2dup bounds-check? ] 2dip
424     [ (find) ] 2curry
425     [ 2drop f f ]
426     if ; inline
427
428 : (find-index) ( seq quot quot' -- i elt )
429     pick [ [ (each-index) ] dip call ] dip finish-find ; inline
430
431 : (find-index-from) ( n seq quot quot' -- i elt )
432     [ 2dup bounds-check? ] 2dip
433     [ (find-index) ] 2curry
434     [ 2drop f f ]
435     if ; inline
436
437 : (accumulate) ( seq identity quot -- identity seq quot )
438     swapd [ curry keep ] curry ; inline
439
440 PRIVATE>
441
442 : each ( ... seq quot: ( ... x -- ... ) -- ... )
443     (each) each-integer ; inline
444
445 : reduce ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... result )
446     swapd each ; inline
447
448 : map-integers ( ... len quot: ( ... i -- ... elt ) exemplar -- ... newseq )
449     [ over ] dip [ [ collect ] keep ] new-like ; inline
450
451 : map-as ( ... seq quot: ( ... elt -- ... newelt ) exemplar -- ... newseq )
452     [ (each) ] dip map-integers ; inline
453
454 : map ( ... seq quot: ( ... elt -- ... newelt ) -- ... newseq )
455     over map-as ; inline
456
457 : replicate-as ( ... len quot: ( ... -- ... newelt ) exemplar -- ... newseq )
458     [ [ drop ] prepose ] dip map-integers ; inline
459
460 : replicate ( ... len quot: ( ... -- ... newelt ) -- ... newseq )
461     { } replicate-as ; inline
462
463 : map! ( ... seq quot: ( ... elt -- ... newelt ) -- ... seq )
464     over [ map-into ] keep ; inline
465
466 : accumulate-as ( ... seq identity quot: ( ... prev elt -- ... next ) exemplar -- ... final newseq )
467     [ (accumulate) ] dip map-as ; inline
468
469 : accumulate ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... final newseq )
470     pick accumulate-as ; inline
471
472 : accumulate! ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... final seq )
473     (accumulate) map! ; inline
474
475 : 2each ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ) -- ... )
476     (2each) each-integer ; inline
477
478 : 2reduce ( ... seq1 seq2 identity quot: ( ... prev elt1 elt2 -- ... next ) -- ... result )
479     [ -rot ] dip 2each ; inline
480
481 : 2map-as ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... newelt ) exemplar -- ... newseq )
482     [ (2each) ] dip map-integers ; inline
483
484 : 2map ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... newelt ) -- ... newseq )
485     pick 2map-as ; inline
486
487 : 2all? ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ? ) -- ... ? )
488     (2each) all-integers? ; inline
489
490 : 3each ( ... seq1 seq2 seq3 quot: ( ... elt1 elt2 elt3 -- ... ) -- ... )
491     (3each) each-integer ; inline
492
493 : 3map-as ( ... seq1 seq2 seq3 quot: ( ... elt1 elt2 elt3 -- ... newelt ) exemplar -- ... newseq )
494     [ (3each) ] dip map-integers ; inline
495
496 : 3map ( ... seq1 seq2 seq3 quot: ( ... elt1 elt2 elt3 -- ... newelt ) -- ... newseq )
497     [ pick ] dip swap 3map-as ; inline
498
499 : find-from ( ... n seq quot: ( ... elt -- ... ? ) -- ... i elt )
500     [ (find-integer) ] (find-from) ; inline
501
502 : find ( ... seq quot: ( ... elt -- ... ? ) -- ... i elt )
503     [ find-integer ] (find) ; inline
504
505 : find-last-from ( ... n seq quot: ( ... elt -- ... ? ) -- ... i elt )
506     [ nip find-last-integer ] (find-from) ; inline
507
508 : find-last ( ... seq quot: ( ... elt -- ... ? ) -- ... i elt )
509     [ [ 1 - ] dip find-last-integer ] (find) ; inline
510
511 : find-index-from ( ... n seq quot: ( ... elt i -- ... ? ) -- ... i elt )
512     [ (find-integer) ] (find-index-from) ; inline
513
514 : find-index ( ... seq quot: ( ... elt i -- ... ? ) -- ... i elt )
515     [ find-integer ] (find-index) ; inline
516
517 : all? ( ... seq quot: ( ... elt -- ... ? ) -- ... ? )
518     (each) all-integers? ; inline
519
520 : push-if ( ..a elt quot: ( ..a elt -- ..b ? ) accum -- ..b )
521     [ keep ] dip rot [ push ] [ 2drop ] if ; inline
522
523 : selector-for ( quot exemplar -- selector accum )
524     [ length ] keep new-resizable [ [ push-if ] 2curry ] keep ; inline
525
526 : selector ( quot -- selector accum )
527     V{ } selector-for ; inline
528
529 : filter-as ( ... seq quot: ( ... elt -- ... ? ) exemplar -- ... subseq )
530     dup [ selector-for [ each ] dip ] curry dip like ; inline
531
532 : filter ( ... seq quot: ( ... elt -- ... ? ) -- ... subseq )
533     over filter-as ; inline
534
535 : push-either ( ..a elt quot: ( ..a elt -- ..b ? ) accum1 accum2 -- ..b )
536     [ keep swap ] 2dip ? push ; inline
537
538 : 2selector ( quot -- selector accum1 accum2 )
539     V{ } clone V{ } clone [ [ push-either ] 3curry ] 2keep ; inline
540
541 : partition ( ... seq quot: ( ... elt -- ... ? ) -- ... trueseq falseseq )
542     over [ 2selector [ each ] 2dip ] dip [ like ] curry bi@ ; inline
543
544 : collector-for ( quot exemplar -- quot' vec )
545     [ length ] keep new-resizable [ [ push ] curry compose ] keep ; inline
546
547 : collector ( quot -- quot' vec )
548     V{ } collector-for ; inline
549
550 : produce-as ( ..a pred: ( ..a -- ..b ? ) quot: ( ..b -- ..a obj ) exemplar -- ..b seq )
551     dup [ collector-for [ while ] dip ] curry dip like ; inline
552
553 : produce ( ..a pred: ( ..a -- ..b ? ) quot: ( ..b -- ..a obj ) -- ..b seq )
554     { } produce-as ; inline
555
556 : follow ( ... obj quot: ( ... prev -- ... result/f ) -- ... seq )
557     [ dup ] swap [ keep ] curry produce nip ; inline
558
559 : each-index ( ... seq quot: ( ... elt index -- ... ) -- ... )
560     (each-index) each-integer ; inline
561
562 : interleave ( ... seq between quot: ( ... elt -- ... ) -- ... )
563     pick empty? [ 3drop ] [
564         [ [ drop first-unsafe ] dip call ]
565         [ [ rest-slice ] 2dip [ bi* ] 2curry each ]
566         3bi
567     ] if ; inline
568
569 : map-index ( ... seq quot: ( ... elt index -- ... newelt ) -- ... newseq )
570     [ dup length iota ] dip 2map ; inline
571
572 : reduce-index ( ... seq identity quot: ( ... prev elt index -- ... next ) -- ... result )
573     swapd each-index ; inline
574
575 : index ( obj seq -- n )
576     [ = ] with find drop ;
577
578 : index-from ( obj i seq -- n )
579     rot [ = ] curry find-from drop ;
580
581 : last-index ( obj seq -- n )
582     [ = ] with find-last drop ;
583
584 : last-index-from ( obj i seq -- n )
585     rot [ = ] curry find-last-from drop ;
586
587 <PRIVATE
588
589 : (indices) ( elt i obj accum -- )
590     [ swap [ = ] dip ] dip [ push ] 2curry when ; inline
591
592 PRIVATE>
593
594 : indices ( obj seq -- indices )
595     swap V{ } clone
596     [ [ (indices) ] 2curry each-index ] keep ;
597
598 : nths ( indices seq -- seq' )
599     [ [ nth ] curry ] keep map-as ;
600
601 : any? ( ... seq quot: ( ... elt -- ... ? ) -- ... ? )
602     find drop >boolean ; inline
603
604 : member? ( elt seq -- ? )
605     [ = ] with any? ;
606
607 : member-eq? ( elt seq -- ? )
608     [ eq? ] with any? ;
609
610 : remove ( elt seq -- newseq )
611     [ = not ] with filter ;
612
613 : remove-eq ( elt seq -- newseq )
614     [ eq? not ] with filter ;
615
616 : sift ( seq -- newseq )
617     [ ] filter ;
618
619 : harvest ( seq -- newseq )
620     [ empty? not ] filter ;
621
622 <PRIVATE
623
624 : mismatch-unsafe ( n seq1 seq2 -- i )
625     [ 2nth-unsafe = not ] 2curry find-integer ; inline
626
627 PRIVATE>
628
629 : mismatch ( seq1 seq2 -- i )
630     [ min-length ] 2keep mismatch-unsafe ; inline
631
632 M: sequence <=>
633     [ mismatch ] 2keep pick
634     [ 2nth-unsafe <=> ] [ [ length ] compare nip ] if ;
635
636 : sequence= ( seq1 seq2 -- ? )
637     2dup [ length ] bi@ dupd =
638     [ -rot mismatch-unsafe not ] [ 3drop f ] if ; inline
639
640 ERROR: assert-sequence got expected ;
641
642 : assert-sequence= ( a b -- )
643     2dup sequence= [ 2drop ] [ assert-sequence ] if ;
644
645 <PRIVATE
646
647 : sequence-hashcode-step ( oldhash newpart -- newhash )
648     integer>fixnum swap [
649         [ -2 fixnum-shift-fast ] [ 5 fixnum-shift-fast ] bi
650         fixnum+fast fixnum+fast
651     ] keep fixnum-bitxor ; inline
652
653 PRIVATE>
654
655 : sequence-hashcode ( n seq -- x )
656     [ 0 ] 2dip [ hashcode* sequence-hashcode-step ] with each ; inline
657
658 M: reversed equal? over reversed? [ sequence= ] [ 2drop f ] if ;
659
660 M: slice equal? over slice? [ sequence= ] [ 2drop f ] if ;
661
662 : move ( to from seq -- )
663     2over =
664     [ 3drop ] [ [ nth swap ] [ set-nth ] bi ] if ; inline
665
666 <PRIVATE
667
668 : move-unsafe ( to from seq -- )
669     2over =
670     [ 3drop ] [ [ nth-unsafe swap ] [ set-nth-unsafe ] bi ] if ; inline
671
672 : (filter!) ( ... quot: ( ... elt -- ... ? ) store scan seq -- ... )
673     2dup length < [
674         [ move-unsafe ] 3keep
675         [ nth-unsafe pick call [ 1 + ] when ] 2keep
676         [ 1 + ] dip
677         (filter!)
678     ] [ nip set-length drop ] if ; inline recursive
679
680 PRIVATE>
681
682 : filter! ( ... seq quot: ( ... elt -- ... ? ) -- ... seq )
683     swap [ [ 0 0 ] dip (filter!) ] keep ; inline
684
685 : remove! ( elt seq -- seq )
686     [ = not ] with filter! ;
687
688 : remove-eq! ( elt seq -- seq )
689     [ eq? not ] with filter! ;
690
691 : prefix ( seq elt -- newseq )
692     over [ over length 1 + ] dip [
693         (1sequence) [ 1 swap copy-unsafe ] keep
694     ] new-like ;
695
696 : suffix ( seq elt -- newseq )
697     over [ over length 1 + ] dip [
698         [ [ over length ] dip set-nth-unsafe ] keep
699         [ 0 swap copy-unsafe ] keep
700     ] new-like ;
701
702 : suffix! ( seq elt -- seq ) over push ; inline
703
704 : append! ( seq1 seq2 -- seq1 ) over push-all ; inline
705
706 : last ( seq -- elt )
707     [ length 1 - ] keep
708     over 0 < [ bounds-error ] [ nth-unsafe ] if ; inline
709
710 <PRIVATE
711
712 : last-unsafe ( seq -- elt ) [ length 1 - ] [ nth-unsafe ] bi ;
713
714 PRIVATE>
715
716 : set-last ( elt seq -- )
717     [ length 1 - ] keep
718     over 0 < [ bounds-error ] [ set-nth-unsafe ] if ; inline
719
720 : pop* ( seq -- ) [ length 1 - ] [ shorten ] bi ;
721
722 <PRIVATE
723
724 : move-backward ( shift from to seq -- )
725     2over = [
726         4drop
727     ] [
728         [ [ 2over + pick ] dip move-unsafe [ 1 + ] dip ] keep
729         move-backward
730     ] if ;
731
732 : move-forward ( shift from to seq -- )
733     2over = [
734         4drop
735     ] [
736         [ [ pick [ dup dup ] dip + swap ] dip move-unsafe 1 - ] keep
737         move-forward
738     ] if ;
739
740 : (open-slice) ( shift from to seq ? -- )
741     [
742         [ [ 1 - ] bi@ ] dip move-forward
743     ] [
744         [ over - ] 2dip move-backward
745     ] if ;
746
747 : open-slice ( shift from seq -- )
748     pick 0 = [
749         3drop
750     ] [
751         pick over length + over
752         [ pick 0 > [ [ length ] keep ] dip (open-slice) ] 2dip
753         set-length
754     ] if ;
755
756 PRIVATE>
757
758 : delete-slice ( from to seq -- )
759     check-slice [ over [ - ] dip ] dip open-slice ;
760
761 : remove-nth! ( n seq -- seq )
762     [ [ dup 1 + ] dip delete-slice ] keep ;
763
764 : snip ( from to seq -- head tail )
765     [ swap head ] [ swap tail ] bi-curry bi* ; inline
766
767 : snip-slice ( from to seq -- head tail )
768     [ swap head-slice ] [ swap tail-slice ] bi-curry bi* ; inline
769
770 : replace-slice ( new from to seq -- seq' )
771     snip-slice surround ;
772
773 : remove-nth ( n seq -- seq' )
774     [ [ { } ] dip dup 1 + ] dip replace-slice ;
775
776 : pop ( seq -- elt )
777     [ length 1 - ] keep over 0 >=
778     [ [ nth-unsafe ] [ shorten ] 2bi ]
779     [ bounds-error ] if ;
780
781 : exchange ( m n seq -- )
782     [ nip bounds-check 2drop ]
783     [ bounds-check 3drop ]
784     [ exchange-unsafe ]
785     3tri ;
786
787 : midpoint@ ( seq -- n ) length 2/ ; inline
788
789 : reverse! ( seq -- seq )
790     [
791         [ midpoint@ iota ] [ length ] [ ] tri
792         [ [ over - 1 - ] dip exchange-unsafe ] 2curry each
793     ] keep ;
794
795 : reverse ( seq -- newseq )
796      [
797         dup [ length ] keep new-sequence
798         [ 0 swap copy-unsafe ] keep reverse!
799     ] keep like ;
800
801 : sum-lengths ( seq -- n )
802     0 [ length + ] reduce ;
803
804 : concat-as ( seq exemplar -- newseq )
805     swap [ { } ] [
806         [ sum-lengths over new-resizable ] keep
807         [ append! ] each
808     ] if-empty swap like ;
809
810 : concat ( seq -- newseq )
811     [ { } ] [ dup first concat-as ] if-empty ;
812
813 <PRIVATE
814
815 : joined-length ( seq glue -- n )
816     [ [ sum-lengths ] [ length 1 [-] ] bi ] dip length * + ;
817
818 PRIVATE>
819
820 : join-as ( seq glue exemplar -- newseq )
821     over empty? [ nip concat-as ] [
822         [
823             2dup joined-length over new-resizable [
824                 [ [ push-all ] 2curry ]
825                 [ nip [ push-all ] curry ] 2bi
826                 interleave
827             ] keep
828         ] dip like
829     ] if ;
830
831 : join ( seq glue -- newseq )
832     dup join-as ; inline
833
834 : padding ( ... seq n elt quot: ( ... seq1 seq2 -- ... newseq ) -- ... newseq )
835     [
836         [ over length [-] dup 0 = [ drop ] ] dip
837         [ <repetition> ] curry
838     ] dip compose if ; inline
839
840 : pad-head ( seq n elt -- padded )
841     [ swap dup append-as ] padding ;
842
843 : pad-tail ( seq n elt -- padded )
844     [ append ] padding ;
845
846 : shorter? ( seq1 seq2 -- ? ) [ length ] bi@ < ;
847
848 : head? ( seq begin -- ? )
849     2dup shorter? [
850         2drop f
851     ] [
852         [ nip ] [ length head-slice ] 2bi sequence=
853     ] if ;
854
855 : tail? ( seq end -- ? )
856     2dup shorter? [
857         2drop f
858     ] [
859         [ nip ] [ length tail-slice* ] 2bi sequence=
860     ] if ;
861
862 : cut-slice ( seq n -- before-slice after-slice )
863     [ head-slice ] [ tail-slice ] 2bi ; inline
864
865 : insert-nth ( elt n seq -- seq' )
866     swap cut-slice [ swap suffix ] dip append ;
867
868 : halves ( seq -- first-slice second-slice )
869     dup midpoint@ cut-slice ; inline
870
871 <PRIVATE
872
873 : nth2-unsafe ( n seq -- a b )
874     [ nth-unsafe ] [ [ 1 + ] dip nth-unsafe ] 2bi ; inline
875
876 : nth3-unsafe ( n seq -- a b c )
877     [ nth2-unsafe ] [ [ 2 + ] dip nth-unsafe ] 2bi ; inline
878
879 : (binary-reduce) ( ... seq start quot: ( ... elt1 elt2 -- ... newelt ) from length -- ... value )
880     #! We can't use case here since combinators depends on
881     #! sequences
882     dup 4 < [
883         integer>fixnum {
884             [ 2drop nip ]
885             [ 2nip swap nth-unsafe ]
886             [ -rot [ drop swap nth2-unsafe ] dip call ]
887             [ -rot [ drop swap nth3-unsafe ] dip bi@ ]
888         } dispatch
889     ] [
890         [ 2/ ] [ over - ] bi [ 2dup + ] dip
891         [ (binary-reduce) ] [ 2curry ] curry 2bi@
892         pick [ 3bi ] dip call
893     ] if ; inline recursive
894
895 PRIVATE>
896
897 : binary-reduce ( ... seq start quot: ( ... elt1 elt2 -- ... newelt ) -- ... value )
898     pick length 0 max 0 swap (binary-reduce) ; inline
899
900 : cut ( seq n -- before after )
901     [ head ] [ tail ] 2bi ;
902
903 : cut* ( seq n -- before after )
904     [ head* ] [ tail* ] 2bi ;
905
906 <PRIVATE
907
908 : (start) ( subseq seq n -- subseq seq ? )
909     pick length iota [
910         [ 3dup ] dip [ + swap nth-unsafe ] keep rot nth-unsafe =
911     ] all? nip ; inline
912
913 PRIVATE>
914
915 : start* ( subseq seq n -- i )
916     pick length pick length swap - 1 + iota
917     [ (start) ] find-from
918     swap [ 3drop ] dip ;
919
920 : start ( subseq seq -- i ) 0 start* ; inline
921
922 : subseq? ( subseq seq -- ? ) start >boolean ;
923
924 : drop-prefix ( seq1 seq2 -- slice1 slice2 )
925     2dup mismatch [ 2dup min-length ] unless*
926     [ tail-slice ] curry bi@ ;
927
928 : unclip ( seq -- rest first )
929     [ rest ] [ first-unsafe ] bi ;
930
931 : unclip-last ( seq -- butlast last )
932     [ but-last ] [ last-unsafe ] bi ;
933
934 : unclip-slice ( seq -- rest-slice first )
935     [ rest-slice ] [ first-unsafe ] bi ; inline
936
937 : map-reduce ( ..a seq map-quot: ( ..a elt -- ..b intermediate ) reduce-quot: ( ..b prev intermediate -- ..a next ) -- ..a result )
938     [ [ dup first ] dip [ call ] keep ] dip compose
939     swapd [ 1 ] 2dip (each) (each-integer) ; inline
940
941 : 2map-reduce ( ..a seq1 seq2 map-quot: ( ..a elt1 elt2 -- ..b intermediate ) reduce-quot: ( ..b prev intermediate -- ..a next ) -- ..a result )
942     [ [ 2dup [ first ] bi@ ] dip [ call ] keep ] dip compose
943     [ -rot ] dip [ 1 ] 3dip (2each) (each-integer) ; inline
944
945 <PRIVATE
946
947 : (map-find) ( seq quot find-quot -- result elt )
948     [ [ f ] 2dip [ [ nip ] dip call dup ] curry ] dip call
949     [ [ drop f ] unless ] dip ; inline
950
951 PRIVATE>
952
953 : map-find ( ... seq quot: ( ... elt -- ... result/f ) -- ... result elt )
954     [ find ] (map-find) ; inline
955
956 : map-find-last ( ... seq quot: ( ... elt -- ... result/f ) -- ... result elt )
957     [ find-last ] (map-find) ; inline
958
959 : unclip-last-slice ( seq -- butlast-slice last )
960     [ but-last-slice ] [ last-unsafe ] bi ; inline
961
962 <PRIVATE
963
964 : (trim-head) ( seq quot -- seq n )
965     over [ [ not ] compose find drop ] dip
966     [ length or ] keep swap ; inline
967
968 : (trim-tail) ( seq quot -- seq n )
969     over [ [ not ] compose find-last drop ?1+ ] dip
970     swap ; inline
971
972 PRIVATE>
973
974 : trim-head-slice ( ... seq quot: ( ... elt -- ... ? ) -- ... slice )
975     (trim-head) tail-slice ; inline
976
977 : trim-head ( ... seq quot: ( ... elt -- ... ? ) -- ... newseq )
978     (trim-head) tail ; inline
979
980 : trim-tail-slice ( ... seq quot: ( ... elt -- ... ? ) -- ... slice )
981     (trim-tail) head-slice ; inline
982
983 : trim-tail ( ... seq quot: ( ... elt -- ... ? ) -- ... newseq )
984     (trim-tail) head ; inline
985
986 : trim-slice ( ... seq quot: ( ... elt -- ... ? ) -- ... slice )
987     [ trim-head-slice ] [ trim-tail-slice ] bi ; inline
988
989 : trim ( ... seq quot: ( ... elt -- ... ? ) -- ... newseq )
990     [ trim-slice ] [ drop ] 2bi like ; inline
991
992 GENERIC: sum ( seq -- n )
993 M: object sum 0 [ + ] binary-reduce ; inline
994
995 : product ( seq -- n ) 1 [ * ] binary-reduce ;
996
997 : infimum ( seq -- n ) [ ] [ min ] map-reduce ;
998
999 : supremum ( seq -- n ) [ ] [ max ] map-reduce ;
1000
1001 : map-sum ( ... seq quot: ( ... elt -- ... n ) -- ... n )
1002     [ 0 ] 2dip [ dip + ] curry [ swap ] prepose each ; inline
1003
1004 : count ( ... seq quot: ( ... elt -- ... ? ) -- ... n )
1005     [ 1 0 ? ] compose map-sum ; inline
1006
1007 : cartesian-each ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ) -- ... )
1008     [ with each ] 2curry each ; inline
1009
1010 : cartesian-map ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... newelt ) -- ... newseq )
1011     [ with map ] 2curry map ; inline
1012
1013 : cartesian-product ( seq1 seq2 -- newseq )
1014     [ { } 2sequence ] cartesian-map ;
1015
1016 ! We hand-optimize flip to such a degree because type hints
1017 ! cannot express that an array is an array of arrays yet, and
1018 ! this word happens to be performance-critical since the compiler
1019 ! itself uses it. Optimizing it like this reduced compile time.
1020 <PRIVATE
1021
1022 : generic-flip ( matrix -- newmatrix )
1023     [
1024         [ first-unsafe length 1 ] keep
1025         [ length min ] (each) (each-integer) iota
1026     ] keep
1027     [ [ nth-unsafe ] with { } map-as ] curry { } map-as ; inline
1028
1029 USE: arrays
1030
1031 : array-length ( array -- len )
1032     { array } declare length>> ; inline
1033
1034 : array-flip ( matrix -- newmatrix )
1035     { array } declare
1036     [
1037         [ first-unsafe array-length 1 ] keep
1038         [ array-length min ] (each) (each-integer) iota
1039     ] keep
1040     [ [ { array } declare array-nth ] with { } map-as ] curry { } map-as ;
1041
1042 PRIVATE>
1043
1044 : flip ( matrix -- newmatrix )
1045     dup empty? [
1046         dup array? [
1047             dup [ array? ] all?
1048             [ array-flip ] [ generic-flip ] if
1049         ] [ generic-flip ] if
1050     ] unless ;