-/* see copyright notice in squirrel.h */\r
-#ifndef _SQARRAY_H_\r
-#define _SQARRAY_H_\r
-\r
-struct SQArray : public CHAINABLE_OBJ\r
-{\r
-private:\r
- SQArray(SQSharedState *ss,int nsize){_values.resize(nsize);_uiRef=0;INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);}\r
- ~SQArray()\r
- {\r
- REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);\r
- }\r
-public:\r
- static SQArray* Create(SQSharedState *ss,int nInitialSize){\r
- SQArray *newarray=(SQArray*)SQ_MALLOC(sizeof(SQArray));\r
- new (newarray) SQArray(ss,nInitialSize);\r
- return newarray;\r
- }\r
-#ifndef NO_GARBAGE_COLLECTOR\r
- void Mark(SQCollectable **chain);\r
-#endif\r
- void Finalize(){\r
- _values.resize(0);\r
- }\r
- bool Get(const int nidx,SQObjectPtr &val)\r
- {\r
- if(nidx>=0 && nidx<(int)_values.size()){\r
- val=_values[nidx];\r
- return true;\r
- }\r
- else return false;\r
- }\r
- bool Set(const int nidx,const SQObjectPtr &val)\r
- {\r
- if(nidx>=0 && nidx<(int)_values.size()){\r
- _values[nidx]=val;\r
- return true;\r
- }\r
- else return false;\r
- }\r
- int Next(const SQObjectPtr &refpos,SQObjectPtr &outkey,SQObjectPtr &outval)\r
- {\r
- unsigned int idx=TranslateIndex(refpos);\r
- while(idx<_values.size()){\r
- //first found\r
- outkey=(SQInteger)idx;\r
- outval=_values[idx];\r
- //return idx for the next iteration\r
- return ++idx;\r
- }\r
- //nothing to iterate anymore\r
- return -1;\r
- }\r
- SQArray *Clone(){SQArray *anew=Create(_opt_ss(this),Size()); anew->_values.copy(_values); return anew; }\r
- int Size() const {return _values.size();}\r
- void Resize(int size,SQObjectPtr &fill = _null_) { _values.resize(size,fill); ShrinkIfNeeded(); }\r
- void Reserve(int size) { _values.reserve(size); }\r
- void Append(const SQObject &o){_values.push_back(o);}\r
- void Extend(const SQArray *a);\r
- SQObjectPtr &Top(){return _values.top();}\r
- void Pop(){_values.pop_back(); ShrinkIfNeeded(); }\r
- void Insert(const SQObject& idx,const SQObject &val){_values.insert((unsigned int)tointeger(idx),val);}\r
- void ShrinkIfNeeded() {\r
- if(_values.size() <= _values.capacity()>>2) //shrink the array\r
- _values.shrinktofit();\r
- }\r
- void Remove(unsigned int idx){\r
- _values.remove(idx);\r
- ShrinkIfNeeded();\r
- }\r
- void Release()\r
- {\r
- sq_delete(this,SQArray);\r
- }\r
- SQObjectPtrVec _values;\r
-};\r
-#endif //_SQARRAY_H_\r
+/* see copyright notice in squirrel.h */
+#ifndef _SQARRAY_H_
+#define _SQARRAY_H_
+
+struct SQArray : public CHAINABLE_OBJ
+{
+private:
+ SQArray(SQSharedState *ss,int nsize){_values.resize(nsize);_uiRef=0;INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);}
+ ~SQArray()
+ {
+ REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
+ }
+public:
+ static SQArray* Create(SQSharedState *ss,int nInitialSize){
+ SQArray *newarray=(SQArray*)SQ_MALLOC(sizeof(SQArray));
+ new (newarray) SQArray(ss,nInitialSize);
+ return newarray;
+ }
+#ifndef NO_GARBAGE_COLLECTOR
+ void Mark(SQCollectable **chain);
+#endif
+ void Finalize(){
+ _values.resize(0);
+ }
+ bool Get(const int nidx,SQObjectPtr &val)
+ {
+ if(nidx>=0 && nidx<(int)_values.size()){
+ val=_values[nidx];
+ return true;
+ }
+ else return false;
+ }
+ bool Set(const int nidx,const SQObjectPtr &val)
+ {
+ if(nidx>=0 && nidx<(int)_values.size()){
+ _values[nidx]=val;
+ return true;
+ }
+ else return false;
+ }
+ int Next(const SQObjectPtr &refpos,SQObjectPtr &outkey,SQObjectPtr &outval)
+ {
+ unsigned int idx=TranslateIndex(refpos);
+ while(idx<_values.size()){
+ //first found
+ outkey=(SQInteger)idx;
+ outval=_values[idx];
+ //return idx for the next iteration
+ return ++idx;
+ }
+ //nothing to iterate anymore
+ return -1;
+ }
+ SQArray *Clone(){SQArray *anew=Create(_opt_ss(this),Size()); anew->_values.copy(_values); return anew; }
+ int Size() const {return _values.size();}
+ void Resize(int size,SQObjectPtr &fill = _null_) { _values.resize(size,fill); ShrinkIfNeeded(); }
+ void Reserve(int size) { _values.reserve(size); }
+ void Append(const SQObject &o){_values.push_back(o);}
+ void Extend(const SQArray *a);
+ SQObjectPtr &Top(){return _values.top();}
+ void Pop(){_values.pop_back(); ShrinkIfNeeded(); }
+ void Insert(const SQObject& idx,const SQObject &val){_values.insert((unsigned int)tointeger(idx),val);}
+ void ShrinkIfNeeded() {
+ if(_values.size() <= _values.capacity()>>2) //shrink the array
+ _values.shrinktofit();
+ }
+ void Remove(unsigned int idx){
+ _values.remove(idx);
+ ShrinkIfNeeded();
+ }
+ void Release()
+ {
+ sq_delete(this,SQArray);
+ }
+ SQObjectPtrVec _values;
+};
+#endif //_SQARRAY_H_