21 lines
653 B
Text
21 lines
653 B
Text
fcn [private] _permuteW(seq){ // lazy version
|
|
N:=seq.len(); NM1:=N-1;
|
|
ds:=(0).pump(N,List,T(Void,-1)).copy(); ds[0]=0; // direction to move e: -1,0,1
|
|
es:=(0).pump(N,List).copy(); // enumerate seq
|
|
|
|
while(1) {
|
|
vm.yield(es.pump(List,seq.__sGet));
|
|
|
|
// find biggest e with d!=0
|
|
reg i=Void, c=-1;
|
|
foreach n in (N){ if(ds[n] and es[n]>c) { c=es[n]; i=n; } }
|
|
if(Void==i) return();
|
|
|
|
d:=ds[i]; j:=i+d;
|
|
es.swap(i,j); ds.swap(i,j); // d tracks e
|
|
if(j==NM1 or j==0 or es[j+d]>c) ds[j]=0;
|
|
foreach e in (N){ if(es[e]>c) ds[e]=(i-e).sign }
|
|
}
|
|
}
|
|
|
|
fcn permuteW(seq) { Utils.Generator(_permuteW,seq) }
|