MDL-33635 improve collatorlib::ksort() coding style and perf

Thanks Eloy Lafuente for spotting my sloppy coding style!
This commit is contained in:
Petr Skoda 2012-06-13 16:37:56 +02:00
parent b71d0b43fc
commit bc53b4e9d7
2 changed files with 15 additions and 4 deletions

View File

@ -476,6 +476,13 @@ class collatorlib_testcase extends basic_testcase {
$this->assertSame(array_values($arr), array('aa', 'ab', 'áb', 'Áb', 'cc'), $this->error);
$this->assertSame(array_keys($arr), array(1, 'b', 'a', 'x', 0), $this->error);
$this->assertTrue($result);
$a = array(2=>'b', 1=>'c');
$c =& $a;
$b =& $a;
collatorlib::asort($b);
$this->assertSame($a, $b);
$this->assertSame($c, $b);
}
/**
@ -565,6 +572,13 @@ class collatorlib_testcase extends basic_testcase {
$this->assertSame(array_keys($arr), array('1.1.1', '1.2', '1.20.2'));
$this->assertSame(array_values($arr), array(array(), $obj, null));
$this->assertTrue($result);
$a = array(2=>'b', 1=>'c');
$c =& $a;
$b =& $a;
collatorlib::ksort($b);
$this->assertSame($a, $b);
$this->assertSame($c, $b);
}
}

View File

@ -872,10 +872,7 @@ class collatorlib {
}
// This is a bit slow, but we need to keep the references
$original = $arr;
$count = count($arr);
for($i=0; $i<$count; $i++) {
array_pop($arr);
}
$arr = array(); // Surprisingly this does not break references outside
foreach ($keys as $key) {
$arr[$key] = $original[$key];
}