Column-wise data storage for matrices in C, any advantage(s)?

Chin Fang fangchin at portia.Stanford.EDU
Sun Feb 3 14:42:21 AEST 1991


In article <15054 at smoke.brl.mil> gwyn at smoke.brl.mil (Doug Gwyn) writes:
>In article <1991Feb2.071930.9879 at portia.Stanford.EDU> fangchin at portia.Stanford.EDU (Chin Fang) writes:
>>"seemly" trivial column element access in fact involves a long ptr jump
>>equal the length of entire row.
>
>If you are concerned about such things, you should traverse the elements
>in an order more suited to the implementation language.  Many experienced
>C programmers would in fact convert some array operations to use one-
>dimensional traversals with access via an appropriately incremented
>pointer, rather than using [] operators.

Doug, you might think my question is trival and I didn't do my homework.
I typed in the following code segment (errors may exist however) to show 
that I am aware what you are talking about, if not in depth.  My question is not
related the capability of C whatsoever.  Maybe I didn't make my wording 
right in my 1st question.  The thing is, sometimes we have our preferences
(could be unreasonable ones too, like some people insist ident N blanks).
 
I happen to be uneasy with switching indices, and have been trying to use 
the following scheme to rid of my own uneasiness.  (sort of like Numerical
Recipes authors like to use non-zero offsets).  From my own point of view,
the scheme below works but probably not efficient.  That's why I posted my
1st question to see if there are better solutions.  Perhaps I should have 
included the attachment below the first time.

Well, if you can forgive my idiosyncrasy..
 
Yeah, you don't like N.R.'s way of indexing, I guess you will blame me 
for the same reason. Sigh....

Best Regards,
 
Chin Fang
Mechanical Engineering Department
Stanford University
fangchin at portia.stanford.edu

**************** How I hide interchanged row, cols **************

#typedef struct {int nrl, nrh, ncl, nch;
                 DATA **m} * Matrix;

Matrix Matrix_alloc(int nrl, int nrh, int ncl, int nch)
{   
    /* routine modified from Numerical Recipes utility matrix() */

    int i;
    Matrix m;
     
    if(nrl > nrh || ncl > nch) errormsg("bounds are wrong in allocation!");
    
    /* no direct malloc() here, get memory from a free block cache using */
    /* routine memory_alloc()                                            */
   
    m=(DATA **)memory_alloc((nch-ncl+1)*sizeof(DATA *));
    assert(m);
    m-=ncl;

    for(i=ncl;i<=nch;i++){
	m[i]=(DATA *)memory_alloc((nrh-nrl+1)*sizeof(DATA));
	assert(m[i]);
	m[i]-=nrl;
    }
    return m;
}

#ifdef MATRIX_DEBUG

DATA bound_checked_access(Matrix m,int row, int col)
{
    int WRONG=FALSE;
    
    if(row <m->ncl||row >m->nch||col <m->nrl||col >m->nrh) WRONG = TRUE;
    if(WRONG) errormsg("input bounds wrong in matrix access!");
    return m->DATA[col][row]; /* we switch indices here so user won't know */
}

#endif

#ifdef MATRIX_DEBUG

    DATA bound_checked_access(Matrix, int, int);
    #define M(m,row,col) bound_checked_access(m,row,col)

#else

    #define M(m,row,col)   m->DATA[col-m->nrl][row-m->ncl]

#endif



More information about the Comp.lang.c mailing list