Johan Wikman / SQUIRREL3

Dependents:   Squirrel

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sqarray.h Source File

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     SQObjectType GetType() {return OT_ARRAY;}
00022 #endif
00023     void Finalize(){
00024         _values.resize(0);
00025     }
00026     bool Get(const SQInteger nidx,SQObjectPtr &val)
00027     {
00028         if(nidx>=0 && nidx<(SQInteger)_values.size()){
00029             SQObjectPtr &o = _values[nidx];
00030             val = _realval(o);
00031             return true;
00032         }
00033         else return false;
00034     }
00035     bool Set(const SQInteger nidx,const SQObjectPtr &val)
00036     {
00037         if(nidx>=0 && nidx<(SQInteger)_values.size()){
00038             _values[nidx]=val;
00039             return true;
00040         }
00041         else return false;
00042     }
00043     SQInteger Next(const SQObjectPtr &refpos,SQObjectPtr &outkey,SQObjectPtr &outval)
00044     {
00045         SQUnsignedInteger idx=TranslateIndex(refpos);
00046         while(idx<_values.size()){
00047             //first found
00048             outkey=(SQInteger)idx;
00049             SQObjectPtr &o = _values[idx];
00050             outval = _realval(o);
00051             //return idx for the next iteration
00052             return ++idx;
00053         }
00054         //nothing to iterate anymore
00055         return -1;
00056     }
00057     SQArray *Clone(){SQArray *anew=Create(_opt_ss(this),0); anew->_values.copy(_values); return anew; }
00058     SQInteger Size() const {return _values.size();}
00059     void Resize(SQInteger size)
00060     {
00061         SQObjectPtr _null;
00062         Resize(size,_null);
00063     }
00064     void Resize(SQInteger size,SQObjectPtr &fill) { _values.resize(size,fill); ShrinkIfNeeded(); }
00065     void Reserve(SQInteger size) { _values.reserve(size); }
00066     void Append(const SQObject &o){_values.push_back(o);}
00067     void Extend(const SQArray *a);
00068     SQObjectPtr &Top(){return _values.top();}
00069     void Pop(){_values.pop_back(); ShrinkIfNeeded(); }
00070     bool Insert(SQInteger idx,const SQObject &val){
00071         if(idx < 0 || idx > (SQInteger)_values.size())
00072             return false;
00073         _values.insert(idx,val);
00074         return true;
00075     }
00076     void ShrinkIfNeeded() {
00077         if(_values.size() <= _values.capacity()>>2) //shrink the array
00078             _values.shrinktofit();
00079     }
00080     bool Remove(SQInteger idx){
00081         if(idx < 0 || idx >= (SQInteger)_values.size())
00082             return false;
00083         _values.remove(idx);
00084         ShrinkIfNeeded();
00085         return true;
00086     }
00087     void Release()
00088     {
00089         sq_delete(this,SQArray);
00090     }
00091     
00092     SQObjectPtrVec _values;
00093 };
00094 #endif //_SQARRAY_H_