Find if keys are in two dimensional associative array

Simen kjaeraas simen.kjaras at gmail.com
Sun Jan 17 09:02:00 PST 2010


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

-- 
Simen


More information about the Digitalmars-d-learn mailing list