[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/ -> HashTable.php (source)

   1  <?php
   2  
   3  /**
   4   * PHPExcel_HashTable
   5   *
   6   * Copyright (c) 2006 - 2015 PHPExcel
   7   *
   8   * This library is free software; you can redistribute it and/or
   9   * modify it under the terms of the GNU Lesser General Public
  10   * License as published by the Free Software Foundation; either
  11   * version 2.1 of the License, or (at your option) any later version.
  12   *
  13   * This library is distributed in the hope that it will be useful,
  14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16   * Lesser General Public License for more details.
  17   *
  18   * You should have received a copy of the GNU Lesser General Public
  19   * License along with this library; if not, write to the Free Software
  20   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  21   *
  22   * @category   PHPExcel
  23   * @package    PHPExcel
  24   * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  25   * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  26   * @version    ##VERSION##, ##DATE##
  27   */
  28  class PHPExcel_HashTable
  29  {
  30      /**
  31       * HashTable elements
  32       *
  33       * @var array
  34       */
  35      protected $items = array();
  36  
  37      /**
  38       * HashTable key map
  39       *
  40       * @var array
  41       */
  42      protected $keyMap = array();
  43  
  44      /**
  45       * Create a new PHPExcel_HashTable
  46       *
  47       * @param    PHPExcel_IComparable[] $pSource    Optional source array to create HashTable from
  48       * @throws    PHPExcel_Exception
  49       */
  50      public function __construct($pSource = null)
  51      {
  52          if ($pSource !== null) {
  53              // Create HashTable
  54              $this->addFromSource($pSource);
  55          }
  56      }
  57  
  58      /**
  59       * Add HashTable items from source
  60       *
  61       * @param    PHPExcel_IComparable[] $pSource    Source array to create HashTable from
  62       * @throws    PHPExcel_Exception
  63       */
  64      public function addFromSource($pSource = null)
  65      {
  66          // Check if an array was passed
  67          if ($pSource == null) {
  68              return;
  69          } elseif (!is_array($pSource)) {
  70              throw new PHPExcel_Exception('Invalid array parameter passed.');
  71          }
  72  
  73          foreach ($pSource as $item) {
  74              $this->add($item);
  75          }
  76      }
  77  
  78      /**
  79       * Add HashTable item
  80       *
  81       * @param    PHPExcel_IComparable $pSource    Item to add
  82       * @throws    PHPExcel_Exception
  83       */
  84      public function add(PHPExcel_IComparable $pSource = null)
  85      {
  86          $hash = $pSource->getHashCode();
  87          if (!isset($this->items[$hash])) {
  88              $this->items[$hash] = $pSource;
  89              $this->keyMap[count($this->items) - 1] = $hash;
  90          }
  91      }
  92  
  93      /**
  94       * Remove HashTable item
  95       *
  96       * @param    PHPExcel_IComparable $pSource    Item to remove
  97       * @throws    PHPExcel_Exception
  98       */
  99      public function remove(PHPExcel_IComparable $pSource = null)
 100      {
 101          $hash = $pSource->getHashCode();
 102          if (isset($this->items[$hash])) {
 103              unset($this->items[$hash]);
 104  
 105              $deleteKey = -1;
 106              foreach ($this->keyMap as $key => $value) {
 107                  if ($deleteKey >= 0) {
 108                      $this->keyMap[$key - 1] = $value;
 109                  }
 110  
 111                  if ($value == $hash) {
 112                      $deleteKey = $key;
 113                  }
 114              }
 115              unset($this->keyMap[count($this->keyMap) - 1]);
 116          }
 117      }
 118  
 119      /**
 120       * Clear HashTable
 121       *
 122       */
 123      public function clear()
 124      {
 125          $this->items = array();
 126          $this->keyMap = array();
 127      }
 128  
 129      /**
 130       * Count
 131       *
 132       * @return int
 133       */
 134      public function count()
 135      {
 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      {
 147          return array_search($pHashCode, $this->keyMap);
 148      }
 149  
 150      /**
 151       * Get by index
 152       *
 153       * @param    int    $pIndex
 154       * @return    PHPExcel_IComparable
 155       *
 156       */
 157      public function getByIndex($pIndex = 0)
 158      {
 159          if (isset($this->keyMap[$pIndex])) {
 160              return $this->getByHashCode($this->keyMap[$pIndex]);
 161          }
 162  
 163          return null;
 164      }
 165  
 166      /**
 167       * Get by hashcode
 168       *
 169       * @param    string    $pHashCode
 170       * @return    PHPExcel_IComparable
 171       *
 172       */
 173      public function getByHashCode($pHashCode = '')
 174      {
 175          if (isset($this->items[$pHashCode])) {
 176              return $this->items[$pHashCode];
 177          }
 178  
 179          return null;
 180      }
 181  
 182      /**
 183       * HashTable to array
 184       *
 185       * @return PHPExcel_IComparable[]
 186       */
 187      public function toArray()
 188      {
 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      {
 197          $vars = get_object_vars($this);
 198          foreach ($vars as $key => $value) {
 199              if (is_object($value)) {
 200                  $this->$key = clone $value;
 201              }
 202          }
 203      }
 204  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1