Pointer class support

I am writing a sparse matrix class in C ++ where each row and column is an array 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 this class, say, LIPointer

I will implement an operator function ++

to traverse the linked lists of a matrix.

Is there an elegant way to do this without traversing the references of the matrix arrays and size elements every time I create linkedlistPointer

?

I cannot use stl::array

etc. because I have to create them myself.

Here are the announcements:


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 guidance would be appreciated.

Refresh . It should work on the whole matrix. This is why I think I need to move (by reference) the arrays and the size of the matrix. The intended effect is that this will be from the last cell in the linked list of the first row to the first cell in the second row.

+1


source to share


2 answers


For compressed row matrices, I use something like:

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

Then I can add an entry using:



    matrix [row] [col] + = val;

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

Edit: the person asking the question points out that they cannot use STL. Maybe they can use some kind of card compared to the linked list. Otherwise, I suggest using a vector of linked lists and keep adding entries to the end of each list. Then sort each linked list as you add records.

+1


source


Could you tell us exactly what the ++ () operator needs?

For example, to make the LIPointer ++ () operator go to the next right element:



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

      

It stops when it comes to the end.

+1


source







All Articles