HashMap.php 3.5 KB

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