Skip to content

Commit

Permalink
Added sentinels to vector views iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
hosseinmoein committed Apr 29, 2024
1 parent f0c6c66 commit 0757b2e
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 74 deletions.
3 changes: 2 additions & 1 deletion include/DataFrame/Vectors/HeteroVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ struct HeteroVector {
[[nodiscard]] std::vector<T, typename allocator_declare<T, A>::type> &
get_vector();
template<typename T>
[[nodiscard]] const std::vector<T, typename allocator_declare<T, A>::type> &
[[nodiscard]]
const std::vector<T, typename allocator_declare<T, A>::type> &
get_vector() const;

// It returns a view of the underlying vector.
Expand Down
127 changes: 88 additions & 39 deletions include/DataFrame/Vectors/VectorPtrView.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace hmdf
{

struct VPVSentinel {

VPVSentinel() = default;
VPVSentinel(const VPVSentinel &) = default;
VPVSentinel(VPVSentinel &&) = default;
VPVSentinel &operator = (const VPVSentinel &) = default;
VPVSentinel &operator = (VPVSentinel &&) = default;
~VPVSentinel() = default;
};

// ----------------------------------------------------------------------------

template<typename T, std::size_t A = 0>
class VectorPtrView {

Expand Down Expand Up @@ -213,18 +225,16 @@ class VectorPtrView {

public:

// NOTE: The constructor with no argument initializes
// the iterator to be the "end" iterator
//
inline iterator () = default;
inline iterator (const iterator &) = default;
inline iterator (iterator &&) = default;
inline iterator &operator = (const iterator &) = default;
inline iterator &operator = (iterator &&) = default;

inline iterator (iter_type node) noexcept : node_ (node) { }
inline iterator (pointer *node) noexcept : node_ (node) { }

iterator () = default;
~iterator () = default;
iterator (const iterator &) = default;
iterator (iterator &&) = default;
iterator &operator = (const iterator &) = default;
iterator &operator = (iterator &&) = default;

inline iterator &operator = (iter_type rhs) noexcept {

node_ = rhs;
Expand Down Expand Up @@ -261,12 +271,24 @@ class VectorPtrView {
return (node_ <= rhs.node_);
}

friend bool
operator == (const iterator &rhs, VPVSentinel) noexcept {

return (rhs.node_ == rhs.vector_.end());
}
friend bool
operator != (const iterator &rhs, VPVSentinel) noexcept {

return (rhs.node_ != rhs.vector_.end());
}

// Following STL style, this iterator appears as a pointer
// to value_type.
//
inline pointer operator -> () noexcept { return (*node_); }
inline reference operator * () noexcept { return (**node_); }
inline const_pointer operator -> () const noexcept { return (*node_); }
inline const_pointer
operator -> () const noexcept { return (*node_); }
inline const_reference
operator * () const noexcept { return (**node_); }
inline operator pointer() noexcept { return (*node_); }
Expand Down Expand Up @@ -313,15 +335,15 @@ class VectorPtrView {
}

template<typename I>
inline iterator operator + (I step) noexcept {
inline iterator operator + (I step) const noexcept {

auto ret_node = node_;

ret_node += static_cast<difference_type>(step);
return (iterator { ret_node });
}
template<>
inline iterator operator + (const iterator &rhs) noexcept {
inline iterator operator + (const iterator &rhs) const noexcept {

return (iterator (node_ + rhs.node_));
}
Expand All @@ -333,7 +355,7 @@ class VectorPtrView {
}

template<std::semiregular I>
inline iterator operator - (I step) noexcept {
inline iterator operator - (I step) const noexcept {

auto ret_node = node_;

Expand Down Expand Up @@ -377,20 +399,18 @@ class VectorPtrView {

public:

// NOTE: The constructor with no argument initializes
// the iterator to be the "end" iterator
//
inline const_iterator () = default;
inline const_iterator (const const_iterator &) = default;
inline const_iterator (const_iterator &&) = default;
inline const_iterator &operator = (const const_iterator &) = default;
inline const_iterator &operator = (const_iterator &&) = default;

inline const_iterator (iter_type node) noexcept : node_ (node) { }
inline const_iterator (pointer const *node) noexcept
: node_ (node) { }
inline const_iterator (const iterator &itr) noexcept { *this = itr; }

const_iterator () = default;
~const_iterator () = default;
const_iterator (const const_iterator &) = default;
const_iterator (const_iterator &&) = default;
const_iterator &operator = (const const_iterator &) = default;
const_iterator &operator = (const_iterator &&) = default;

inline const_iterator &operator = (iter_type rhs) noexcept {

node_ = rhs;
Expand Down Expand Up @@ -432,6 +452,17 @@ class VectorPtrView {
return (node_ <= rhs.node_);
}

friend bool
operator == (const const_iterator &rhs, VPVSentinel) noexcept {

return (rhs.node_ == rhs.vector_.end());
}
friend bool
operator != (const const_iterator &rhs, VPVSentinel) noexcept {

return (rhs.node_ != rhs.vector_.end());
}

// Following STL style, this iterator appears as a pointer
// to value_type.
//
Expand Down Expand Up @@ -491,15 +522,16 @@ class VectorPtrView {
}

template<typename I>
inline const_iterator operator + (I step) noexcept {
inline const_iterator operator + (I step) const noexcept {

auto const ret_node = node_;

ret_node += static_cast<difference_type>(step);
return (const_iterator { ret_node });
}
template<>
inline const_iterator operator + (const const_iterator &rhs) noexcept {
inline const_iterator
operator + (const const_iterator &rhs) const noexcept {

return (const_iterator (node_ + rhs.node_));
}
Expand All @@ -511,7 +543,7 @@ class VectorPtrView {
}

template<std::semiregular I>
inline const_iterator operator - (I step) noexcept {
inline const_iterator operator - (I step) const noexcept {

auto const ret_node = node_;

Expand All @@ -533,6 +565,9 @@ class VectorPtrView {
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;

friend class iterator;
friend class const_iterator;

[[nodiscard]] inline iterator
begin () noexcept { return (iterator(vector_.begin())); }
[[nodiscard]] inline iterator
Expand Down Expand Up @@ -628,7 +663,8 @@ class VectorConstPtrView {
swap (VectorConstPtrView &rhs) noexcept { vector_.swap (rhs.vector_); }

[[nodiscard]] inline friend bool
operator == (const VectorConstPtrView &lhs, const VectorConstPtrView &rhs) {
operator == (const VectorConstPtrView &lhs,
const VectorConstPtrView &rhs) {

return (lhs.vector_ == rhs.vector_);
}
Expand Down Expand Up @@ -746,19 +782,17 @@ class VectorConstPtrView {

public:

// NOTE: The constructor with no argument initializes
// the iterator to be the "end" iterator
//
inline const_iterator () = default;
inline const_iterator (const const_iterator &) = default;
inline const_iterator (const_iterator &&) = default;
inline const_iterator &operator = (const const_iterator &) = default;
inline const_iterator &operator = (const_iterator &&) = default;

inline const_iterator (iter_type node) noexcept : node_ (node) { }
inline const_iterator (pointer const *node) noexcept
: node_ (node) { }

const_iterator () = default;
~const_iterator () = default;
const_iterator (const const_iterator &) = default;
const_iterator (const_iterator &&) = default;
const_iterator &operator = (const const_iterator &) = default;
const_iterator &operator = (const_iterator &&) = default;

inline const_iterator &operator = (iter_type rhs) noexcept {

node_ = rhs;
Expand Down Expand Up @@ -795,6 +829,17 @@ class VectorConstPtrView {
return (node_ <= rhs.node_);
}

friend bool
operator == (const const_iterator &rhs, VPVSentinel) noexcept {

return (rhs.node_ == rhs.vector_.end());
}
friend bool
operator != (const const_iterator &rhs, VPVSentinel) noexcept {

return (rhs.node_ != rhs.vector_.end());
}

// Following STL style, this iterator appears as a pointer
// to value_type.
//
Expand Down Expand Up @@ -853,31 +898,33 @@ class VectorConstPtrView {
return (*this);
}

inline const_iterator operator + (int step) noexcept {
inline const_iterator operator + (int step) const noexcept {

value_type const **ret_node = node_;

ret_node += step;
return (const_iterator (ret_node));
}

inline const_iterator operator - (int step) noexcept {
inline const_iterator operator - (int step) const noexcept {

value_type const **ret_node = node_;

ret_node -= step;
return (const_iterator (ret_node));
}

inline const_iterator operator + (difference_type step) noexcept {
inline const_iterator
operator + (difference_type step) const noexcept {

value_type const **ret_node = node_;

ret_node += step;
return (const_iterator (ret_node));
}

inline const_iterator operator - (difference_type step) noexcept {
inline const_iterator
operator - (difference_type step) const noexcept {

value_type const **ret_node = node_;

Expand All @@ -898,6 +945,8 @@ class VectorConstPtrView {
using reverse_iterator = std::reverse_iterator<const_iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;

friend class const_iterator;

[[nodiscard]] inline const_iterator
begin() const noexcept { return (const_iterator(vector_.begin())); }
[[nodiscard]] inline const_iterator
Expand Down
Loading

0 comments on commit 0757b2e

Please sign in to comment.