Manifold 3.0
Robust geometry
 
Loading...
Searching...
No Matches
vec_view.h
1// Copyright 2023 The Manifold Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <cstddef>
18#include <limits>
19#include <type_traits>
20#include <vector>
21
22#include "manifold/optional_assert.h"
23
24namespace manifold {
25
31template <typename T>
32class VecView {
33 public:
34 using Iter = T *;
35 using IterC = const T *;
36
37 VecView() : ptr_(nullptr), size_(0) {}
38
39 VecView(T *ptr, size_t size) : ptr_(ptr), size_(size) {}
40
41 VecView(const std::vector<std::remove_cv_t<T>> &v)
42 : ptr_(v.data()), size_(v.size()) {}
43
44 VecView(const VecView &other) {
45 ptr_ = other.ptr_;
46 size_ = other.size_;
47 }
48
49 VecView &operator=(const VecView &other) {
50 ptr_ = other.ptr_;
51 size_ = other.size_;
52 return *this;
53 }
54
55 // allows conversion to a const VecView
56 operator VecView<const T>() const { return {ptr_, size_}; }
57
58 inline const T &operator[](size_t i) const {
59 ASSERT(i < size_, std::out_of_range("Vec out of range"));
60 return ptr_[i];
61 }
62
63 inline T &operator[](size_t i) {
64 ASSERT(i < size_, std::out_of_range("Vec out of range"));
65 return ptr_[i];
66 }
67
68 IterC cbegin() const { return ptr_; }
69 IterC cend() const { return ptr_ + size_; }
70
71 IterC begin() const { return cbegin(); }
72 IterC end() const { return cend(); }
73
74 Iter begin() { return ptr_; }
75 Iter end() { return ptr_ + size_; }
76
77 const T &front() const {
78 ASSERT(size_ != 0,
79 std::out_of_range("Attempt to take the front of an empty vector"));
80 return ptr_[0];
81 }
82
83 const T &back() const {
84 ASSERT(size_ != 0,
85 std::out_of_range("Attempt to take the back of an empty vector"));
86 return ptr_[size_ - 1];
87 }
88
89 T &front() {
90 ASSERT(size_ != 0,
91 std::out_of_range("Attempt to take the front of an empty vector"));
92 return ptr_[0];
93 }
94
95 T &back() {
96 ASSERT(size_ != 0,
97 std::out_of_range("Attempt to take the back of an empty vector"));
98 return ptr_[size_ - 1];
99 }
100
101 size_t size() const { return size_; }
102
103 bool empty() const { return size_ == 0; }
104
105 VecView<T> view(size_t offset = 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);
113 }
114
115 VecView<const T> cview(
116 size_t offset = 0,
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"));
123 return VecView<const T>(this->ptr_ + offset, length);
124 }
125
126 VecView<const T> view(
127 size_t offset = 0,
128 size_t length = std::numeric_limits<size_t>::max()) const {
129 return cview(offset, length);
130 }
131
132 T *data() { return this->ptr_; }
133
134 const T *data() const { return this->ptr_; }
135
136#ifdef MANIFOLD_DEBUG
137 void Dump() const {
138 std::cout << "Vec = " << std::endl;
139 for (size_t i = 0; i < size(); ++i) {
140 std::cout << i << ", " << ptr_[i] << ", " << std::endl;
141 }
142 std::cout << std::endl;
143 }
144#endif
145
146 protected:
147 T *ptr_ = nullptr;
148 size_t size_ = 0;
149};
150
151} // namespace manifold
Definition vec_view.h:32