123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- namespace addons\blog\library;
- class HashMap
- {
-
- protected $hashTable = array();
- public function __construct()
- {
- }
-
- public function put($key, $value)
- {
- if (!array_key_exists($key, $this->hashTable)) {
- $this->hashTable[$key] = $value;
- return null;
- }
- $_temp = $this->hashTable[$key];
- $this->hashTable[$key] = $value;
- return $_temp;
- }
-
- public function get($key)
- {
- if (array_key_exists($key, $this->hashTable)) {
- return $this->hashTable[$key];
- }
- return null;
- }
-
- public function remove($key)
- {
- $temp_table = array();
- if (array_key_exists($key, $this->hashTable)) {
- $tempValue = $this->hashTable[$key];
- while ($curValue = current($this->hashTable)) {
- if (!(key($this->hashTable) == $key)) {
- $temp_table[key($this->hashTable)] = $curValue;
- }
- next($this->hashTable);
- }
- $this->hashTable = null;
- $this->hashTable = $temp_table;
- return $tempValue;
- }
- return null;
- }
-
- public function keys()
- {
- return array_keys($this->hashTable);
- }
-
- public function values()
- {
- return array_values($this->hashTable);
- }
-
- public function putAll($map)
- {
- if (!$map->isEmpty() && $map->size() > 0) {
- $keys = $map->keys();
- foreach ($keys as $key) {
- $this->put($key, $map->get($key));
- }
- }
- return;
- }
-
- public function removeAll()
- {
- $this->hashTable = null;
- return true;
- }
-
- public function containsValue($value)
- {
- while ($curValue = current($this->H_table)) {
- if ($curValue == $value) {
- return true;
- }
- next($this->hashTable);
- }
- return false;
- }
-
- public function containsKey($key)
- {
- if (array_key_exists($key, $this->hashTable)) {
- return true;
- } else {
- return false;
- }
- }
-
- public function size()
- {
- return count($this->hashTable);
- }
-
- public function isEmpty()
- {
- return (count($this->hashTable) == 0);
- }
- }
|