1
0
Fork 0

Remove Pool::getMaxId and the solver's reliance on it

pull/1168/head
Jordi Boggiano 2012-10-12 18:33:27 +02:00
parent c0e5736ae7
commit 18492a1f84
3 changed files with 17 additions and 41 deletions

View File

@ -29,12 +29,7 @@ class Decisions implements \Iterator, \Countable
public function __construct($pool) public function __construct($pool)
{ {
$this->pool = $pool; $this->pool = $pool;
$this->decisionMap = array();
if (version_compare(PHP_VERSION, '5.3.4', '>=')) {
$this->decisionMap = new \SplFixedArray($this->pool->getMaxId() + 1);
} else {
$this->decisionMap = array_fill(0, $this->pool->getMaxId() + 1, 0);
}
} }
public function decide($literal, $level, $why) public function decide($literal, $level, $why)
@ -51,8 +46,8 @@ class Decisions implements \Iterator, \Countable
$packageId = abs($literal); $packageId = abs($literal);
return ( return (
$literal > 0 && $this->decisionMap[$packageId] > 0 || $literal > 0 && isset($this->decisionMap[$packageId]) && $this->decisionMap[$packageId] > 0 ||
$literal < 0 && $this->decisionMap[$packageId] < 0 $literal < 0 && isset($this->decisionMap[$packageId]) && $this->decisionMap[$packageId] < 0
); );
} }
@ -61,29 +56,36 @@ class Decisions implements \Iterator, \Countable
$packageId = abs($literal); $packageId = abs($literal);
return ( return (
($this->decisionMap[$packageId] > 0 && $literal < 0) || (isset($this->decisionMap[$packageId]) && $this->decisionMap[$packageId] > 0 && $literal < 0) ||
($this->decisionMap[$packageId] < 0 && $literal > 0) (isset($this->decisionMap[$packageId]) && $this->decisionMap[$packageId] < 0 && $literal > 0)
); );
} }
public function decided($literalOrPackageId) public function decided($literalOrPackageId)
{ {
return $this->decisionMap[abs($literalOrPackageId)] != 0; return !empty($this->decisionMap[abs($literalOrPackageId)]);
} }
public function undecided($literalOrPackageId) public function undecided($literalOrPackageId)
{ {
return $this->decisionMap[abs($literalOrPackageId)] == 0; return empty($this->decisionMap[abs($literalOrPackageId)]);
} }
public function decidedInstall($literalOrPackageId) public function decidedInstall($literalOrPackageId)
{ {
return $this->decisionMap[abs($literalOrPackageId)] > 0; $packageId = abs($literalOrPackageId);
return isset($this->decisionMap[$packageId]) && $this->decisionMap[$packageId] > 0;
} }
public function decisionLevel($literalOrPackageId) public function decisionLevel($literalOrPackageId)
{ {
return abs($this->decisionMap[abs($literalOrPackageId)]); $packageId = abs($literalOrPackageId);
if (isset($this->decisionMap[$packageId])) {
return abs($this->decisionMap[$packageId]);
}
return 0;
} }
public function decisionRule($literalOrPackageId) public function decisionRule($literalOrPackageId)
@ -179,7 +181,7 @@ class Decisions implements \Iterator, \Countable
{ {
$packageId = abs($literal); $packageId = abs($literal);
$previousDecision = $this->decisionMap[$packageId]; $previousDecision = isset($this->decisionMap[$packageId]) ? $this->decisionMap[$packageId] : null;
if ($previousDecision != 0) { if ($previousDecision != 0) {
$literalString = $this->pool->literalToString($literal); $literalString = $this->pool->literalToString($literal);
$package = $this->pool->literalToPackage($literal); $package = $this->pool->literalToPackage($literal);

View File

@ -206,16 +206,6 @@ class Pool
return $this->packages[$id - 1]; return $this->packages[$id - 1];
} }
/**
* Retrieves the highest id assigned to a package in this pool
*
* @return int Highest package id
*/
public function getMaxId()
{
return $this->id - 1;
}
/** /**
* Searches all packages providing the given package name and match the constraint * Searches all packages providing the given package name and match the constraint
* *

View File

@ -129,20 +129,4 @@ class PoolTest extends TestCase
$this->assertEquals(array(), $pool->whatProvides('foo')); $this->assertEquals(array(), $pool->whatProvides('foo'));
} }
public function testGetMaxId()
{
$pool = new Pool;
$repository = new ArrayRepository;
$firstPackage = $this->getPackage('foo', '1');
$secondPackage = $this->getPackage('foo1', '1');
$this->assertEquals(0, $pool->getMaxId());
$repository->addPackage($firstPackage);
$repository->addPackage($secondPackage);
$pool->addRepository($repository);
$this->assertEquals(2, $pool->getMaxId());
}
} }