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]));
62 for(
size_t i = 0; i < n2; ++i) {
65 allocator.deallocate(data2);
69 CUDA void reallocate() {
71 reallocate(
max(cap,
size_t(1)) * GROWING_FACTOR);
75 template<
class T2,
class Allocator2>
friend class vector;
77 CUDA void inplace_new(
size_t i) {
78 if constexpr(std::is_constructible<value_type, const allocator_type&>{}) {
87 CUDA void inplace_new(
size_t i,
const U& value) {
88 if constexpr(std::is_constructible<value_type, const U&, const allocator_type&>{}) {
100 n(n), cap(n), allocator(alloc), data_(allocate())
102 for(
size_t i = 0; i < n; ++i) {
109 : n(0), cap(0), allocator(alloc), data_(nullptr) {}
116 : n(n), cap(n), allocator(alloc), data_(allocate())
118 for(
size_t i = 0; i < n; ++i) {
119 inplace_new(i, from[i]);
124 template <
class U,
class Allocator2>
141 : n(n), cap(n), allocator(alloc), data_(allocate())
143 for(
size_t i = 0; i < n; ++i) {
144 inplace_new(i, from);
153 : n(init.
size()), cap(init.
size()), allocator(alloc), data_(allocate())
156 for(
const T& v : init) {
162 template <
class U,
class Alloc2>
164 : n(other.
size()), cap(other.
size()), allocator(alloc), data_(allocate())
166 for(
size_t i = 0; i < n; ++i) {
167 inplace_new(i, other[i]);
172 for(
size_t i = 0; i < n; ++i) {
175 allocator.deallocate(data_);
187 template <
class U,
class Allocator2>
190 for(
size_t i = 0; i < other.n; ++i) {
192 data_[i].~value_type();
194 inplace_new(i, other.data_[i]);
196 for(
size_t i = other.n; i < n; ++i) {
197 data_[i].~value_type();
205 return assignment(other);
209 template <
class U,
class Allocator2>
211 return assignment(other);
236 CUDA void reserve(
size_t new_cap) {
if(new_cap > cap) reallocate(new_cap); }
243 for(
size_t i = 0; i < n2; ++i) {
250 inplace_new(n, value);
256 new(&data_[n]) T(std::forward<T>(value));
260 template<
class... Args>
264 new(&data_[n])
value_type(std::forward<Args>(args)..., allocator);
267 new(&data_[n])
value_type(std::forward<Args>(args)...);
281 for(
size_t i = count; i < n; ++i) {
290 for(
size_t i = n; i < count; ++i) {
305 for(
size_t i = 0; i < n; ++i) {
314template<
class T1,
class Alloc1,
class T2,
class Alloc2>
320 for(
size_t i = 0; i < lhs.
size(); ++i) {
321 if(!(lhs[i] == rhs[i])) {
329template<
class T1,
class Alloc1,
class T2,
class Alloc2>
330CUDA bool operator!=(
const vector<T1, Alloc1>& lhs,
const vector<T2, Alloc2>& rhs) {
331 return !(lhs == rhs);
CUDA size_t capacity() const
Definition vector.hpp:237
CUDA void push_back(const T &value)
Definition vector.hpp:248
CUDA value_type & front()
Definition vector.hpp:228
CUDA void reserve(size_t new_cap)
Definition vector.hpp:236
CUDA NI vector(size_t n, const allocator_type &alloc=allocator_type())
Definition vector.hpp:99
Allocator allocator_type
Definition vector.hpp:24
CUDA const value_type & front() const
Definition vector.hpp:229
CUDA value_type & emplace_back(Args &&... args)
Definition vector.hpp:261
CUDA this_type & operator=(this_type &&other)
Definition vector.hpp:181
CUDA vector(const vector< U, allocator_type > &from)
Definition vector.hpp:130
CUDA value_type & operator[](size_t i)
Definition vector.hpp:218
CUDA NI vector(std::initializer_list< T > init, const allocator_type &alloc=allocator_type{})
Definition vector.hpp:152
vector(const std::vector< U, Alloc2 > &other, const allocator_type &alloc=allocator_type{})
Definition vector.hpp:163
CUDA vector(const vector< U, Allocator2 > &from, const allocator_type &alloc=allocator_type{})
Definition vector.hpp:125
CUDA NI ~vector()
Definition vector.hpp:171
CUDA void shrink_to_fit()
Definition vector.hpp:238
T value_type
Definition vector.hpp:23
CUDA void swap(this_type &other)
Definition vector.hpp:297
CUDA NI vector(this_type &&other)
Definition vector.hpp:148
CUDA NI void resize(size_t count)
Definition vector.hpp:279
CUDA vector(const this_type &from)
Definition vector.hpp:134
CUDA NI vector(size_t n, const U &from, const allocator_type &alloc=allocator_type{})
Definition vector.hpp:140
CUDA const value_type * data() const
Definition vector.hpp:233
CUDA NI this_type & operator=(const vector< U, Allocator2 > &other)
Definition vector.hpp:210
vector< value_type, allocator_type > this_type
Definition vector.hpp:25
CUDA allocator_type get_allocator() const
Definition vector.hpp:214
CUDA void pop_back()
Definition vector.hpp:273
CUDA void push_back(T &&value)
Definition vector.hpp:254
CUDA NI void print() const
Definition vector.hpp:304
CUDA NI void clear()
Definition vector.hpp:240
CUDA const value_type & operator[](size_t i) const
Definition vector.hpp:223
CUDA bool empty() const
Definition vector.hpp:234
CUDA size_t size() const
Definition vector.hpp:235
CUDA NI vector(const U *from, size_t n, const allocator_type &alloc=allocator_type{})
Definition vector.hpp:115
CUDA vector(const allocator_type &alloc=allocator_type{})
Definition vector.hpp:108
CUDA NI this_type & operator=(const this_type &other)
Definition vector.hpp:204
CUDA value_type & back()
Definition vector.hpp:230
CUDA const value_type & back() const
Definition vector.hpp:231
CUDA value_type * data()
Definition vector.hpp:232
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:707
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