/ hashmap

PHP实现HashMap类

貌似是小日本的源代码,找不到源地址了……

class HashTable { var $H_table; function __construct() { $this->H_table = array (); } function put($key, $value) { if (!array_key_exists($key, $this->H_table)) { $this->H_table[$key] = $value; return null; } else { $tempValue = $this->H_table[$key]; $this->H_table[$key] = $value; return $tempValue; } } function clear() { $this->H_table = null; $this->H_table = array (); } function containsValue($value) { while ($curValue = current($this->H_table)) { if ($curValue == $value) { return true; } next($this->H_table); } return false; } function containsKey($key) { if (array_key_exists($key, $this->H_table)) { return true; } else { return false; } } function get($key) { if (array_key_exists($key, $this->H_table)) return $this->H_table[$key]; else return null; } function isEmpty() { return (count($this->H_table) == 0); } function size() { return count($this->H_table); } function remove($key) { $temp_table = array (); if (array_key_exists($key, $this->H_table)) { $tempValue = $this->H_table[$key]; while ($curValue = current($this->H_table)) { if (!(key($this->H_table) == $key)) $temp_table[key($this->H_table)] = $curValue; next($this->H_table); } $this->H_table = null; $this->H_table = $temp_table; return $tempValue; } else return null; } function toString() { print_r($this->H_table); } }

PHP实现HashMap类
Share this