Benoit Mathieu wrote:[color=blue]
>
> Maybe this issue is strongly related to the implementation
> details in g++ (which might be particularly conservative
> about aliasing) and I should ask in another group. But if
> this is a g++ issue and the answer is g++ specific, then I
> will probably not use it...
>
> I would be more interested if somebody knows about a C++
> construct which clearly tells the compiler that the actual
> arrays will never alias these indexes and pointers... Does
> the standard say something about that ?[/color]
What do you want to achieve?
As I see it, you want the compiler to replace the index
calculation in
for (j=0; j<nj; j++, k++)
b(k,0) = a(i,j);
with some sort of pointer magic. The same thing it can
do when you write
for( i = 0; i < size; ++i )
m[i] = ...
where the compiler can replace the whole thing with
basePtr = m;
for( i = 0; i < size; ++i, basePtr++ )
*basePtr = ...
If your compiler is unable to do this transformation in
your current case, then clearly it is a quality of implementation
issue (meaning: g++ specific).
But what can you do?
One thing you can do in any case is to look how others solve this
(or a similar) problem. What immediatly comes to my mind is the
STL. How do they do it? The STL has introduced a concept called
iterators. There are functions for getting the first iterator
and incrementing such an iterator. Well, in the above, transformed
example, basePtr acts as an iterator! It is assigned a starting value
and after that the pointer (aehh: iterator) is incremented and evaluated.
Hmm.
So you could add functions to your class IntTab:
class intTab
{
int* GetFirstColumn( int Row ) { return &data[ Row*dim1 ]; }
int GetNextColumnValue( int*& Pos ) { return *(Pos++); }
...
};
And your looping construct modifies to:
for( i = 0, k = 0; i < n1; ++i ) {
int* PosA = a.GetFirstColumn( i );
for( j = 0; j < nj; j++, k++ )
b(k,0) = a.GetNextColumnValue( PosA );
}
Try it and see, if the compiler is able to optimize this in a better
way.
Of course one could polish up the above by introducing a seperate
iterator class etc., but for a quick test to see if your compiler can
do a better job the above should be sufficient.
Yes I am aware, that this could be seen as premature oprimization
and as we all know this is the root of all evil :-)
--
Karl Heinz Buchegger
kbuchegg@gascad.at