Adding dimensions() and maxDimensions()

Splitting the Set::countDim() into two methods.  This creates a simpler
API, as each method only does one job, and only has one argument.  Also
replacing recursion with iteration.
This commit is contained in:
mark_story 2012-01-14 22:01:08 -05:00
parent db3485d47c
commit 51e3ee0425
2 changed files with 127 additions and 6 deletions

View file

@ -142,4 +142,83 @@ class Set2Test extends CakeTestCase {
$result = Set2::get($data, '1.Article'); $result = Set2::get($data, '1.Article');
$this->assertEquals($data[1]['Article'], $result); $this->assertEquals($data[1]['Article'], $result);
} }
/**
* Test dimensions.
*
* @return void
*/
public function testDimensions() {
$result = Set2::dimensions(array());
$this->assertEquals($result, 0);
$data = array('one', '2', 'three');
$result = Set2::dimensions($data);
$this->assertEquals($result, 1);
$data = array('1' => '1.1', '2', '3');
$result = Set2::dimensions($data);
$this->assertEquals($result, 1);
$data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => '3.1.1'));
$result = Set2::dimensions($data);
$this->assertEquals($result, 2);
$data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
$result = Set2::dimensions($data);
$this->assertEquals($result, 1);
$data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
$result = Set2::dimensions($data);
$this->assertEquals($result, 2);
}
/**
* Test maxDimensions
*
* @return void
*/
public function testMaxDimensions() {
$data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
$result = Set2::maxDimensions($data);
$this->assertEquals($result, 2);
$data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
$result = Set2::maxDimensions($data);
$this->assertEquals($result, 3);
$data = array(
'1' => array('1.1' => '1.1.1'),
array('2' => array('2.1' => array('2.1.1' => '2.1.1.1'))),
'3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
);
$result = Set2::maxDimensions($data);
$this->assertEquals($result, 4);
$data = array(
'1' => array('1.1' => '1.1.1'),
array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1')))),
'3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
);
$result = Set2::maxDimensions($data);
$this->assertEquals($result, 5);
$data = array(
'1' => array('1.1' => '1.1.1'),
array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))),
'3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
);
$result = Set2::maxDimensions($data);
$this->assertEquals($result, 5);
$data = array(
'1' => array('1.1' => '1.1.1'),
array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))),
'3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
);
$result = Set2::maxDimensions($data);
$this->assertEquals($result, 5);
}
} }

View file

@ -90,8 +90,50 @@ class Set2 {
} }
/**
* Counts the dimensions of an array.
* Only considers the dimension of the first element in the array.
*
* If you have an un-even or hetrogenous array, consider using Set2::maxDimensions()
* to get the dimensions of the array.
*
* @param array $array Array to count dimensions on
* @return integer The number of dimensions in $data
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::countDim
*/
public static function dimensions(array $data) { public static function dimensions(array $data) {
if (empty($data)) {
return 0;
}
reset($data);
$depth = 1;
while ($elem = array_shift($data)) {
if (is_array($elem)) {
$depth += 1;
$data =& $elem;
} else {
break;
}
}
return $depth;
}
/**
* Counts the dimensions of *all* array elements. Useful for finding the maximum
* number of dimensions in a mixed array.
*
* @param array $data Array to count dimensions on
* @return integer The maximum number of dimensions in $data
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::countDim
*/
public static function maxDimensions(array $data) {
$depth = array();
if (is_array($data) && reset($data) !== false) {
foreach ($data as $value) {
$depth[] = Set2::dimensions((array)$value) + 1;
}
}
return max($depth);
} }
/** /**