Ones() and Zeros()

Post your working scripts, libraries and tools for AHK v1.1 and older
Sam_
Posts: 146
Joined: 20 Mar 2014, 20:24

Ones() and Zeros()

Post by Sam_ » 09 May 2022, 14:46

The following approach to initializing large simple numeric arrays to 0 or 1 results in noticeably improved performance over manually iterating over the array to initialize its values, even when the .SetCapacity() method is used first. The technique could easily be generalized to initialize array values to any single printable ASCII character, if desired.

The functions:

Code: Select all

; Returns a simple numeric array of length n with values initialized to 0
Zeros(n){
	Local
	VarSetCapacity(bli,n,48)
	Return StrSplit(StrGet(&bli,n,"CP0"))
}

; Returns a simple numeric array of length n with values initialized to 1
Ones(n){
	Local
	VarSetCapacity(bli,n,49)
	Return StrSplit(StrGet(&bli,n,"CP0"))
}

; Returns a simple numeric array of keys from 0 to n with values initialized to 0
Zeros0(n){
	Local
	VarSetCapacity(bli,n,48), Ali:=StrSplit(StrGet(&bli,n,"CP0")), Ali[0]:=0
	Return Ali
}

; Returns a simple numeric array of keys from 0 to n with values initialized to 1
Ones0(n){
	Local
	VarSetCapacity(bli,n,49), Ali:=StrSplit(StrGet(&bli,n,"CP0")), Ali[0]:=1
	Return Ali
}
Example Usage:

Code: Select all

MsgBox, , Zeros(), % st_printArr(Zeros(10))
MsgBox, , Ones(), % st_printArr(Ones(10))

MsgBox, , Zeros0(), % st_printArr(Zeros0(10))
MsgBox, , Ones0(), % st_printArr(Ones0(10))

ExitApp



; Returns a simple numeric array of length n with values initialized to 0
Zeros(n){
	Local
	VarSetCapacity(bli,n,48)
	Return StrSplit(StrGet(&bli,n,"CP0"))
}

; Returns a simple numeric array of length n with values initialized to 1
Ones(n){
	Local
	VarSetCapacity(bli,n,49)
	Return StrSplit(StrGet(&bli,n,"CP0"))
}

; Returns a simple numeric array of keys from 0 to n with values initialized to 0
Zeros0(n){
	Local
	VarSetCapacity(bli,n,48), Ali:=StrSplit(StrGet(&bli,n,"CP0")), Ali[0]:=0
	Return Ali
}

; Returns a simple numeric array of keys from 0 to n with values initialized to 1
Ones0(n){
	Local
	VarSetCapacity(bli,n,49), Ali:=StrSplit(StrGet(&bli,n,"CP0")), Ali[0]:=1
	Return Ali
}

st_printArr(array, depth=5, indentLevel=""){
	list:=""
   for k,v in Array
   {
      list.= indentLevel "[" k "]"
      if (IsObject(v) && depth>1)
         list.="`r`n" st_printArr(v, depth-1, indentLevel . "    ")
      Else
         list.=" => " v
      list.="`r`n"
   }
   return rtrim(list)
}

iPhilip
Posts: 827
Joined: 02 Oct 2013, 12:21

Re: Ones() and Zeros()

Post by iPhilip » 12 May 2022, 11:29

@Sam_, Thank you for sharing this interesting approach. Here is a simple function that I use to initialize arrays:

Code: Select all

; Returns a linear array of dimension n initialized with the specified character.
; Char can be an ANSI or Unicode character. The default is 0.

InitArray(n, Char := 0) {
   local String := Format("{:0" n "}", "")  ; String of n zeros
   return Char = 0 ? StrSplit(String) : StrSplit(StrReplace(String, 0, Char))
}
It has the slight benefit of allowing the array to be initialized with ANSI or Unicode characters. Other than that, it's about as fast as your functions.

P.S.: I like your st_printArr() function. :)

Edit: Typo
Last edited by iPhilip on 12 May 2022, 16:50, edited 1 time in total.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

Sam_
Posts: 146
Joined: 20 Mar 2014, 20:24

Re: Ones() and Zeros()

Post by Sam_ » 12 May 2022, 11:43

iPhilip wrote:
12 May 2022, 11:29
@Sam_, Thank you for sharing this interesting approach. Here is a simple function that I use to initialize arrays:

Code: Select all

; Returns a linear array of dimension n initialized with the specified character.
; Char can be an ANSI or Unicode character. The default is 0.

InitArray(n, Char := 0) {
   local String := Format("{:0" n "}", "")  ; String on n zeros
   return Char = 0 ? StrSplit(String) : StrSplit(StrReplace(String, 0, Char))
}
It has the slight benefit of allowing the array to be initialized with ANSI or Unicode characters. Other than that, it's about as fast as your functions.
Nice, thanks for sharing!
iPhilip wrote:
12 May 2022, 11:29
P.S.: I like your st_printArr() function. :)
Actually I picked that one up from here, but I guess it's part of String Things. Super handy tho!

Post Reply

Return to “Scripts and Functions (v1)”