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

Documentation generated on Mon, 27 Oct 2008 08:39:51 +0100 by phpDocumentor 1.4.1