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

Documentation generated on Tue, 01 Jun 2010 17:04:51 +0200 by phpDocumentor 1.4.3