digitalmars.D.learn - bidirectional map
- bioinfornatics (21/21) Nov 11 2013 Dear,
- Andrea Fontana (9/30) Nov 12 2013 Maybe we can get a better (faster/lighter) implementation if T==U
- bioinfornatics (3/43) Nov 12 2013 Maybe using Variant will allow to use 1 associative array
- Ellery Newcomer (8/29) Nov 12 2013 you could build one using multi_index.
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
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
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:Maybe using Variant will allow to use 1 associative arrayDear, 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
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