1 /* see copyright notice in squirrel.h */
5 struct SQArray : public CHAINABLE_OBJ
8 SQArray(SQSharedState *ss,int nsize){_values.resize(nsize);_uiRef=0;INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);}
11 REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
14 static SQArray* Create(SQSharedState *ss,int nInitialSize){
15 SQArray *newarray=(SQArray*)SQ_MALLOC(sizeof(SQArray));
16 new (newarray) SQArray(ss,nInitialSize);
19 #ifndef NO_GARBAGE_COLLECTOR
20 void Mark(SQCollectable **chain);
25 bool Get(const int nidx,SQObjectPtr &val)
27 if(nidx>=0 && nidx<(int)_values.size()){
33 bool Set(const int nidx,const SQObjectPtr &val)
35 if(nidx>=0 && nidx<(int)_values.size()){
41 int Next(const SQObjectPtr &refpos,SQObjectPtr &outkey,SQObjectPtr &outval)
43 unsigned int idx=TranslateIndex(refpos);
44 while(idx<_values.size()){
46 outkey=(SQInteger)idx;
48 //return idx for the next iteration
51 //nothing to iterate anymore
54 SQArray *Clone(){SQArray *anew=Create(_opt_ss(this),Size()); anew->_values.copy(_values); return anew; }
55 int Size() const {return _values.size();}
56 void Resize(int size,SQObjectPtr &fill = _null_) { _values.resize(size,fill); ShrinkIfNeeded(); }
57 void Reserve(int size) { _values.reserve(size); }
58 void Append(const SQObject &o){_values.push_back(o);}
59 void Extend(const SQArray *a);
60 SQObjectPtr &Top(){return _values.top();}
61 void Pop(){_values.pop_back(); ShrinkIfNeeded(); }
62 void Insert(const SQObject& idx,const SQObject &val){_values.insert((unsigned int)tointeger(idx),val);}
63 void ShrinkIfNeeded() {
64 if(_values.size() <= _values.capacity()>>2) //shrink the array
65 _values.shrinktofit();
67 void Remove(unsigned int idx){
73 sq_delete(this,SQArray);
75 SQObjectPtrVec _values;