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 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.6, 2011-02-27
  26.  */
  27.  
  28.  
  29. /**
  30.  * PHPExcel_HashTable
  31.  *
  32.  * @category   PHPExcel
  33.  * @package    PHPExcel
  34.  * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  35.  */
  36. {
  37.     /**
  38.      * HashTable elements
  39.      *
  40.      * @var array 
  41.      */
  42.     public $_items = array();
  43.  
  44.     /**
  45.      * HashTable key map
  46.      *
  47.      * @var array 
  48.      */
  49.     public $_keyMap = array();
  50.  
  51.     /**
  52.      * Create a new PHPExcel_HashTable
  53.      *
  54.      * @param    PHPExcel_IComparable[] $pSource    Optional source array to create HashTable from
  55.      * @throws    Exception
  56.      */
  57.     public function __construct($pSource null)
  58.     {
  59.         if (!is_null($pSource)) {
  60.             // Create HashTable
  61.             $this->addFromSource($pSource);
  62.         }
  63.     }
  64.  
  65.     /**
  66.      * Add HashTable items from source
  67.      *
  68.      * @param    PHPExcel_IComparable[] $pSource    Source array to create HashTable from
  69.      * @throws    Exception
  70.      */
  71.     public function addFromSource($pSource null{
  72.         // Check if an array was passed
  73.         if ($pSource == null{
  74.             return;
  75.         else if (!is_array($pSource)) {
  76.             throw new Exception('Invalid array parameter passed.');
  77.         }
  78.  
  79.         foreach ($pSource as $item{
  80.             $this->add($item);
  81.         }
  82.     }
  83.  
  84.     /**
  85.      * Add HashTable item
  86.      *
  87.      * @param    PHPExcel_IComparable $pSource    Item to add
  88.      * @throws    Exception
  89.      */
  90.     public function add(PHPExcel_IComparable $pSource null{
  91.         $hash $pSource->getHashCode();
  92.         if (!isset($this->_items[$hash])) {
  93.             $this->_items[$hash$pSource;
  94.             $this->_keyMap[count($this->_items1$hash;
  95.         }
  96.     }
  97.  
  98.     /**
  99.      * Remove HashTable item
  100.      *
  101.      * @param    PHPExcel_IComparable $pSource    Item to remove
  102.      * @throws    Exception
  103.      */
  104.     public function remove(PHPExcel_IComparable $pSource null{
  105.         $hash $pSource->getHashCode();
  106.         if (isset($this->_items[$hash])) {
  107.             unset($this->_items[$hash]);
  108.  
  109.             $deleteKey = -1;
  110.             foreach ($this->_keyMap as $key => $value{
  111.                 if ($deleteKey >= 0{
  112.                     $this->_keyMap[$key 1$value;
  113.                 }
  114.  
  115.                 if ($value == $hash{
  116.                     $deleteKey $key;
  117.                 }
  118.             }
  119.             unset($this->_keyMap[count($this->_keyMap1]);
  120.         }
  121.     }
  122.  
  123.     /**
  124.      * Clear HashTable
  125.      *
  126.      */
  127.     public function clear({
  128.         $this->_items = array();
  129.         $this->_keyMap = array();
  130.     }
  131.  
  132.     /**
  133.      * Count
  134.      *
  135.      * @return int 
  136.      */
  137.     public function count({
  138.         return count($this->_items);
  139.     }
  140.  
  141.     /**
  142.      * Get index for hash code
  143.      *
  144.      * @param    string    $pHashCode 
  145.      * @return    int    Index
  146.      */
  147.     public function getIndexForHashCode($pHashCode ''{
  148.         return array_search($pHashCode$this->_keyMap);
  149.     }
  150.  
  151.     /**
  152.      * Get by index
  153.      *
  154.      * @param    int    $pIndex 
  155.      * @return    PHPExcel_IComparable 
  156.      *
  157.      */
  158.     public function getByIndex($pIndex 0{
  159.         if (isset($this->_keyMap[$pIndex])) {
  160.             return $this->getByHashCode$this->_keyMap[$pIndex);
  161.         }
  162.  
  163.         return null;
  164.     }
  165.  
  166.     /**
  167.      * Get by hashcode
  168.      *
  169.      * @param    string    $pHashCode 
  170.      * @return    PHPExcel_IComparable 
  171.      *
  172.      */
  173.     public function getByHashCode($pHashCode ''{
  174.         if (isset($this->_items[$pHashCode])) {
  175.             return $this->_items[$pHashCode];
  176.         }
  177.  
  178.         return null;
  179.     }
  180.  
  181.     /**
  182.      * HashTable to array
  183.      *
  184.      * @return PHPExcel_IComparable[] 
  185.      */
  186.     public function toArray({
  187.         return $this->_items;
  188.     }
  189.  
  190.     /**
  191.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  192.      */
  193.     public function __clone({
  194.         $vars get_object_vars($this);
  195.         foreach ($vars as $key => $value{
  196.             if (is_object($value)) {
  197.                 $this->$key clone $value;
  198.             }
  199.         }
  200.     }
  201. }

Documentation generated on Sun, 27 Feb 2011 16:32:13 -0800 by phpDocumentor 1.4.3