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

Documentation generated on Mon, 11 Jan 2010 08:11:12 +0100 by phpDocumentor 1.4.1