03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4.10. CURSORS AND PROPERTY MAPS 125<br />

The cursor is initialized with one offset. Many cursor classes will keep a reference to the traversed<br />

matrix object but we do not need this here. The cursor can be incremented, compared, and<br />

dereferred. The result of the dereferentiation is a ‘key’. For simplicity we used an int as key<br />

type.<br />

Like the begin and end functions in STL we define:<br />

template <br />

nz cursor nz begin(const Matrix& A)<br />

{<br />

return nz cursor(0);<br />

}<br />

template <br />

nz cursor nz end(const Matrix& A)<br />

{<br />

return nz cursor(A.nnz());<br />

}<br />

the function nz begin that returns a cursor on the first non-zero entry and nz end which gives a<br />

past-the-end cursor to terminate the traversal<br />

A key can be used as argument <strong>for</strong> a property map that we will define now:<br />

template <br />

struct coo col<br />

{<br />

typedef int key type;<br />

coo col(const Matrix& ref) : ref(ref) {}<br />

int operator()(key type k) const { return ref.col index[k]; }<br />

private:<br />

const Matrix& ref;<br />

};<br />

Property maps have typically a reference to the matrix in order to read internal data from it.<br />

They are often declared as friends because they are an important tool to access the object’s<br />

internal data — it might even be the only way to access data as in the Boost Graph Library.<br />

Property maps to read the row index or the value fo the offset key are equivalent and there<strong>for</strong>e<br />

omitted here.<br />

A property map <strong>for</strong> mutable entries is implemented as follows:<br />

template <br />

struct coo value<br />

{<br />

typedef int key type;<br />

typedef typename Matrix::value type value type;<br />

coo value(Matrix& ref) : ref(ref) {}<br />

value type operator()(key type k) const { return ref.data[k]; }<br />

void operator()(key type k, const value type& v) { ref.data[k]= v; }

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!