1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
|
/*
* Copyright 2008-2018 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*! \file host_vector.h
* \brief A dynamically-sizable array of elements which resides in memory
* accessible to hosts.
*/
#pragma once
#include <thrust/detail/config.h>
#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
# pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
# pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
# pragma system_header
#endif // no system header
#include <thrust/detail/memory_wrapper.h>
#include <thrust/detail/vector_base.h>
#include <initializer_list>
#include <vector>
#include <utility>
THRUST_NAMESPACE_BEGIN
/*! \addtogroup container_classes Container Classes
* \addtogroup host_containers Host Containers
* \ingroup container_classes
* \{
*/
/*! A \p host_vector is a container that supports random access to elements,
* constant time removal of elements at the end, and linear time insertion
* and removal of elements at the beginning or in the middle. The number of
* elements in a \p host_vector may vary dynamically; memory management is
* automatic. The memory associated with a \p host_vector resides in memory
* accessible to hosts.
*
* \see https://en.cppreference.com/w/cpp/container/vector
* \see device_vector
* \see universal_vector
*/
template<typename T, typename Alloc = std::allocator<T> >
class host_vector
: public detail::vector_base<T,Alloc>
{
private:
typedef detail::vector_base<T,Alloc> Parent;
public:
/*! \cond
*/
typedef typename Parent::size_type size_type;
typedef typename Parent::value_type value_type;
/*! \endcond
*/
/*! This constructor creates an empty \p host_vector.
*/
__host__
host_vector(void)
:Parent() {}
/*! This constructor creates an empty \p host_vector.
* \param alloc The allocator to use by this host_vector.
*/
__host__
host_vector(const Alloc &alloc)
:Parent(alloc) {}
/*! The destructor erases the elements.
*/
// Define an empty destructor to explicitly specify
// its execution space qualifier, as a workaround for nvcc warning
__host__
~host_vector(void) {}
/*! This constructor creates a \p host_vector with the given
* size.
* \param n The number of elements to initially create.
*/
__host__
explicit host_vector(size_type n)
:Parent(n) {}
/*! This constructor creates a \p host_vector with the given
* size.
* \param n The number of elements to initially create.
* \param alloc The allocator to use by this host_vector.
*/
__host__
explicit host_vector(size_type n, const Alloc &alloc)
:Parent(n,alloc) {}
/*! This constructor creates a \p host_vector with copies
* of an exemplar element.
* \param n The number of elements to initially create.
* \param value An element to copy.
*/
__host__
explicit host_vector(size_type n, const value_type &value)
:Parent(n,value) {}
/*! This constructor creates a \p host_vector with copies
* of an exemplar element.
* \param n The number of elements to initially create.
* \param value An element to copy.
* \param alloc The allocator to use by this host_vector.
*/
__host__
explicit host_vector(size_type n, const value_type &value, const Alloc &alloc)
:Parent(n,value,alloc) {}
/*! Copy constructor copies from an exemplar \p host_vector.
* \param v The \p host_vector to copy.
*/
__host__
host_vector(const host_vector &v)
:Parent(v) {}
/*! Copy constructor copies from an exemplar \p host_vector.
* \param v The \p host_vector to copy.
* \param alloc The allocator to use by this host_vector.
*/
__host__
host_vector(const host_vector &v, const Alloc &alloc)
:Parent(v,alloc) {}
#if THRUST_CPP_DIALECT >= 2011
/*! Move constructor moves from another host_vector.
* \param v The host_vector to move.
*/
__host__
host_vector(host_vector &&v)
:Parent(std::move(v)) {}
/*! Move constructor moves from another host_vector.
* \param v The host_vector to move.
* \param alloc The allocator to use by this host_vector.
*/
__host__
host_vector(host_vector &&v, const Alloc &alloc)
:Parent(std::move(v),alloc) {}
#endif
/*! Assign operator copies from an exemplar \p host_vector.
* \param v The \p host_vector to copy.
*/
__host__
host_vector &operator=(const host_vector &v)
{ Parent::operator=(v); return *this; }
#if THRUST_CPP_DIALECT >= 2011
/*! Move assign operator moves from another host_vector.
* \param v The host_vector to move.
*/
__host__
host_vector &operator=(host_vector &&v)
{ Parent::operator=(std::move(v)); return *this; }
#endif
/*! Copy constructor copies from an exemplar \p host_vector with different type.
* \param v The \p host_vector to copy.
*/
template<typename OtherT, typename OtherAlloc>
__host__
host_vector(const host_vector<OtherT,OtherAlloc> &v)
:Parent(v) {}
/*! Assign operator copies from an exemplar \p host_vector with different type.
* \param v The \p host_vector to copy.
*/
template<typename OtherT, typename OtherAlloc>
__host__
host_vector &operator=(const host_vector<OtherT,OtherAlloc> &v)
{ Parent::operator=(v); return *this; }
/*! Copy constructor copies from an exemplar <tt>std::vector</tt>.
* \param v The <tt>std::vector</tt> to copy.
*/
template<typename OtherT, typename OtherAlloc>
__host__
host_vector(const std::vector<OtherT,OtherAlloc> &v)
:Parent(v) {}
/*! Assign operator copies from an exemplar <tt>std::vector</tt>.
* \param v The <tt>std::vector</tt> to copy.
*/
template<typename OtherT, typename OtherAlloc>
__host__
host_vector &operator=(const std::vector<OtherT,OtherAlloc> &v)
{ Parent::operator=(v); return *this;}
/*! Copy construct from a \p vector_base whose element type is convertible
* to \c T.
*
* \param v The \p vector_base to copy.
*/
template<typename OtherT, typename OtherAlloc>
__host__
host_vector(const detail::vector_base<OtherT,OtherAlloc> &v)
:Parent(v) {}
/*! Assign a \p vector_base whose element type is convertible to \c T.
*
* \param v The \p vector_base to copy.
*/
template<typename OtherT, typename OtherAlloc>
__host__
host_vector &operator=(const detail::vector_base<OtherT,OtherAlloc> &v)
{ Parent::operator=(v); return *this; }
/*! This constructor builds a \p host_vector from an intializer_list.
* \param il The intializer_list.
*/
host_vector(std::initializer_list<T> il)
:Parent(il) {}
/*! This constructor builds a \p host_vector from an intializer_list.
* \param il The intializer_list.
* \param alloc The allocator to use by this host_vector.
*/
host_vector(std::initializer_list<T> il, const Alloc &alloc)
:Parent(il, alloc) {}
/*! Assign an \p intializer_list with a matching element type
* \param il The intializer_list.
*/
host_vector &operator=(std::initializer_list<T> il)
{ Parent::operator=(il); return *this; }
/*! This constructor builds a \p host_vector from a range.
* \param first The beginning of the range.
* \param last The end of the range.
*/
template<typename InputIterator>
__host__
host_vector(InputIterator first, InputIterator last)
:Parent(first, last) {}
/*! This constructor builds a \p host_vector from a range.
* \param first The beginning of the range.
* \param last The end of the range.
* \param alloc The allocator to use by this host_vector.
*/
template<typename InputIterator>
__host__
host_vector(InputIterator first, InputIterator last, const Alloc &alloc)
:Parent(first, last, alloc) {}
// declare these members for the purpose of Doxygenating them
// they actually exist in a derived-from class
#if 0
/*! \brief Resizes this vector to the specified number of elements.
* \param new_size Number of elements this vector should contain.
* \param x Data with which new elements should be populated.
* \throw std::length_error If n exceeds max_size().
*
* This method will resize this vector to the specified number of
* elements. If the number is smaller than this vector's current
* size this vector is truncated, otherwise this vector is
* extended and new elements are populated with given data.
*/
void resize(size_type new_size, const value_type &x = value_type());
/*! Returns the number of elements in this vector.
*/
size_type size(void) const;
/*! Returns the size() of the largest possible vector.
* \return The largest possible return value of size().
*/
size_type max_size(void) const;
/*! \brief If n is less than or equal to capacity(), this call has no effect.
* Otherwise, this method is a request for allocation of additional memory. If
* the request is successful, then capacity() is greater than or equal to
* n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
* \throw std::length_error If n exceeds max_size().
*/
void reserve(size_type n);
/*! Returns the number of elements which have been reserved in this
* vector.
*/
size_type capacity(void) const;
/*! This method shrinks the capacity of this vector to exactly
* fit its elements.
*/
void shrink_to_fit(void);
/*! \brief Subscript access to the data contained in this vector_dev.
* \param n The index of the element for which data should be accessed.
* \return Read/write reference to data.
*
* This operator allows for easy, array-style, data access.
* Note that data access with this operator is unchecked and
* out_of_range lookups are not defined.
*/
reference operator[](size_type n);
/*! \brief Subscript read access to the data contained in this vector_dev.
* \param n The index of the element for which data should be accessed.
* \return Read reference to data.
*
* This operator allows for easy, array-style, data access.
* Note that data access with this operator is unchecked and
* out_of_range lookups are not defined.
*/
const_reference operator[](size_type n) const;
/*! This method returns an iterator pointing to the beginning of
* this vector.
* \return mStart
*/
iterator begin(void);
/*! This method returns a const_iterator pointing to the beginning
* of this vector.
* \return mStart
*/
const_iterator begin(void) const;
/*! This method returns a const_iterator pointing to the beginning
* of this vector.
* \return mStart
*/
const_iterator cbegin(void) const;
/*! This method returns a reverse_iterator pointing to the beginning of
* this vector's reversed sequence.
* \return A reverse_iterator pointing to the beginning of this
* vector's reversed sequence.
*/
reverse_iterator rbegin(void);
/*! This method returns a const_reverse_iterator pointing to the beginning of
* this vector's reversed sequence.
* \return A const_reverse_iterator pointing to the beginning of this
* vector's reversed sequence.
*/
const_reverse_iterator rbegin(void) const;
/*! This method returns a const_reverse_iterator pointing to the beginning of
* this vector's reversed sequence.
* \return A const_reverse_iterator pointing to the beginning of this
* vector's reversed sequence.
*/
const_reverse_iterator crbegin(void) const;
/*! This method returns an iterator pointing to one element past the
* last of this vector.
* \return begin() + size().
*/
iterator end(void);
/*! This method returns a const_iterator pointing to one element past the
* last of this vector.
* \return begin() + size().
*/
const_iterator end(void) const;
/*! This method returns a const_iterator pointing to one element past the
* last of this vector.
* \return begin() + size().
*/
const_iterator cend(void) const;
/*! This method returns a reverse_iterator pointing to one element past the
* last of this vector's reversed sequence.
* \return rbegin() + size().
*/
reverse_iterator rend(void);
/*! This method returns a const_reverse_iterator pointing to one element past the
* last of this vector's reversed sequence.
* \return rbegin() + size().
*/
const_reverse_iterator rend(void) const;
/*! This method returns a const_reverse_iterator pointing to one element past the
* last of this vector's reversed sequence.
* \return rbegin() + size().
*/
const_reverse_iterator crend(void) const;
/*! This method returns a const_reference referring to the first element of this
* vector.
* \return The first element of this vector.
*/
const_reference front(void) const;
/*! This method returns a reference pointing to the first element of this
* vector.
* \return The first element of this vector.
*/
reference front(void);
/*! This method returns a const reference pointing to the last element of
* this vector.
* \return The last element of this vector.
*/
const_reference back(void) const;
/*! This method returns a reference referring to the last element of
* this vector_dev.
* \return The last element of this vector.
*/
reference back(void);
/*! This method returns a pointer to this vector's first element.
* \return A pointer to the first element of this vector.
*/
pointer data(void);
/*! This method returns a const_pointer to this vector's first element.
* \return a const_pointer to the first element of this vector.
*/
const_pointer data(void) const;
/*! This method resizes this vector to 0.
*/
void clear(void);
/*! This method returns true iff size() == 0.
* \return true if size() == 0; false, otherwise.
*/
bool empty(void) const;
/*! This method appends the given element to the end of this vector.
* \param x The element to append.
*/
void push_back(const value_type &x);
/*! This method erases the last element of this vector, invalidating
* all iterators and references to it.
*/
void pop_back(void);
/*! This method swaps the contents of this host_vector with another vector.
* \param v The vector with which to swap.
*/
void swap(host_vector &v);
/*! This method removes the element at position pos.
* \param pos The position of the element of interest.
* \return An iterator pointing to the new location of the element that followed the element
* at position pos.
*/
iterator erase(iterator pos);
/*! This method removes the range of elements [first,last) from this vector.
* \param first The beginning of the range of elements to remove.
* \param last The end of the range of elements to remove.
* \return An iterator pointing to the new location of the element that followed the last
* element in the sequence [first,last).
*/
iterator erase(iterator first, iterator last);
/*! This method inserts a single copy of a given exemplar value at the
* specified position in this vector.
* \param position The insertion position.
* \param x The exemplar element to copy & insert.
* \return An iterator pointing to the newly inserted element.
*/
iterator insert(iterator position, const T &x);
/*! This method inserts a copy of an exemplar value to a range at the
* specified position in this vector.
* \param position The insertion position
* \param n The number of insertions to perform.
* \param x The value to replicate and insert.
*/
void insert(iterator position, size_type n, const T &x);
/*! This method inserts a copy of an input range at the specified position
* in this vector.
* \param position The insertion position.
* \param first The beginning of the range to copy.
* \param last The end of the range to copy.
*
* \tparam InputIterator is a model of <a href="https://en.cppreference.com/w/cpp/iterator/input_iterator>Input Iterator</a>,
* and \p InputIterator's \c value_type is a model of <a href="https://en.cppreference.com/w/cpp/named_req/CopyAssignable">Assignable</a>.
*/
template<typename InputIterator>
void insert(iterator position, InputIterator first, InputIterator last);
/*! This version of \p assign replicates a given exemplar
* \p n times into this vector.
* \param n The number of times to copy \p x.
* \param x The exemplar element to replicate.
*/
void assign(size_type n, const T &x);
/*! This version of \p assign makes this vector a copy of a given input range.
* \param first The beginning of the range to copy.
* \param last The end of the range to copy.
*
* \tparam InputIterator is a model of <a href="https://en.cppreference.com/w/cpp/named_req/InputIterator">Input Iterator</a>.
*/
template<typename InputIterator>
void assign(InputIterator first, InputIterator last);
/*! This method returns a copy of this vector's allocator.
* \return A copy of the alloctor used by this vector.
*/
allocator_type get_allocator(void) const;
#endif // end doxygen-only members
};
/*! Exchanges the values of two vectors.
* \p x The first \p host_vector of interest.
* \p y The second \p host_vector of interest.
*/
template<typename T, typename Alloc>
void swap(host_vector<T,Alloc> &a, host_vector<T,Alloc> &b)
{
a.swap(b);
}
/*! \}
*/
THRUST_NAMESPACE_END
|