25 # define TRACE( lvl, ... )
26 # define UNDEF_TRACE_AT_END
29 #define USE_UNIQUE_PTR 0
31 static inline void *QV_MEMALIGN(
size_t boundary,
size_t size) {
void *retadr; posix_memalign(&retadr, boundary, size);
return retadr; }
33 #ifndef QUICKVEC_DO_TEMPLATE
34 # define QUICKVEC_DO_TEMPLATE 1
38 #if !defined(__GCCXML__) && defined(__GXX_EXPERIMENTAL_CXX0X__)
39 # define NOT_OLD_CXXSTD 1
42 #if QUICKVEC_DO_TEMPLATE == 0
44 # define QUICKVEC_TT unsigned long long
46 # define TT_ QUICKVEC_TT
47 # define QUICKVEC_TEMPLATE
48 # define QUICKVEC QuickVec
49 # define QUICKVEC_TN QuickVec
50 # define QUICKVEC_VERSION
52 # define QUICKVEC_TEMPLATE template <typename TT_>
53 # define QUICKVEC QuickVec<TT_>
54 # define QUICKVEC_TN typename QuickVec<TT_>
56 # define QUICKVEC_VERSION static short Class_Version() { return 5; } // proper version for templates
64 typedef TT_* iterator;
65 typedef const TT_* const_iterator;
66 typedef TT_& reference;
67 typedef const TT_& const_reference;
68 typedef TT_ value_type;
69 typedef ptrdiff_t difference_type;
70 typedef size_t size_type;
74 # if USE_UNIQUE_PTR == 0
78 # define PTR_(xx) xx.get()
82 : size_(other.size()), data_((TT_*)QV_MEMALIGN(QV_ALIGN, other.capacity() *
sizeof(TT_))), capacity_(other.capacity())
84 memcpy(PTR_(data_), (
void*)&other[0], size_ *
sizeof(TT_));
86 void clear() { size_ = 0; }
90 : size_(other.size_), data_((TT_*)QV_MEMALIGN(QV_ALIGN, other.capacity() *
sizeof(TT_))), capacity_(other.capacity_)
92 TRACE(10,
"QuickVec copy ctor this=%p data_=%p other.data_=%p size_=%d other.size_=%d"
93 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_), size_, other.size_);
94 memcpy(PTR_(data_), PTR_(other.data_), size_ *
sizeof(TT_));
96 QUICKVEC & operator=(
const QuickVec & other)
98 TRACE(10,
"QuickVec copy assign this=%p data_=%p other.data_=%p size_=%d other.size_=%d"
99 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_), size_, other.size_);
101 memcpy(PTR_(data_), PTR_(other.data_), size_ *
sizeof(TT_));
106 : size_(other.size_), data_(std::move(other.data_)), capacity_(other.capacity_)
108 TRACE(10,
"QuickVec move ctor this=%p data_=%p other.data_=%p"
109 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_));
110 # if USE_UNIQUE_PTR == 0
111 other.data_ =
nullptr;
114 QUICKVEC & operator=(
QuickVec && other) noexcept
116 TRACE(10,
"QuickVec move assign this=%p data_=%p other.data_=%p"
117 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_));
121 data_ = std::move(other.data_);
122 capacity_ = other.capacity_;
123 # if USE_UNIQUE_PTR == 0
124 other.data_ =
nullptr;
130 TT_& operator[](
int idx);
131 const TT_& operator[](
int idx)
const;
133 size_t capacity()
const;
135 const_iterator begin()
const;
137 const_iterator end()
const;
138 void reserve(
size_t size);
139 void resize(
size_t size);
140 void resize(
size_t size, TT_ val);
141 iterator insert(const_iterator position,
size_t nn,
const TT_& val);
142 iterator insert(const_iterator position, const_iterator first
143 , const_iterator last);
144 iterator erase(const_iterator first, const_iterator last);
146 void push_back(
const value_type& val);
155 # if USE_UNIQUE_PTR == 0
158 std::unique_ptr<TT_[]> data_;
164 inline QUICKVEC::QuickVec(
size_t sz)
166 : size_(sz), data_((TT_*)QV_MEMALIGN(QV_ALIGN, sz * sizeof(TT_))), capacity_(sz)
168 TRACE(15,
"QuickVec %p ctor sz=%d data_=%p", (
void*)
this, size_, (
void*)PTR_(data_));
171 inline QUICKVEC::QuickVec(
size_t sz, TT_ val)
173 : size_(sz), data_((TT_*)QV_MEMALIGN(QV_ALIGN, sz * sizeof(TT_))), capacity_(sz)
175 TRACE(15,
"QuickVec %p ctor sz=%d/v data_=%p", (
void*)
this, size_, (
void*)PTR_(data_));
176 for (iterator ii = begin(); ii != end(); ++ii) *ii = val;
180 #if USE_UNIQUE_PTR == 0
182 inline QUICKVEC::~QuickVec() noexcept
184 TRACE(15,
"QuickVec %p dtor start data_=%p size_=%d"
185 , (
void*)
this, (
void*)PTR_(data_), size_);
188 TRACE(15,
"QuickVec %p dtor return", (
void*)
this);
193 inline TT_& QUICKVEC::operator[](
int idx)
195 assert(idx < (
int)size_);
return data_[idx];
199 inline const TT_& QUICKVEC::operator[](
int idx)
const
201 assert(idx < (
int)size_);
206 inline size_t QUICKVEC::size()
const {
return size_; }
208 inline size_t QUICKVEC::capacity()
const {
return capacity_; }
211 inline QUICKVEC_TN::iterator QUICKVEC::begin() {
return iterator(PTR_(data_)); }
213 inline QUICKVEC_TN::const_iterator QUICKVEC::begin()
const {
return iterator(PTR_(data_)); }
215 inline QUICKVEC_TN::iterator QUICKVEC::end() {
return iterator(PTR_(data_) + size_); }
217 inline QUICKVEC_TN::const_iterator QUICKVEC::end()
const {
return const_iterator(PTR_(data_) + size_); }
220 inline void QUICKVEC::reserve(
size_t size)
222 if (size > capacity_)
224 # if USE_UNIQUE_PTR == 0
227 data_ = (TT_*)QV_MEMALIGN(QV_ALIGN, size *
sizeof(TT_));
228 memcpy(data_, old, size_ *
sizeof(TT_));
229 TRACE(13,
"QUICKVEC::reserve this=%p old=%p data_=%p"
230 , (
void*)
this, (
void*)old, (
void*)data_);
234 std::unique_ptr<TT_[]> old = std::move(data_);
236 data_ = std::unique_ptr<TT_[]>((TT_*)QV_MEMALIGN(QV_ALIGN, size *
sizeof(TT_)));
237 memcpy(data_.get(), old.get(), size_ *
sizeof(TT_));
245 inline void QUICKVEC::resize(
size_t size)
247 if (size < size_) size_ = size;
248 else if (size <= capacity_) size_ = size;
251 # if USE_UNIQUE_PTR == 0
254 data_ = (TT_*)QV_MEMALIGN(QV_ALIGN, size *
sizeof(TT_));
255 memcpy(data_, old, size_ *
sizeof(TT_));
256 TRACE(13,
"QUICKVEC::resize this=%p old=%p data_=%p"
257 , (
void*)
this, (
void*)old, (
void*)data_);
261 std::unique_ptr<TT_[]> old = std::move(data_);
263 data_ = std::unique_ptr<TT_[]>((TT_*)QV_MEMALIGN(QV_ALIGN, size *
sizeof(TT_)));
264 memcpy(data_.get(), old.get(), size_ *
sizeof(TT_));
266 size_ = capacity_ = size;
271 inline void QUICKVEC::resize(size_type size, TT_ val)
273 size_type old_size = size;
276 for (iterator ii = begin() + old_size; ii != end(); ++ii) *ii = val;
280 inline QUICKVEC_TN::iterator QUICKVEC::insert(const_iterator position
284 assert(position <= end());
285 size_t offset = position - begin();
288 iterator dst = end() + nn;
289 iterator src = end();
290 size_t cnt = end() - (begin() + offset);
291 while (cnt--) *--dst = *--src;
293 dst = begin() + offset;
295 while (nn--) *dst++ = val;
296 return begin() + offset;
299 inline QUICKVEC_TN::iterator QUICKVEC::insert(const_iterator position
300 , const_iterator first
301 , const_iterator last)
303 assert(position <= end());
304 size_t nn = (last - first);
305 size_t offset = position - begin();
308 iterator dst = end() + nn;
309 iterator src = end();
310 size_t cnt = end() - (begin() + offset);
311 while (cnt--) *--dst = *--src;
313 dst = begin() + offset;
315 while (nn--) *dst++ = *first++;
316 return begin() + offset;
320 inline QUICKVEC_TN::iterator QUICKVEC::erase(const_iterator first
321 , const_iterator last)
323 assert(last <= end());
324 size_t nn = (last - first);
325 size_t offset = first - begin();
327 iterator dst = begin() + offset;
328 iterator src = dst + nn;
329 size_t cnt = end() - src;
330 while (cnt--) *dst++ = *src++;
333 return begin() + offset;
337 inline void QUICKVEC::swap(QuickVec& x) noexcept
339 TRACE(12,
"QUICKVEC::swap this=%p enter data_=%p x.data_=%p"
340 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(x.data_));
341 std::swap(data_, x.data_);
342 std::swap(size_, x.size_);
343 std::swap(capacity_, x.capacity_);
344 TRACE(12,
"QUICKVEC::swap return data_=%p x.data_=%p"
345 , (
void*)PTR_(data_), (
void*)PTR_(x.data_));
349 inline void QUICKVEC::push_back(
const value_type& val)
351 if (size_ == capacity_)
353 reserve(size_ + size_ / 10 + 1);
361 #ifdef UNDEF_TRACE_AT_END