TYPO3 CMS  TYPO3_8-7
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  }
62  if ($this->offsetGet($divider)->compareTo($node) > 0) {
63  return $this->binarySearch($node, $start, $divider - 1);
64  }
65  return $this->binarySearch($node, $divider + 1, $end);
66  }
67  if ($this->offsetGet($start)->equals($node)) {
68  return $start;
69  }
70  if ($this->offsetGet($end)->equals($node)) {
71  return $end;
72  }
73  return -1;
74  }
75 
79  protected function normalize()
80  {
81  $nodes = [];
82  foreach ($this as $node) {
83  $nodes[] = $node;
84  }
85  $this->exchangeArray($nodes);
86  }
87 
93  public function append($node)
94  {
95  parent::append($node);
96  $this->asort();
97  $this->normalize();
98  }
99 }
offsetOf(\TYPO3\CMS\Backend\Tree\TreeNode $node)
contains(\TYPO3\CMS\Backend\Tree\TreeNode $node)
binarySearch(\TYPO3\CMS\Backend\Tree\TreeNode $node, $start, $end)