numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
vector.h
Go to the documentation of this file.
1/*
2 * Copyright 2023 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <cstddef>
23#include <initializer_list>
24#include <vector>
25
27
28namespace num_collect::util {
29
36template <typename T>
37class vector {
38public:
40 using internal_vector_type = std::vector<T>;
41
43 using value_type = typename internal_vector_type::value_type;
44
47
49 using difference_type = typename internal_vector_type::difference_type;
50
52 using reference = typename internal_vector_type::reference;
53
55 using const_reference = typename internal_vector_type::const_reference;
56
58 using pointer = typename internal_vector_type::pointer;
59
61 using const_pointer = typename internal_vector_type::const_pointer;
62
64 using iterator = typename internal_vector_type::iterator;
65
67 using const_iterator = typename internal_vector_type::const_iterator;
68
70 using reverse_iterator = typename internal_vector_type::reverse_iterator;
71
74 typename internal_vector_type::const_reverse_iterator;
75
81 vector() = default;
82
90 : vector_(static_cast<std::size_t>(size), value) {}
91
97 vector(std::initializer_list<T> values) : vector_(values) {}
98
105 [[nodiscard]] auto operator[](index_type index) -> reference {
106 return vector_[static_cast<std::size_t>(index)];
107 }
108
115 [[nodiscard]] auto operator[](index_type index) const -> const_reference {
116 return vector_[static_cast<std::size_t>(index)];
117 }
118
124 [[nodiscard]] auto front() -> reference { return vector_.front(); }
125
131 [[nodiscard]] auto front() const -> const_reference {
132 return vector_.front();
133 }
134
140 [[nodiscard]] auto back() -> reference { return vector_.back(); }
141
147 [[nodiscard]] auto back() const -> const_reference {
148 return vector_.back();
149 }
150
156 [[nodiscard]] auto begin() -> iterator { return vector_.begin(); }
157
163 [[nodiscard]] auto begin() const -> const_iterator {
164 return vector_.begin();
165 }
166
172 [[nodiscard]] auto cbegin() const -> const_iterator {
173 return vector_.begin();
174 }
175
181 [[nodiscard]] auto end() -> iterator { return vector_.end(); }
182
188 [[nodiscard]] auto end() const -> const_iterator { return vector_.end(); }
189
195 [[nodiscard]] auto cend() const -> const_iterator { return vector_.end(); }
196
202 [[nodiscard]] auto data() noexcept -> pointer { return vector_.data(); }
203
209 [[nodiscard]] auto data() const noexcept -> const_pointer {
210 return vector_.data();
211 }
212
219 [[nodiscard]] auto empty() const -> bool { return vector_.empty(); }
220
226 [[nodiscard]] auto size() const -> index_type {
227 return static_cast<index_type>(vector_.size());
228 }
229
236 vector_.reserve(static_cast<std::size_t>(size));
237 }
238
245 vector_.resize(static_cast<std::size_t>(size));
246 }
247
251 void clear() { vector_.clear(); }
252
258 void push_back(const value_type& value) { vector_.push_back(value); }
259
265 void push_back(value_type&& value) { vector_.push_back(std::move(value)); }
266
267private:
270};
271
272} // namespace num_collect::util
Class of vectors wrapping std::vector class to use singed integers as indices.
Definition vector.h:37
auto size() const -> index_type
Get the size of this vector.
Definition vector.h:226
auto data() const noexcept -> const_pointer
Get the pointer to the first element.
Definition vector.h:209
typename internal_vector_type::const_reverse_iterator const_reverse_iterator
Type of const reverse iterators.
Definition vector.h:73
typename internal_vector_type::const_reference const_reference
Type of const references.
Definition vector.h:55
typename internal_vector_type::iterator iterator
Type of iterators.
Definition vector.h:64
auto begin() const -> const_iterator
Get the iterator to the first element.
Definition vector.h:163
typename internal_vector_type::reverse_iterator reverse_iterator
Type of reverse iterators.
Definition vector.h:70
auto front() const -> const_reference
Access to the front element.
Definition vector.h:131
typename internal_vector_type::const_iterator const_iterator
Type of const iterators.
Definition vector.h:67
auto end() -> iterator
Get the past-the-end iterator.
Definition vector.h:181
index_type size_type
Type of sizes.
Definition vector.h:46
std::vector< T > internal_vector_type
Type of actual vectors.
Definition vector.h:40
typename internal_vector_type::difference_type difference_type
Type of differences of pointers.
Definition vector.h:49
vector(std::initializer_list< T > values)
Constructor.
Definition vector.h:97
typename internal_vector_type::reference reference
Type of references.
Definition vector.h:52
auto cend() const -> const_iterator
Get the past-the-end iterator.
Definition vector.h:195
auto data() noexcept -> pointer
Get the pointer to the first element.
Definition vector.h:202
auto front() -> reference
Access to the front element.
Definition vector.h:124
typename internal_vector_type::value_type value_type
Type of values.
Definition vector.h:43
internal_vector_type vector_
Actual vector.
Definition vector.h:269
vector(index_type size, value_type value=value_type())
Constructor.
Definition vector.h:89
vector()=default
Constructor.
auto empty() const -> bool
Check whether this vector is empty.
Definition vector.h:219
auto operator[](index_type index) -> reference
Access to an element.
Definition vector.h:105
auto back() const -> const_reference
Access to the final element.
Definition vector.h:147
void resize(index_type size)
Change the size.
Definition vector.h:244
auto operator[](index_type index) const -> const_reference
Access to an element.
Definition vector.h:115
auto begin() -> iterator
Get the iterator to the first element.
Definition vector.h:156
auto cbegin() const -> const_iterator
Get the iterator to the first element.
Definition vector.h:172
auto end() const -> const_iterator
Get the past-the-end iterator.
Definition vector.h:188
void reserve(index_type size)
Reserve memory.
Definition vector.h:235
typename internal_vector_type::pointer pointer
Type of pointers.
Definition vector.h:58
void push_back(const value_type &value)
Add an element.
Definition vector.h:258
void clear()
Remove the all elements in this vector.
Definition vector.h:251
auto back() -> reference
Access to the final element.
Definition vector.h:140
void push_back(value_type &&value)
Add an element.
Definition vector.h:265
typename internal_vector_type::const_pointer const_pointer
Type of const pointers.
Definition vector.h:61
Definition of index_type type.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
Namespace of utilities.
Definition assert.h:30
STL namespace.