Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet;
   4  
   5  class HashTable
   6  {
   7      /**
   8       * HashTable elements.
   9       *
  10       * @var IComparable[]
  11       */
  12      protected $items = [];
  13  
  14      /**
  15       * HashTable key map.
  16       *
  17       * @var string[]
  18       */
  19      protected $keyMap = [];
  20  
  21      /**
  22       * Create a new \PhpOffice\PhpSpreadsheet\HashTable.
  23       *
  24       * @param IComparable[] $pSource Optional source array to create HashTable from
  25       */
  26      public function __construct($pSource = null)
  27      {
  28          if ($pSource !== null) {
  29              // Create HashTable
  30              $this->addFromSource($pSource);
  31          }
  32      }
  33  
  34      /**
  35       * Add HashTable items from source.
  36       *
  37       * @param IComparable[] $pSource Source array to create HashTable from
  38       */
  39      public function addFromSource(?array $pSource = null): void
  40      {
  41          // Check if an array was passed
  42          if ($pSource == null) {
  43              return;
  44          }
  45  
  46          foreach ($pSource as $item) {
  47              $this->add($item);
  48          }
  49      }
  50  
  51      /**
  52       * Add HashTable item.
  53       *
  54       * @param IComparable $pSource Item to add
  55       */
  56      public function add(IComparable $pSource): void
  57      {
  58          $hash = $pSource->getHashCode();
  59          if (!isset($this->items[$hash])) {
  60              $this->items[$hash] = $pSource;
  61              $this->keyMap[count($this->items) - 1] = $hash;
  62          }
  63      }
  64  
  65      /**
  66       * Remove HashTable item.
  67       *
  68       * @param IComparable $pSource Item to remove
  69       */
  70      public function remove(IComparable $pSource): void
  71      {
  72          $hash = $pSource->getHashCode();
  73          if (isset($this->items[$hash])) {
  74              unset($this->items[$hash]);
  75  
  76              $deleteKey = -1;
  77              foreach ($this->keyMap as $key => $value) {
  78                  if ($deleteKey >= 0) {
  79                      $this->keyMap[$key - 1] = $value;
  80                  }
  81  
  82                  if ($value == $hash) {
  83                      $deleteKey = $key;
  84                  }
  85              }
  86              unset($this->keyMap[count($this->keyMap) - 1]);
  87          }
  88      }
  89  
  90      /**
  91       * Clear HashTable.
  92       */
  93      public function clear(): void
  94      {
  95          $this->items = [];
  96          $this->keyMap = [];
  97      }
  98  
  99      /**
 100       * Count.
 101       *
 102       * @return int
 103       */
 104      public function count()
 105      {
 106          return count($this->items);
 107      }
 108  
 109      /**
 110       * Get index for hash code.
 111       *
 112       * @param string $pHashCode
 113       *
 114       * @return int Index
 115       */
 116      public function getIndexForHashCode($pHashCode)
 117      {
 118          return array_search($pHashCode, $this->keyMap);
 119      }
 120  
 121      /**
 122       * Get by index.
 123       *
 124       * @param int $pIndex
 125       *
 126       * @return IComparable
 127       */
 128      public function getByIndex($pIndex)
 129      {
 130          if (isset($this->keyMap[$pIndex])) {
 131              return $this->getByHashCode($this->keyMap[$pIndex]);
 132          }
 133  
 134          return null;
 135      }
 136  
 137      /**
 138       * Get by hashcode.
 139       *
 140       * @param string $pHashCode
 141       *
 142       * @return IComparable
 143       */
 144      public function getByHashCode($pHashCode)
 145      {
 146          if (isset($this->items[$pHashCode])) {
 147              return $this->items[$pHashCode];
 148          }
 149  
 150          return null;
 151      }
 152  
 153      /**
 154       * HashTable to array.
 155       *
 156       * @return IComparable[]
 157       */
 158      public function toArray()
 159      {
 160          return $this->items;
 161      }
 162  
 163      /**
 164       * Implement PHP __clone to create a deep clone, not just a shallow copy.
 165       */
 166      public function __clone()
 167      {
 168          $vars = get_object_vars($this);
 169          foreach ($vars as $key => $value) {
 170              if (is_object($value)) {
 171                  $this->$key = clone $value;
 172              }
 173          }
 174      }
 175  }