Thread overview
bidirectional map
Nov 12, 2013
bioinfornatics
Nov 12, 2013
Andrea Fontana
Nov 12, 2013
bioinfornatics
Nov 13, 2013
Ellery Newcomer
November 12, 2013
Dear,
I am looking for a bidirectional map i.e http://en.wikipedia.org/wiki/Bidirectional_map

My seach into D documentation seem to said to me that this structure is not implemented.

Something like (for primary idea):


struct BidirectionalMap(T,U){
    private:
        T[U] _forwardHash;
        U[T] _reverseHash;

    public:

        @safe
        void opIndeyAssign( T k, U v){ // pure?
            _forwardHash[k] = v;
            _reverseHash[v] = k;
        }


        @property
        BidirectionalMap!(T,U) dup(){
            return BidirectionalMap!(T,U)( this );
        }
}
November 12, 2013
On Tuesday, 12 November 2013 at 01:14:47 UTC, bioinfornatics wrote:
> Dear,
> I am looking for a bidirectional map i.e http://en.wikipedia.org/wiki/Bidirectional_map
>
> My seach into D documentation seem to said to me that this structure is not implemented.
>
> Something like (for primary idea):
>
>
> struct BidirectionalMap(T,U){
>     private:
>         T[U] _forwardHash;
>         U[T] _reverseHash;
>
>     public:
>
>         @safe
>         void opIndeyAssign( T k, U v){ // pure?
>             _forwardHash[k] = v;
>             _reverseHash[v] = k;
>         }
>
>
>         @property
>         BidirectionalMap!(T,U) dup(){
>             return BidirectionalMap!(T,U)( this );
>         }
> }

Maybe we can get a better (faster/lighter) implementation if T==U (using sorted (T,T) tuples?)

By the way:

map[10] = 5; //   =>  _forwardHash[10] = 5; _reverseHash[5] = 10;
map[10] = 3; //   =>  _forwardHash[10] = 3; _reverseHash[3] = 10;

So:
_reverseHash[5] == 10; // True, it should not, should it?






November 12, 2013
On Tuesday, 12 November 2013 at 09:10:07 UTC, Andrea Fontana wrote:
> On Tuesday, 12 November 2013 at 01:14:47 UTC, bioinfornatics wrote:
>> Dear,
>> I am looking for a bidirectional map i.e http://en.wikipedia.org/wiki/Bidirectional_map
>>
>> My seach into D documentation seem to said to me that this structure is not implemented.
>>
>> Something like (for primary idea):
>>
>>
>> struct BidirectionalMap(T,U){
>>    private:
>>        T[U] _forwardHash;
>>        U[T] _reverseHash;
>>
>>    public:
>>
>>        @safe
>>        void opIndeyAssign( T k, U v){ // pure?
>>            _forwardHash[k] = v;
>>            _reverseHash[v] = k;
>>        }
>>
>>
>>        @property
>>        BidirectionalMap!(T,U) dup(){
>>            return BidirectionalMap!(T,U)( this );
>>        }
>> }
>
> Maybe we can get a better (faster/lighter) implementation if T==U (using sorted (T,T) tuples?)
>
> By the way:
>
> map[10] = 5; //   =>  _forwardHash[10] = 5; _reverseHash[5] = 10;
> map[10] = 3; //   =>  _forwardHash[10] = 3; _reverseHash[3] = 10;
>
> So:
> _reverseHash[5] == 10; // True, it should not, should it?

Maybe using Variant will allow to use 1 associative array
November 13, 2013
On 11/11/2013 05:14 PM, bioinfornatics wrote:
> Dear,
> I am looking for a bidirectional map i.e
> http://en.wikipedia.org/wiki/Bidirectional_map
>
> My seach into D documentation seem to said to me that this structure is
> not implemented.
>
> Something like (for primary idea):
>
>
> struct BidirectionalMap(T,U){
>      private:
>          T[U] _forwardHash;
>          U[T] _reverseHash;
>
>      public:
>
>          @safe
>          void opIndeyAssign( T k, U v){ // pure?
>              _forwardHash[k] = v;
>              _reverseHash[v] = k;
>          }
>
>
>          @property
>          BidirectionalMap!(T,U) dup(){
>              return BidirectionalMap!(T,U)( this );
>          }
> }

you could build one using multi_index.

https://bitbucket.org/ariovistus/multi_index

at least, that is how boost::bimap was done. I always assumed it would be trivial in D, but I haven't tried doing it. Something like

alias MultiIndexContainer!(Tuple!(T,"t",U,"u"), IndexedBy!(HashedUnique!("a.t"), HashedUnique!("a.u"))) BiMap;

and then wrap as you see fit.