35 #ifdef ENABLE_VECTORDEBUG 36 #define VECTORDEBUG(...) { printf(__VA_ARGS__); printf(" this=%p [%p,%p,%p]\n",(void*)this,(void*)&mData,mStart,mEnd); } 39 #define VECTORDEBUG(...) 42 #define BITVECTOR_REFCNTS 0 46 template <
class T>
class RCData :
public RefCntBase {
126 #if BITVECTOR_REFCNTS 138 mData = elements ?
new T[elements] : NULL;
140 mEnd = mStart + elements;
147 VECTORDEBUG(
"VectorBase::shiftMem(%p)",(
void*)&other);
149 this->mData=other.mData;
150 this->mStart=other.mStart;
151 this->mEnd=other.mEnd;
158 if (this->getData()) {
159 assert(this->getData() != other.
getData());
162 this->mStart=
const_cast<T*
>(other.
mStart);
163 this->mEnd=
const_cast<T*
>(other.
mEnd);
171 assert(mStart>=mData);
172 assert(mEnd>=mStart);
173 return mEnd - mStart;
177 size_t bytes()
const {
return this->size()*
sizeof(T); }
182 VECTORDEBUG(
"VectorBase::resize(%p,%d) %s",
this,newElements, (mData?
"delete":
""));
183 if (mData!=NULL)
delete[] mData;
193 this->resize(other.
size());
198 this->resize(other1.
size()+other2.
size());
199 memcpy(this->mStart, other1.
mStart, other1.
bytes());
208 VectorBase(VectorDataType wData, T* wStart, T* wEnd) :mData(wData),mStart(wStart),mEnd(wEnd) {
210 VECTORDEBUG(
"VectorBase(%p,%p,%p)",this->getData(),wStart,wEnd);
226 snprintf(buf,100,
" mData=%p mStart=%p mEnd=%p ",(
void*)mData,mStart,mEnd);
227 return std::string(buf);
239 T* base = other.
mStart + start;
240 assert(base+span<=other.
mEnd);
241 assert(mStart+span<=mEnd);
242 memcpy(base,mStart,span*
sizeof(T));
259 const T* base = mStart + start;
260 assert(base+span<=mEnd);
262 memcpy(other.
mStart,base,span*
sizeof(T));
268 while (dp<mEnd) *dp++=val;
271 void fill(
const T& val,
unsigned start,
unsigned length)
276 while (dp<end) *dp++=val;
287 this->makeAlias(other);
295 assert(mStart+index<mEnd);
296 return mStart[index];
301 assert(mStart+index<mEnd);
302 return mStart[index];
305 const T*
begin()
const {
return this->mStart; }
307 const T*
end()
const {
return this->mEnd; }
308 T*
end() {
return this->mEnd; }
309 #if BITVECTOR_REFCNTS 310 const T*getData()
const {
return this->mData.isNULL() ? 0 : this->mData->mPointer; }
312 const T*
getData()
const {
return this->mData; }
322 Vector(
size_t wSize=0) { this->resize(wSize); }
338 assert(this->mData == 0);
339 this->vConcat(other1,other2);
353 T* wStart = this->mStart + start;
354 T* wEnd = wStart + span;
355 assert(wEnd<=this->mEnd);
362 T* wStart = this->mStart + start;
363 T* wEnd = wStart + span;
364 assert(wEnd<=this->mEnd);
371 const Vector<T> tail(
size_t start)
const {
return segment(start,this->size()-start); }
388 std::ostream& operator<<(std::ostream& os, const Vector<T>& v)
390 for (
unsigned i=0; i<v.size(); i++) os << v[i] <<
" ";
void makeAlias(const VectorBase< T > &other)
Definition: Vector.h:156
void operator=(const VectorBase< T > &other)
Definition: Vector.h:282
const Vector< T > tail(size_t start) const
Definition: Vector.h:371
T * mStart
start of useful data
Definition: Vector.h:132
const T * getData() const
Definition: Vector.h:312
void vInit(size_t elements)
Definition: Vector.h:136
void copyToSegment(VectorBase< T > &other, size_t start, size_t span) const
Definition: Vector.h:237
void operator=(Vector< T > &other)
Definition: Vector.h:345
size_t size() const
Definition: Vector.h:169
void vConcat(const VectorBase< T > &other1, const VectorBase< T > &other2)
Definition: Vector.h:197
void segmentCopyTo(VectorBase< T > &other, size_t start, size_t span) const
Definition: Vector.h:257
const Vector< T > segment(size_t start, size_t span) const
Definition: Vector.h:360
void fill(const T &val)
Definition: Vector.h:265
void resize(size_t newElements)
Definition: Vector.h:180
T * VectorDataType
Definition: Vector.h:129
VectorBase()
Definition: Vector.h:205
VectorDataType mData
allocated data block.
Definition: Vector.h:131
void fill(const T &val, unsigned start, unsigned length)
Definition: Vector.h:271
Vector(const Vector< T > &other)
Definition: Vector.h:328
Vector(const Vector< T > &other1, const Vector< T > &other2)
Definition: Vector.h:337
T * mEnd
end of useful data + 1
Definition: Vector.h:133
const T & operator[](size_t index) const
Definition: Vector.h:299
const T * begin() const
Definition: Vector.h:305
#define VECTORDEBUG(...)
Definition: Vector.h:39
void shiftMem(VectorBase< T > &other)
Definition: Vector.h:145
Vector< T > head(size_t span)
Definition: Vector.h:368
const T * end() const
Definition: Vector.h:307
Vector(size_t wSize=0)
Definition: Vector.h:322
Vector(T *wData, T *wStart, T *wEnd)
Definition: Vector.h:331
bool isOwner()
Definition: Vector.h:222
Vector< T > segment(size_t start, size_t span)
Definition: Vector.h:351
T * end()
Definition: Vector.h:308
void clear()
Definition: Vector.h:188
void copyTo(VectorBase< T > &other) const
Definition: Vector.h:248
void operator=(const Vector< T > &other)
Definition: Vector.h:348
std::string inspect() const
Definition: Vector.h:224
void clone(const VectorBase< T > &other)
Definition: Vector.h:192
Vector(Vector< T > &other)
Definition: Vector.h:325
const Vector< T > head(size_t span) const
Definition: Vector.h:369
T & operator[](size_t index)
Definition: Vector.h:293
Vector< T > tail(size_t start)
Definition: Vector.h:370
void copyToSegment(VectorBase< T > &other, size_t start=0) const
Definition: Vector.h:246
T * iterator
Definition: Vector.h:375
const T * const_iterator
Definition: Vector.h:376
size_t bytes() const
Definition: Vector.h:177
T * begin()
Definition: Vector.h:306
Vector(T *wStart, size_t span)
Definition: Vector.h:334
~VectorBase()
Definition: Vector.h:216
VectorBase(VectorDataType wData, T *wStart, T *wEnd)
Definition: Vector.h:208