Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
Well, I was bored so I whipped up an alternate version:
JASS:
library NextPow2 initializer Init
globals
private integer array pow
// pivot point of list (for first comparison)
private constant integer PIVOT = 15
endglobals
function NextPow2 takes integer number returns integer
local integer first = 0
local integer last = 31
local integer middle = PIVOT
if number < 1 then
return -1
endif
loop
exitwhen (last - first) == 1
if number == pow[middle] then
return middle
elseif number > pow[middle] then
set first = middle
elseif number < pow[middle] then
set last = middle
endif
set middle = (first + last) / 2
endloop
return last
endfunction
private function Init takes nothing returns nothing
local integer i = 1
set pow[0] = 1
set pow[31] = 2147483647
loop
exitwhen i == 31
set pow[i] = pow[i - 1] * 2
set i = i + 1
endloop
endfunction
endlibrary
It is much longer and far more convoluted than the standard method. That instantly means it is better.
It pretty much has a static complexity of 5 iterations (unless the number input is a power of 2, then it can be lower). If you alter the pivot, you might be able to get different complexities (e.g. if you lower it, you may chip off 1 iteration for some lower powers, but it may increase the complexity for powers above 2^pivot).
Remember the formula for success: sugar + spice + everything nice + unnecessary complications = nanoseconds saved for your life to spend time with your future children
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.