Find if keys are in two dimensional associative array

Michal Minich michal.minich at gmail.com
Sun Jan 17 10:47:15 PST 2010


On Sun, 17 Jan 2010 18:02:00 +0100, Simen kjaeraas wrote:

> Michal Minich <michal.minich at gmail.com> wrote:
> 
>> Is there more elegant way / built in into D?
> 
> As far as I know, there is no built-in way. A more general solution than
> yours can be created with templates, however:
> 
> template elementTypeOfDepth( T : V[ K ], int depth, V, K ) {
>      static if ( depth == 0 ) {
>          alias V elementTypeOfDepth;
>      } else {
>          alias elementTypeOfDepth!( V, depth -1 )  elementTypeOfDepth;
>      }
> }
> 
> elementTypeOfDepth!( T, U.length )* isIn( T : V[ K ], V, K, U... )( T
> arr, K key, U index ) {
>      auto p = key in arr;
>      static if ( U.length > 0 ) {
>          if ( p ) {
>              return isIn( *p, index );
>          } else {
>              return null;
>          }
>      } else {
>          return p;
>      }
> }
> 
> Usage:
> 
> int[char][int][char] foo;
> 
> if ( isIn( foo, 'a', 3, 'b' ) ) {
>      writeln( "Excelsior!" );
> }

This is great! I was thinking such generic template would be good, but I 
did not have need for it right now.

This should be definitely posted to bugzilla as enhancement for Phobos, 
and also for Tango.

I was thinking this also may be solved by modification in language:

the problem with statement "123 in *('a' in aa)" is that if "('a' in 
aa')" returns null, then next check will fail: (123 in null) -> null 
reference error.

if D simply check if the second part of InExpression is null, and if yes, 
return null, otherwise evaluate as is currently done.


More information about the Digitalmars-d-learn mailing list