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
62 typedef TT_* iterator;
63 typedef const TT_* const_iterator;
64 typedef TT_& reference;
65 typedef const TT_& const_reference;
66 typedef TT_ value_type;
67 typedef ptrdiff_t difference_type;
68 typedef size_t size_type;
72 # if USE_UNIQUE_PTR == 0
76 # define PTR_(xx) xx.get()
80 : size_(other.size()), data_((TT_*)QV_MEMALIGN(QV_ALIGN,other.capacity()*
sizeof(TT_))), capacity_(other.capacity())
81 { memcpy( PTR_(data_), (
void*)&other[0], size_*
sizeof(TT_) );
83 void clear() { size_=0; }
87 : size_(other.size_), data_((TT_*)QV_MEMALIGN(QV_ALIGN,other.capacity()*
sizeof(TT_))), capacity_(other.capacity_)
88 { TRACE( 10,
"QuickVec copy ctor this=%p data_=%p other.data_=%p size_=%d other.size_=%d"
89 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_), size_, other.size_ );
90 memcpy( PTR_(data_), PTR_(other.data_), size_*
sizeof(TT_) );
92 QUICKVEC & operator=(
const QuickVec & other )
93 { TRACE( 10,
"QuickVec copy assign this=%p data_=%p other.data_=%p size_=%d other.size_=%d"
94 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_), size_, other.size_ );
95 resize( other.size_ );
96 memcpy( PTR_(data_), PTR_(other.data_), size_*
sizeof(TT_) );
101 : size_(other.size_), data_(std::move(other.data_)), capacity_(other.capacity_)
102 { TRACE( 10,
"QuickVec move ctor this=%p data_=%p other.data_=%p"
103 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_) );
104 # if USE_UNIQUE_PTR == 0
105 other.data_ =
nullptr;
108 QUICKVEC & operator=(
QuickVec && other )
109 { TRACE( 10,
"QuickVec move assign this=%p data_=%p other.data_=%p"
110 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_) );
114 data_ = std::move(other.data_);
115 capacity_ = other.capacity_;
116 # if USE_UNIQUE_PTR == 0
117 other.data_ =
nullptr;
123 TT_& operator[](
int idx);
124 const TT_& operator[](
int idx)
const;
126 size_t capacity()
const;
128 const_iterator begin()
const;
130 const_iterator end()
const;
131 void reserve(
size_t size );
132 void resize(
size_t size );
133 void resize(
size_t size, TT_ val );
134 iterator insert( const_iterator position,
size_t nn,
const TT_& val );
135 iterator insert( const_iterator position, const_iterator first
136 , const_iterator last);
137 iterator erase( const_iterator first, const_iterator last );
139 void push_back(
const value_type& val );
148 # if USE_UNIQUE_PTR == 0
151 std::unique_ptr<TT_[]> data_;
157 inline QUICKVEC::QuickVec(
size_t sz )
159 : size_(sz), data_((TT_*)QV_MEMALIGN(QV_ALIGN,sz*sizeof(TT_))), capacity_(sz)
160 { TRACE( 15,
"QuickVec %p ctor sz=%d data_=%p", (
void*)
this, size_, (
void*)PTR_(data_) );
163 inline QUICKVEC::QuickVec(
size_t sz, TT_ val )
165 : size_(sz), data_((TT_*)QV_MEMALIGN(QV_ALIGN,sz*sizeof(TT_))), capacity_(sz)
166 { TRACE( 15,
"QuickVec %p ctor sz=%d/v data_=%p", (
void*)
this, size_, (
void*)PTR_(data_) );
167 for (iterator ii=begin(); ii!=end(); ++ii) *ii=val;
171 #if USE_UNIQUE_PTR == 0
173 inline QUICKVEC::~QuickVec()
174 { TRACE( 15,
"QuickVec %p dtor start data_=%p size_=%d"
175 , (
void*)
this, (
void*)PTR_(data_), size_ );
178 TRACE( 15,
"QuickVec %p dtor return", (
void*)
this );
183 inline TT_& QUICKVEC::operator[](
int idx)
184 { assert(idx<(
int)size_);
return data_[idx];
188 inline const TT_& QUICKVEC::operator[](
int idx)
const
189 { assert(idx < (
int)size_);
194 inline size_t QUICKVEC::size()
const {
return size_; }
196 inline size_t QUICKVEC::capacity()
const {
return capacity_; }
199 inline QUICKVEC_TN::iterator QUICKVEC::begin() {
return iterator(PTR_(data_)); }
201 inline QUICKVEC_TN::const_iterator QUICKVEC::begin()
const {
return iterator(PTR_(data_)); }
203 inline QUICKVEC_TN::iterator QUICKVEC::end() {
return iterator(PTR_(data_)+size_); }
205 inline QUICKVEC_TN::const_iterator QUICKVEC::end()
const {
return const_iterator(PTR_(data_)+size_); }
208 inline void QUICKVEC::reserve(
size_t size )
209 {
if (size > capacity_)
211 # if USE_UNIQUE_PTR == 0
214 data_ = (TT_*)QV_MEMALIGN(QV_ALIGN,size*
sizeof(TT_));
215 memcpy( data_, old, size_*
sizeof(TT_) );
216 TRACE( 13,
"QUICKVEC::reserve this=%p old=%p data_=%p"
217 , (
void*)
this, (
void*)old, (
void*)data_ );
221 std::unique_ptr<TT_[]> old=std::move(data_);
223 data_ = std::unique_ptr<TT_[]>((TT_*)QV_MEMALIGN(QV_ALIGN,size*
sizeof(TT_)));
224 memcpy( data_.get(), old.get(), size_*
sizeof(TT_) );
232 inline void QUICKVEC::resize(
size_t size )
233 {
if (size < size_) size_ = size;
234 else if (size <= capacity_) size_ = size;
237 # if USE_UNIQUE_PTR == 0
240 data_ = (TT_*)QV_MEMALIGN(QV_ALIGN,size*
sizeof(TT_));
241 memcpy( data_, old, size_*
sizeof(TT_) );
242 TRACE( 13,
"QUICKVEC::resize this=%p old=%p data_=%p"
243 , (
void*)
this, (
void*)old, (
void*)data_ );
247 std::unique_ptr<TT_[]> old=std::move(data_);
249 data_ = std::unique_ptr<TT_[]>((TT_*)QV_MEMALIGN(QV_ALIGN,size*
sizeof(TT_)));
250 memcpy( data_.get(), old.get(), size_*
sizeof(TT_) );
252 size_ = capacity_ = size;
257 inline void QUICKVEC::resize( size_type size, TT_ val )
258 { size_type old_size=size;
261 for (iterator ii=begin()+old_size; ii!=end(); ++ii) *ii=val;
265 inline QUICKVEC_TN::iterator QUICKVEC::insert( const_iterator position
268 { assert(position<=end());
269 size_t offset=position-begin();
272 iterator dst=end()+nn;
274 size_t cnt=end()-(begin()+offset);
275 while (cnt--) *--dst = *--src;
279 while (nn--) *dst++ = val;
280 return begin()+offset;
283 inline QUICKVEC_TN::iterator QUICKVEC::insert( const_iterator position
284 , const_iterator first
285 , const_iterator last)
286 { assert(position<=end());
287 size_t nn=(last-first);
288 size_t offset=position-begin();
291 iterator dst=end()+nn;
293 size_t cnt=end()-(begin()+offset);
294 while (cnt--) *--dst = *--src;
298 while (nn--) *dst++ = *first++;
299 return begin()+offset;
303 inline QUICKVEC_TN::iterator QUICKVEC::erase( const_iterator first
304 ,const_iterator last )
305 { assert(last<=end());
306 size_t nn=(last-first);
307 size_t offset=first-begin();
309 iterator dst=begin()+offset;
311 size_t cnt=end()-src;
312 while (cnt--) *dst++ = *src++;
315 return begin()+offset;
319 inline void QUICKVEC::swap(
QuickVec& x )
320 { TRACE( 12,
"QUICKVEC::swap this=%p enter data_=%p x.data_=%p"
321 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(x.data_) );
322 std::swap( data_, x.data_ );
323 std::swap( size_, x.size_ );
324 std::swap( capacity_, x.capacity_ );
325 TRACE( 12,
"QUICKVEC::swap return data_=%p x.data_=%p"
326 , (
void*)PTR_(data_), (
void*)PTR_(x.data_) );
330 inline void QUICKVEC::push_back(
const value_type& val )
331 {
if (size_ == capacity_)
332 { reserve( size_ + size_/10 + 1 );
338 #ifdef UNDEF_TRACE_AT_END