27 # define TRACE( lvl, ... )
28 # define UNDEF_TRACE_AT_END
31 #define USE_UNIQUE_PTR 0
40 static inline void* QV_MEMALIGN(
size_t boundary,
size_t size)
43 posix_memalign(&retadr, boundary, size);
47 #ifndef QUICKVEC_DO_TEMPLATE
48 # define QUICKVEC_DO_TEMPLATE 1
52 #if !defined(__GCCXML__) && defined(__GXX_EXPERIMENTAL_CXX0X__)
53 # define NOT_OLD_CXXSTD 1
56 #if QUICKVEC_DO_TEMPLATE == 0
58 # define QUICKVEC_TT unsigned long long
60 # define TT_ QUICKVEC_TT
61 # define QUICKVEC_TEMPLATE
62 # define QUICKVEC QuickVec
63 # define QUICKVEC_TN QuickVec
64 # define QUICKVEC_VERSION
66 # define QUICKVEC_TEMPLATE template <typename TT_>
67 # define QUICKVEC QuickVec<TT_>
68 # define QUICKVEC_TN typename QuickVec<TT_>
69 # define QUICKVEC_VERSION \
76 static short Class_Version() { return 5; } // proper version for templates
109 # if USE_UNIQUE_PTR == 0
117 # define PTR_(xx) xx.get()
125 : size_(other.
size())
126 , data_((TT_*)QV_MEMALIGN(QV_ALIGN, other.
capacity() * sizeof(TT_)))
129 memcpy(PTR_(data_), (
void*)&other[0], size_ *
sizeof(TT_));
144 , data_((TT_*)QV_MEMALIGN(QV_ALIGN, other.
capacity() * sizeof(TT_)))
145 , capacity_(other.capacity_)
147 TRACE(10,
"QuickVec copy ctor this=%p data_=%p other.data_=%p size_=%d other.size_=%d"
148 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_), size_, other.size_);
149 memcpy(PTR_(data_), PTR_(other.data_), size_ *
sizeof(TT_));
159 TRACE(10,
"QuickVec copy assign this=%p data_=%p other.data_=%p size_=%d other.size_=%d"
160 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_), size_, other.size_);
162 memcpy(PTR_(data_), PTR_(other.data_), size_ *
sizeof(TT_));
172 , data_(std::move(other.data_))
173 , capacity_(other.capacity_)
175 TRACE(10,
"QuickVec move ctor this=%p data_=%p other.data_=%p"
176 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_));
177 # if USE_UNIQUE_PTR == 0
178 other.data_ =
nullptr;
189 TRACE(10,
"QuickVec move assign this=%p data_=%p other.data_=%p"
190 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_));
194 data_ = std::move(other.data_);
195 capacity_ = other.capacity_;
196 # if USE_UNIQUE_PTR == 0
197 other.data_ =
nullptr;
341 # if USE_UNIQUE_PTR == 0
344 std::unique_ptr<TT_[]> data_;
350 inline QUICKVEC::QuickVec(
size_t sz)
353 , data_((TT_*)QV_MEMALIGN(QV_ALIGN, sz * sizeof(TT_)))
356 TRACE(15,
"QuickVec %p ctor sz=%d data_=%p", (
void*)
this, size_, (
void*)PTR_(data_));
360 inline QUICKVEC::QuickVec(
size_t sz, TT_ val)
363 , data_((TT_*)QV_MEMALIGN(QV_ALIGN, sz * sizeof(TT_)))
366 TRACE(15,
"QuickVec %p ctor sz=%d/v data_=%p", (
void*)
this, size_, (
void*)PTR_(data_));
371 #if USE_UNIQUE_PTR == 0
373 inline QUICKVEC::~QuickVec() noexcept
375 TRACE(15,
"QuickVec %p dtor start data_=%p size_=%d"
376 , (
void*)
this, (
void*)PTR_(data_), size_);
379 TRACE(15,
"QuickVec %p dtor return", (
void*)
this);
384 inline TT_& QUICKVEC::operator[](
int idx)
386 assert(idx < (
int)size_);
391 inline const TT_& QUICKVEC::operator[](
int idx)
const
393 assert(idx < (
int)size_);
398 inline size_t QUICKVEC::size()
const {
return size_; }
401 inline size_t QUICKVEC::capacity()
const {
return capacity_; }
404 inline QUICKVEC_TN::iterator QUICKVEC::begin() {
return iterator(PTR_(data_)); }
407 inline QUICKVEC_TN::const_iterator QUICKVEC::begin()
const {
return iterator(PTR_(data_)); }
410 inline QUICKVEC_TN::iterator QUICKVEC::end() {
return iterator(PTR_(data_) + size_); }
413 inline QUICKVEC_TN::const_iterator QUICKVEC::end()
const {
return const_iterator(PTR_(data_) + size_); }
416 inline void QUICKVEC::reserve(
size_t size)
418 if (size > capacity_)
420 # if USE_UNIQUE_PTR == 0
423 data_ = (TT_*)QV_MEMALIGN(QV_ALIGN, size *
sizeof(TT_));
424 memcpy(data_, old, size_ *
sizeof(TT_));
425 TRACE(13,
"QUICKVEC::reserve this=%p old=%p data_=%p"
426 , (
void*)
this, (
void*)old, (
void*)data_);
430 std::unique_ptr<TT_[]> old = std::move(data_);
432 data_ = std::unique_ptr<TT_[]>((TT_*)QV_MEMALIGN(QV_ALIGN, size *
sizeof(TT_)));
433 memcpy(data_.get(), old.get(), size_ *
sizeof(TT_));
441 inline void QUICKVEC::resize(
size_t size)
443 if (size < size_) size_ = size;
444 else if (size <= capacity_) size_ = size;
447 # if USE_UNIQUE_PTR == 0
450 data_ = (TT_*)QV_MEMALIGN(QV_ALIGN, size *
sizeof(TT_));
451 memcpy(data_, old, size_ *
sizeof(TT_));
452 TRACE(13,
"QUICKVEC::resize this=%p old=%p data_=%p"
453 , (
void*)
this, (
void*)old, (
void*)data_);
457 std::unique_ptr<TT_[]> old = std::move(data_);
459 data_ = std::unique_ptr<TT_[]>((TT_*)QV_MEMALIGN(QV_ALIGN, size *
sizeof(TT_)));
460 memcpy(data_.get(), old.get(), size_ *
sizeof(TT_));
462 size_ = capacity_ = size;
473 for (
iterator ii = begin() + old_size; ii != end(); ++ii) *ii = val;
481 assert(position <= end());
482 size_t offset = position - begin();
487 size_t cnt = end() - (begin() + offset);
488 while (cnt--) *--dst = *--src;
490 dst = begin() + offset;
492 while (nn--) *dst++ = val;
493 return begin() + offset;
501 assert(position <= end());
502 size_t nn = (last - first);
503 size_t offset = position - begin();
508 size_t cnt = end() - (begin() + offset);
509 while (cnt--) *--dst = *--src;
511 dst = begin() + offset;
513 while (nn--) *dst++ = *first++;
514 return begin() + offset;
521 assert(last <= end());
522 size_t nn = (last - first);
523 size_t offset = first - begin();
527 size_t cnt = end() - src;
528 while (cnt--) *dst++ = *src++;
531 return begin() + offset;
537 TRACE(12,
"QUICKVEC::swap this=%p enter data_=%p x.data_=%p"
538 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(x.data_));
539 std::swap(data_, x.data_);
540 std::swap(size_, x.size_);
541 std::swap(capacity_, x.capacity_);
542 TRACE(12,
"QUICKVEC::swap return data_=%p x.data_=%p"
543 , (
void*)PTR_(data_), (
void*)PTR_(x.data_));
549 if (size_ == capacity_)
551 reserve(size_ + size_ / 10 + 1);
559 #ifdef UNDEF_TRACE_AT_END
iterator erase(const_iterator first, const_iterator last)
Erases elements in given range from the QuickVec.
iterator end()
Gets an iterator to the end of the QuickVec.
void clear()
Sets the size to 0. QuickVec does not reinitialize memory, so no further action will be taken...
void reserve(size_t size)
Allocates memory for the QuickVec so that its capacity is at least size.
void resize(size_t size)
Resizes the QuickVec.
TT_ & operator[](int idx)
Returns a reference to a given element.
size_t size_type
size_type is size_t
QuickVec(std::vector< TT_ > &other)
Copies the contents of a std::vector into a new QuickVec object.
TT_ value_type
value_type is member type
size_t size() const
Accesses the current size of the QuickVec.
TT_ * iterator
Iterator is pointer-to-member type.
ptrdiff_t difference_type
difference_type is ptrdiff_t
size_t capacity() const
Accesses the current capacity of the QuickVec.
void swap(QuickVec &other) noexcept
Exchanges references to two QuickVec objects.
iterator insert(const_iterator position, size_t nn, const TT_ &val)
Inserts an element into the QuickVec.
void push_back(const value_type &val)
Adds a value to the QuickVec, resizing if necessary (adds 10% capacity)
A QuickVec behaves like a std::vector, but does no initialization of its data, making it faster at th...
QuickVec(size_t sz)
Allocates a QuickVec object, doing no initialization of allocated memory.
const TT_ * const_iterator
const_iterator is const-pointer-to-member type
const TT_ & const_reference
const_reference is const-reference-to-member type
iterator begin()
Gets an iterator to the beginning of the QuickVec.
QuickVec< TT_ > & operator=(const QuickVec &other)
Copy assignment operator.
virtual ~QuickVec() noexcept
Destructor calls free on data. QuickVec compiled in non-unique_ptr mode.
TT_ & reference
reference is reference-to-member tpye
QuickVec(const QuickVec &other)
Copy Constructor.