From 1dbe4a9edd23d7bb85626ace120ca2399bbff643 Mon Sep 17 00:00:00 2001 From: "Ryan H. Lewis" Date: Wed, 6 May 2015 19:14:14 -0700 Subject: [PATCH] comments. --- soa-iterator.cc | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/soa-iterator.cc b/soa-iterator.cc index dc23060..f77eba5 100644 --- a/soa-iterator.cc +++ b/soa-iterator.cc @@ -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 + class Points { + typedef std::vector< double> Vector; public: - typedef vector::size_type size_type; + typedef typename Vector::size_type size_type; class iterator { 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::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; }; size_type size() const { return x.size(); }