Author: david
Date: Fri Feb 11 17:27:13 2011
New Revision: 8963
Log:
Use hash table to find children.
Modified:
trunk/lib/task/nestedsetBuildTask.class.php
Modified: trunk/lib/task/nestedsetBuildTask.class.php
==============================================================================
--- trunk/lib/task/nestedsetBuildTask.class.php Fri Feb 11 16:07:52 2011
(r8962)
+++ trunk/lib/task/nestedsetBuildTask.class.php Fri Feb 11 17:27:13 2011
(r8963)
@@ -68,55 +68,59 @@
$sth = $conn->query($sql);
$infoObjects = $sth->fetchAll(PDO::FETCH_ASSOC);
- $tree = array();
+ $tree = $children = array();
- // Add root node to tree
foreach ($infoObjects as $key => $item)
{
+ // Add root node to tree
if (QubitInformationObject::ROOT_ID == $item['id'])
{
- array_push($tree, $item + array('children' => array()));
- unset($infoObjects[$key]);
- break;
+ array_push($tree, $item + array(
+ 'lft' => 1,
+ 'rgt' => null,
+ 'children' => array())
+ );
+ }
+ else
+ {
+ // build hash of child rows keyed on parent_id
+ if (isset($children[$item['parent_id']]))
+ {
+ array_push($children[$item['parent_id']], $item['id']);
+ }
+ else
+ {
+ $children[$item['parent_id']] = array($item['id']);
+ }
}
}
// Recursively add child nodes
- self::addChildren($tree[0], $infoObjects);
-
- //self::get_width($tree[1]);
+ self::addChildren($tree[0], $children, 1);
print_r($tree);
}
- protected function addChildren(&$node, &$infoObjects)
+ protected function addChildren(&$node, $children, $lft)
{
- foreach ($infoObjects as $key => $item)
+ $width = 2;
+
+ if (isset($children[$node['id']]))
{
- if ($node['id'] == $item['parent_id'])
+ $lft++;
+ foreach ($children[$node['id']] as $id)
{
- $child = $item + array('children' => array());
+ $child = array('id' => $id, 'lft' => $lft, 'rgt' => null, 'children'
=> array());
- self::addChildren($child, $infoObjects);
+ $width = self::addChildren($child, $infoObjects, $lft);
+ $lft += $width;
array_push($node['children'], $child);
- unset($infoObjects[$key]);
}
}
- }
- protected function get_width(&$node)
- {
- foreach ($node['children'] as $childId)
- {
- if (isset($tree[$childId]))
- {
- $node['width'] = self::get_width($tree[$childId]) + 1;
- }
- else
- {
- return 2;
- }
- }
+ $node['rgt'] = $lft + 1;
+
+ return $width;
}
}
--
You received this message because you are subscribed to the Google Groups
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/qubit-commits?hl=en.