27 # define TRACE( lvl, ... )
28 # define UNDEF_TRACE_AT_END
39 static inline void* QV_MEMALIGN(
size_t boundary,
size_t size)
42 posix_memalign(&retadr, boundary, size);
46 #ifndef QUICKVEC_DO_TEMPLATE
47 # define QUICKVEC_DO_TEMPLATE 1
51 #if !defined(__GCCXML__) && defined(__GXX_EXPERIMENTAL_CXX0X__)
52 # define NOT_OLD_CXXSTD 1
55 #if QUICKVEC_DO_TEMPLATE == 0
57 # define QUICKVEC_TT unsigned long long
59 # define TT_ QUICKVEC_TT
60 # define QUICKVEC_TEMPLATE
61 # define QUICKVEC QuickVec
62 # define QUICKVEC_TN QuickVec
63 # define QUICKVEC_VERSION
65 # define QUICKVEC_TEMPLATE template <typename TT_>
66 # define QUICKVEC QuickVec<TT_>
67 # define QUICKVEC_TN typename QuickVec<TT_>
68 # define QUICKVEC_VERSION \
75 static short Class_Version() { return 5; } // proper version for templates
119 : size_(other.
size())
120 , data_((TT_*)QV_MEMALIGN(QV_ALIGN, other.
capacity() * sizeof(TT_)))
123 TRACE(10,
"QuickVec std::vector ctor b4 memcpy this=%p data_=%p &other[0]=%p size_=%d other.size()=%d"
124 , (
void*)
this, (
void*)data_, (
void*)&other[0], size_, other.size());
125 memcpy( data_, (
void*)&other[0], size_ *
sizeof(TT_));
140 , data_((TT_*)QV_MEMALIGN(QV_ALIGN, other.
capacity() * sizeof(TT_)))
141 , capacity_(other.capacity_)
143 TRACE(10,
"QuickVec copy ctor b4 memcpy this=%p data_=%p other.data_=%p size_=%d other.size_=%d"
144 , (
void*)
this, (
void*)data_, (
void*)other.data_, size_, other.size_);
145 memcpy( data_, other.data_, size_ *
sizeof(TT_));
155 TRACE(10,
"QuickVec copy assign b4 resize/memcpy this=%p data_=%p other.data_=%p size_=%d other.size_=%d"
156 , (
void*)
this, (
void*)data_, (
void*)other.data_, size_, other.size_);
158 memcpy( data_, other.data_, size_ *
sizeof(TT_));
168 , data_(std::move(other.data_))
169 , capacity_(other.capacity_)
171 TRACE(10,
"QuickVec move ctor this=%p data_=%p other.data_=%p"
172 , (
void*)
this, (
void*)data_, (
void*)other.data_ );
173 other.data_ =
nullptr;
183 TRACE(10,
"QuickVec move assign this=%p data_=%p other.data_=%p"
184 , (
void*)
this, (
void*)data_, (
void*)other.data_ );
188 data_ = std::move(other.data_);
189 capacity_ = other.capacity_;
190 other.data_ =
nullptr;
338 inline QUICKVEC::QuickVec(
size_t sz)
340 , data_((TT_*)QV_MEMALIGN(QV_ALIGN, sz * sizeof(TT_)))
343 TRACE(15,
"QuickVec %p ctor sz=%d data_=%p", (
void*)
this, size_, (
void*)data_ );
347 inline QUICKVEC::QuickVec(
size_t sz, TT_ val)
349 , data_((TT_*)QV_MEMALIGN(QV_ALIGN, sz * sizeof(TT_)))
352 TRACE(15,
"QuickVec %p ctor sz=%d/v data_=%p", (
void*)
this, size_, (
void*)data_ );
358 inline QUICKVEC::~QuickVec() noexcept
360 TRACE(15,
"QuickVec %p dtor start data_=%p size_=%d"
361 , (
void*)
this, (
void*)data_, size_);
363 TRACE(15,
"QuickVec %p dtor return", (
void*)
this);
367 inline TT_& QUICKVEC::operator[](
int idx)
369 assert(idx < (
int)size_);
374 inline const TT_& QUICKVEC::operator[](
int idx)
const
376 assert(idx < (
int)size_);
381 inline size_t QUICKVEC::size()
const {
return size_; }
384 inline size_t QUICKVEC::capacity()
const {
return capacity_; }
387 inline QUICKVEC_TN::iterator QUICKVEC::begin() {
return iterator(data_); }
390 inline QUICKVEC_TN::const_iterator QUICKVEC::begin()
const {
return iterator(data_); }
393 inline QUICKVEC_TN::iterator QUICKVEC::end() {
return iterator(data_+size_); }
396 inline QUICKVEC_TN::const_iterator QUICKVEC::end()
const {
return const_iterator(data_+size_); }
399 inline void QUICKVEC::reserve(
size_t size)
401 if (size > capacity_)
405 data_ = (TT_*)QV_MEMALIGN(QV_ALIGN, size *
sizeof(TT_));
406 memcpy(data_, old, size_ *
sizeof(TT_));
407 TRACE(13,
"QUICKVEC::reserve after memcpy this=%p old=%p data_=%p"
408 , (
void*)
this, (
void*)old, (
void*)data_);
415 inline void QUICKVEC::resize(
size_t size)
417 if (size < size_) size_ = size;
418 else if (size <= capacity_) size_ = size;
422 data_ = (TT_*)QV_MEMALIGN(QV_ALIGN, size *
sizeof(TT_));
423 memcpy(data_, old, size_ *
sizeof(TT_));
424 TRACE(13,
"QUICKVEC::resize after memcpy this=%p old=%p data_=%p"
425 , (
void*)
this, (
void*)old, (
void*)data_);
427 size_ = capacity_ = size;
436 if (size > old_size) {
437 TRACE( 13,
"QUICKVEC::resize initializing %zu elements", size-old_size );
438 for (
iterator ii = begin() + old_size; ii != end(); ++ii) *ii = val;
447 assert(position <= end());
448 size_t offset = position - begin();
453 size_t cnt = end() - (begin() + offset);
454 while (cnt--) *--dst = *--src;
456 dst = begin() + offset;
458 while (nn--) *dst++ = val;
459 return begin() + offset;
467 assert(position <= end());
468 size_t nn = (last - first);
469 size_t offset = position - begin();
474 size_t cnt = end() - (begin() + offset);
475 while (cnt--) *--dst = *--src;
477 dst = begin() + offset;
479 while (nn--) *dst++ = *first++;
480 return begin() + offset;
487 assert(last <= end());
488 size_t nn = (last - first);
489 size_t offset = first - begin();
493 size_t cnt = end() - src;
494 while (cnt--) *dst++ = *src++;
497 return begin() + offset;
503 TRACE(12,
"QUICKVEC::swap this=%p enter data_=%p x.data_=%p"
504 , (
void*)
this, (
void*)data_, (
void*)x.data_ );
505 std::swap(data_, x.data_);
506 std::swap(size_, x.size_);
507 std::swap(capacity_, x.capacity_);
508 TRACE(12,
"QUICKVEC::swap return data_=%p x.data_=%p"
509 , (
void*)data_, (
void*)x.data_ );
515 if (size_ == capacity_)
517 reserve(size_ + size_ / 10 + 1);
525 #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
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.
TT_ & reference
reference is reference-to-member tpye
QuickVec(const QuickVec &other)
Copy Constructor.