TYPO3 CMS  TYPO3_7-6
SortedTreeNodeCollection.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Backend\Tree;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
24 {
31  public function contains(\TYPO3\CMS\Backend\Tree\TreeNode $node)
32  {
33  return $this->offsetOf($node) !== -1;
34  }
35 
42  protected function offsetOf(\TYPO3\CMS\Backend\Tree\TreeNode $node)
43  {
44  return $this->binarySearch($node, 0, $this->count() - 1);
45  }
46 
55  protected function binarySearch(\TYPO3\CMS\Backend\Tree\TreeNode $node, $start, $end)
56  {
57  if (!$start && $end - $start >= 2 || $end - $start > 2) {
58  $divider = ceil(($end - $start) / 2);
59  if ($this->offsetGet($divider)->equals($node)) {
60  return $divider;
61  } elseif ($this->offsetGet($divider)->compareTo($node) > 0) {
62  return $this->binarySearch($node, $start, $divider - 1);
63  } else {
64  return $this->binarySearch($node, $divider + 1, $end);
65  }
66  } else {
67  if ($this->offsetGet($start)->equals($node)) {
68  return $start;
69  } elseif ($this->offsetGet($end)->equals($node)) {
70  return $end;
71  } else {
72  return -1;
73  }
74  }
75  }
76 
82  protected function normalize()
83  {
84  $nodes = [];
85  foreach ($this as $node) {
86  $nodes[] = $node;
87  }
88  $this->exchangeArray($nodes);
89  }
90 
97  public function append($node)
98  {
99  parent::append($node);
100  $this->asort();
101  $this->normalize();
102  }
103 }
offsetOf(\TYPO3\CMS\Backend\Tree\TreeNode $node)
contains(\TYPO3\CMS\Backend\Tree\TreeNode $node)
binarySearch(\TYPO3\CMS\Backend\Tree\TreeNode $node, $start, $end)