Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HashMap.php 3.5KB

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