import { binarySearch } from "@thi.ng/arrays/binary-search" ; import type { Reducer , SortOpts , Transducer } from "./api.js" ; import { __drain } from "./internal/drain.js" ; import { __sortOpts } from "./internal/sort-opts.js" ; import { __iter , iterator } from "./iterator.js" ; /**
 * Transducer. Similar to  {@link (partitionSort:1)} , however uses proper
 * sliding window and insertion sort instead of fully sorting window as
 * done by `partitionSort`.
 *
 *  example param n param key param cmp export function streamSort < A , B > ( n , opts ? ) ; export function streamSort < A , B > ( n , src ) ; export function streamSort < A , B > ( n , opts , src ) ; export function streamSort < A , B > ( ... args ) { const iter = __iter ( streamSort , args , iterator ) ; if ( iter ) { return iter ; } const { key , compare } = __sortOpts < A , B > ( args [ 1 ] ) ; const n = args [ 0 ] ; return ( [ init , complete , reduce ] ) => { const buf = [ ] ; return < Reducer < any , A > > [ init , __drain ( buf , complete , reduce ) , ( acc , x ) => { const idx = binarySearch ( buf , x , key , compare ) ; buf . splice ( idx < 0 ? - ( idx + 1 ) : idx , 0 , x ) ; if ( buf . length === n ) { acc = reduce ( acc , buf . shift ( ) ! ) ; } return acc ; } , ] ; } ; }