Templates and Associative Arrays

dsimcha dsimcha at yahoo.com
Sun Jan 20 21:04:14 PST 2008


Is it possible to create a template function that treats regular dynamic
arrays the same as associative arrays?  For example, I want to create a very
simple template function to extract a column from a 2-d array of T's.  If the
array is non-associative in both dimensions, the function could be trivially
implemented as:

T[] extract_column(T)(T[][] data, size_t column_to_extract)
{
    T[] extracted_column;
    foreach(row; data)
    {
        extracted_column~=row[column_to_extract];
    }
    return extracted_column;
}

However, in the case of an associatve array, I cannot think of any obvious way
to make this work without essentially implementing it four different times,
once each for an associative and non-associative row dimension and likewise
for the column dimension.



More information about the Digitalmars-d mailing list