php-ds / ext-ds

An extension providing efficient data structures for PHP 7
https://medium.com/p/9dda7af674cd
MIT License
2.11k stars 95 forks source link

Consider adding Bidirectional Map #103

Closed evpav closed 6 years ago

evpav commented 6 years ago

https://en.wikipedia.org/wiki/Bidirectional_map

rtheunissen commented 6 years ago

You can create one very easily.

class BiMap 
{
    protected $map;

    public function __construct()
    {
        $this->map = new \Ds\Map();
    }

    public function put($key, $value) 
    {
        $this->map->put($key, $value);
        $this->map->put($value, $key);
    }

    public function get($key)
    {
        return $this->map->get($key);
    }
}

Or am I missing something here?

rtheunissen commented 6 years ago

The ideal is to provide fundamental structures that allow for easy composition.

evpav commented 6 years ago

Or am I missing something here?

length, keys, values would be 2x; few corner cases and specifics; room for memory optimization (2 maps/tree).

The ideal is to provide fundamental structures that allow for easy composition.

I'm ok with that, just thought it would be convenient for users to have a good implementation(s) of the bimap here, because I haven't seen one for php yet.

rtheunissen commented 6 years ago

Good points, especially around length being 2x. Could you compose using 2 maps instead and override length? What would length return anyway; max(k.length, v.length)?

I'll take a closer look at BiMaps because I don't know much about them.

rtheunissen commented 6 years ago

I am not convinced of the practical benefits at this stage, might reconsider later on.