bidirectional map

Andrea Fontana nospam at example.com
Tue Nov 12 01:10:05 PST 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?








More information about the Digitalmars-d-learn mailing list