26 # define TRACE( lvl, ... )
27 # define UNDEF_TRACE_AT_END
30 #define USE_UNIQUE_PTR 0
32 static inline void *QV_MEMALIGN(
size_t boundary,
size_t size ) {
void *retadr; posix_memalign(&retadr,boundary,size);
return retadr;}
34 #ifndef QUICKVEC_DO_TEMPLATE
35 # define QUICKVEC_DO_TEMPLATE 1
39 #if !defined(__GCCXML__) && defined(__GXX_EXPERIMENTAL_CXX0X__)
40 # define NOT_OLD_CXXSTD 1
43 #if QUICKVEC_DO_TEMPLATE == 0
45 # define QUICKVEC_TT unsigned long long
47 # define TT_ QUICKVEC_TT
48 # define QUICKVEC_TEMPLATE
49 # define QUICKVEC QuickVec
50 # define QUICKVEC_TN QuickVec
51 # define QUICKVEC_VERSION
53 # define QUICKVEC_TEMPLATE template <typename TT_>
54 # define QUICKVEC QuickVec<TT_>
55 # define QUICKVEC_TN typename QuickVec<TT_>
57 # define QUICKVEC_VERSION static short Class_Version() { return 5; } // proper version for templates
63 typedef TT_* iterator;
64 typedef const TT_* const_iterator;
65 typedef TT_& reference;
66 typedef const TT_& const_reference;
67 typedef TT_ value_type;
68 typedef ptrdiff_t difference_type;
69 typedef size_t size_type;
73 # if USE_UNIQUE_PTR == 0
77 # define PTR_(xx) xx.get()
81 : size_(other.size()), data_((TT_*)QV_MEMALIGN(QV_ALIGN,other.capacity()*
sizeof(TT_))), capacity_(other.capacity())
82 { memcpy( PTR_(data_), (
void*)&other[0], size_*
sizeof(TT_) );
84 void clear() { size_=0; }
88 : size_(other.size_), data_((TT_*)QV_MEMALIGN(QV_ALIGN,other.capacity()*
sizeof(TT_))), capacity_(other.capacity_)
89 { TRACE( 10,
"QuickVec copy ctor this=%p data_=%p other.data_=%p size_=%d other.size_=%d"
90 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_), size_, other.size_ );
91 memcpy( PTR_(data_), PTR_(other.data_), size_*
sizeof(TT_) );
93 QUICKVEC & operator=(
const QuickVec & other )
94 { TRACE( 10,
"QuickVec copy assign this=%p data_=%p other.data_=%p size_=%d other.size_=%d"
95 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_), size_, other.size_ );
96 resize( other.size_ );
97 memcpy( PTR_(data_), PTR_(other.data_), size_*
sizeof(TT_) );
102 : size_(other.size_), data_(std::move(other.data_)), capacity_(other.capacity_)
103 { TRACE( 10,
"QuickVec move ctor this=%p data_=%p other.data_=%p"
104 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_) );
105 # if USE_UNIQUE_PTR == 0
106 other.data_ =
nullptr;
109 QUICKVEC & operator=(
QuickVec && other )
110 { TRACE( 10,
"QuickVec move assign this=%p data_=%p other.data_=%p"
111 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_) );
115 data_ = std::move(other.data_);
116 capacity_ = other.capacity_;
117 # if USE_UNIQUE_PTR == 0
118 other.data_ =
nullptr;
124 TT_& operator[](
int idx);
125 const TT_& operator[](
int idx)
const;
127 size_t capacity()
const;
129 const_iterator begin()
const;
131 const_iterator end()
const;
132 void reserve(
size_t size );
133 void resize(
size_t size );
134 void resize(
size_t size, TT_ val );
135 iterator insert( const_iterator position,
size_t nn,
const TT_& val );
136 iterator insert( const_iterator position, const_iterator first
137 , const_iterator last);
138 iterator erase( const_iterator first, const_iterator last );
140 void push_back(
const value_type& val );
149 # if USE_UNIQUE_PTR == 0
152 std::unique_ptr<TT_[]> data_;
158 inline QUICKVEC::QuickVec(
size_t sz )
160 : size_(sz), data_((TT_*)QV_MEMALIGN(QV_ALIGN,sz*sizeof(TT_))), capacity_(sz)
161 { TRACE( 15,
"QuickVec %p ctor sz=%d data_=%p", (
void*)
this, size_, (
void*)PTR_(data_) );
164 inline QUICKVEC::QuickVec(
size_t sz, TT_ val )
166 : size_(sz), data_((TT_*)QV_MEMALIGN(QV_ALIGN,sz*sizeof(TT_))), capacity_(sz)
167 { TRACE( 15,
"QuickVec %p ctor sz=%d/v data_=%p", (
void*)
this, size_, (
void*)PTR_(data_) );
168 for (iterator ii=begin(); ii!=end(); ++ii) *ii=val;
172 #if USE_UNIQUE_PTR == 0
174 inline QUICKVEC::~QuickVec()
175 { TRACE( 15,
"QuickVec %p dtor start data_=%p size_=%d"
176 , (
void*)
this, (
void*)PTR_(data_), size_ );
179 TRACE( 15,
"QuickVec %p dtor return", (
void*)
this );
184 inline TT_& QUICKVEC::operator[](
int idx)
185 { assert(idx<(
int)size_);
return data_[idx];
189 inline const TT_& QUICKVEC::operator[](
int idx)
const
190 { assert(idx < (
int)size_);
195 inline size_t QUICKVEC::size()
const {
return size_; }
197 inline size_t QUICKVEC::capacity()
const {
return capacity_; }
200 inline QUICKVEC_TN::iterator QUICKVEC::begin() {
return iterator(PTR_(data_)); }
202 inline QUICKVEC_TN::const_iterator QUICKVEC::begin()
const {
return iterator(PTR_(data_)); }
204 inline QUICKVEC_TN::iterator QUICKVEC::end() {
return iterator(PTR_(data_)+size_); }
206 inline QUICKVEC_TN::const_iterator QUICKVEC::end()
const {
return const_iterator(PTR_(data_)+size_); }
209 inline void QUICKVEC::reserve(
size_t size )
210 {
if (size > capacity_)
212 # if USE_UNIQUE_PTR == 0
215 data_ = (TT_*)QV_MEMALIGN(QV_ALIGN,size*
sizeof(TT_));
216 memcpy( data_, old, size_*
sizeof(TT_) );
217 TRACE( 13,
"QUICKVEC::reserve this=%p old=%p data_=%p"
218 , (
void*)
this, (
void*)old, (
void*)data_ );
222 std::unique_ptr<TT_[]> old=std::move(data_);
224 data_ = std::unique_ptr<TT_[]>((TT_*)QV_MEMALIGN(QV_ALIGN,size*
sizeof(TT_)));
225 memcpy( data_.get(), old.get(), size_*
sizeof(TT_) );
233 inline void QUICKVEC::resize(
size_t size )
234 {
if (size < size_) size_ = size;
235 else if (size <= capacity_) size_ = size;
238 # if USE_UNIQUE_PTR == 0
241 data_ = (TT_*)QV_MEMALIGN(QV_ALIGN,size*
sizeof(TT_));
242 memcpy( data_, old, size_*
sizeof(TT_) );
243 TRACE( 13,
"QUICKVEC::resize this=%p old=%p data_=%p"
244 , (
void*)
this, (
void*)old, (
void*)data_ );
248 std::unique_ptr<TT_[]> old=std::move(data_);
250 data_ = std::unique_ptr<TT_[]>((TT_*)QV_MEMALIGN(QV_ALIGN,size*
sizeof(TT_)));
251 memcpy( data_.get(), old.get(), size_*
sizeof(TT_) );
253 size_ = capacity_ = size;
258 inline void QUICKVEC::resize( size_type size, TT_ val )
259 { size_type old_size=size;
262 for (iterator ii=begin()+old_size; ii!=end(); ++ii) *ii=val;
266 inline QUICKVEC_TN::iterator QUICKVEC::insert( const_iterator position
269 { assert(position<=end());
270 size_t offset=position-begin();
273 iterator dst=end()+nn;
275 size_t cnt=end()-(begin()+offset);
276 while (cnt--) *--dst = *--src;
280 while (nn--) *dst++ = val;
281 return begin()+offset;
284 inline QUICKVEC_TN::iterator QUICKVEC::insert( const_iterator position
285 , const_iterator first
286 , const_iterator last)
287 { assert(position<=end());
288 size_t nn=(last-first);
289 size_t offset=position-begin();
292 iterator dst=end()+nn;
294 size_t cnt=end()-(begin()+offset);
295 while (cnt--) *--dst = *--src;
299 while (nn--) *dst++ = *first++;
300 return begin()+offset;
304 inline QUICKVEC_TN::iterator QUICKVEC::erase( const_iterator first
305 ,const_iterator last )
306 { assert(last<=end());
307 size_t nn=(last-first);
308 size_t offset=first-begin();
310 iterator dst=begin()+offset;
312 size_t cnt=end()-src;
313 while (cnt--) *dst++ = *src++;
316 return begin()+offset;
320 inline void QUICKVEC::swap(
QuickVec& x )
321 { TRACE( 12,
"QUICKVEC::swap this=%p enter data_=%p x.data_=%p"
322 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(x.data_) );
323 std::swap( data_, x.data_ );
324 std::swap( size_, x.size_ );
325 std::swap( capacity_, x.capacity_ );
326 TRACE( 12,
"QUICKVEC::swap return data_=%p x.data_=%p"
327 , (
void*)PTR_(data_), (
void*)PTR_(x.data_) );
331 inline void QUICKVEC::push_back(
const value_type& val )
332 {
if (size_ == capacity_)
333 { reserve( size_ + size_/10 + 1 );
339 #ifdef UNDEF_TRACE_AT_END