1 /* see copyright notice in squirrel.h */
5 struct SQArray : public CHAINABLE_OBJ
8 SQArray(SQSharedState *ss,SQInteger nsize){_values.resize(nsize); 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,SQInteger 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 SQInteger nidx,SQObjectPtr &val)
27 if(nidx>=0 && nidx<(SQInteger)_values.size()){
28 SQObjectPtr &o = _values[nidx];
34 bool Set(const SQInteger nidx,const SQObjectPtr &val)
36 if(nidx>=0 && nidx<(SQInteger)_values.size()){
42 SQInteger Next(const SQObjectPtr &refpos,SQObjectPtr &outkey,SQObjectPtr &outval)
44 SQUnsignedInteger idx=TranslateIndex(refpos);
45 while(idx<_values.size()){
47 outkey=(SQInteger)idx;
48 SQObjectPtr &o = _values[idx];
50 //return idx for the next iteration
53 //nothing to iterate anymore
56 SQArray *Clone(){SQArray *anew=Create(_opt_ss(this),Size()); anew->_values.copy(_values); return anew; }
57 SQInteger Size() const {return _values.size();}
58 void Resize(SQInteger size,SQObjectPtr &fill = _null_) { _values.resize(size,fill); ShrinkIfNeeded(); }
59 void Reserve(SQInteger size) { _values.reserve(size); }
60 void Append(const SQObject &o){_values.push_back(o);}
61 void Extend(const SQArray *a);
62 SQObjectPtr &Top(){return _values.top();}
63 void Pop(){_values.pop_back(); ShrinkIfNeeded(); }
64 bool Insert(SQInteger idx,const SQObject &val){
65 if(idx < 0 || idx > (SQInteger)_values.size())
67 _values.insert(idx,val);
70 void ShrinkIfNeeded() {
71 if(_values.size() <= _values.capacity()>>2) //shrink the array
72 _values.shrinktofit();
74 bool Remove(SQInteger idx){
75 if(idx < 0 || idx >= (SQInteger)_values.size())
83 sq_delete(this,SQArray);
85 SQObjectPtrVec _values;