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

Documentation generated on Mon, 05 Jan 2009 20:37:50 +0100 by phpDocumentor 1.4.1