Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions soa-iterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,44 @@ using std::endl;
using std::vector;
using std::for_each;

struct element {
//TODO: add all the relevant constructors.
double& x() { return points_.get_x()[index_]; }
double& y() { return points_.get_y()[index_]; }
Points* points_ = nullptr;
//TODO: perhaps make this private and give iterator friendship relations
std::size_t index_ = 0;
}; //end struct element
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, I think it makes more sense to do this.


class Points {
typedef std::vector< double> Vector;
public:

typedef vector<double>::size_type size_type;
typedef typename Vector::size_type size_type;

class iterator {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ryan, do you think the iterator class should be defined outside of the container class?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I do.

public:
typedef iterator self_type;
typedef iterator value_type;
typedef iterator& reference;
typedef iterator* pointer;
//This can be a bit messy, strongly suggest using the boost::iterator_facade interface
//operator* returning the iterator is akward imho. Its not natural to write ********x in code and
//have it do what your code did..
typedef element value_type;
typedef element& reference;
typedef element* pointer;

//typedef std::forward_iterator_tag iterator_category;
typedef vector<double>::difference_type difference_type;
iterator(Points& points, Points::size_type index) :
points_(points), index_(index) { }
self_type operator++() { self_type i = *this; index_++; return i; }
self_type operator++(int junk) { index_++; return *this; }
iterator& operator*() { return *this; }
iterator* operator->() { return this; }
bool operator==(const self_type& rhs) { return index_ == rhs.index_; }
bool operator!=(const self_type& rhs) { return index_ != rhs.index_; }
double& x() { return points_.get_x()[index_]; }
double& y() { return points_.get_y()[index_]; }
iterator(Points& points, Points::size_type index) : elt_(points, index){ }
//good
value_type operator++() { self_type i = *this; index_++; return i; }
reference operator++(int junk) { index_++; return *this; }
//operator--?
iterator& operator*() { return elt; }
//This should not be necessary, the language implements this for you..
//iterator* operator->() { return this; }
bool operator==(const self_type& rhs) { return elt == rhs.elt; }
bool operator!=(const self_type& rhs) { return elt != rhs.elt; }
private:
Points& points_;
Points::size_type index_;
element elt;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea here also! thanks

};

size_type size() const { return x.size(); }
Expand Down