PHP 双向 map

标签 php dictionary bidirectional

我正在将一段使用大量双向映射的 Java 代码移植到 PHP ( Guava's BiMap )。类似 Java 的 map 由 PHP 数组或 SplObjectStorage 提供。 ,但是有 PHP 双向 map 库可用吗?

最佳答案

此类应满足双向 map 的大多数需求:

class BiMap
{

    private $KtoV, $VtoK;

    public function __constructor()
    {
        $this->KtoV = []; // for version < 5.4.0, syntax must be: $this->KtoV = array();
        $this->VtoK = [];
    }

    public function getKey($v)
    {
        if($this->hasValue($v))
        {
            return $this->VtoK[$v];
        }
        else
        {
            return null;
        }
    }

    public function getAllKeys()
    {
        if($this->KtoV)
        {
            return array_keys($this->KtoV);
        }
        else
        {
            return $this->KtoV;
        }
    }

    public function getValue($k)
    {
        if($this->hasKey($k))
        {
            return $this->KtoV[$k];
        }
        else
        {
            return null;
        }
    }

    public function getAllValues()
    {
        if($this->VtoK)
        {
            return array_keys($this->VtoK);
        }
        else
        {
            return $this->VtoK;
        }
    }

    public function hasKey($k)
    {
        return isset($this->KtoV[$k]);
    }

    public function hasValue($v)
    {
        return isset($this->VtoK[$v]);
    }

    public function put($k, $v)
    {
        if($this->hasKey($k))
        {
            $this->removeKey($k);
        }
        if($this->hasValue($v))
        {
            $this->removeValue($v);
        }
        $this->KtoV[$k] = $v;
        $this->VtoK[$v] = $k;
    }

    public function putAll($array)
    {
        foreach($array as $k => $v)
        {
            $this->put($k, $v);
        }
    }

    public function removeKey($k)
    {
        if($this->hasKey($k))
        {
            unset($this->VtoK[$this->KtoV[$k]]);
            $v = $this->KtoV[$k];
            unset($this->KtoV[$k]);
            return $v;
        }
        else
        {
            return null;
        }
    }

    public function removeValue($v)
    {
        if($this->hasValue($v))
        {
            unset($this->KtoV[$this->VtoK[$v]]);
            $k = $this->VtoK[$v];
            unset($this->VtoK[$v]);
            return $k;
        }
        else
        {
            return null;
        }
    }

}

但是,如果您需要对键/值和/或对象/数组检查进行 null 检查,则应在函数体内给出类似于以下代码行的处理,并在 hasKey($ k)hasValue($v)put($k, $v) 方法:

    if($item === null)
    {
        throw new Exception('null as BiMap key / value is invalid.');
    }
    if(is_object($item) || is_array($item))
    {
        throw new Exception('Object / Array as BiMap key / value is invalid.');
    }

关于PHP 双向 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15794858/

相关文章:

python-3.x - python 字典获取键

html - 如何修复带数字的双向文本?

java - hibernate 一对一双向不工作

php - 这是域对象类吗?

php - 找到最小值并输出用户名

phpMyAdmin 无法在 MAMP 中工作

php - 使用在同一个 ini 文件中定义的 ini 值

objective-c - 与数十万个单词进行比较时,查看单词是否存在的有效方法是什么?

google-app-engine - 无法将 []datastore.PropertyList 传递给 GetMulti 函数(数据存储 : src has invalid type)

sublimetext3 - 用双向文本解决 Sublime Text 中的波斯字符