views:

338

answers:

3

I'm writing a sparse matrix class in C++ in which every row and column are arrays of linked lists from a class I created (aptly named: LinkedList).

I want to write a class that is a "smart" pointer to one cell in this matrix.

In that class, let say LIPointer, I will implement a ++ operator function for moving in the linked lists of the matrix.

Is there an elegant way of doing this without moving the references of the matrix arrays and sized elements each time I create a linkedlistPointer?

I can't use stl::array etc. because I have to build them myself.

Here are the declarations:


class LinkedItem 
{ 
private:
    int Column, Row;
    double Value;
    LinkedItem* Right;
    LinkedItem* Down;
public:
...
};


class SparseLinkedMatrix
{
private: //members
    int ColSize;
    int RowSize;
    LinkedItem ** Columns;
    LinkedItem ** Rows;
public: //functions
    SparseLinkedMatrix();
...

};


class LIPointer;
private:
    LinkedItem * CellPointer;
public:
    LIPointer();
        void operator++();//???
...
};

Any advice or direction would be appreciated.

Update: It needs to run on the whole matrix. That is why I think I need to move (by reference) the arrays and the size of the matrix. The intended effect is that this would from the last cell in the linked list of the first row to the first cell in the second row.

+1  A: 

For compressed row matrices, I use something like:

    std::vector<std::map<size_t, double> > matrix;

I can then add an entry using:

    matrix[row][col] += val;

For each row, I can then iterate through the column entries in ascending order and read out the value.

Edit: The person posing the question does point out that they cannot use the STL. Perhaps they can use some kind of map versus a linked list. Otherwise I suggest using a vector of linked lists and keep adding entries to the end of each list. Then do a sort of each linked list when adding entries has been completed.

+1  A: 

Can you please elaborate on exactly what you want operator++() to do?

For instance, to have LIPointer's operator++() go to the next right element:

void operator++()
{
    if ( CellPointer != NULL )
        CellPointer = CellPointer->Right;
}

It stops when it gets to the end, though.

csl
A: 

It needs to run on the whole matrix. That is why I think I need to move (by reference) the arrays and the size of the matrix. The intended effect is that this would from the last cell in the linked list of the first row to the first cell in the second row.

Boaz Mohar
Comment directly on the answer you are referring to, or edit your question -- an 'answer' shouldn't be put here unless it's a true answer to the question you posed.
George Stocker