Best method for sorting multiple arrays

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Best method for sorting multiple arrays

09 Jun 2015, 18:55

Hi.

I'm making a function that is containing 3 arrays, same number of entries.

To make the function as effective as possible I want to be able to exchange the values of index 4 and 5 on all three arrays.

Array one would contain a color code, array two contain x-coordinat of that color and array three contains y-coordinat. The goal of this is when searching for a color code in array one, each time a color code matches, that index (for all three arrays) shift one position down, so that search time for often used colors reduces as much as possible.

Is there a better (faster to compute) way to do this you think?
hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: Best method for sorting multiple arrays

10 Jun 2015, 01:15

From your description I get the impression that you use pseudo arrays instead of associative arrays. In the latter you can store all values in one array, you can use the color code as key and have direct access, no shifting or sorting needed.

Hubert
User avatar
MilesAhead
Posts: 232
Joined: 03 Oct 2013, 09:44

Re: Best method for sorting multiple arrays

11 Jun 2015, 05:54

hd0202 wrote:From your description I get the impression that you use pseudo arrays instead of associative arrays. In the latter you can store all values in one array, you can use the color code as key and have direct access, no shifting or sorting needed.

Hubert
+1. The moving of data would take much longer than the built in indexing using the color in an associative array or dictionary. If you had large data sorting with binary search likely would not be able to out perform db code done in actual compiled code.
"My plan is to ghostwrite my biography. Then hire another writer to put his
name on it and take the blame."

- MilesAhead
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: Best method for sorting multiple arrays

11 Jun 2015, 18:48

Thanks for answers.

I've worked out a solution that works for me. I've using three independent arrays.
Each hit (for a color) - the position (in all three arrays) move one place forward in order to reduce search time for the most recently used colors.

Maybe it's possible to make the code more efficient, but I haven't got an idea on how yet. Maybe you have a better idea.

Code: Select all

; Select color (MS paint in Windows XP)
msPaintSetColorDecimal(r,g,b, colorSameValueHEX:=0x000000) {
	Static r_last, g_last, b_last
	Static defaultColorsArray, colorPickPosition_X, colorPickPosition_Y, indexOfMatchingColor
	
	; Run only first time this function is called.
	If (!defaultColorsArray.MaxIndex()) {
		defaultColorsArray := [0x000000,0x400000,0x800000,0x804040,0xFF0000,0xFF8080, 0x808000,0x804000,0xFF8000,0xFF8040,0xFFFF00,0xFFFF80 ,0x808040,0x004000,0x008000,0x00FF00,0x80FF00,0x80FF80 ,0x808080,0x004040,0x008040,0x008080,0x00FF40,0x00FF80 ,0x408080,0x000080,0x0000FF,0x004080,0x00FFFF,0x80FFFF ,0xC0C0C0,0x000040,0x0000A0,0x8080FF,0x0080C0,0x0080FF ,0x400040,0x400040,0x800080,0x800040,0x8080C0,0xFF80C0 ,0xFFFFFF,0x400080,0x8000FF,0xFF0080,0xFF00FF,0xFF80FF]
		colorPickPosition_X := [22,22,22,22,22,22,48,48,48,48,48,48,71,71,71,71,71,71,96,96,96,96,96,96,121,121,121,121,121,121,147,147,147,147,147,147,172,172,172,172,172,172,197,197,197,197,197,197]
		colorPickPosition_Y := [172,152,129,104,84,64,172,152,129,104,84,64,172,152,129,104,84,64,172,152,129,104,84,64,172,152,129,104,84,64,172,152,129,104,84,64,172,152,129,104,84,64,172,152,129,104,84,64]
		indexOfMatchingColor := 0
	}
	
	If (r=r_last && g=g_last && b=b_last)	; Color value is the same since last function call
		Return
	
	IfWinNotActive, ahk_class MSPaintApp
	{
		MsgBox, This function requires that MS Paint has focus.
		Return
	}
	MouseGetPos, musX, musY
	Click 157,39	; Open menu "Colors"
	Sleep 5
	;SendInput {Down}{Enter}	; This doesn't always work in english version of Ms Paint (XP), but always work in norwegian version for some odd reason.
	Click 178,62	; Click on sub menu "Edit Colors"
	WinWaitActive, Edit Colors
	
	; Call the function that takes care of the three arrays. That is looking for default colors (less clicks).
	If colorIsDefault(colorSameValueHEX, defaultColorsArray, colorPickPosition_X, colorPickPosition_Y, indexOfMatchingColor)
	{
		TrayTip, Info, Value of default color found is %colorSameValueHEX%
		MouseMove, colorPickPosition_X[indexOfMatchingColor], colorPickPosition_Y[indexOfMatchingColor]
		Sleep 4
		Click
	}
	Else
	{
		; Have to define the color, because it's not in the list of default colors (MS Paint color palette).
		Click, 82,279	; Click button "Define Custom Colors >>"
		Sleep, 10
		If (r != r_last) {
			SendInput {Alt down}r{Alt up}	; Select input field "Red"
			Sleep 2
			SendInput %r%
			Sleep 2
		}
		If (g != g_last) {
			SendInput {Alt down}g{Alt up}	; Select input field "Green"
			Sleep 2
			SendInput %g%
			Sleep 2
		}
		If (b != b_last) {
			SendInput {Alt down}u{Alt up}	; Select input field "Blue"
			Sleep 2
			SendInput %b%
			Sleep 2
		}
	}
	Sleep 10
	Click 45,304	; Button "Ok" to confirm the color
	
	r_last := r
	g_last := g
	b_last := b
}


; Dealing with array of default colors and the colors X and Y position (in "Edit Colors" dialog box).
; Possible return values. 1=color value is in present array, 0=not found.
colorIsDefault(colorSameValueHEX, ByRef defaultColorsArray, ByRef colorPickPosition_X, ByRef colorPickPosition_Y, ByRef indexOfMatchingColor) {
	Loop, 48
	{
		
		If (colorSameValueHEX = defaultColorsArray[A_Index]) {
			
			If (A_Index < 3) {
				indexOfMatchingColor := A_Index
				Return 1
			}
				
			n_minus := A_Index -1
			
			tmp_standardFarger := defaultColorsArray[n_minus]
			tmp_x := colorPickPosition_X[n_minus]
			tmp_y := colorPickPosition_Y[n_minus]
			
			defaultColorsArray[n_minus] := defaultColorsArray[A_Index]
			colorPickPosition_X[n_minus] := colorPickPosition_X[A_Index]
			colorPickPosition_Y[n_minus] := colorPickPosition_Y[A_Index]
			
			defaultColorsArray[A_Index] := tmp_standardFarger
			colorPickPosition_X[A_Index] := tmp_x
			colorPickPosition_Y[A_Index] := tmp_y
			
			indexOfMatchingColor := n_minus
			Return 1
		}
	}
	Return 0
}
hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: Best method for sorting multiple arrays

12 Jun 2015, 03:32

This is my suggestion:

Code: Select all

; Select color (MS paint in Windows XP)
msPaintSetColorDecimal(r,g,b, colorSameValueHEX:=0x000000) {
	Static r_last, g_last, b_last
	Static defaultColorsArray
	
	; Run only first time this function is called.
	If (!defaultColorsArray.MaxIndex()) {
		defaultColorsArray_T := [0x000000,0x400000,0x800000,0x804040,0xFF0000,0xFF8080, 0x808000,0x804000,0xFF8000,0xFF8040,0xFFFF00,0xFFFF80 ,0x808040,0x004000,0x008000,0x00FF00,0x80FF00,0x80FF80 ,0x808080,0x004040,0x008040,0x008080,0x00FF40,0x00FF80 ,0x408080,0x000080,0x0000FF,0x004080,0x00FFFF,0x80FFFF ,0xC0C0C0,0x000040,0x0000A0,0x8080FF,0x0080C0,0x0080FF ,0x400040,0x400040,0x800080,0x800040,0x8080C0,0xFF80C0 ,0xFFFFFF,0x400080,0x8000FF,0xFF0080,0xFF00FF,0xFF80FF]
		colorPickPosition_X := [22,22,22,22,22,22,48,48,48,48,48,48,71,71,71,71,71,71,96,96,96,96,96,96,121,121,121,121,121,121,147,147,147,147,147,147,172,172,172,172,172,172,197,197,197,197,197,197]
		colorPickPosition_Y := [172,152,129,104,84,64,172,152,129,104,84,64,172,152,129,104,84,64,172,152,129,104,84,64,172,152,129,104,84,64,172,152,129,104,84,64,172,152,129,104,84,64,172,152,129,104,84,64]
		defaultColorsArray := {}
		loop, % defaultColorsArray_T.MaxIndex()
			defaultColorsArray[defaultColorsArray_T[A_Index]] := {"X" : colorPickPosition_X[A_Index], "Y" : colorPickPosition_Y[A_Index]}
	}
	
	If (r=r_last && g=g_last && b=b_last)	; Color value is the same since last function call
		Return
	
	IfWinNotActive, ahk_class MSPaintApp
	{
		MsgBox, This function requires that MS Paint has focus.
		Return
	}
	MouseGetPos, musX, musY
	Click 157,39	; Open menu "Colors"
	Sleep 5
	;SendInput {Down}{Enter}	; This doesn't always work in english version of Ms Paint (XP), but always work in norwegian version for some odd reason.
	Click 178,62	; Click on sub menu "Edit Colors"
	WinWaitActive, Edit Colors

	; looking for default colors (less clicks).
	if defaultColorsArray.haskey(colorSameValueHEX)
	{
		TrayTip, Info, Value of default color found is %colorSameValueHEX%
		MouseMove, defaultColorsArray[colorSameValueHEX].X, defaultColorsArray[colorSameValueHEX].Y
		Sleep 4
		Click
	}
	Else
	{
		; Have to define the color, because it's not in the list of default colors (MS Paint color palette).
		Click, 82,279	; Click button "Define Custom Colors >>"
		Sleep, 10
		If (r != r_last) {
			SendInput {Alt down}r{Alt up}	; Select input field "Red"
			Sleep 2
			SendInput %r%
			Sleep 2
		}
		If (g != g_last) {
			SendInput {Alt down}g{Alt up}	; Select input field "Green"
			Sleep 2
			SendInput %g%
			Sleep 2
		}
		If (b != b_last) {
			SendInput {Alt down}u{Alt up}	; Select input field "Blue"
			Sleep 2
			SendInput %b%
			Sleep 2
		}
	}
	Sleep 10
	Click 45,304	; Button "Ok" to confirm the color
	
	r_last := r
	g_last := g
	b_last := b
}
Hubert
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: Best method for sorting multiple arrays

12 Jun 2015, 17:33

Hi Hubert.

Thanks for an educational example.

Haven't tried your code yet, I miss some skills regarding arrays in AutoHotkey. I hope to get explained the following questions.

Line 11: defaultColorsArray := {}
Is this any different compared to defaultColorsArray := Object()?

Line 13: defaultColorsArray[defaultColorsArray_T[A_Index]] := {"X" : colorPickPosition_X[A_Index], "Y" : colorPickPosition_Y[A_Index]}
Just by reading your code, I see I do simply not have the skills to fully understand. Still I give a try, and I'll be very thankfull for any corrections of what I'm thinking about the code:
I assume you've made a two dimensional array where defaultColorsArray is tha "host array" (please help me with the correct terminology here). It contains three sub-arrays.
However I don't understand the use of the standalone colon and the curly bracets.

Line 11: MouseMove, defaultColorsArray[colorSameValueHEX].X, defaultColorsArray[colorSameValueHEX].Y
This tells me that you've using a two dimensional array, where the color code is actually the key/index.
hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: Best method for sorting multiple arrays

13 Jun 2015, 04:44

Hi,
Almost_there wrote:Line 11: defaultColorsArray := {}
Is this any different compared to defaultColorsArray := Object()?
No.
Line 13: defaultColorsArray[defaultColorsArray_T[A_Index]] := {"X" : colorPickPosition_X[A_Index], "Y" : colorPickPosition_Y[A_Index]}
I use an associative array where "color code" is the key. It is associated with a value, for which I use a sub-array with two keys: "X" and "Y", all filled from your temporary arrays. For the first three color codes I get :

Key 0x000000 - X 22 - Y 172
Key 0x400000 - X 22 - Y 152
Key 0x800000 - X 22 - Y 129 and so on

For further explanation see the chapter "associoative array" in "object" in the online docu.

Hubert

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, DiegoSouto, mikeyww, Rohwedder, Sniperman, Swiftly9767 and 383 guests