BigInteger: rm loopforeach method

This commit is contained in:
terrafrost 2016-09-09 20:59:54 -08:00
parent 72d1bdf60b
commit 0ee24aa218
2 changed files with 0 additions and 103 deletions

View File

@ -3847,42 +3847,4 @@ class BigInteger
}
return $max;
}
/**
* Execute a function n times, where n is the current BigInteger.
* The passed function must accept a parameter that will be set to the the current number
* (that number will range from zero to BigInteger - 1 if the BigInteger is positive, and from BigInteger - 1 to zero if the BigInteger is negative).
* You can also set it to accept an optional parameter, that will be equal to the optional $userdata parameter.
*
* @access public
* @param callable $function
* @param &$userdata
* @return \phpseclib\Math\BigInteger
*/
function loopforeach(callable $function, &$userdata = null)
{
static $zero;
if (!isset($zero)) {
$zero = new static(0);
}
if ($this->compare($zero) < 0) { // negative
$limit = new static(-PHP_INT_MAX - 1);
$one = new static(-1);
} else { // positive
$limit = new static(PHP_INT_MAX);
$one = new static(1);
}
if ($this->compare($limit) == -((int) $one->toString())) {
$oneint = (int) $one->toString();
$thisint = (int) $this->toString();
for ($loop = 0; $loop != $thisint; $loop+= $oneint) {
$function($loop, $userdata);
}
} else {
for ($loop = new static(0); !$loop->equals($this); $loop = $loop->add($one)) {
$function((string) $loop, $userdata);
}
}
}
}

View File

@ -416,69 +416,4 @@ abstract class Unit_Math_BigInteger_TestCase extends PhpseclibTestCase
$this->assertSame((string) $min, (string) \phpseclib\Math\BigInteger::min($min, $max));
$this->assertSame((string) $min, (string) \phpseclib\Math\BigInteger::min($max, $min));
}
public function testLoopForeach()
{
$maxBigInteger = new \phpseclib\Math\BigInteger('34');
$one = new \phpseclib\Math\BigInteger('1');
$vars = [];
$maxBigInteger->loopforeach(
function ($i, &$vars) {
if ($i == 0) {
$vars['first'] = $i;
} else {
$vars['last'] = $i;
}
},
$vars
);
$this->assertSame(0, $vars['first']);
$this->assertSame(33, $vars['last']);
/* Nope, too slow
$maxBigInteger = new \phpseclib\Math\BigInteger(PHP_INT_MAX);
$maxBigInteger = $maxBigInteger->add($one);
$maxBigInteger->loopforeach(
function ($i, &$vars) {
if ($i == 0) {
$vars["first"] = $i;
} else {
$vars["last"] = $i;
}
},
$vars
);
$this->assertSame("0", $vars["first"]);
$this->assertSame((string)$maxBigInteger->subtract($one), $vars["last"]);*/
$maxBigInteger = new \phpseclib\Math\BigInteger(-34);
$maxBigInteger->loopforeach(
function ($i, &$vars) {
if ($i == 0) {
$vars['first'] = $i;
} else {
$vars['last'] = $i;
}
},
$vars
);
$this->assertSame(0, $vars['first']);
$this->assertSame(-33, $vars['last']);
/* Nope, too slow
$maxBigInteger = new \phpseclib\Math\BigInteger(-PHP_INT_MAX - 1);
$maxBigInteger = $maxBigInteger->subtract($one);
$maxBigInteger->loopforeach(
function ($i, &$vars) {
if ($i == 0) {
$vars["first"] = $i;
} else {
$vars["last"] = $i;
}
},
$vars
);
$this->assertSame("0", $vars["first"]);
$this->assertSame((string)$maxBigInteger->add($one), $vars["last"]);*/
}
}