Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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

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