Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Array.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2022 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_UTIL_ARRAY_HPP
18#define TOOLBOX_UTIL_ARRAY_HPP
19
20#include <vector>
21
22namespace toolbox {
23inline namespace util {
24
25template <typename ValueT, std::size_t SizeN>
26constexpr std::size_t array_size(const ValueT (&)[SizeN]) noexcept
27{
28 return SizeN;
29}
30
31template <typename ValueT>
32class ArrayView {
33 public:
35
36 using pointer = const ValueT*;
37 using const_pointer [[maybe_unused]] = const ValueT*;
38
39 using reference = const ValueT&;
40 using const_reference = const ValueT&;
41
42 using iterator = const ValueT*;
43 using const_iterator = const ValueT*;
44
45 using reverse_iterator [[maybe_unused]] = std::reverse_iterator<iterator>;
46 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
47
48 using difference_type = std::ptrdiff_t;
49 using size_type = std::size_t;
50
51 constexpr ArrayView(const ValueT* ptr, std::size_t len) noexcept
52 : len_{len}
53 , ptr_{ptr}
54 {
55 }
56 template <typename TypeU, std::size_t SizeN>
57 constexpr ArrayView(TypeU (&arr)[SizeN]) noexcept // NOLINT(hicpp-explicit-conversions)
58 : len_{SizeN}
59 , ptr_{arr}
60 {
61 }
62 ArrayView(const std::vector<ValueT>& arr) noexcept // NOLINT(hicpp-explicit-conversions)
63 : len_{arr.size()}
64 , ptr_{arr.empty() ? nullptr : &arr[0]}
65 {
66 }
67 constexpr ArrayView() noexcept = default;
69
70 // Copy.
73
74 // Move.
77
79 constexpr const_iterator end() const noexcept { return ptr_ + len_; }
88 constexpr const_iterator cbegin() const noexcept { return ptr_; }
89 constexpr const_iterator cend() const noexcept { return ptr_ + len_; }
98 constexpr const_reference operator[](size_type pos) const noexcept { return ptr_[pos]; }
99 constexpr const_reference front() const noexcept { return ptr_[0]; }
100 constexpr const_reference back() const noexcept { return ptr_[len_ - 1]; }
101 constexpr const ValueT* data() const noexcept { return ptr_; }
102 constexpr bool empty() const noexcept { return len_ == 0; }
103 constexpr std::size_t size() const noexcept { return len_; }
104 constexpr void clear() noexcept
105 {
106 len_ = 0;
107 ptr_ = nullptr;
108 }
109 void swap(ArrayView& rhs) noexcept
110 {
111 std::swap(len_, rhs.len_);
112 std::swap(ptr_, rhs.ptr_);
113 }
114
115 private:
116 // Length in the first cache-line.
117 std::size_t len_{0};
118 const ValueT* ptr_{nullptr};
119};
120
121template <typename ValueT>
123 std::size_t len) noexcept
124{
125 return {ptr, len};
126}
127
128template <typename ValueT, std::size_t SizeN>
130{
131 return {arr};
132}
133
134} // namespace util
135} // namespace toolbox
136
137#endif // TOOLBOX_UTIL_ARRAY_HPP
constexpr const_iterator end() const noexcept
Definition Array.hpp:79
const ValueT & reference
Definition Array.hpp:39
constexpr std::size_t size() const noexcept
Definition Array.hpp:103
constexpr const_reference operator[](size_type pos) const noexcept
Definition Array.hpp:98
constexpr ArrayView(const ValueT *ptr, std::size_t len) noexcept
Definition Array.hpp:51
constexpr const_iterator begin() const noexcept
Definition Array.hpp:78
const ValueT * const_iterator
Definition Array.hpp:43
const ValueT * iterator
Definition Array.hpp:42
const ValueT * pointer
Definition Array.hpp:36
constexpr ArrayView(TypeU(&arr)[SizeN]) noexcept
Definition Array.hpp:57
void swap(ArrayView &rhs) noexcept
Definition Array.hpp:109
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition Array.hpp:46
constexpr bool empty() const noexcept
Definition Array.hpp:102
const ValueT * const_pointer
Definition Array.hpp:37
std::ptrdiff_t difference_type
Definition Array.hpp:48
constexpr const_reference front() const noexcept
Definition Array.hpp:99
ArrayView(const std::vector< ValueT > &arr) noexcept
Definition Array.hpp:62
constexpr const_reference back() const noexcept
Definition Array.hpp:100
constexpr const_reverse_iterator rbegin() const noexcept
Definition Array.hpp:80
constexpr const ValueT * data() const noexcept
Definition Array.hpp:101
std::size_t size_type
Definition Array.hpp:49
constexpr const_reverse_iterator crbegin() const noexcept
Definition Array.hpp:90
std::reverse_iterator< iterator > reverse_iterator
Definition Array.hpp:45
constexpr const_iterator cend() const noexcept
Definition Array.hpp:89
constexpr void clear() noexcept
Definition Array.hpp:104
constexpr const_reverse_iterator crend() const noexcept
Definition Array.hpp:94
constexpr const_reverse_iterator rend() const noexcept
Definition Array.hpp:84
const ValueT & const_reference
Definition Array.hpp:40
constexpr ArrayView() noexcept=default
constexpr const_iterator cbegin() const noexcept
Definition Array.hpp:88
constexpr ArrayView< std::remove_volatile_t< ValueT > > make_array_view(const ValueT *ptr, std::size_t len) noexcept
Definition Array.hpp:122
constexpr std::size_t array_size(const ValueT(&)[SizeN]) noexcept
Definition Array.hpp:26
constexpr auto bind() noexcept
Definition Slot.hpp:92