Optimize my code =)

bearophile bearophileHUGS at lycos.com
Fri Feb 14 14:32:45 PST 2014


Nick Sabalausky:

>> T opIndex(size_t row, size_t col) const {
>>     immutable size_t i = this.dim.offset(row, col);
>>     if (i >= this.dim.size) {
>>         // TODO - have to learn exception handling in D first. 
>> :P
>>     }
>>     return this.data[i];
>> }
>
> No need for the bounds check. D already does bounds checks 
> automatically

But the row-column bounds of the matrix are not the same as the 
1D bounds of the 1D array. You can have out-of-column-bounds and 
still be inside the 1D bounds. So that test should go in the 
precondition and it should test row and col separately:

T opIndex(in size_t row, in size_t col) const pure nothrow
in {
     assert(row < nRows);
     assert(col < nCols);
} body {
     return data[dim.offset(row, col)];
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list