%!PS % begin included library code % see https://codeberg.org/Firedrake/postscript-libraries/ /quicksort.with_keygen { % [ a c b ] { keygen } -> [ a b c ] 3 dict begin /kg exch def /arr exch def /kl << arr { dup kg } forall >> def arr { exch kl exch get exch kl exch get quicksort.cmp } quicksort.with_comparator } bind def /quicksort.partition { 3 dict begin /pivot arr hi lo add 2 idiv get def /i lo 1 sub def /j hi 1 add def { { /i i 1 add def arr i get pivot cmp 0 ge { exit } if } loop { /j j 1 sub def arr j get pivot cmp 0 le { exit } if } loop i j ge { j exit } if i j quicksort.swap } loop end } bind def /keys { % dict -> array of dict keys [ exch { pop } forall ] } bind def /test { /test.count test.count 1 add def { /test.pass test.pass 1 add def } { ( ) print test.count (....) cvs print (-fail) print } ifelse } bind def /test.start { print (:) print /test.pass 0 def /test.count 0 def } bind def /quicksort.swap { 2 dict begin /bi exch def /ai exch def arr ai get arr bi get arr exch ai exch put arr exch bi exch put end } bind def /quicksort { { quicksort.cmp } quicksort.with_comparator } bind def /permute.swap { /permute.bi exch def /permute.ai exch def permute.a permute.ai get permute.a permute.bi get permute.a exch permute.ai exch put permute.a exch permute.bi exch put } bind def /test.end { ( ) print test.count 0 gt { (Passed ) print test.pass (...) cvs print (/) print test.count (...) cvs print ( \() print test.pass 100 mul test.count idiv (...) cvs print (%\)) print (\r\n) print } if } bind def /quicksort.cmp { 2 copy lt { pop pop -1 } { gt { 1 } { 0 } ifelse } ifelse } bind def /deepcopy { 2 dict begin /a exch def a type /dicttype eq { << a keys { /k exch def k a k get deepcopy } forall >> } { a type /arraytype eq { [ a { deepcopy } forall ] } { a type /stringtype eq { a dup length string cvs } { a } ifelse } ifelse } ifelse end } bind def /quicksort.main { % lo hi -> (null) 3 dict begin /hi exch def /lo exch def /xit false def lo 0 lt { /xit true def } if hi 0 lt { /xit true def } if lo hi ge { /xit true def } if xit not { /p quicksort.partition def lo p quicksort.main p 1 add hi quicksort.main } if end } bind def /quicksort.with_comparator { % [ a c b ] { comparator } -> [ a b c ] 2 dict begin /cmp exch def /arr exch def arr length 0 gt { 0 arr length 1 sub quicksort.main } if arr end } bind def /deepeq { 2 dict begin /a exch def /b exch def a type b type eq { a type /dicttype eq { a length b length eq { << a { pop true } forall b { pop true } forall >> true exch { pop dup a exch known { dup b exch known { dup a exch get exch b exch get deepeq not { pop false } if } { false } ifelse } { false } ifelse } forall } { false } ifelse } { a type dup /arraytype eq exch /stringtype eq or { a length b length eq { true 0 1 a length 1 sub { dup a exch get exch b exch get deepeq not { pop false exit } if } for } { false } ifelse } { a b eq } ifelse } ifelse } { false } ifelse end } bind def /permute { % [array] {proc} permute runs proc on each permutation of array 7 dict begin /permute.subproc exch def /permute.a exch def /permute.n permute.a length def /permute.c [ permute.n { 0 } repeat ] def permute.a permute.subproc /permute.i 0 def { permute.i permute.n ge { exit } if permute.c permute.i get permute.i lt { permute.i 2 mod 0 eq { 0 permute.i permute.swap } { permute.c permute.i get permute.i permute.swap } ifelse permute.a permute.subproc permute.c permute.i get 1 add permute.c exch permute.i exch put /permute.i 0 def } { permute.c permute.i 0 put /permute.i permute.i 1 add def } ifelse } loop end } bind def % end included library code /nextpermutation { 0 dict begin /a exch def /b a deepcopy quicksort def /flag false def /out 0 array def [ b { [ exch aload pop ] } permute ] { dup dup 0 get 100 mul 3 1 roll 1 get 10 mul 3 1 roll 2 get add add } quicksort.with_keygen { /px exch def out length 0 eq { /out px def } if flag { /out px def exit } if px a deepeq { /flag true def } if } forall out end } bind def (nextpermutation) test.start [1 2 3] nextpermutation [1 3 2] deepeq test [2 1 3] nextpermutation [2 3 1] deepeq test [3 1 2] nextpermutation [3 2 1] deepeq test test.end