‪TYPO3CMS  ‪main
SortedTreeNodeCollection.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
25 {
31  public function ‪contains(‪TreeNode $node)
32  {
33  return $this->‪offsetOf($node) !== -1;
34  }
35 
41  protected function ‪offsetOf(‪TreeNode $node)
42  {
43  return $this->‪binarySearch($node, 0, $this->count() - 1);
44  }
45 
53  protected function ‪binarySearch(‪TreeNode $node, $start, $end)
54  {
55  if (!$start && $end - $start >= 2 || $end - $start > 2) {
56  $divider = (int)ceil(($end - $start) / 2);
57  if ($this->offsetGet($divider)->equals($node)) {
58  return $divider;
59  }
60  if ($this->offsetGet($divider)->compareTo($node) > 0) {
61  return $this->‪binarySearch($node, $start, $divider - 1);
62  }
63  return $this->‪binarySearch($node, $divider + 1, $end);
64  }
65  if ($this->offsetGet($start)->equals($node)) {
66  return $start;
67  }
68  if ($this->offsetGet($end)->equals($node)) {
69  return $end;
70  }
71  return -1;
72  }
73 
77  protected function ‪normalize()
78  {
79  $nodes = [];
80  foreach ($this as $node) {
81  $nodes[] = $node;
82  }
83  $this->exchangeArray($nodes);
84  }
85 
91  public function ‪append($node): void
92  {
93  parent::append($node);
94  $this->‪asort();
95  $this->‪normalize();
96  }
97 }
‪TYPO3\CMS\Backend\Tree
Definition: AbstractTree.php:16
‪TYPO3\CMS\Backend\Tree\SortedTreeNodeCollection\contains
‪bool contains(TreeNode $node)
Definition: SortedTreeNodeCollection.php:31
‪TYPO3\CMS\Backend\Tree\SortedTreeNodeCollection\normalize
‪normalize()
Definition: SortedTreeNodeCollection.php:77
‪TYPO3\CMS\Backend\Tree\SortedTreeNodeCollection\append
‪append($node)
Definition: SortedTreeNodeCollection.php:91
‪TYPO3\CMS\Backend\Tree\TreeNodeCollection
Definition: TreeNodeCollection.php:25
‪TYPO3\CMS\Backend\Tree\TreeNode
Definition: TreeNode.php:25
‪TYPO3\CMS\Backend\Tree\SortedTreeNodeCollection\binarySearch
‪int binarySearch(TreeNode $node, $start, $end)
Definition: SortedTreeNodeCollection.php:53
‪TYPO3\CMS\Backend\Tree\SortedTreeNodeCollection\offsetOf
‪int offsetOf(TreeNode $node)
Definition: SortedTreeNodeCollection.php:41
‪TYPO3\CMS\Backend\Tree\SortedTreeNodeCollection
Definition: SortedTreeNodeCollection.php:25
‪TYPO3\CMS\Backend\Tree\TreeNodeCollection\asort
‪asort(int $flags=SORT_REGULAR)
Definition: TreeNodeCollection.php:50