PHP实现哈希表_PHP教程

php中文网
发布: 2016-07-13 09:51:46
原创
1186人浏览过

PHP实现哈希表

//一个简单的哈希表实现。。。。


LuckyCola工具库
LuckyCola工具库

LuckyCola工具库是您工作学习的智能助手,提供一系列AI驱动的工具,旨在为您的生活带来便利与高效。

LuckyCola工具库 133
查看详情 LuckyCola工具库

<?php

class hashTable
{
    private $collection;
    private $size = 100;

    //初始化哈希表的大小
    public function __construct($size='')
    {
        $bucketsSize = is_int($size)?$size:$this->size;
        $this->collection = new SplFixedArray($bucketsSize);
    }

    //生成散列值,作为存储数据的位置
    private function _hashAlgorithm($key)
    {
        $length = strlen($key);
        $hashValue = 0;
        for($i=0; $i<$length; $i++) {
            $hashValue += ord($key[$i]);
        }
        return ($hashValue%($this->size));
    }

    //在相应的位置存储对应的值
    public function set($key, $val)
    {
        $index = $this->_hashAlgorithm($key);
        $this->collection[$index] = $val;
    }

    //根据键生成散列值,进而找到对应的值
    public function get($key)
    {
        $index = $this->_hashAlgorithm($key);
        return $this->collection[$index];
    }

    //删除某个值,成功返回1,失败返回0
    public function del($key)
    {
        $index = $this->_hashAlgorithm($key);
        if(isset($this->collection[$index])) {
            unset($this->collection[$index]);
            return 1;
        } else {
            return 0;
        }
    }

    //判断某个值是否存在,存在返回1, 不存在返回0
    public function exist($key)
    {
        $index = $this->_hashAlgorithm($key);
        if($this->collection[$index]){
            return 1;
        } else {
            return 0;
        }
    }

    //返回key的个数
    public function size()
    {
        $size = 0;
        $length = count($this->collection);
        for($i=0; $i<$length; $i++) {
            if($this->collection[$i]) {
                $size++;
            }
        }
        return $size;
    }

    //返回value的序列
    public function val()
    {
        $size = 0;
        $length = count($this->collection);
        for($i=0; $i<$length; $i++) {
            if($this->collection[$i]) {
                echo $this->collection[$i]."<br />";
            }
        }
    }

    //排序输出
    public function sort($type=1)
    {
        $length = count($this->collection);
        $temp = array();
        for($i=0; $i<$length; $i++) {
            if($this->collection[$i]) {
                $temp[] = $this->collection[$i];
            }
        }

        switch ($type) {
            case 1:
                //正常比较
                sort($temp, SORT_REGULAR);
                break;
            case 2:
                //按照数字比较
                sort($temp, SORT_NUMERIC);
                break;
            //按照字符串进行比较
            case 3:
                sort($temp, SORT_STRING);
                break;
            //根据本地字符编码环境进行比较
            case 4:
                sort($temp, SORT_LOCALE_STRING);
                break;

        }
        echo "<pre class="code">";
        print_r($temp);
    }

    //逆序输出
    public function rev($type=1)
    {
        $length = count($this->collection);
        $temp = array();
        for($i=0; $i<$length; $i++) {
            if($this->collection[$i]) {
                $temp[] = $this->collection[$i];
            }
        }

        switch ($type) {
            case 1:
                //正常比较
                rsort($temp, SORT_REGULAR);
                break;
            case 2:
                //按照数字比较
                rsort($temp, SORT_NUMERIC);
                break;
            //按照字符串进行比较
            case 3:
                rsort($temp, SORT_STRING);
                break;
            //根据本地字符编码环境进行比较
            case 4:
                rsort($temp, SORT_LOCALE_STRING);
                break;

        }
        echo "<pre class="code">";
        print_r($temp);
    }


}

//简单的测试
$list = new hashTable(200);
$list->set("zero", "zero compare");
$list->set("one", "first test");
$list->set("two", "second test");
$list->set("three", "three test");
$list->set("four", "fouth test");
echo $list->val();
echo "after sorted : <br />";
$list->rev(3);
登录后复制


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1011356.htmlTechArticlePHP实现哈希表 //一个简单的哈希表实现。。。。 size; $this->collection = new SplFixedArray($bucketsSize); } //生成散列值,作为存储数据的位置 private...
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号