16 #if !defined(__APPLE__)
28 # define TRACE( lvl, ... )
29 # define UNDEF_TRACE_AT_END
32 #define USE_UNIQUE_PTR 0
34 static inline void *QV_MEMALIGN(
size_t boundary,
size_t size ) {
void *retadr; posix_memalign(&retadr,boundary,size);
return retadr;}
36 #ifndef QUICKVEC_DO_TEMPLATE
37 # define QUICKVEC_DO_TEMPLATE 1
41 #if !defined(__GCCXML__) && defined(__GXX_EXPERIMENTAL_CXX0X__)
42 # define NOT_OLD_CXXSTD 1
45 #if QUICKVEC_DO_TEMPLATE == 0
47 # define QUICKVEC_TT unsigned long long
49 # define TT_ QUICKVEC_TT
50 # define QUICKVEC_TEMPLATE
51 # define QUICKVEC QuickVec
52 # define QUICKVEC_TN QuickVec
53 # define QUICKVEC_VERSION
55 # define QUICKVEC_TEMPLATE template <typename TT_>
56 # define QUICKVEC QuickVec<TT_>
57 # define QUICKVEC_TN typename QuickVec<TT_>
59 # define QUICKVEC_VERSION static short Class_Version() { return 5; } // proper version for templates
65 typedef TT_* iterator;
66 typedef const TT_* const_iterator;
67 typedef TT_& reference;
68 typedef const TT_& const_reference;
69 typedef TT_ value_type;
70 typedef ptrdiff_t difference_type;
71 typedef size_t size_type;
75 # if USE_UNIQUE_PTR == 0
79 # define PTR_(xx) xx.get()
83 : 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_)
91 { TRACE( 10,
"QuickVec copy ctor this=%p data_=%p other.data_=%p size_=%d other.size_=%d"
92 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_), size_, other.size_ );
93 memcpy( PTR_(data_), PTR_(other.data_), size_*
sizeof(TT_) );
95 QUICKVEC & operator=(
const QuickVec & other )
96 { TRACE( 10,
"QuickVec copy assign this=%p data_=%p other.data_=%p size_=%d other.size_=%d"
97 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_), size_, other.size_ );
98 resize( other.size_ );
99 memcpy( PTR_(data_), PTR_(other.data_), size_*
sizeof(TT_) );
104 : size_(other.size_), data_(std::move(other.data_)), capacity_(other.capacity_)
105 { TRACE( 10,
"QuickVec move ctor this=%p data_=%p other.data_=%p"
106 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_) );
107 # if USE_UNIQUE_PTR == 0
108 other.data_ =
nullptr;
111 QUICKVEC & operator=(
QuickVec && other )
112 { TRACE( 10,
"QuickVec move assign this=%p data_=%p other.data_=%p"
113 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_) );
117 data_ = std::move(other.data_);
118 capacity_ = other.capacity_;
119 # if USE_UNIQUE_PTR == 0
120 other.data_ =
nullptr;
126 TT_& operator[](
int idx);
127 const TT_& operator[](
int idx)
const;
129 size_t capacity()
const;
131 const_iterator begin()
const;
133 const_iterator end()
const;
134 void reserve(
size_t size );
135 void resize(
size_t size );
136 void resize(
size_t size, TT_ val );
137 iterator insert( const_iterator position,
size_t nn,
const TT_& val );
138 iterator insert( const_iterator position, const_iterator first
139 , const_iterator last);
140 iterator erase( const_iterator first, const_iterator last );
142 void push_back(
const value_type& val );
151 # if USE_UNIQUE_PTR == 0
154 std::unique_ptr<TT_[]> data_;
160 inline QUICKVEC::QuickVec(
size_t sz )
162 : size_(sz), data_((TT_*)QV_MEMALIGN(QV_ALIGN,sz*sizeof(TT_))), capacity_(sz)
163 { TRACE( 15,
"QuickVec %p ctor sz=%d data_=%p", (
void*)
this, size_, (
void*)PTR_(data_) );
166 inline QUICKVEC::QuickVec(
size_t sz, TT_ val )
168 : size_(sz), data_((TT_*)QV_MEMALIGN(QV_ALIGN,sz*sizeof(TT_))), capacity_(sz)
169 { TRACE( 15,
"QuickVec %p ctor sz=%d/v data_=%p", (
void*)
this, size_, (
void*)PTR_(data_) );
170 for (iterator ii=begin(); ii!=end(); ++ii) *ii=val;
174 #if USE_UNIQUE_PTR == 0
176 inline QUICKVEC::~QuickVec()
177 { TRACE( 15,
"QuickVec %p dtor start data_=%p size_=%d"
178 , (
void*)
this, (
void*)PTR_(data_), size_ );
181 TRACE( 15,
"QuickVec %p dtor return", (
void*)
this );
186 inline TT_& QUICKVEC::operator[](
int idx)
187 { assert(idx<(
int)size_);
return data_[idx];
191 inline const TT_& QUICKVEC::operator[](
int idx)
const
192 { assert(idx < (
int)size_);
197 inline size_t QUICKVEC::size()
const {
return size_; }
199 inline size_t QUICKVEC::capacity()
const {
return capacity_; }
202 inline QUICKVEC_TN::iterator QUICKVEC::begin() {
return iterator(PTR_(data_)); }
204 inline QUICKVEC_TN::const_iterator QUICKVEC::begin()
const {
return iterator(PTR_(data_)); }
206 inline QUICKVEC_TN::iterator QUICKVEC::end() {
return iterator(PTR_(data_)+size_); }
208 inline QUICKVEC_TN::const_iterator QUICKVEC::end()
const {
return const_iterator(PTR_(data_)+size_); }
211 inline void QUICKVEC::reserve(
size_t size )
212 {
if (size > capacity_)
214 # if USE_UNIQUE_PTR == 0
217 data_ = (TT_*)QV_MEMALIGN(QV_ALIGN,size*
sizeof(TT_));
218 memcpy( data_, old, size_*
sizeof(TT_) );
219 TRACE( 13,
"QUICKVEC::reserve this=%p old=%p data_=%p"
220 , (
void*)
this, (
void*)old, (
void*)data_ );
224 std::unique_ptr<TT_[]> old=std::move(data_);
226 data_ = std::unique_ptr<TT_[]>((TT_*)QV_MEMALIGN(QV_ALIGN,size*
sizeof(TT_)));
227 memcpy( data_.get(), old.get(), size_*
sizeof(TT_) );
235 inline void QUICKVEC::resize(
size_t size )
236 {
if (size < size_) size_ = size;
237 else if (size <= capacity_) size_ = size;
240 # if USE_UNIQUE_PTR == 0
243 data_ = (TT_*)QV_MEMALIGN(QV_ALIGN,size*
sizeof(TT_));
244 memcpy( data_, old, size_*
sizeof(TT_) );
245 TRACE( 13,
"QUICKVEC::resize this=%p old=%p data_=%p"
246 , (
void*)
this, (
void*)old, (
void*)data_ );
250 std::unique_ptr<TT_[]> old=std::move(data_);
252 data_ = std::unique_ptr<TT_[]>((TT_*)QV_MEMALIGN(QV_ALIGN,size*
sizeof(TT_)));
253 memcpy( data_.get(), old.get(), size_*
sizeof(TT_) );
255 size_ = capacity_ = size;
260 inline void QUICKVEC::resize( size_type size, TT_ val )
261 { size_type old_size=size;
264 for (iterator ii=begin()+old_size; ii!=end(); ++ii) *ii=val;
268 inline QUICKVEC_TN::iterator QUICKVEC::insert( const_iterator position
271 { assert(position<=end());
272 size_t offset=position-begin();
275 iterator dst=end()+nn;
277 size_t cnt=end()-(begin()+offset);
278 while (cnt--) *--dst = *--src;
282 while (nn--) *dst++ = val;
283 return begin()+offset;
286 inline QUICKVEC_TN::iterator QUICKVEC::insert( const_iterator position
287 , const_iterator first
288 , const_iterator last)
289 { assert(position<=end());
290 size_t nn=(last-first);
291 size_t offset=position-begin();
294 iterator dst=end()+nn;
296 size_t cnt=end()-(begin()+offset);
297 while (cnt--) *--dst = *--src;
301 while (nn--) *dst++ = *first++;
302 return begin()+offset;
306 inline QUICKVEC_TN::iterator QUICKVEC::erase( const_iterator first
307 ,const_iterator last )
308 { assert(last<=end());
309 size_t nn=(last-first);
310 size_t offset=first-begin();
312 iterator dst=begin()+offset;
314 size_t cnt=end()-src;
315 while (cnt--) *dst++ = *src++;
318 return begin()+offset;
322 inline void QUICKVEC::swap(
QuickVec& x )
323 { TRACE( 12,
"QUICKVEC::swap this=%p enter data_=%p x.data_=%p"
324 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(x.data_) );
325 std::swap( data_, x.data_ );
326 std::swap( size_, x.size_ );
327 std::swap( capacity_, x.capacity_ );
328 TRACE( 12,
"QUICKVEC::swap return data_=%p x.data_=%p"
329 , (
void*)PTR_(data_), (
void*)PTR_(x.data_) );
333 inline void QUICKVEC::push_back(
const value_type& val )
334 {
if (size_ == capacity_)
335 { reserve( size_ + size_/10 + 1 );
341 #ifdef UNDEF_TRACE_AT_END