TYPO3 API  SVNRelease
class.t3lib_tree_sortednodecollection.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2010-2011 TYPO3 Tree Team <http://forge.typo3.org/projects/typo3v4-extjstrees>
00006  *  All rights reserved
00007  *
00008  *  This script is part of the TYPO3 project. The TYPO3 project is
00009  *  free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  The GNU General Public License can be found at
00015  *  http://www.gnu.org/copyleft/gpl.html.
00016  *  A copy is found in the textfile GPL.txt and important notices to the license
00017  *  from the author is found in LICENSE.txt distributed with these scripts.
00018  *
00019  *
00020  *  This script is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  *  GNU General Public License for more details.
00024  *
00025  *  This copyright notice MUST APPEAR in all copies of the script!
00026  ***************************************************************/
00027 
00028 /**
00029  * Sorted Tree Node Collection
00030  *
00031  * Note: This collection works only with integers as offset keys and not
00032  * with much datasets. You have been warned!
00033  *
00034  * @author Stefan Galinski <stefan.galinski@gmail.com>
00035  * @author Steffen Ritter <info@steffen-ritter.net>
00036  * @package TYPO3
00037  * @subpackage t3lib
00038  */
00039 class t3lib_tree_SortedNodeCollection extends t3lib_tree_NodeCollection {
00040     /**
00041      * Checks if a specific node is inside the collection
00042      *
00043      * @param t3lib_tree_Node $node
00044      * @return boolean
00045      */
00046     public function contains(t3lib_tree_Node $node) {
00047         return $this->offsetOf($node) !== -1;
00048     }
00049 
00050     /**
00051      * Returns the offset key of given node
00052      *
00053      * @param t3lib_tree_Node $node
00054      * @return int
00055      */
00056     protected function offsetOf(t3lib_tree_Node $node) {
00057         return $this->binarySearch($node, 0, $this->count() - 1);
00058     }
00059 
00060     /**
00061      * Binary search that returns the offset of a given node
00062      *
00063      * @param t3lib_tree_Node $node
00064      * @param int $start
00065      * @param int $end
00066      * @return int
00067      */
00068     protected function binarySearch(t3lib_tree_Node $node, $start, $end) {
00069         if ((!$start && ($end - $start) >= 2) || ($end - $start) > 2) {
00070             $divider = ceil(($end - $start) / 2);
00071             if ($this->offsetGet($divider)->equals($node)) {
00072                 return $divider;
00073             } elseif ($this->offsetGet($divider)->compareTo($node) > 0) {
00074                 return $this->binarySearch($node, $start, $divider - 1);
00075             } else {
00076                 return $this->binarySearch($node, $divider + 1, $end);
00077             }
00078         } else {
00079             if ($this->offsetGet($start)->equals($node)) {
00080                 return $start;
00081             } elseif ($this->offsetGet($end)->equals($node)) {
00082                 return $end;
00083             } else {
00084                 return -1;
00085             }
00086         }
00087     }
00088 
00089     /**
00090      * Normalizes the array by reordering the keys
00091      *
00092      * @return void
00093      */
00094     protected function normalize() {
00095         $nodes = array();
00096         foreach ($this as $node) {
00097             $nodes[] = $node;
00098         }
00099         $this->exchangeArray($nodes);
00100     }
00101 
00102     /**
00103      * Adds a node to the internal list in a sorted approach
00104      *
00105      * @param t3lib_tree_Node $node
00106      * @return void
00107      */
00108     public function append(t3lib_tree_Node $node) {
00109         parent::append($node);
00110         $this->asort();
00111         $this->normalize();
00112     }
00113 
00114 }
00115 ?>