gdk-graphics 0b051eb9b5c1eaa0658babaa4463dd7a80aa9d2c
Loading...
Searching...
No Matches
contiguous_view.h
1// © Joseph Cameron - All Rights Reserved
2
3#ifndef JFC_CONTIGUOUS_VIEW_H
4#define JFC_CONTIGUOUS_VIEW_H
5
6namespace jfc {
20template<class value_type_param>
21class contiguous_view final {
22public:
23 using size_type = size_t;
24 using value_type = value_type_param;
25
28 template<class Pointer>
29 constexpr contiguous_view(Pointer *pBegin, const size_type aSize)
30 : m_pBegin(pBegin)
31 , m_pEnd(pBegin + aSize)
32 , m_Size(aSize)
33 {}
34
37 template<class Iterator>
38 constexpr contiguous_view(Iterator iBegin, const size_type aSize)
39 : contiguous_view(static_cast<value_type_param *>(&(*iBegin)), aSize)
40 {}
41
45 template<class Collection>
46 constexpr contiguous_view(Collection &aCollection)
47 : contiguous_view(static_cast<value_type_param *>(&(*aCollection.begin())), aCollection.size())
48 {}
49
54 template<class Pointer>
55 contiguous_view(Pointer *pBegin, Pointer *pEnd)
56 : contiguous_view(static_cast<value_type_param *>(&(*pBegin)), [pBegin, pEnd]() {
57 size_type size(0);
58
59 for(auto iter(pBegin); iter != pEnd; ++iter)
60 ++size;
61
62 return size;
63 }())
64 {}
65
67 template<class Iterator>
68 contiguous_view(Iterator aBegin, Iterator aEnd)
69 : contiguous_view(static_cast<value_type_param *>(&(*aBegin)),
70 static_cast<value_type_param *>(&(*aEnd)))
71 {}
72
74 constexpr contiguous_view() = default;
75
77 constexpr contiguous_view(const contiguous_view &) = default;
78
80 constexpr contiguous_view &operator=(const contiguous_view &rhv) = default;
81
83 constexpr contiguous_view(contiguous_view &&) = default;
84
86 constexpr contiguous_view &operator=(contiguous_view &&rhv) = default;
87
89 [[nodiscard]] constexpr value_type &operator[](const size_type index) {
90 return *(m_pBegin + index);
91 }
92
94 [[nodiscard]] constexpr const value_type &operator[](const size_type index) const {
95 return *(m_pBegin + index);
96 }
97
99 [[nodiscard]] constexpr value_type_param *begin() {
100 return m_pBegin;
101 }
102
104 [[nodiscard]] constexpr value_type_param *begin() const {
105 return static_cast<const value_type *>(m_pBegin);
106 }
107
109 [[nodiscard]] constexpr value_type_param *end() {
110 return m_pEnd;
111 }
112
114 [[nodiscard]] constexpr value_type_param *end() const {
115 return static_cast<const value_type *>(m_pEnd);
116 }
117
119 [[nodiscard]] constexpr size_t size() const {
120 return m_Size;
121 }
122
124 [[nodiscard]] constexpr bool empty() const {
125 return m_Size == 0;
126 }
127
128private:
129 value_type_param *m_pBegin = nullptr;
130
131 value_type_param *m_pEnd = nullptr;
132
133 size_type m_Size = 0;
134};
135}
136
137#endif
138
constexpr contiguous_view(const contiguous_view &)=default
support copy semantics
constexpr bool empty() const
whether or not size is 0
Definition contiguous_view.h:124
constexpr const value_type & operator[](const size_type index) const
access an element by index, constant
Definition contiguous_view.h:94
constexpr size_t size() const
number of elements accessible through the view
Definition contiguous_view.h:119
constexpr contiguous_view(contiguous_view &&)=default
support move semantics
constexpr value_type & operator[](const size_type index)
access an element by index
Definition contiguous_view.h:89
constexpr value_type_param * end()
pointer to the element following the last
Definition contiguous_view.h:109
constexpr value_type_param * begin()
pointer to the beginning of the data
Definition contiguous_view.h:99
contiguous_view(Pointer *pBegin, Pointer *pEnd)
Definition contiguous_view.h:55
constexpr value_type_param * end() const
pointer to the element following the last, constant
Definition contiguous_view.h:114
constexpr value_type_param * begin() const
pointer to the beginning of the data, constant
Definition contiguous_view.h:104
constexpr contiguous_view & operator=(contiguous_view &&rhv)=default
support move semantics
contiguous_view(Iterator aBegin, Iterator aEnd)
construct from a pair of iterators.
Definition contiguous_view.h:68
constexpr contiguous_view()=default
constructs an empty view
constexpr contiguous_view(Pointer *pBegin, const size_type aSize)
Definition contiguous_view.h:29
constexpr contiguous_view(Collection &aCollection)
Definition contiguous_view.h:46
constexpr contiguous_view & operator=(const contiguous_view &rhv)=default
support copy semantics
constexpr contiguous_view(Iterator iBegin, const size_type aSize)
Definition contiguous_view.h:38