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