PHPExcel
[ class tree: PHPExcel ] [ index: PHPExcel ] [ all elements ]

Source for file HashTable.php

Documentation is available at HashTable.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2009 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20.  *
  21.  * @category   PHPExcel
  22.  * @package    PHPExcel
  23.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.0, 2009-08-10
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../');
  35. }
  36.  
  37. /** PHPExcel_IComparable */
  38. require_once PHPEXCEL_ROOT 'PHPExcel/IComparable.php';
  39.  
  40.  
  41. /**
  42.  * PHPExcel_HashTable
  43.  *
  44.  * @category   PHPExcel
  45.  * @package    PHPExcel
  46.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  47.  */
  48. {
  49.     /**
  50.      * HashTable elements
  51.      *
  52.      * @var array 
  53.      */
  54.     public $_items = array();
  55.     
  56.     /**
  57.      * HashTable key map
  58.      *
  59.      * @var array 
  60.      */
  61.     public $_keyMap = array();
  62.     
  63.     /**
  64.      * Create a new PHPExcel_HashTable
  65.      *
  66.      * @param     PHPExcel_IComparable[] $pSource    Optional source array to create HashTable from
  67.      * @throws     Exception
  68.      */
  69.     public function __construct($pSource null)
  70.     {
  71.         if (!is_null($pSource)) {
  72.             // Create HashTable
  73.             $this->addFromSource($pSource);
  74.         }
  75.     }
  76.     
  77.     /**
  78.      * Add HashTable items from source
  79.      *
  80.      * @param     PHPExcel_IComparable[] $pSource    Source array to create HashTable from
  81.      * @throws     Exception
  82.      */
  83.     public function addFromSource($pSource null{
  84.         // Check if an array was passed
  85.         if ($pSource == null{
  86.             return;
  87.         else if (!is_array($pSource)) {
  88.             throw new Exception('Invalid array parameter passed.');
  89.         }
  90.         
  91.         foreach ($pSource as $item{
  92.             $this->add($item);
  93.         }
  94.     }
  95.  
  96.     /**
  97.      * Add HashTable item
  98.      *
  99.      * @param     PHPExcel_IComparable $pSource    Item to add
  100.      * @throws     Exception
  101.      */
  102.     public function add(PHPExcel_IComparable $pSource null{
  103.         // Determine hashcode
  104.         $hashCode     null;
  105.         $hashIndex $pSource->getHashIndex();
  106.         if is_null $hashIndex ) ) {
  107.             $hashCode $pSource->getHashCode();
  108.         else if isset $this->_keyMap[$hashIndex) ) {
  109.             $hashCode $this->_keyMap[$hashIndex];
  110.         else {
  111.             $hashCode $pSource->getHashCode();
  112.         }
  113.             
  114.         // Add value      
  115.            if (!isset($this->_items$hashCode ])) {
  116.             $this->_items$hashCode $pSource;
  117.             $index count($this->_items1;
  118.             $this->_keyMap$index  $hashCode;
  119.             $pSource->setHashIndex$index );
  120.            else {
  121.             $pSource->setHashIndex$this->_items$hashCode ]->getHashIndex() );
  122.         }
  123.     }
  124.     
  125.     /**
  126.      * Remove HashTable item
  127.      *
  128.      * @param     PHPExcel_IComparable $pSource    Item to remove
  129.      * @throws     Exception
  130.      */
  131.     public function remove(PHPExcel_IComparable $pSource null{
  132.         if (isset($this->_items[  $pSource->getHashCode()  ])) {
  133.                unset($this->_items[  $pSource->getHashCode()  ]);
  134.                 
  135.                $deleteKey = -1;
  136.                foreach ($this->_keyMap as $key => $value{                
  137.                    if ($deleteKey >= 0{
  138.                        $this->_keyMap[$key 1$value;
  139.                    }
  140.                     
  141.                    if ($value == $pSource->getHashCode()) {
  142.                        $deleteKey $key;
  143.                    }
  144.                }
  145.                unset($this->_keyMapcount($this->_keyMap]);   
  146.         }         
  147.     }
  148.     
  149.     /**
  150.      * Clear HashTable
  151.      *
  152.      */
  153.     public function clear({
  154.         $this->_items = array();
  155.         $this->_keyMap = array();
  156.     }
  157.     
  158.     /**
  159.      * Count
  160.      *
  161.      * @return int 
  162.      */
  163.     public function count({
  164.         return count($this->_items);
  165.     }
  166.     
  167.     /**
  168.      * Get index for hash code
  169.      *
  170.      * @param     string     $pHashCode 
  171.      * @return     int     Index
  172.      */
  173.     public function getIndexForHashCode($pHashCode ''{
  174.         return array_search($pHashCode$this->_keyMap);
  175.     }
  176.     
  177.     /**
  178.      * Get by index
  179.      *
  180.      * @param    int    $pIndex 
  181.      * @return     PHPExcel_IComparable 
  182.      *
  183.      */
  184.     public function getByIndex($pIndex 0{
  185.         if (isset($this->_keyMap[$pIndex])) {
  186.             return $this->getByHashCode$this->_keyMap[$pIndex);
  187.         }
  188.         
  189.         return null;
  190.     }
  191.     
  192.     /**
  193.      * Get by hashcode
  194.      *
  195.      * @param    string    $pHashCode 
  196.      * @return     PHPExcel_IComparable 
  197.      *
  198.      */
  199.     public function getByHashCode($pHashCode ''{
  200.         if (isset($this->_items[$pHashCode])) {
  201.             return $this->_items[$pHashCode];
  202.         }
  203.         
  204.         return null;
  205.     }
  206.     
  207.     /**
  208.      * HashTable to array
  209.      *
  210.      * @return PHPExcel_IComparable[] 
  211.      */
  212.     public function toArray({
  213.         return $this->_items;
  214.     }
  215.         
  216.     /**
  217.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  218.      */
  219.     public function __clone({
  220.         $vars get_object_vars($this);
  221.         foreach ($vars as $key => $value{
  222.             if (is_object($value)) {
  223.                 $this->$key clone $value;
  224.             }
  225.         }
  226.     }
  227. }

Documentation generated on Mon, 10 Aug 2009 08:05:46 +0200 by phpDocumentor 1.4.1