<?php

/*
+------------------------------------------------------------------------------
| datetime 2013-10-29 12:46:44
+------------------------------------------------------------------------------
| author baijm
+------------------------------------------------------------------------------
*/ /**
* hash节点的数据结构,用类模拟一个数据链表
*/
class HashNode { public $key;
public $value;
public $nextNode; public function __construct($key, $value, $nextNode = NULL) {
$this->key = $key;
$this->value = $value;
$this->nextNode = $nextNode;
} } class HashTable { private $buckets; //用于存储数据的数组
private $size = 10; //数组的大小 public function __construct() {
//$this->buckets=new SplFixedArray($this->size);
$this->buckets = array();
} /**
* 计算key的hash值,使用最简单的hash算法
* @param type $key
* @return type
*/
private function hashFunc($key) {
for ($i = 0, $len = strlen($key); $i < $len; $i++) {
$hashVal+=ord($key{$i});
}
return $hashVal % $this->size;
} public function insert($key, $value) {
$index = $this->hashFunc($key);
if (isset($this->buckets[$index])) {
$nextNode = new HashNode($key, $value, $this->buckets[$index]);
} else {
$nextNode = new HashNode($key, $value);
}
$this->buckets[$index] = $nextNode;
} public function find($key) {
$index = $this->hashFunc($key);
$current = $this->buckets[$index];
while(isset($current)){
if($current->key==$key){
break;
}else{
$current=$current->nextNode;
}
}
print_r($this->buckets);
return $current->value;
} } $hash = new HashTable();
$hash->insert('key1', 'value1');
$hash->insert('key12', 'value12');
$hash->find('key1');
$hash->find('key12');
?>

最新文章

  1. Tesla P4 在深度学习上的性价比辗压目前所有量产的FPGA
  2. Eclipse侧边栏Outline设置字体
  3. cookie和浏览器
  4. MxNet Windows下安装
  5. ASP.NET跨服务器上传文件的相关解决方案
  6. HDU 2136:Computer(树形DP)
  7. Eclipse 环境下安装PhoneGap开发插件
  8. PHP克隆魔术方法
  9. 向上取整Ceil,向下取整Floor,四舍五入Round
  10. 吕鑫VC6.0-VS2015 全套C/C++、MFC新手实战入门教程、Linux视频教程 最好的基础入门教程没有之一
  11. Build to win--来自小黄衫
  12. 【公众号系列】SAP HANA和区块链
  13. fiddler启用过滤规则只显示想要的接口数据
  14. 泛微云桥e-birdge之金蝶云之家集成配置手册
  15. Bugku-CTF之管理员系统+程序员本地网站
  16. 使用 PLSQL 连接 Oracle9i 数据库
  17. SQLSERVER最简单的同名数据库恢复过程.
  18. MySQL--线程池(Thread Pool)
  19. django使用session报错:no such table: django_session
  20. 第十周PSP&amp;进度条

热门文章

  1. 【iOS】环形渐变进度条实现
  2. android fragment 的用法以及与activity的交互和保存数据的方法,包括屏幕切换(转载)!
  3. 【代码笔记】iOS-对iphone手机进行判断的一些函数
  4. 记一次eclipse无法启动的排查过程
  5. 新版微耕软件(N3000)与旧版2000的实体功能区别
  6. 学习 OAuth2.0
  7. android Gui系统之SurfaceFlinger(5)---Vsync(2)
  8. yii2 GridView常见操作
  9. LinkedHashMap及其源码分析
  10. (ios)MPMoviePlayerController首次播放视频的时候,没有控制条