24 # define TRACE( lvl, ... )
25 # define UNDEF_TRACE_AT_END
28 #define USE_UNIQUE_PTR 0
30 #ifndef QUICKVEC_DO_TEMPLATE
31 # define QUICKVEC_DO_TEMPLATE 1
35 #if !defined(__GCCXML__) && defined(__GXX_EXPERIMENTAL_CXX0X__)
36 # define NOT_OLD_CXXSTD 1
39 #if QUICKVEC_DO_TEMPLATE == 0
41 # define QUICKVEC_TT unsigned long long
43 # define TT_ QUICKVEC_TT
44 # define QUICKVEC_TEMPLATE
45 # define QUICKVEC QuickVec
46 # define QUICKVEC_TN QuickVec
47 # define QUICKVEC_VERSION
49 # define QUICKVEC_TEMPLATE template <typename TT_>
50 # define QUICKVEC QuickVec<TT_>
51 # define QUICKVEC_TN typename QuickVec<TT_>
53 # define QUICKVEC_VERSION static short Class_Version() { return 5; } // proper version for templates
59 typedef TT_* iterator;
60 typedef const TT_* const_iterator;
61 typedef TT_& reference;
62 typedef const TT_& const_reference;
63 typedef TT_ value_type;
64 typedef ptrdiff_t difference_type;
65 typedef size_t size_type;
69 # if USE_UNIQUE_PTR == 0
73 # define PTR_(xx) xx.get()
76 : size_(other.size()), data_(
new TT_[other.capacity()]), capacity_(other.capacity())
77 { memcpy( PTR_(data_), (
void*)&other[0], size_*
sizeof(TT_) );
79 void clear() { size_=0; }
82 : size_(other.size_), data_(
new TT_[other.capacity_]), capacity_(other.capacity_)
83 { TRACE( 10,
"QuickVec copy ctor this=%p data_=%p other.data_=%p size_=%d other.size_=%d"
84 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_), size_, other.size_ );
85 memcpy( PTR_(data_), PTR_(other.data_), size_*
sizeof(TT_) );
87 QUICKVEC & operator=(
const QuickVec & other )
88 { TRACE( 10,
"QuickVec copy assign 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 resize( other.size_ );
91 memcpy( PTR_(data_), PTR_(other.data_), size_*
sizeof(TT_) );
96 : size_(other.size_), data_(std::move(other.data_)), capacity_(other.capacity_)
97 { TRACE( 10,
"QuickVec move ctor this=%p data_=%p other.data_=%p"
98 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_) );
99 # if USE_UNIQUE_PTR == 0
100 other.data_ =
nullptr;
103 QUICKVEC & operator=(
QuickVec && other )
104 { TRACE( 10,
"QuickVec move assign this=%p data_=%p other.data_=%p"
105 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(other.data_) );
108 data_ = std::move(other.data_);
109 capacity_ = other.capacity_;
110 # if USE_UNIQUE_PTR == 0
111 other.data_ =
nullptr;
117 TT_& operator[](
int idx);
118 const TT_& operator[](
int idx)
const;
120 size_t capacity()
const;
122 const_iterator begin()
const;
124 const_iterator end()
const;
125 void reserve(
size_t size );
126 void resize(
size_t size );
127 void resize(
size_t size, TT_ val );
128 iterator insert( const_iterator position,
size_t nn,
const TT_& val );
129 iterator insert( const_iterator position, const_iterator first
130 , const_iterator last);
131 iterator erase( const_iterator first, const_iterator last );
133 void push_back(
const value_type& val );
142 # if USE_UNIQUE_PTR == 0
145 std::unique_ptr<TT_[]> data_;
151 inline QUICKVEC::QuickVec(
size_t sz )
152 : size_(sz), data_(new TT_[sz]), capacity_(sz)
153 { TRACE( 15,
"QuickVec %p ctor sz=%d data_=%p", (
void*)
this, size_, (
void*)PTR_(data_) );
156 inline QUICKVEC::QuickVec(
size_t sz, TT_ val )
157 : size_(sz), data_(new TT_[sz]), capacity_(sz)
158 { TRACE( 15,
"QuickVec %p ctor sz=%d/v data_=%p", (
void*)
this, size_, (
void*)PTR_(data_) );
159 for (iterator ii=begin(); ii!=end(); ++ii) *ii=val;
163 #if USE_UNIQUE_PTR == 0
165 inline QUICKVEC::~QuickVec()
166 { TRACE( 15,
"QuickVec %p dtor start data_=%p size_=%d"
167 , (
void*)
this, (
void*)PTR_(data_), size_ );
169 TRACE( 15,
"QuickVec %p dtor return", (
void*)
this );
174 inline TT_& QUICKVEC::operator[](
int idx)
175 { assert(idx<(
int)size_);
return data_[idx];
179 inline const TT_& QUICKVEC::operator[](
int idx)
const
180 { assert(idx < (
int)size_);
185 inline size_t QUICKVEC::size()
const {
return size_; }
187 inline size_t QUICKVEC::capacity()
const {
return capacity_; }
190 inline QUICKVEC_TN::iterator QUICKVEC::begin() {
return iterator(PTR_(data_)); }
192 inline QUICKVEC_TN::const_iterator QUICKVEC::begin()
const {
return iterator(PTR_(data_)); }
194 inline QUICKVEC_TN::iterator QUICKVEC::end() {
return iterator(PTR_(data_)+size_); }
196 inline QUICKVEC_TN::const_iterator QUICKVEC::end()
const {
return const_iterator(PTR_(data_)+size_); }
199 inline void QUICKVEC::reserve(
size_t size )
200 {
if (size > capacity_)
202 # if USE_UNIQUE_PTR == 0
204 data_ =
new TT_[size];
205 memcpy( data_, old, size_*
sizeof(TT_) );
206 TRACE( 13,
"QUICKVEC::reserve this=%p old=%p data_=%p"
207 , (
void*)
this, (
void*)old, (
void*)data_ );
210 std::unique_ptr<TT_[]> old=std::move(data_);
211 data_ = std::unique_ptr<TT_[]>(
new TT_[size]);
212 memcpy( data_.get(), old.get(), size_*
sizeof(TT_) );
220 inline void QUICKVEC::resize(
size_t size )
221 {
if (size < size_) size_ = size;
222 else if (size <= capacity_) size_ = size;
225 # if USE_UNIQUE_PTR == 0
227 data_ =
new TT_[size];
228 memcpy( data_, old, size_*
sizeof(TT_) );
229 TRACE( 13,
"QUICKVEC::resize this=%p old=%p data_=%p"
230 , (
void*)
this, (
void*)old, (
void*)data_ );
233 std::unique_ptr<TT_[]> old=std::move(data_);
234 data_ = std::unique_ptr<TT_[]>(
new TT_[size]);
235 memcpy( data_.get(), old.get(), size_*
sizeof(TT_) );
237 size_ = capacity_ = size;
242 inline void QUICKVEC::resize( size_type size, TT_ val )
243 { size_type old_size=size;
246 for (iterator ii=begin()+old_size; ii!=end(); ++ii) *ii=val;
250 inline QUICKVEC_TN::iterator QUICKVEC::insert( const_iterator position
253 { assert(position<=end());
254 size_t offset=position-begin();
257 iterator dst=end()+nn;
259 size_t cnt=end()-(begin()+offset);
260 while (cnt--) *--dst = *--src;
264 while (nn--) *dst++ = val;
265 return begin()+offset;
268 inline QUICKVEC_TN::iterator QUICKVEC::insert( const_iterator position
269 , const_iterator first
270 , const_iterator last)
271 { assert(position<=end());
272 size_t nn=(last-first);
273 size_t offset=position-begin();
276 iterator dst=end()+nn;
278 size_t cnt=end()-(begin()+offset);
279 while (cnt--) *--dst = *--src;
283 while (nn--) *dst++ = *first++;
284 return begin()+offset;
288 inline QUICKVEC_TN::iterator QUICKVEC::erase( const_iterator first
289 ,const_iterator last )
290 { assert(last<=end());
291 size_t nn=(last-first);
292 size_t offset=first-begin();
294 iterator dst=begin()+offset;
296 size_t cnt=end()-src;
297 while (cnt--) *dst++ = *src++;
300 return begin()+offset;
304 inline void QUICKVEC::swap(
QuickVec& x )
305 { TRACE( 12,
"QUICKVEC::swap this=%p enter data_=%p x.data_=%p"
306 , (
void*)
this, (
void*)PTR_(data_), (
void*)PTR_(x.data_) );
307 std::swap( data_, x.data_ );
308 std::swap( size_, x.size_ );
309 std::swap( capacity_, x.capacity_ );
310 TRACE( 12,
"QUICKVEC::swap return data_=%p x.data_=%p"
311 , (
void*)PTR_(data_), (
void*)PTR_(x.data_) );
315 inline void QUICKVEC::push_back(
const value_type& val )
316 {
if (size_ == capacity_)
317 { reserve( size_ + size_/10 + 1 );
323 #ifdef UNDEF_TRACE_AT_END