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.7, 2009-04-22
  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.         // Determine hashcode
  96.         $hashCode     null;
  97.         $hashIndex $pSource->getHashIndex();
  98.         if is_null $hashIndex ) ) {
  99.             $hashCode $pSource->getHashCode();
  100.         else if isset $this->_keyMap[$hashIndex) ) {
  101.             $hashCode $this->_keyMap[$hashIndex];
  102.         else {
  103.             $hashCode $pSource->getHashCode();
  104.         }
  105.             
  106.         // Add value      
  107.            if (!isset($this->_items$hashCode ])) {
  108.             $this->_items$hashCode $pSource;
  109.             $index count($this->_items1;
  110.             $this->_keyMap$index  $hashCode;
  111.             $pSource->setHashIndex$index );
  112.            else {
  113.             $pSource->setHashIndex$this->_items$hashCode ]->getHashIndex() );
  114.         }
  115.     }
  116.     
  117.     /**
  118.      * Remove HashTable item
  119.      *
  120.      * @param     PHPExcel_IComparable $pSource    Item to remove
  121.      * @throws     Exception
  122.      */
  123.     public function remove(PHPExcel_IComparable $pSource null{
  124.         if (isset($this->_items[  $pSource->getHashCode()  ])) {
  125.                unset($this->_items[  $pSource->getHashCode()  ]);
  126.                 
  127.                $deleteKey = -1;
  128.                foreach ($this->_keyMap as $key => $value{                
  129.                    if ($deleteKey >= 0{
  130.                        $this->_keyMap[$key 1$value;
  131.                    }
  132.                     
  133.                    if ($value == $pSource->getHashCode()) {
  134.                        $deleteKey $key;
  135.                    }
  136.                }
  137.                unset($this->_keyMapcount($this->_keyMap]);   
  138.         }         
  139.     }
  140.     
  141.     /**
  142.      * Clear HashTable
  143.      *
  144.      */
  145.     public function clear({
  146.         $this->_items = array();
  147.         $this->_keyMap = array();
  148.     }
  149.     
  150.     /**
  151.      * Count
  152.      *
  153.      * @return int 
  154.      */
  155.     public function count({
  156.         return count($this->_items);
  157.     }
  158.     
  159.     /**
  160.      * Get index for hash code
  161.      *
  162.      * @param     string     $pHashCode 
  163.      * @return     int     Index
  164.      */
  165.     public function getIndexForHashCode($pHashCode ''{
  166.         return array_search($pHashCode$this->_keyMap);
  167.     }
  168.     
  169.     /**
  170.      * Get by index
  171.      *
  172.      * @param    int    $pIndex 
  173.      * @return     PHPExcel_IComparable 
  174.      *
  175.      */
  176.     public function getByIndex($pIndex 0{
  177.         if (isset($this->_keyMap[$pIndex])) {
  178.             return $this->getByHashCode$this->_keyMap[$pIndex);
  179.         }
  180.         
  181.         return null;
  182.     }
  183.     
  184.     /**
  185.      * Get by hashcode
  186.      *
  187.      * @param    string    $pHashCode 
  188.      * @return     PHPExcel_IComparable 
  189.      *
  190.      */
  191.     public function getByHashCode($pHashCode ''{
  192.         if (isset($this->_items[$pHashCode])) {
  193.             return $this->_items[$pHashCode];
  194.         }
  195.         
  196.         return null;
  197.     }
  198.     
  199.     /**
  200.      * HashTable to array
  201.      *
  202.      * @return PHPExcel_IComparable[] 
  203.      */
  204.     public function toArray({
  205.         return $this->_items;
  206.     }
  207.         
  208.     /**
  209.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  210.      */
  211.     public function __clone({
  212.         $vars get_object_vars($this);
  213.         foreach ($vars as $key => $value{
  214.             if (is_object($value)) {
  215.                 $this->$key clone $value;
  216.             }
  217.         }
  218.     }
  219. }

Documentation generated on Wed, 22 Apr 2009 08:59:48 +0200 by phpDocumentor 1.4.1