# sqarray.h

    00001 /*  see copyright notice in squirrel.h */
    00002 #ifndef _SQARRAY_H_
    00003 #define _SQARRAY_H_
    00004
    00005 struct SQArray : public CHAINABLE_OBJ
    00006 {
    00007 private:
    00008   SQArray(SQSharedState *ss,SQInteger nsize){_values.resize(nsize); INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);}
    00009   ~SQArray()
    00010   {
    00011     REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
    00012   }
    00013 public:
    00014   static SQArray* Create(SQSharedState *ss,SQInteger nInitialSize){
    00015     SQArray *newarray=(SQArray*)SQ_MALLOC(sizeof(SQArray));
    00016     new (newarray) SQArray(ss,nInitialSize);
    00017     return newarray;
    00018   }
    00019 #ifndef NO_GARBAGE_COLLECTOR
    00020   void Mark(SQCollectable **chain);
    00021 #endif
    00022   void Finalize(){
    00023     _values.resize(0);
    00024   }
    00025   bool Get(const SQInteger nidx,SQObjectPtr &val)
    00026   {
    00027     if(nidx>=0 && nidx<(SQInteger)_values.size()){
    00028       SQObjectPtr &o = _values[nidx];
    00029       val = _realval(o);
    00030       return true;
    00031     }
    00032     else return false;
    00033   }
    00034   bool Set(const SQInteger nidx,const SQObjectPtr &val)
    00035   {
    00036     if(nidx>=0 && nidx<(SQInteger)_values.size()){
    00037       _values[nidx]=val;
    00038       return true;
    00039     }
    00040     else return false;
    00041   }
    00042   SQInteger Next(const SQObjectPtr &refpos,SQObjectPtr &outkey,SQObjectPtr &outval)
    00043   {
    00044     SQUnsignedInteger idx=TranslateIndex(refpos);
    00045     while(idx<_values.size()){
    00046       //first found
    00047       outkey=(SQInteger)idx;
    00048       SQObjectPtr &o = _values[idx];
    00049       outval = _realval(o);
    00050       //return idx for the next iteration
    00051       return ++idx;
    00052     }
    00053     //nothing to iterate anymore
    00054     return -1;
    00055   }
    00056   SQArray *Clone(){SQArray *anew=Create(_opt_ss(this),Size()); anew->_values.copy(_values); return anew; }
    00057   SQInteger Size() const {return _values.size();}
    00058   void Resize(SQInteger size,SQObjectPtr &fill = _null_) { _values.resize(size,fill); ShrinkIfNeeded(); }
    00059   void Reserve(SQInteger size) { _values.reserve(size); }
    00060   void Append(const SQObject &o){_values.push_back(o);}
    00061   void Extend(const SQArray *a);
    00062   SQObjectPtr &Top(){return _values.top();}
    00063   void Pop(){_values.pop_back(); ShrinkIfNeeded(); }
    00064   bool Insert(SQInteger idx,const SQObject &val){
    00065     if(idx < 0 || idx > (SQInteger)_values.size())
    00066       return false;
    00067     _values.insert(idx,val);
    00068     return true;
    00069   }
    00070   void ShrinkIfNeeded() {
    00071     if(_values.size() <= _values.capacity()>>2) //shrink the array
    00072       _values.shrinktofit();
    00073   }
    00074   bool Remove(SQInteger idx){
    00075     if(idx < 0 || idx >= (SQInteger)_values.size())
    00076       return false;
    00077     _values.remove(idx);
    00078     ShrinkIfNeeded();
    00079     return true;
    00080   }
    00081   void Release()
    00082   {
    00083     sq_delete(this,SQArray);
    00084   }
    00085   SQObjectPtrVec _values;
    00086 };
    00087 #endif //_SQARRAY_H_