-
Notifications
You must be signed in to change notification settings - Fork 71
/
Permutation.ahk
54 lines (50 loc) · 1008 Bytes
/
Permutation.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
;
; AutoHotkey Version: 1.1.24.03
; Language: English
; Platform: Optimized for Windows 10
; Author: Sam.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; https://autohotkey.com/board/topic/72724-lexicographical-next-permutation-in-o1-time/
; by nimda , Posted 23 October 2011 - 04:56 PM
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
perm_NextObj(obj){
p := 0, objM := ObjMaxIndex(obj)
Loop % objM
{
If A_Index=1
continue
t := obj[objM+1-A_Index]
n := obj[objM+2-A_Index]
If ( t < n )
{
p := objM+1-A_Index, pC := obj[p]
break
}
}
If !p
return false
Loop
{
t := obj[objM+1-A_Index]
If ( t > pC )
{
n := objM+1-A_Index, nC := obj[n]
break
}
}
obj[n] := pC, obj[p] := nC
return ObjReverse(obj, objM-p)
}
ObjReverse(Obj, tail){
o := ObjClone(Obj), ObjM := ObjMaxIndex(O)
Loop % tail
o[ObjM-A_Index+1] := Obj[ObjM+A_Index-tail]
return o
}
ObjDisp(obj){
s := "["
For k, v in obj
s .= v ", "
return SubStr(s, 1, strLen(s)-2) . "]"
}