January 21, 2008 Templates and Associative Arrays | ||||
|---|---|---|---|---|
| ||||
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.
| ||||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply