Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vecset: Don't use reference in iterator #67

Merged
merged 1 commit into from
Jan 18, 2020
Merged
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
18 changes: 9 additions & 9 deletions src/vecset.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ template<class T> class VecSet{
*
* Pre: 0 <= i <= vec.size()
*/
const_iterator(int i, const std::vector<T> &vec)
const_iterator(int i, const std::vector<T> *vec)
: i(i), vec(vec) {
assert(0 <= i && i <= int(vec.size()));
assert(0 <= i && i <= int(vec->size()));
};
const_iterator(const const_iterator &) = default;
const_iterator &operator=(const const_iterator&) = default;
Expand All @@ -149,11 +149,11 @@ template<class T> class VecSet{
bool operator>=(const const_iterator &it) const { return i >= it.i; };
const_iterator operator++(int) {
const_iterator it = *this;
if(i < int(vec.size())) i++;
if(i < int(vec->size())) i++;
return it;
};
const_iterator &operator++(){
if(i < int(vec.size())) i++;
if(i < int(vec->size())) i++;
return *this;
};
const_iterator operator--(int) {
Expand All @@ -166,17 +166,17 @@ template<class T> class VecSet{
return *this;
};
const T &operator*() const{
return vec[i];
return (*vec)[i];
};
const T *operator->() const{
return &vec[i];
return &(*vec)[i];
};
private:
int i;
const std::vector<T> &vec;
const std::vector<T> *vec;
};
const_iterator begin() const { return const_iterator(0,vec); };
const_iterator end() const { return const_iterator(vec.size(),vec); };
const_iterator begin() const { return const_iterator(0,&vec); };
const_iterator end() const { return const_iterator(vec.size(),&vec); };
const std::vector<T> &get_vector() const { return vec; };
bool operator==(const VecSet<T> &s) const { return vec == s.vec; };
bool operator<(const VecSet<T> &s) const { return vec < s.vec; };
Expand Down