bidirectional map

2013-11-11 Thread bioinfornatics

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 );
}
}


Re: bidirectional map

2013-11-12 Thread Andrea Fontana
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?








Re: bidirectional map

2013-11-12 Thread bioinfornatics
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


Re: bidirectional map

2013-11-12 Thread Ellery Newcomer

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.