]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences.factor
sequences: define a single "?set-nth" that is used.
[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 TUPLE: slice-error from to seq reason ;
221
222 : slice-error ( from to seq ? string -- from to seq )
223     [ \ slice-error boa throw ] curry when ; inline
224
225 : check-slice ( from to seq -- from to seq )
226     3dup
227     [ 2drop 0 < "start < 0" slice-error ]
228     [ [ drop ] 2dip length > "end > sequence" slice-error ]
229     [ drop > "start > end" 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>> ] [ [ 1 - ] dip [ ((copy)) ] [ (copy) ] 2bi ] if ;
295     inline recursive
296
297 : subseq>copy ( from to seq -- n copy )
298     [ over - check-length swap ] dip
299     3dup nip new-sequence 0 swap <copy> ; inline
300
301 : bounds-check-head ( n seq -- n seq )
302     over 0 < [ bounds-error ] when ; inline
303
304 : check-copy ( src n dst -- src n dst )
305     3dup bounds-check-head
306     [ swap length + ] dip lengthen ; inline
307
308 PRIVATE>
309
310 : subseq ( from to seq -- subseq )
311     [ check-slice subseq>copy (copy) ] keep like ;
312
313 : head ( seq n -- headseq ) (head) subseq ;
314
315 : tail ( seq n -- tailseq ) (tail) subseq ;
316
317 : rest ( seq -- tailseq ) 1 tail ;
318
319 : head* ( seq n -- headseq ) from-end head ;
320
321 : tail* ( seq n -- tailseq ) from-end tail ;
322
323 : but-last ( seq -- headseq ) 1 head* ;
324
325 : copy ( src i dst -- )
326     #! The check-length call forces partial dispatch
327     [ [ length check-length 0 ] keep ] 2dip
328     check-copy <copy> (copy) drop ; inline
329
330 M: sequence clone-like
331     [ dup length ] dip new-sequence [ 0 swap copy ] keep ; inline
332
333 M: immutable-sequence clone-like like ; inline
334
335 : push-all ( src dst -- ) [ length ] [ copy ] bi ; inline
336
337 <PRIVATE
338
339 : (append) ( seq1 seq2 accum -- accum )
340     [ [ over length ] dip copy ]
341     [ 0 swap copy ]
342     [ ] tri ; inline
343
344 PRIVATE>
345
346 : append-as ( seq1 seq2 exemplar -- newseq )
347     [ 2dup [ length ] bi@ + ] dip
348     [ (append) ] new-like ; inline
349
350 : 3append-as ( seq1 seq2 seq3 exemplar -- newseq )
351     [ 3dup [ length ] tri@ + + ] dip [
352         [ [ 2over [ length ] bi@ + ] dip copy ]
353         [ (append) ] bi
354     ] new-like ; inline
355
356 : append ( seq1 seq2 -- newseq ) over append-as ;
357
358 : prepend-as ( seq1 seq2 exemplar -- newseq ) swapd append-as ; inline
359
360 : prepend ( seq1 seq2 -- newseq ) over prepend-as ;
361
362 : 3append ( seq1 seq2 seq3 -- newseq ) pick 3append-as ;
363
364 : surround ( seq1 seq2 seq3 -- newseq ) swapd 3append ; inline
365
366 : glue ( seq1 seq2 seq3 -- newseq ) swap 3append ; inline
367
368 : change-nth ( ..a i seq quot: ( ..a elt -- ..b newelt ) -- ..b )
369     [ [ nth ] dip call ] 3keep drop set-nth-unsafe ; inline
370
371 : min-length ( seq1 seq2 -- n ) [ length ] bi@ min ; inline
372
373 : max-length ( seq1 seq2 -- n ) [ length ] bi@ max ; inline
374
375 <PRIVATE
376
377 : ((each)) ( seq -- n quot )
378     [ length ] keep [ nth-unsafe ] curry ; inline
379
380 : (each) ( seq quot -- n quot' )
381     [ ((each)) ] dip compose ; inline
382
383 : (each-index) ( seq quot -- n quot' )
384     [ ((each)) [ keep ] curry ] dip compose ; inline
385
386 : (collect) ( quot into -- quot' )
387     [ [ keep ] dip set-nth-unsafe ] 2curry ; inline
388
389 : collect ( n quot into -- )
390     (collect) each-integer ; inline
391
392 : map-into ( seq quot into -- )
393     [ (each) ] dip collect ; inline
394
395 : 2nth-unsafe ( n seq1 seq2 -- elt1 elt2 )
396     [ nth-unsafe ] bi-curry@ bi ; inline
397
398 : ((2each)) ( seq1 seq2 -- n quot )
399     [ min-length ] 2keep [ 2nth-unsafe ] 2curry ; inline
400
401 : (2each) ( seq1 seq2 quot -- n quot' )
402     [ ((2each)) ] dip compose ; inline
403
404 : 3nth-unsafe ( n seq1 seq2 seq3 -- elt1 elt2 elt3 )
405     [ nth-unsafe ] tri-curry@ tri ; inline
406
407 : (3each) ( seq1 seq2 seq3 quot -- n quot' )
408     [
409         [ [ length ] tri@ min min ]
410         [ [ 3nth-unsafe ] 3curry ] 3bi
411     ] dip compose ; inline
412
413 : finish-find ( i seq -- i elt )
414     over [ dupd nth-unsafe ] [ drop f ] if ; inline
415
416 : (find) ( seq quot quot' -- i elt )
417     pick [ [ (each) ] dip call ] dip finish-find ; inline
418
419 : (find-from) ( n seq quot quot' -- i elt )
420     [ 2dup bounds-check? ] 2dip
421     [ (find) ] 2curry
422     [ 2drop f f ]
423     if ; inline
424
425 : (find-index) ( seq quot quot' -- i elt )
426     pick [ [ (each-index) ] dip call ] dip finish-find ; inline
427
428 : (find-index-from) ( n seq quot quot' -- i elt )
429     [ 2dup bounds-check? ] 2dip
430     [ (find-index) ] 2curry
431     [ 2drop f f ]
432     if ; inline
433
434 : (accumulate) ( seq identity quot -- identity seq quot )
435     swapd [ curry keep ] curry ; inline
436
437 PRIVATE>
438
439 : each ( ... seq quot: ( ... x -- ... ) -- ... )
440     (each) each-integer ; inline
441
442 : reduce ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... result )
443     swapd each ; inline
444
445 : map-integers ( ... len quot: ( ... i -- ... elt ) exemplar -- ... newseq )
446     [ over ] dip [ [ collect ] keep ] new-like ; inline
447
448 : map-as ( ... seq quot: ( ... elt -- ... newelt ) exemplar -- ... newseq )
449     [ (each) ] dip map-integers ; inline
450
451 : map ( ... seq quot: ( ... elt -- ... newelt ) -- ... newseq )
452     over map-as ; inline
453
454 : replicate-as ( ... len quot: ( ... -- ... newelt ) exemplar -- ... newseq )
455     [ [ drop ] prepose ] dip map-integers ; inline
456
457 : replicate ( ... len quot: ( ... -- ... newelt ) -- ... newseq )
458     { } replicate-as ; inline
459
460 : map! ( ... seq quot: ( ... elt -- ... newelt ) -- ... seq )
461     over [ map-into ] keep ; inline
462
463 : accumulate-as ( ... seq identity quot: ( ... prev elt -- ... next ) exemplar -- ... final newseq )
464     [ (accumulate) ] dip map-as ; inline
465
466 : accumulate ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... final newseq )
467     pick accumulate-as ; inline
468
469 : accumulate! ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... final seq )
470     (accumulate) map! ; inline
471
472 : 2each ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ) -- ... )
473     (2each) each-integer ; inline
474
475 : 2reduce ( ... seq1 seq2 identity quot: ( ... prev elt1 elt2 -- ... next ) -- ... result )
476     [ -rot ] dip 2each ; inline
477
478 : 2map-as ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... newelt ) exemplar -- ... newseq )
479     [ (2each) ] dip map-integers ; inline
480
481 : 2map ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... newelt ) -- ... newseq )
482     pick 2map-as ; inline
483
484 : 2all? ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ? ) -- ... ? )
485     (2each) all-integers? ; inline
486
487 : 3each ( ... seq1 seq2 seq3 quot: ( ... elt1 elt2 elt3 -- ... ) -- ... )
488     (3each) each-integer ; inline
489
490 : 3map-as ( ... seq1 seq2 seq3 quot: ( ... elt1 elt2 elt3 -- ... newelt ) exemplar -- ... newseq )
491     [ (3each) ] dip map-integers ; inline
492
493 : 3map ( ... seq1 seq2 seq3 quot: ( ... elt1 elt2 elt3 -- ... newelt ) -- ... newseq )
494     [ pick ] dip swap 3map-as ; inline
495
496 : find-from ( ... n seq quot: ( ... elt -- ... ? ) -- ... i elt )
497     [ (find-integer) ] (find-from) ; inline
498
499 : find ( ... seq quot: ( ... elt -- ... ? ) -- ... i elt )
500     [ find-integer ] (find) ; inline
501
502 : find-last-from ( ... n seq quot: ( ... elt -- ... ? ) -- ... i elt )
503     [ nip find-last-integer ] (find-from) ; inline
504
505 : find-last ( ... seq quot: ( ... elt -- ... ? ) -- ... i elt )
506     [ [ 1 - ] dip find-last-integer ] (find) ; inline
507
508 : find-index-from ( ... n seq quot: ( ... elt i -- ... ? ) -- ... i elt )
509     [ (find-integer) ] (find-index-from) ; inline
510
511 : find-index ( ... seq quot: ( ... elt i -- ... ? ) -- ... i elt )
512     [ find-integer ] (find-index) ; inline
513
514 : all? ( ... seq quot: ( ... elt -- ... ? ) -- ... ? )
515     (each) all-integers? ; inline
516
517 : push-if ( ..a elt quot: ( ..a elt -- ..b ? ) accum -- ..b )
518     [ keep ] dip rot [ push ] [ 2drop ] if ; inline
519
520 : selector-for ( quot exemplar -- selector accum )
521     [ length ] keep new-resizable [ [ push-if ] 2curry ] keep ; inline
522
523 : selector ( quot -- selector accum )
524     V{ } selector-for ; inline
525
526 : filter-as ( ... seq quot: ( ... elt -- ... ? ) exemplar -- ... subseq )
527     dup [ selector-for [ each ] dip ] curry dip like ; inline
528
529 : filter ( ... seq quot: ( ... elt -- ... ? ) -- ... subseq )
530     over filter-as ; inline
531
532 : push-either ( ..a elt quot: ( ..a elt -- ..b ? ) accum1 accum2 -- ..b )
533     [ keep swap ] 2dip ? push ; inline
534
535 : 2selector ( quot -- selector accum1 accum2 )
536     V{ } clone V{ } clone [ [ push-either ] 3curry ] 2keep ; inline
537
538 : partition ( ... seq quot: ( ... elt -- ... ? ) -- ... trueseq falseseq )
539     over [ 2selector [ each ] 2dip ] dip [ like ] curry bi@ ; inline
540
541 : collector-for ( quot exemplar -- quot' vec )
542     [ length ] keep new-resizable [ [ push ] curry compose ] keep ; inline
543
544 : collector ( quot -- quot' vec )
545     V{ } collector-for ; inline
546
547 : produce-as ( ..a pred: ( ..a -- ..b ? ) quot: ( ..b -- ..a obj ) exemplar -- ..b seq )
548     dup [ collector-for [ while ] dip ] curry dip like ; inline
549
550 : produce ( ..a pred: ( ..a -- ..b ? ) quot: ( ..b -- ..a obj ) -- ..b seq )
551     { } produce-as ; inline
552
553 : follow ( ... obj quot: ( ... prev -- ... result/f ) -- ... seq )
554     [ dup ] swap [ keep ] curry produce nip ; inline
555
556 : each-index ( ... seq quot: ( ... elt index -- ... ) -- ... )
557     (each-index) each-integer ; inline
558
559 : interleave ( ... seq between quot: ( ... elt -- ... ) -- ... )
560     pick empty? [ 3drop ] [
561         [ [ drop first-unsafe ] dip call ]
562         [ [ rest-slice ] 2dip [ bi* ] 2curry each ]
563         3bi
564     ] if ; inline
565
566 : map-index ( ... seq quot: ( ... elt index -- ... newelt ) -- ... newseq )
567     [ dup length iota ] dip 2map ; inline
568
569 : reduce-index ( ... seq identity quot: ( ... prev elt index -- ... next ) -- ... result )
570     swapd each-index ; inline
571
572 : index ( obj seq -- n )
573     [ = ] with find drop ;
574
575 : index-from ( obj i seq -- n )
576     rot [ = ] curry find-from drop ;
577
578 : last-index ( obj seq -- n )
579     [ = ] with find-last drop ;
580
581 : last-index-from ( obj i seq -- n )
582     rot [ = ] curry find-last-from drop ;
583
584 <PRIVATE
585
586 : (indices) ( elt i obj accum -- )
587     [ swap [ = ] dip ] dip [ push ] 2curry when ; inline
588
589 PRIVATE>
590
591 : indices ( obj seq -- indices )
592     swap V{ } clone
593     [ [ (indices) ] 2curry each-index ] keep ;
594
595 : nths ( indices seq -- seq' )
596     [ [ nth ] curry ] keep map-as ;
597
598 : any? ( ... seq quot: ( ... elt -- ... ? ) -- ... ? )
599     find drop >boolean ; inline
600
601 : member? ( elt seq -- ? )
602     [ = ] with any? ;
603
604 : member-eq? ( elt seq -- ? )
605     [ eq? ] with any? ;
606
607 : remove ( elt seq -- newseq )
608     [ = not ] with filter ;
609
610 : remove-eq ( elt seq -- newseq )
611     [ eq? not ] with filter ;
612
613 : sift ( seq -- newseq )
614     [ ] filter ;
615
616 : harvest ( seq -- newseq )
617     [ empty? not ] filter ;
618
619 <PRIVATE
620
621 : mismatch-unsafe ( n seq1 seq2 -- i )
622     [ 2nth-unsafe = not ] 2curry find-integer ; inline
623
624 PRIVATE>
625
626 : mismatch ( seq1 seq2 -- i )
627     [ min-length ] 2keep mismatch-unsafe ; inline
628
629 M: sequence <=>
630     [ mismatch ] 2keep pick
631     [ 2nth-unsafe <=> ] [ [ length ] compare nip ] if ;
632
633 : sequence= ( seq1 seq2 -- ? )
634     2dup [ length ] bi@ dupd =
635     [ -rot mismatch-unsafe not ] [ 3drop f ] if ; inline
636
637 ERROR: assert-sequence got expected ;
638
639 : assert-sequence= ( a b -- )
640     2dup sequence= [ 2drop ] [ assert-sequence ] if ;
641
642 <PRIVATE
643
644 : sequence-hashcode-step ( oldhash newpart -- newhash )
645     integer>fixnum swap [
646         [ -2 fixnum-shift-fast ] [ 5 fixnum-shift-fast ] bi
647         fixnum+fast fixnum+fast
648     ] keep fixnum-bitxor ; inline
649
650 PRIVATE>
651
652 : sequence-hashcode ( n seq -- x )
653     [ 0 ] 2dip [ hashcode* sequence-hashcode-step ] with each ; inline
654
655 M: reversed equal? over reversed? [ sequence= ] [ 2drop f ] if ;
656
657 M: slice equal? over slice? [ sequence= ] [ 2drop f ] if ;
658
659 : move ( to from seq -- )
660     2over =
661     [ 3drop ] [ [ nth swap ] [ set-nth ] bi ] if ; inline
662
663 <PRIVATE
664
665 : move-unsafe ( to from seq -- )
666     2over =
667     [ 3drop ] [ [ nth-unsafe swap ] [ set-nth-unsafe ] bi ] if ; inline
668
669 : (filter!) ( ... quot: ( ... elt -- ... ? ) store scan seq -- ... )
670     2dup length < [
671         [ move-unsafe ] 3keep
672         [ nth-unsafe pick call [ 1 + ] when ] 2keep
673         [ 1 + ] dip
674         (filter!)
675     ] [ nip set-length drop ] if ; inline recursive
676
677 PRIVATE>
678
679 : filter! ( ... seq quot: ( ... elt -- ... ? ) -- ... seq )
680     swap [ [ 0 0 ] dip (filter!) ] keep ; inline
681
682 : remove! ( elt seq -- seq )
683     [ = not ] with filter! ;
684
685 : remove-eq! ( elt seq -- seq )
686     [ eq? not ] with filter! ;
687
688 : prefix ( seq elt -- newseq )
689     over [ over length 1 + ] dip [
690         (1sequence) [ 1 swap copy ] keep
691     ] new-like ;
692
693 : suffix ( seq elt -- newseq )
694     over [ over length 1 + ] dip [
695         [ [ over length ] dip set-nth-unsafe ] keep
696         [ 0 swap copy ] keep
697     ] new-like ;
698
699 : suffix! ( seq elt -- seq ) over push ; inline
700
701 : append! ( seq1 seq2 -- seq1 ) over push-all ; inline
702
703 : last ( seq -- elt )
704     [ length 1 - ] keep
705     over 0 < [ bounds-error ] [ nth-unsafe ] if ; inline
706
707 <PRIVATE
708
709 : last-unsafe ( seq -- elt ) [ length 1 - ] [ nth-unsafe ] bi ;
710
711 PRIVATE>
712
713 : set-last ( elt seq -- )
714     [ length 1 - ] keep
715     over 0 < [ bounds-error ] [ set-nth-unsafe ] if ; inline
716
717 : pop* ( seq -- ) [ length 1 - ] [ shorten ] bi ;
718
719 <PRIVATE
720
721 : move-backward ( shift from to seq -- )
722     2over = [
723         4drop
724     ] [
725         [ [ 2over + pick ] dip move-unsafe [ 1 + ] dip ] keep
726         move-backward
727     ] if ;
728
729 : move-forward ( shift from to seq -- )
730     2over = [
731         4drop
732     ] [
733         [ [ pick [ dup dup ] dip + swap ] dip move-unsafe 1 - ] keep
734         move-forward
735     ] if ;
736
737 : (open-slice) ( shift from to seq ? -- )
738     [
739         [ [ 1 - ] bi@ ] dip move-forward
740     ] [
741         [ over - ] 2dip move-backward
742     ] if ;
743
744 : open-slice ( shift from seq -- )
745     pick 0 = [
746         3drop
747     ] [
748         pick over length + over
749         [ pick 0 > [ [ length ] keep ] dip (open-slice) ] 2dip
750         set-length
751     ] if ;
752
753 PRIVATE>
754
755 : delete-slice ( from to seq -- )
756     check-slice [ over [ - ] dip ] dip open-slice ;
757
758 : remove-nth! ( n seq -- seq )
759     [ [ dup 1 + ] dip delete-slice ] keep ;
760
761 : snip ( from to seq -- head tail )
762     [ swap head ] [ swap tail ] bi-curry bi* ; inline
763
764 : snip-slice ( from to seq -- head tail )
765     [ swap head-slice ] [ swap tail-slice ] bi-curry bi* ; inline
766
767 : replace-slice ( new from to seq -- seq' )
768     snip-slice surround ;
769
770 : remove-nth ( n seq -- seq' )
771     [ [ { } ] dip dup 1 + ] dip replace-slice ;
772
773 : pop ( seq -- elt )
774     [ length 1 - ] keep over 0 >=
775     [ [ nth-unsafe ] [ shorten ] 2bi ]
776     [ bounds-error ] if ;
777
778 : exchange ( m n seq -- )
779     [ nip bounds-check 2drop ]
780     [ bounds-check 3drop ]
781     [ exchange-unsafe ]
782     3tri ;
783
784 : midpoint@ ( seq -- n ) length 2/ ; inline
785
786 : reverse! ( seq -- seq )
787     [
788         [ midpoint@ iota ] [ length ] [ ] tri
789         [ [ over - 1 - ] dip exchange-unsafe ] 2curry each
790     ] keep ;
791
792 : reverse ( seq -- newseq )
793     [
794         dup [ length ] keep new-sequence
795         [ 0 swap copy ] keep reverse!
796     ] keep like ;
797
798 : sum-lengths ( seq -- n )
799     0 [ length + ] reduce ;
800
801 : concat-as ( seq exemplar -- newseq )
802     swap [ { } ] [
803         [ sum-lengths over new-resizable ] keep
804         [ append! ] each
805     ] if-empty swap like ;
806
807 : concat ( seq -- newseq )
808     [ { } ] [ dup first concat-as ] if-empty ;
809
810 <PRIVATE
811
812 : joined-length ( seq glue -- n )
813     [ [ sum-lengths ] [ length 1 [-] ] bi ] dip length * + ;
814
815 PRIVATE>
816
817 : join-as ( seq glue exemplar -- newseq )
818     over empty? [ nip concat-as ] [
819         [
820             2dup joined-length over new-resizable [
821                 [ [ push-all ] 2curry ] [ [ nip push-all ] 2curry ] 2bi
822                 interleave
823             ] keep
824         ] dip like
825     ] if ;
826
827 : join ( seq glue -- newseq )
828     dup join-as ; inline
829
830 : padding ( ... seq n elt quot: ( ... seq1 seq2 -- ... newseq ) -- ... newseq )
831     [
832         [ over length [-] dup 0 = [ drop ] ] dip
833         [ <repetition> ] curry
834     ] dip compose if ; inline
835
836 : pad-head ( seq n elt -- padded )
837     [ swap dup append-as ] padding ;
838
839 : pad-tail ( seq n elt -- padded )
840     [ append ] padding ;
841
842 : shorter? ( seq1 seq2 -- ? ) [ length ] bi@ < ;
843
844 : head? ( seq begin -- ? )
845     2dup shorter? [
846         2drop f
847     ] [
848         [ nip ] [ length head-slice ] 2bi sequence=
849     ] if ;
850
851 : tail? ( seq end -- ? )
852     2dup shorter? [
853         2drop f
854     ] [
855         [ nip ] [ length tail-slice* ] 2bi sequence=
856     ] if ;
857
858 : cut-slice ( seq n -- before-slice after-slice )
859     [ head-slice ] [ tail-slice ] 2bi ; inline
860
861 : insert-nth ( elt n seq -- seq' )
862     swap cut-slice [ swap suffix ] dip append ;
863
864 : halves ( seq -- first-slice second-slice )
865     dup midpoint@ cut-slice ; inline
866
867 <PRIVATE
868
869 : nth2-unsafe ( n seq -- a b )
870     [ nth-unsafe ] [ [ 1 + ] dip nth-unsafe ] 2bi ; inline
871
872 : nth3-unsafe ( n seq -- a b c )
873     [ nth2-unsafe ] [ [ 2 + ] dip nth-unsafe ] 2bi ; inline
874
875 : (binary-reduce) ( ... seq start quot: ( ... elt1 elt2 -- ... newelt ) from length -- ... value )
876     #! We can't use case here since combinators depends on
877     #! sequences
878     dup 4 < [
879         integer>fixnum {
880             [ 2drop nip ]
881             [ 2nip swap nth-unsafe ]
882             [ -rot [ drop swap nth2-unsafe ] dip call ]
883             [ -rot [ drop swap nth3-unsafe ] dip bi@ ]
884         } dispatch
885     ] [
886         [ 2/ ] [ over - ] bi [ 2dup + ] dip
887         [ (binary-reduce) ] [ 2curry ] curry 2bi@
888         pick [ 3bi ] dip call
889     ] if ; inline recursive
890
891 PRIVATE>
892
893 : binary-reduce ( ... seq start quot: ( ... elt1 elt2 -- ... newelt ) -- ... value )
894     pick length 0 max 0 swap (binary-reduce) ; inline
895
896 : cut ( seq n -- before after )
897     [ head ] [ tail ] 2bi ;
898
899 : cut* ( seq n -- before after )
900     [ head* ] [ tail* ] 2bi ;
901
902 <PRIVATE
903
904 : (start) ( subseq seq n -- subseq seq ? )
905     pick length iota [
906         [ 3dup ] dip [ + swap nth-unsafe ] keep rot nth-unsafe =
907     ] all? nip ; inline
908
909 PRIVATE>
910
911 : start* ( subseq seq n -- i )
912     pick length pick length swap - 1 + iota
913     [ (start) ] find-from
914     swap [ 3drop ] dip ;
915
916 : start ( subseq seq -- i ) 0 start* ; inline
917
918 : subseq? ( subseq seq -- ? ) start >boolean ;
919
920 : drop-prefix ( seq1 seq2 -- slice1 slice2 )
921     2dup mismatch [ 2dup min-length ] unless*
922     [ tail-slice ] curry bi@ ;
923
924 : unclip ( seq -- rest first )
925     [ rest ] [ first-unsafe ] bi ;
926
927 : unclip-last ( seq -- butlast last )
928     [ but-last ] [ last-unsafe ] bi ;
929
930 : unclip-slice ( seq -- rest-slice first )
931     [ rest-slice ] [ first-unsafe ] bi ; inline
932
933 : map-reduce ( ..a seq map-quot: ( ..a elt -- ..b intermediate ) reduce-quot: ( ..b prev intermediate -- ..a next ) -- ..a result )
934     [ [ dup first ] dip [ call ] keep ] dip compose
935     swapd [ 1 ] 2dip (each) (each-integer) ; inline
936
937 : 2map-reduce ( ..a seq1 seq2 map-quot: ( ..a elt1 elt2 -- ..b intermediate ) reduce-quot: ( ..b prev intermediate -- ..a next ) -- ..a result )
938     [ [ 2dup [ first ] bi@ ] dip [ call ] keep ] dip compose
939     [ -rot ] dip [ 1 ] 3dip (2each) (each-integer) ; inline
940
941 <PRIVATE
942
943 : (map-find) ( seq quot find-quot -- result elt )
944     [ [ f ] 2dip [ [ nip ] dip call dup ] curry ] dip call
945     [ [ drop f ] unless ] dip ; inline
946
947 PRIVATE>
948
949 : map-find ( ... seq quot: ( ... elt -- ... result/f ) -- ... result elt )
950     [ find ] (map-find) ; inline
951
952 : map-find-last ( ... seq quot: ( ... elt -- ... result/f ) -- ... result elt )
953     [ find-last ] (map-find) ; inline
954
955 : unclip-last-slice ( seq -- butlast-slice last )
956     [ but-last-slice ] [ last-unsafe ] bi ; inline
957
958 <PRIVATE
959
960 : (trim-head) ( seq quot -- seq n )
961     over [ [ not ] compose find drop ] dip
962     [ length or ] keep swap ; inline
963
964 : (trim-tail) ( seq quot -- seq n )
965     over [ [ not ] compose find-last drop ?1+ ] dip
966     swap ; inline
967
968 PRIVATE>
969
970 : trim-head-slice ( ... seq quot: ( ... elt -- ... ? ) -- ... slice )
971     (trim-head) tail-slice ; inline
972
973 : trim-head ( ... seq quot: ( ... elt -- ... ? ) -- ... newseq )
974     (trim-head) tail ; inline
975
976 : trim-tail-slice ( ... seq quot: ( ... elt -- ... ? ) -- ... slice )
977     (trim-tail) head-slice ; inline
978
979 : trim-tail ( ... seq quot: ( ... elt -- ... ? ) -- ... newseq )
980     (trim-tail) head ; inline
981
982 : trim-slice ( ... seq quot: ( ... elt -- ... ? ) -- ... slice )
983     [ trim-head-slice ] [ trim-tail-slice ] bi ; inline
984
985 : trim ( ... seq quot: ( ... elt -- ... ? ) -- ... newseq )
986     [ trim-slice ] [ drop ] 2bi like ; inline
987
988 GENERIC: sum ( seq -- n )
989 M: object sum 0 [ + ] binary-reduce ; inline
990
991 : product ( seq -- n ) 1 [ * ] binary-reduce ;
992
993 : infimum ( seq -- n ) [ ] [ min ] map-reduce ;
994
995 : supremum ( seq -- n ) [ ] [ max ] map-reduce ;
996
997 : map-sum ( ... seq quot: ( ... elt -- ... n ) -- ... n )
998     [ 0 ] 2dip [ dip + ] curry [ swap ] prepose each ; inline
999
1000 : count ( ... seq quot: ( ... elt -- ... ? ) -- ... n )
1001     [ 1 0 ? ] compose map-sum ; inline
1002
1003 : cartesian-each ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ) -- ... )
1004     [ with each ] 2curry each ; inline
1005
1006 : cartesian-map ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... newelt ) -- ... newseq )
1007     [ with map ] 2curry map ; inline
1008
1009 : cartesian-product ( seq1 seq2 -- newseq )
1010     [ { } 2sequence ] cartesian-map ;
1011
1012 ! We hand-optimize flip to such a degree because type hints
1013 ! cannot express that an array is an array of arrays yet, and
1014 ! this word happens to be performance-critical since the compiler
1015 ! itself uses it. Optimizing it like this reduced compile time.
1016 <PRIVATE
1017
1018 : generic-flip ( matrix -- newmatrix )
1019     [
1020         [ first-unsafe length 1 ] keep
1021         [ length min ] (each) (each-integer) iota
1022     ] keep
1023     [ [ nth-unsafe ] with { } map-as ] curry { } map-as ; inline
1024
1025 USE: arrays
1026
1027 : array-length ( array -- len )
1028     { array } declare length>> ; inline
1029
1030 : array-flip ( matrix -- newmatrix )
1031     { array } declare
1032     [
1033         [ first-unsafe array-length 1 ] keep
1034         [ array-length min ] (each) (each-integer) iota
1035     ] keep
1036     [ [ { array } declare array-nth ] with { } map-as ] curry { } map-as ;
1037
1038 PRIVATE>
1039
1040 : flip ( matrix -- newmatrix )
1041     dup empty? [
1042         dup array? [
1043             dup [ array? ] all?
1044             [ array-flip ] [ generic-flip ] if
1045         ] [ generic-flip ] if
1046     ] unless ;