Next version is done. Now I have added a counter and a bubble-sort (slightly modified) routine so that the function that returns true the most number of times, is the function that is called first.
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
/*
Scripted by Grobe <smiley goes here>
*/
; Writes function order to ini file, and display traytip to the user.
; Gives errormessage when trying to launch same script twice - need to fix that.
OnExit("script_terminates")
; I decided for using two separate arrays because it is easier for me (personally) to deal with.
; Later on, I may change it to use a two-dimensional array instead.
function_names_list := Object()
hitCounts := [0,0,0,0]
; Create the INI file if it doesn't already exists
IfNotExist, ahk_setting.ini
{
FileAppend,
(
[function_call_order]
rekkefolge_1=1
rekkefolge_2=2
rekkefolge_3=3
rekkefolge_4=4
number_of_functions_call=4
[function_call_order_other]
), ahk_setting.ini
}
; IniRead, OutputVar, Filename, Section, Key [, Default]
IniRead, OutputVar, ahk_setting.ini, function_call_order, rekkefolge_1, 1
function_names_list[1] := OutputVar
IniRead, OutputVar, ahk_setting.ini, function_call_order, rekkefolge_2, 2
function_names_list[2] := OutputVar
IniRead, OutputVar, ahk_setting.ini, function_call_order, rekkefolge_3, 3
function_names_list[3] := OutputVar
IniRead, OutputVar, ahk_setting.ini, function_call_order, rekkefolge_4, 4
function_names_list[4] := OutputVar
IniRead, number_of_functions_call, ahk_setting.ini, function_call_order, number_of_functions_call, -1
F8::
mainLoop:
Loop, %number_of_functions_call%
{
func_index := function_names_list[A_Index]
function_return_value := funksjon_%func_index%()
If (function_return_value == 1) {
If (A_Index == 1) {
hitCounts[1]++
Break, mainLoop ; Function called was already first in queue and no sorting is required.
}
Else {
; ACTION 1 - Poeng legges til
hitCounts[A_Index]++
index_last_used_last := A_Index -1 ; number of iterations used in bubble-sort (performance tweak - limits iterations).
; Here I can allow making performance tweak for bubble-sort because the counts would never increase by more than 1 between each
; time that sorting is performed.
; ACTION 2 - Sorting routine - Sorting based on hitCounts[], but the move of array elements is equal in both arrays.
loop_search_all_decending:
Loop, %index_last_used_last%
{
index_less_than_next := A_index
If (hitCounts[A_Index+1] > hitCounts[A_Index]) { ; If one index found to have less counts than next, run bubble-sort.
status_sorted_secondary := 0 ; If value is 1, the hitCounts[] is properly sorted (highest counts first)
loop_sort_bubble:
While, !status_sorted_secondary
{
loop_backwards:
Loop, %index_less_than_next%
{
status_sorted_secondary := 1
i_backwards := index_last_used_last - A_index +1
; Searching backwards for entries that is not sorted. That normally requires less iterations.
If ( hitCounts[i_backwards+1] > hitCounts[i_backwards] ) {
status_sorted_secondary := 0 ; Resets sorting status - this makes the while loop to iterate one more time (until sorted)
; TEST - display for the user that bubblesort actually works. Because of measurements that limits
; the number of iterations for bubble-sort (we always know what index of function returns 1, and counts never increases more
; than once per iteration of main loop) you should see this only once.
arrayContentText1(hitCounts)
; Swap number in table index in hitCounts[] with the index above.
hitCounts_temp := hitCounts[i_backwards]
hitCounts[i_backwards] := hitCounts[i_backwards+1]
hitCounts[i_backwards+1] := hitCounts_temp
; Swap number in table index in function_names_list[] with the index above.
rekkefolge_temp := function_names_list[i_backwards]
function_names_list[i_backwards] := function_names_list[i_backwards+1]
function_names_list[i_backwards+1] := rekkefolge_temp
}
}
} ; while "not sorted"
} ; If "next indeks have a higher number of counts"
Else { ; next one is already bigger - skip (this whole block can be deleted)
Continue, loop_search_all_decending
}
} ; main sorting loop
Break ; Prevents the code to call more than one function.
} ; Check if the function called is already first in queue.
} ; if "return of called function is 1"
} ; Loop main - loops through a number of functions.
; ACTION - VIEW ARRAY CONTENT.
arrayContentText2(function_names_list, hitCounts)
Return ; F8
; Display the values in both arrays - that is function call order and the number of hits for each function call
arrayContentText2(arrayID, counterArray) {
textOut := "Index`t`tfunction_id`t`tCounts`n"
For i , Value in arrayID
textOut .= i . "`t-`t" . Value . "`t-`t" . counterArray[i] . "`n"
MsgBox, %textOut%
}
; Displan values in one array.
arrayContentText1(arrayID) {
textOut := "Index`t`tVerdi`n"
For i , Value in arrayID
textOut .= i . "`t-`t" . Value . "`n"
MsgBox, %textOut%
}
; ACTION 2 - Write to INI ved ahk exit.
script_terminates() {
Global function_names_list
Global hitCounts
TrayTip, Script terminates shortly, Wait until sort order is saved to ini file `n---------------`nReason for script termination: %A_ExitReason%
For i , Value in function_names_list
IniWrite, %Value%, ahk_setting.ini, function_call_order, rekkefolge_%i%
For i , Value in hitCounts
{
If (i == 1) ; for some rare reason, the IF statements fails when pharanteses is removed.
counts := Value
Else
counts .= " - " . Value
}
; Writes some more information to the ini file. Not used later, just simple information.
; Maybe add total number of counts in a later version.
IniWrite, %counts%, ahk_setting.ini, function_call_order_other, function_call_counts_sorted
IniWrite, %A_ExitReason%, ahk_setting.ini, function_call_order_other, exit_reason
Sleep 3000
}
; ----------------------- The functions that is to be called - main code will try to call the functions in most effective order (most used function is tried first).
funksjon_1() {
MsgBox, 0x124, Funksjon 1, Do you want function number 1 return true?`n - - - - - - `n`nYou got 4 seconds to decide before I decide -NO- for you..., 4
IfMsgBox Yes
Return 1
Return 0
}
funksjon_2() {
MsgBox, 0x124, Funksjon 2, Do you want function number 2 return true?`n - - - - - - `n`nYou got 4 seconds to decide before I decide -NO- for you..., 4
IfMsgBox Yes
Return 1
Return 0
}
funksjon_3() {
MsgBox, 0x124, Funksjon 3, Do you want function number 3 return true?`n - - - - - - `n`nYou got 4 seconds to decide before I decide -NO- for you..., 4
IfMsgBox Yes
Return 1
Return 0
}
funksjon_4() {
MsgBox, 0x124, Funksjon 4, Do you want function number 4 return true?`n - - - - - - `n`nYou got 4 seconds to decide before I decide -NO- for you..., 4
IfMsgBox Yes
Return 1
Return 0
}