Permalink
Browse files

Performance tweaks for get_previous_sibling and get_next_sibling methods

  • Loading branch information...
stefangabos committed Jul 14, 2017
1 parent d378b67 commit 3e27ffc70b79241eee470bf2916c18ea8b530759
Showing with 10 additions and 20 deletions.
  1. +10 −20 Zebra_Mptt.php
View
@@ -825,23 +825,18 @@ public function get_descendant_count($node, $direct_descendants_only = true) {
*/
public function get_next_sibling($node) {
// if parent node exists in the lookup array OR we're looking for the topmost nodes
if (isset($this->lookup[$node])) {
// properties of the node
$properties = $this->lookup[$node];
// get node's siblings
$siblings = $this->get_descendants($properties['parent']);
// if node exists, get its siblings
// (if $node exists this will never be an empty array as it will contain at least $node)
if ($siblings = $this->get_siblings($node, true)) {
// get the node's position among the siblings
$node_position = array_search($node, array_keys($siblings));
// get next node
$siblings = array_slice($siblings, $node_position + 1, 1);
$sibling = array_slice($siblings, $node_position + 1, 1);
// return result
return !empty($siblings) ? array_pop($siblings) : 0;
return !empty($sibling) ? array_pop($sibling) : 0;
}
@@ -933,23 +928,18 @@ public function get_path($node) {
*/
public function get_previous_sibling($node) {
// if parent node exists in the lookup array OR we're looking for the topmost nodes
if (isset($this->lookup[$node])) {
// properties of the node
$properties = $this->lookup[$node];
// get node's siblings
$siblings = $this->get_descendants($properties['parent']);
// if node exists, get its siblings
// (if $node exists this will never be an empty array as it will contain at least $node)
if ($siblings = $this->get_siblings($node, true)) {
// get the node's position among the siblings
$node_position = array_search($node, array_keys($siblings));
// get previous node
$siblings = $node_position > 0 ? array_slice($siblings, $node_position - 1, 1) : array();
$sibling = $node_position > 0 ? array_slice($siblings, $node_position - 1, 1) : array();
// return result
return !empty($siblings) ? array_pop($siblings) : 0;
return !empty($sibling) ? array_pop($sibling) : 0;
}

0 comments on commit 3e27ffc

Please sign in to comment.