Util::range 추가

This commit is contained in:
2020-04-09 02:07:50 +09:00
parent d540cd0a77
commit 09d28eff90
18 changed files with 77 additions and 43 deletions
+36 -1
View File
@@ -721,10 +721,45 @@ class Util extends \utilphp\util
return 0;
}
function isPowerOfTwo(int $number):bool{
public static function isPowerOfTwo(int $number):bool{
if($number <= 0){
return false;
}
return ($number & ($number - 1)) == 0;
}
/**
* Python 3의 range와 동일
* @param int $from
* @param null|int $to
* @param null|int $step
* @return \Traversable
* @throws InvalidArgumentException
*/
public static function range(int $from, ?int $to=null, ?int $step=null):\Traversable{
if($to === null){
$to = $from;
$from = 0;
}
if($step === null){
$step = 1;
}
else if($step === 0){
throw new \InvalidArgumentException('xrange() arg 3 must not be zero');
}
if($step > 0){
while($from < $to){
yield $from;
$from += 1;
}
}
else{
while($from > $to){
yield $from;
$from -= 1;
}
}
}
};