HashMap.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * php构建哈希表类.
  4. * User: Lustre
  5. * Date: 17/3/9
  6. * Time: 上午9:10
  7. * Url: https://github.com/FireLustre/php-dfa-sensitive
  8. **/
  9. namespace addons\cms\library;
  10. class HashMap
  11. {
  12. /**
  13. * 哈希表变量
  14. *
  15. * @var array|null
  16. */
  17. protected $hashTable = array();
  18. public function __construct()
  19. {
  20. }
  21. /**
  22. * 向HashMap中添加一个键值对
  23. *
  24. * @param $key
  25. * @param $value
  26. * @return mixed|null
  27. */
  28. public function put($key, $value)
  29. {
  30. if (!array_key_exists($key, $this->hashTable)) {
  31. $this->hashTable[$key] = $value;
  32. return null;
  33. }
  34. $_temp = $this->hashTable[$key];
  35. $this->hashTable[$key] = $value;
  36. return $_temp;
  37. }
  38. /**
  39. * 根据key获取对应的value
  40. *
  41. * @param $key
  42. * @return mixed|null
  43. */
  44. public function get($key)
  45. {
  46. if (array_key_exists($key, $this->hashTable)) {
  47. return $this->hashTable[$key];
  48. }
  49. return null;
  50. }
  51. /**
  52. * 删除指定key的键值对
  53. *
  54. * @param $key
  55. * @return mixed|null
  56. */
  57. public function remove($key)
  58. {
  59. $temp_table = array();
  60. if (array_key_exists($key, $this->hashTable)) {
  61. $tempValue = $this->hashTable[$key];
  62. while ($curValue = current($this->hashTable)) {
  63. if (!(key($this->hashTable) == $key)) {
  64. $temp_table[key($this->hashTable)] = $curValue;
  65. }
  66. next($this->hashTable);
  67. }
  68. $this->hashTable = null;
  69. $this->hashTable = $temp_table;
  70. return $tempValue;
  71. }
  72. return null;
  73. }
  74. /**
  75. * 获取HashMap的所有键值
  76. *
  77. * @return array
  78. */
  79. public function keys()
  80. {
  81. return array_keys($this->hashTable);
  82. }
  83. /**
  84. * 获取HashMap的所有value值
  85. *
  86. * @return array
  87. */
  88. public function values()
  89. {
  90. return array_values($this->hashTable);
  91. }
  92. /**
  93. * 将一个HashMap的值全部put到当前HashMap中
  94. *
  95. * @param $map
  96. */
  97. public function putAll($map)
  98. {
  99. if (!$map->isEmpty() && $map->size() > 0) {
  100. $keys = $map->keys();
  101. foreach ($keys as $key) {
  102. $this->put($key, $map->get($key));
  103. }
  104. }
  105. return;
  106. }
  107. /**
  108. * 移除HashMap中所有元素
  109. *
  110. * @return bool
  111. */
  112. public function removeAll()
  113. {
  114. $this->hashTable = null;
  115. return true;
  116. }
  117. /**
  118. * 判断HashMap中是否包含指定的值
  119. *
  120. * @param $value
  121. * @return bool
  122. */
  123. public function containsValue($value)
  124. {
  125. while ($curValue = current($this->H_table)) {
  126. if ($curValue == $value) {
  127. return true;
  128. }
  129. next($this->hashTable);
  130. }
  131. return false;
  132. }
  133. /**
  134. * 判断HashMap中是否包含指定的键key
  135. *
  136. * @param $key
  137. * @return bool
  138. */
  139. public function containsKey($key)
  140. {
  141. if (array_key_exists($key, $this->hashTable)) {
  142. return true;
  143. } else {
  144. return false;
  145. }
  146. }
  147. /**
  148. * 获取HashMap中元素个数
  149. *
  150. * @return int
  151. */
  152. public function size()
  153. {
  154. return count($this->hashTable);
  155. }
  156. /**
  157. * 判断HashMap是否为空
  158. *
  159. * @return bool
  160. */
  161. public function isEmpty()
  162. {
  163. return (count($this->hashTable) == 0);
  164. }
  165. }