35 using IterC =
const T *;
37 VecView() : ptr_(
nullptr), size_(0) {}
39 VecView(T *ptr,
size_t size) : ptr_(ptr), size_(size) {}
41 VecView(
const std::vector<std::remove_cv_t<T>> &v)
42 : ptr_(v.data()), size_(v.size()) {}
58 inline const T &operator[](
size_t i)
const {
59 ASSERT(i < size_, std::out_of_range(
"Vec out of range"));
63 inline T &operator[](
size_t i) {
64 ASSERT(i < size_, std::out_of_range(
"Vec out of range"));
68 IterC cbegin()
const {
return ptr_; }
69 IterC cend()
const {
return ptr_ + size_; }
71 IterC begin()
const {
return cbegin(); }
72 IterC end()
const {
return cend(); }
74 Iter begin() {
return ptr_; }
75 Iter end() {
return ptr_ + size_; }
77 const T &front()
const {
79 std::out_of_range(
"Attempt to take the front of an empty vector"));
83 const T &back()
const {
85 std::out_of_range(
"Attempt to take the back of an empty vector"));
86 return ptr_[size_ - 1];
91 std::out_of_range(
"Attempt to take the front of an empty vector"));
97 std::out_of_range(
"Attempt to take the back of an empty vector"));
98 return ptr_[size_ - 1];
101 size_t size()
const {
return size_; }
103 bool empty()
const {
return size_ == 0; }
106 size_t length = std::numeric_limits<size_t>::max()) {
107 if (length == std::numeric_limits<size_t>::max())
108 length = this->size_ - offset;
109 ASSERT(length >= 0, std::out_of_range(
"Vec::view out of range"));
110 ASSERT(offset + length <= this->size_ && offset >= 0,
111 std::out_of_range(
"Vec::view out of range"));
112 return VecView<T>(this->ptr_ + offset, length);
117 size_t length = std::numeric_limits<size_t>::max())
const {
118 if (length == std::numeric_limits<size_t>::max())
119 length = this->size_ - offset;
120 ASSERT(length >= 0, std::out_of_range(
"Vec::cview out of range"));
121 ASSERT(offset + length <= this->size_ && offset >= 0,
122 std::out_of_range(
"Vec::cview out of range"));
128 size_t length = std::numeric_limits<size_t>::max())
const {
129 return cview(offset, length);
132 T *data() {
return this->ptr_; }
134 const T *data()
const {
return this->ptr_; }
138 std::cout <<
"Vec = " << std::endl;
139 for (
size_t i = 0; i < size(); ++i) {
140 std::cout << i <<
", " << ptr_[i] <<
", " << std::endl;
142 std::cout << std::endl;