www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - bidirectional map

reply "bioinfornatics" <bioinfornatics fedoraproject.org> writes:
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 );
         }
}
Nov 11 2013
next sibling parent reply "Andrea Fontana" <nospam example.com> writes:
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?
Nov 12 2013
parent "bioinfornatics" <bioinfornatics fedoraproject.org> writes:
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
Nov 12 2013
prev sibling parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
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.
Nov 12 2013