A collection of small ahk functions

Post your working scripts, libraries and tools
User avatar
jNizM
Posts: 3150
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: A collection of small ahk functions

28 Aug 2014, 03:28

better? =)
[AHK] v2.0.2 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3150
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: A collection of small ahk functions

29 Aug 2014, 01:46

toralf wrote:i tried to test the speed of the 3 functions.
The tenary operator was the fastes.
The bitwise ^& took twice as long.
The for loop took 3 times as long. But at least has the option to work with more then 2 parameters.
just fyi
Image
[AHK] v2.0.2 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: A collection of small ahk functions

29 Aug 2014, 02:35

I admittedly haven't done a speed test, but I happened to notice that your graph included "count bits set" and remembered that I did that for a Rosetta Code task. It was basically a direct translation of the Wikipedia code.

Code: Select all

PopCount(x) {	;https://en.wikipedia.org/wiki/Hamming_weight#Efficient_implementation
	x -= (x >> 1) & 0x5555555555555555
	, x := (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
	, x := (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
	return (x * 0x0101010101010101) >> 56
}
User avatar
jNizM
Posts: 3150
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: A collection of small ahk functions

29 Aug 2014, 03:17

Faster population counts
Its Wikipedia #3... seems pretty fast =)

This is a small bit faster:
1 = 0x1 | 2 = 0x2 | 4 = 0x4 | 56 = 0x38

Code: Select all

loops      n             time with dec (56)      time with hex (0x38)
1000000    0x1101        1.021466 sec            1.019847 sec
1000000    0x11010110    1.029797 sec            1.022559 sec

Code: Select all

popCount(x) {
    x -= (x >> 0x1) & 0x5555555555555555
    x := (x & 0x3333333333333333) + ((x >> 0x2) & 0x3333333333333333)
    x := (x + (x >> 0x4)) & 0x0f0f0f0f0f0f0f0f
    return (x * 0x0101010101010101) >> 0x38
}
Other Ways (Slower):

Code: Select all

countSetBits(x) {
    return !x ? 0 : 1 + countSetBits(x & (x - 1))
}

Code: Select all

popCount(x) {
    count := 0
    while (x)
        count++, x &= x - 1
    return count
}
[AHK] v2.0.2 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
joedf
Posts: 8667
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: A collection of small ahk functions

29 Aug 2014, 07:14

How about a Dllcall to bin2str then get errorlevel of stringreplace ?
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: A collection of small ahk functions

29 Aug 2014, 23:14

jNizM wrote:Its Wikipedia #3... seems pretty fast =)

This is a small bit faster:
It's even faster if the comma operators are left in it. (see comma performance)

Edit: Thanks for the link to the "Faster Population Counts" article. It's quite interesting so far.
joedf wrote:How about a Dllcall to bin2str then get errorlevel of stringreplace ?
Where do I find bin2str? Is that an MCode function? I think I've seen it before but can't recall where. It's just a guess, but I think Stringreplace will be slower; I'd like to try it though.
User avatar
joedf
Posts: 8667
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: A collection of small ahk functions

30 Aug 2014, 03:02

You're probably right but the curiosity is getting to me. ;)
I'll try it when I get back :)

Cheers
User avatar
jNizM
Posts: 3150
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: A collection of small ahk functions

01 Sep 2014, 01:15

kon wrote:It's even faster if the comma operators are left in it. (see comma performance)
Thanks for the Info =)
[AHK] v2.0.2 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
huyaowen
Posts: 109
Joined: 28 Jul 2014, 01:15

Re: A collection of small ahk functions

25 Sep 2014, 23:30

julka wrote:Updated my msgbox to work with non-english locales.

Code: Select all

/*
Usual MsgBox, Text or MsgBox [, Options, Title, Text, Timeout]
with 3 extra parameters - custom button texts

If you pass text for a button, options that determine the buttons are ignored

Returns the text of the button pressed by the user or the word Timeout.
	Your problems if you name your buttons so.
		Seriusly.

Important! If you want to have custom text for 1 button,
	you have to pass text for all buttons
	
Important! You cannot have label named ¥Ω¦§ɵǕɶʱ͏ in your script.
	How did you manage though...

Important! Try "Try Again" button in second example.
	Too lazy to fix, since it is the only case when returned value
	will not match the button text.
		If you need exact match, check third example.
			Notice, how options are ignored.
	
msgbox % "You've chosen " julka_msgbox("I like trains.",,,,"Ok...","Woot?")
msgbox % "You've chosen " julka_msgbox(0x6,"Test","This is a test.")
msgbox % "You've chosen " julka_msgbox(0x6,"Train","While this is a train.",,"Cancel","Try Again")
msgbox % julka_msgbox("",,"You have 1 second to press this useful button.",1,"Congrats!")

Thanks titbit, tlm and GeekDude for proof-reading
*/

julka_msgbox(Options:=0,Title:="",Text:="",Timeout:="",B1:="",B2:="",B3:="")
{
	static s_Options,s_Title,s_Text,s_TimeOut,s_B1,s_B2,s_B3

	s_Options:=(Text!="") ? Options : 0
	s_Title:=(Title!="") ? Title : A_ScriptName
	s_Text:=(Text!="") ? Text : Options ? Options : "Press OK to continue"
	s_TimeOut:=Timeout
	if B1
	{
		s_B1:=B1
		if B2, s_B2:=B2
			if B3, s_B3:=B3
				s_Options-=Mod(s_Options,6)-3
			else s_Options-=Mod(s_Options,6)-4
		else s_Options-=Mod(s_Options,6)
	}
	SetTimer, ¥Ω¦§ɵǕɶʱ͏, 15
	msgbox % s_Options ,% s_Title,% s_Text,% s_TimeOut
	;If you can shrink following code, pm julka
	IfMsgBox, %s_B1%
		result:=s_B1
	else IfMsgBox, %s_B2%
			result:=s_B2	
	else ifMsgbox, %s_B3%
		result:=s_B3
	else IfMsgBox, OK
		result:=s_B1
	else IfMsgBox, Yes
		result:=s_B1
	else IfMsgBox, No
		result:=s_B2
	else IfMsgBox, Cancel
		result:=s_B3
	else result:="Timeout"
	s_Options:=s_Title:=s_Text:=s_TimeOut:=s_B1:=s_B2:=s_B3:=""
	return result
	
	¥Ω¦§ɵǕɶʱ͏:
	{	
		IfWinNotExist, % s_Title
			return  ; Keep waiting.
		SetTimer, ¥Ω¦§ɵǕɶʱ͏, off
		WinActivate
		if (s_B1)
			ControlSetText, Button1, % "&" s_B1
		else
		{
			ControlGetText,s_B1, Button1,%s_Title%
			s_B1:=RegExReplace(s_B1,"(\&(?!\&))|\s")
		}
		if (s_B2)
			ControlSetText, Button2, % "&" s_B2
		else
		{
			ControlGetText,s_B2, Button2,%s_Title%
			s_B2:=RegExReplace(s_B2,"(\&(?!\&))|\s")
		}
		if (s_B3)
			ControlSetText, Button3, % "&" s_B3
		else
		{
			ControlGetText,s_B3, Button3,%s_Title%
			s_B3:=RegExReplace(s_B3,"(\&(?!\&))|\s")
		}	
		return
	}
}
1.Could you add a parameter for "always on top" .
2.The special label is fixed, and cannt be edited. Why is that?
3.msgbox with Timeout cannt works well as follow:
1111.jpg
User avatar
jNizM
Posts: 3150
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: A collection of small ahk functions

12 Dec 2014, 06:35

Not sure if that is still current, but its interesting
==> Source Code Optimisation Tips (eng)
==> Quellcode Optimierungs-Tips (de)
[AHK] v2.0.2 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3150
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: A collection of small ahk functions

12 Dec 2014, 09:10

A few shlwapi.dll functions

StrTrim()
Removes specified leading and trailing characters from a string.

Code: Select all

MsgBox % StrTrim("_!ABCDEFG#", "#A!_\0")    ; ==> BCDEFG

StrTrim(String, TrimChars)
{
    if !(DllCall("shlwapi.dll\StrTrim", "Ptr", &String, "Ptr", &TrimChars))
        return FALSE
    return StrGet(&String)
}

StrToInt()
Converts a string that represents a decimal value to an integer.

Code: Select all

MsgBox % StrToInt("123456")        ; ==> 123456
MsgBox % StrToInt("-123456abc")    ; ==> -123456

StrToInt(String)
{
    return DllCall("shlwapi.dll\StrToInt", "Ptr", &String)
}

StrToIntEx()
Converts a string representing a decimal or hexadecimal number to an integer.

Code: Select all

MsgBox % StrToIntEx("61680")        ; ==> 61680
MsgBox % StrToIntEx("0xF0F0", 1)    ; ==> 61680

StrToIntEx(String, Flags := 0)
{
    if !(DllCall("shlwapi.dll\StrToIntEx", "Ptr", &String, "Int", Flags, "Int*", Ret))
        return FALSE
    return Ret
}

StrToInt64Ex()
Converts a string representing a decimal or hexadecimal value to a 64-bit integer.

Code: Select all

MsgBox % StrToInt64Ex("1099511627775")        ; ==> 1099511627775
MsgBox % StrToInt64Ex("-1099511627775abc")    ; ==> 1099511627775
MsgBox % StrToInt64Ex("0xFFFFFFFFFF", 1)      ; ==> 1099511627775

StrToInt64Ex(String, Flags := 0)
{
    if !(DllCall("shlwapi.dll\StrToInt64Ex", "Ptr", &String, "Int", Flags, "Int64*", Ret))
        return FALSE
    return Ret
}
Last edited by jNizM on 12 Dec 2014, 09:34, edited 3 times in total.
[AHK] v2.0.2 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Coco-guest

Re: A collection of small ahk functions

12 Dec 2014, 09:25

Haven't done a comparison, but Round() can imitate StrToInt. I'm not so sure on Round's limitation though. e.g.: MsgBox % Round("123456abc")
User avatar
jNizM
Posts: 3150
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: A collection of small ahk functions

22 Jan 2015, 04:15

Random using RtlRandom() (MSDN: RtlRandom routine)

Code: Select all

MsgBox % RtlRandom(1, 1, 100)

RtlRandom(seed, min, max)
{
    ret := DllCall("ntdll.dll\RtlRandom", "UInt*", seed, "UInt")
    return (Mod(ret, max) - min) + min
}
Random using RtlRandomEx() (MSDN: RtlRandomEx routine)

Code: Select all

MsgBox % RtlRandomEx(1, 1, 100)

RtlRandomEx(seed, min, max)
{
    ret := DllCall("ntdll.dll\RtlRandomEx", "UInt*", seed, "UInt")
    return (Mod(ret, max) - min) + min
}

Remarks:
The RtlRandomEx function is an improved version of the RtlRandom function that is twice as fast and produces better random numbers.
[AHK] v2.0.2 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3150
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: A collection of small ahk functions

20 Apr 2015, 04:20

[AHK] v2.0.2 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
-_+
Posts: 70
Joined: 06 Dec 2014, 12:43

Re: A collection of small ahk functions

10 May 2015, 11:36

Maybe it's worth to split this topic into a few ones?
I suggest to move all the bit/integer operations and pure math functions into a separate topic, all string operations - into another topic + leave one more topic for functions of the more general use.
I think that for most plain users those math related functions are nothing more than the demonstrations of solutions of some academical tasks that rarely have any use in everyday coding tasks of a plain user (no offense meant to the programmers working mostly with maths).
What do you think?
User avatar
jNizM
Posts: 3150
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: A collection of small ahk functions

14 Jul 2015, 09:10

OSInstallDate
Returns the install date of your operating system

Code: Select all

OSInstallDate()
{
    for objItem in ComObjGet("winmgmts:").ExecQuery("SELECT * FROM Win32_OperatingSystem")
    {
        FormatTime, InstOS, % SubStr(objItem.InstallDate, 1, 14), % "dd. MMM yyyy - HH:mm"
        FormatTime, DateFrom, % SubStr(objItem.InstallDate, 1, 14), % "yyyyMMdd"
        DateTo := A_Now
        DateTo -= DateFrom, days
        return InstOS "   |   (" DateTo " days)"
    }
}
example:

Code: Select all

MsgBox % OSInstallDate()
output:

Code: Select all

12. Jun 2015 - 16:23   |   (32 days)
[AHK] v2.0.2 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3150
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: A collection of small ahk functions

11 Sep 2015, 08:11

WheelScrollLines
- Retrieves the number of lines to scroll when the vertical mouse wheel is moved.
- Sets the number of lines to scroll when the vertical mouse wheel is moved. (default is 3)

Code: Select all

Class WheelScrollLines
{
    ; Retrieves the number of lines to scroll when the vertical mouse wheel is moved.
    GET()
    {
        VarSetCapacity(pvParam, 4, 0)
        if !(DllCall("user32.dll\SystemParametersInfo", "UInt", 0x0068, "UInt", 0, "Ptr", &pvParam, "UInt", 0))
            return "*" A_LastError
        return NumGet(pvParam, 0, "UInt")
    }

    ; Sets the number of lines to scroll when the vertical mouse wheel is moved. (Default is 3)
    SET(NUMBER_OF_LINES_TO_SCROLL := 3)
    {
        if !(DllCall("user32.dll\SystemParametersInfo", "UInt", 0x0069, "UInt", NUMBER_OF_LINES_TO_SCROLL, "Ptr", 0, "UInt", 0))
            return "*" A_LastError
        if !(DllCall("user32.dll\SendMessageTimeout", "Ptr", 0xFFFF, "UInt", 0x001A, "UPtr", 0x0069, "Ptr", 0, "UInt", 0, "UInt", 1000, "UPtr", 0, "Ptr"))
            return "*" A_LastError
        return this.GET()
    }
}
example:

Code: Select all

#Numpad7::
    MsgBox % WheelScrollLines.GET()
return


#Numpad8::
    MsgBox % WheelScrollLines.SET(3)
return
output:

Code: Select all

3

Ref:
- SystemParametersInfo function
[AHK] v2.0.2 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3150
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: A collection of small ahk functions

10 May 2016, 02:42

DnsFlushResolverCache (Flush entire DNS cache, same as ipconfig /flushdns)

Source:
FlushDNS.ahk

example:

Code: Select all

MsgBox % DnsFlushResolverCache()
output:

Code: Select all

If the function succeeds, the return value is TRUE.

Ref:
- undocumented dnsapi.dll function
[AHK] v2.0.2 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3150
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: A collection of small ahk functions

20 May 2016, 07:53

RSHash (Robert Sedgewick's string hashing algorithm)
google.books wrote:[...] The RS hash algorithm is a multiplicative hash algorithm which takes 8 bits of hash key in a cycle, and it uses multiple constants in the multiply process to increase randomness in each cycle [...]

Code: Select all

RSHash(str)
{
    a := 0xF8C9, b := 0x5C6B7, h := 0
    loop, parse, str
        h := h * a + Asc(A_LoopField), a *= b
    return (h & 0x7FFFFFFF)
}
example:

Code: Select all

MsgBox % RSHash("21EC2020-3AEA-4069-A2DD-08002B30309D")
output:

Code: Select all

1866374240

Ref:
- Hash Algorithm Design for a Non-uniformly Distributed Database (Google Books - Page 75)
[AHK] v2.0.2 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
boiler
Posts: 13608
Joined: 21 Dec 2014, 02:44

Re: A collection of small ahk functions

20 May 2016, 14:53

I must be missing something, but how did you create the string "21EC2020-3AEA-4069-A2DD-08002B30309D"? Is there a function that is supposed to convert the "1866374240" to that? I'm sure I'm showing my ignorance on this subject.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 34 guests