c++ - Designing iterators for a Matrix class -


i creating matrix<t> class. while implementing iterators stumbled upon design conundrum.

internally, matrix holds data in std::vector<t> (row-major).
1 way of iterating through matrix double iterators (nested) (specified row_double template parameter):

for (auto it_i = mat.begin<row_double>(); it_i < mat.end<row_double>(); ++it_i) {   (auto it_j = it_i->begin(); it_j < it_i->end(); ++it_j) {     cout << *it_j << " ";   }   cout << endl;     } 

the first iterator matrix<t>::iterator<row_double>:

  • iterates through rows of matrix
  • has rowproxy member
  • dereferencing returns rowproxy.

rowproxy returns std::vector<t>::iterator iterators via methods begin() , end().

my idea rowproxy know beginning of line , size of line (number of matrix columns).

the problem how rowproxy holds beginning of line reference:

  • my first approach make beginning of line std::vector<t>::iterator.
    problem in visual studio iterator aware of vector , there debug checks iterator arithmetics. throws error when constructing reverseend iterator (for line before first line): beginning of line num_columns before vector start. please note has nothing dereferencing (whitch ub). can't create iterator..

  • my second approach make beginning of line raw pointer t *
    problem here lineproxy needs return std::vector<t>::iterator (via it's own begin etc.) , cannot (don't know how to) construct std::vector<t>::iterator t * in standard way. (didn't find reference specific std::vector<t>::iterator, iterator concepts. in visual studio there seems constructor (t *, std::vector<t> *), in gcc (t *), neither 1 works in other compiler).

the solution see make own iterator identical std::vector<t>::iterator isn't bound vector , can constructed t * , make rowproxy return that. seems reinventing wheel.

since part of library (and code resides in headers), compiler options out of question (including macros control compiler options because modify whole behaviour of program includes header, not behaviour of library code). solution must conforming standard. language c++11.

the simplest way stated above use eigen, nice package. next simplest thing store size information in class have nice way of getting specific element out of matrix. write (i,j) operator returns vector[i + j*rowlength]. iterators should work looping on whole vector in 1 loop, not sure how sense makes loop on in two.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -