3#ifndef CUDA_BATTERY_VECTOR_HPP
4#define CUDA_BATTERY_VECTOR_HPP
9#include <initializer_list>
19template<
class T,
class Allocator = standard_allocator>
28 static constexpr const size_t GROWING_FACTOR = 3;
36 return static_cast<T*
>(allocator.allocate(
sizeof(T) * cap));
43 CUDA void deallocate() {
44 allocator.deallocate(data_);
47 CUDA NI void reallocate(
size_t new_cap) {
53 for(
size_t i = 0; i < n; ++i) {
54 if constexpr(std::is_move_constructible_v<value_type>) {
55 new(&data_[i])
value_type(std::move(data2[i]));
61 if constexpr(!std::is_trivially_destructible_v<value_type>) {
63 for(
size_t i = 0; i < n2; ++i) {
67 allocator.deallocate(data2);
71 CUDA void reallocate() {
73 reallocate(
max(cap,
size_t(1)) * GROWING_FACTOR);
77 template<
class T2,
class Allocator2>
friend class vector;
79 CUDA void inplace_new(
size_t i) {
80 if constexpr(std::is_constructible<value_type, const allocator_type&>{}) {
89 CUDA void inplace_new(
size_t i,
const U& value) {
90 if constexpr(std::is_constructible<value_type, const U&, const allocator_type&>{}) {
102 n(n), cap(n), allocator(alloc), data_(allocate())
104 for(
size_t i = 0; i < n; ++i) {
111 : n(0), cap(0), allocator(alloc), data_(nullptr) {}
118 : n(n), cap(n), allocator(alloc), data_(allocate())
120 for(
size_t i = 0; i < n; ++i) {
121 inplace_new(i, from[i]);
126 template <
class U,
class Allocator2>
143 : n(n), cap(n), allocator(alloc), data_(allocate())
145 for(
size_t i = 0; i < n; ++i) {
146 inplace_new(i, from);
155 : n(init.
size()), cap(init.
size()), allocator(alloc), data_(allocate())
158 for(
const T& v : init) {
164 template <
class U,
class Alloc2>
166 : n(other.
size()), cap(other.
size()), allocator(alloc), data_(allocate())
168 for(
size_t i = 0; i < n; ++i) {
169 inplace_new(i, other[i]);
174 if constexpr(!std::is_trivially_destructible_v<value_type>) {
175 for(
size_t i = 0; i < n; ++i) {
179 allocator.deallocate(data_);
191 template <
class U,
class Allocator2>
194 constexpr bool fast_copy =
196 std::is_same_v<value_type, U>
197 && std::is_same_v<Allocator2, allocator_type>
198 && std::is_trivially_copyable_v<value_type>;
200 std::is_same_v<value_type, U>
201 && std::is_trivially_copyable_v<value_type>;
204 if constexpr(fast_copy) {
205 memcpy(data_, other.data_, other.n *
sizeof(
value_type));
208 for(
size_t i = 0; i < other.n; ++i) {
209 if constexpr(!std::is_trivially_destructible_v<value_type>) {
211 data_[i].~value_type();
214 inplace_new(i, other.data_[i]);
217 if constexpr(!std::is_trivially_destructible_v<value_type>) {
218 for(
size_t i = other.n; i < n; ++i) {
219 data_[i].~value_type();
228 return assignment(other);
232 template <
class U,
class Allocator2>
234 return assignment(other);
259 CUDA void reserve(
size_t new_cap) {
if(new_cap > cap) reallocate(new_cap); }
266 for(
size_t i = 0; i < n2; ++i) {
273 inplace_new(n, value);
279 new(&data_[n]) T(std::forward<T>(value));
283 template<
class... Args>
287 new(&data_[n])
value_type(std::forward<Args>(args)..., allocator);
290 new(&data_[n])
value_type(std::forward<Args>(args)...);
304 if constexpr(!std::is_trivially_destructible_v<value_type>) {
305 for(
size_t i = count; i < n; ++i) {
315 for(
size_t i = n; i < count; ++i) {
330 for(
size_t i = 0; i < n; ++i) {
339template<
class T1,
class Alloc1,
class T2,
class Alloc2>
345 for(
size_t i = 0; i < lhs.
size(); ++i) {
346 if(!(lhs[i] == rhs[i])) {
354template<
class T1,
class Alloc1,
class T2,
class Alloc2>
355CUDA bool operator!=(
const vector<T1, Alloc1>& lhs,
const vector<T2, Alloc2>& rhs) {
356 return !(lhs == rhs);
CUDA size_t capacity() const
Definition vector.hpp:260
CUDA void push_back(const T &value)
Definition vector.hpp:271
CUDA value_type & front()
Definition vector.hpp:251
CUDA void reserve(size_t new_cap)
Definition vector.hpp:259
CUDA NI vector(size_t n, const allocator_type &alloc=allocator_type())
Definition vector.hpp:101
Allocator allocator_type
Definition vector.hpp:24
CUDA const value_type & front() const
Definition vector.hpp:252
CUDA value_type & emplace_back(Args &&... args)
Definition vector.hpp:284
CUDA this_type & operator=(this_type &&other)
Definition vector.hpp:185
CUDA vector(const vector< U, allocator_type > &from)
Definition vector.hpp:132
CUDA value_type & operator[](size_t i)
Definition vector.hpp:241
CUDA NI vector(std::initializer_list< T > init, const allocator_type &alloc=allocator_type{})
Definition vector.hpp:154
vector(const std::vector< U, Alloc2 > &other, const allocator_type &alloc=allocator_type{})
Definition vector.hpp:165
CUDA vector(const vector< U, Allocator2 > &from, const allocator_type &alloc=allocator_type{})
Definition vector.hpp:127
CUDA NI ~vector()
Definition vector.hpp:173
CUDA void shrink_to_fit()
Definition vector.hpp:261
T value_type
Definition vector.hpp:23
CUDA void swap(this_type &other)
Definition vector.hpp:322
CUDA NI vector(this_type &&other)
Definition vector.hpp:150
CUDA NI void resize(size_t count)
Definition vector.hpp:302
CUDA vector(const this_type &from)
Definition vector.hpp:136
CUDA NI vector(size_t n, const U &from, const allocator_type &alloc=allocator_type{})
Definition vector.hpp:142
CUDA const value_type * data() const
Definition vector.hpp:256
CUDA NI this_type & operator=(const vector< U, Allocator2 > &other)
Definition vector.hpp:233
vector< value_type, allocator_type > this_type
Definition vector.hpp:25
CUDA allocator_type get_allocator() const
Definition vector.hpp:237
CUDA void pop_back()
Definition vector.hpp:296
CUDA void push_back(T &&value)
Definition vector.hpp:277
CUDA NI void print() const
Definition vector.hpp:329
CUDA NI void clear()
Definition vector.hpp:263
CUDA const value_type & operator[](size_t i) const
Definition vector.hpp:246
CUDA bool empty() const
Definition vector.hpp:257
CUDA size_t size() const
Definition vector.hpp:258
CUDA NI vector(const U *from, size_t n, const allocator_type &alloc=allocator_type{})
Definition vector.hpp:117
CUDA vector(const allocator_type &alloc=allocator_type{})
Definition vector.hpp:110
CUDA NI this_type & operator=(const this_type &other)
Definition vector.hpp:227
CUDA value_type & back()
Definition vector.hpp:253
CUDA const value_type & back() const
Definition vector.hpp:254
CUDA value_type * data()
Definition vector.hpp:255
Definition algorithm.hpp:10
CUDA INLINE constexpr T min(T a, T b)
Definition utility.hpp:116
CUDA INLINE constexpr T max(T a, T b)
Definition utility.hpp:128
CUDA constexpr void swap(T &a, T &b)
Definition utility.hpp:91
CUDA NI void print(const T &t)
Definition utility.hpp:732
CUDA bool operator==(const string< Alloc1 > &lhs, const string< Alloc2 > &rhs)
Definition string.hpp:110
#define CUDA
Definition utility.hpp:59
#define NI
Definition utility.hpp:62