Update Squirrel to 2.2.2 (although README says 2.2.1)
[supertux.git] / src / squirrel / squirrel / sqarray.h
1 /*      see copyright notice in squirrel.h */
2 #ifndef _SQARRAY_H_
3 #define _SQARRAY_H_
4
5 struct SQArray : public CHAINABLE_OBJ
6 {
7 private:
8         SQArray(SQSharedState *ss,SQInteger nsize){_values.resize(nsize); INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);}
9         ~SQArray()
10         {
11                 REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
12         }
13 public:
14         static SQArray* Create(SQSharedState *ss,SQInteger nInitialSize){
15                 SQArray *newarray=(SQArray*)SQ_MALLOC(sizeof(SQArray));
16                 new (newarray) SQArray(ss,nInitialSize);
17                 return newarray;
18         }
19 #ifndef NO_GARBAGE_COLLECTOR
20         void Mark(SQCollectable **chain);
21 #endif
22         void Finalize(){
23                 _values.resize(0);
24         }
25         bool Get(const SQInteger nidx,SQObjectPtr &val)
26         {
27                 if(nidx>=0 && nidx<(SQInteger)_values.size()){
28                         SQObjectPtr &o = _values[nidx];
29                         val = _realval(o);
30                         return true;
31                 }
32                 else return false;
33         }
34         bool Set(const SQInteger nidx,const SQObjectPtr &val)
35         {
36                 if(nidx>=0 && nidx<(SQInteger)_values.size()){
37                         _values[nidx]=val;
38                         return true;
39                 }
40                 else return false;
41         }
42         SQInteger Next(const SQObjectPtr &refpos,SQObjectPtr &outkey,SQObjectPtr &outval)
43         {
44                 SQUnsignedInteger idx=TranslateIndex(refpos);
45                 while(idx<_values.size()){
46                         //first found
47                         outkey=(SQInteger)idx;
48                         SQObjectPtr &o = _values[idx];
49                         outval = _realval(o);
50                         //return idx for the next iteration
51                         return ++idx;
52                 }
53                 //nothing to iterate anymore
54                 return -1;
55         }
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())
66                         return false;
67                 _values.insert(idx,val);
68                 return true;
69         }
70         void ShrinkIfNeeded() {
71                 if(_values.size() <= _values.capacity()>>2) //shrink the array
72                         _values.shrinktofit();
73         }
74         bool Remove(SQInteger idx){
75                 if(idx < 0 || idx >= (SQInteger)_values.size())
76                         return false;
77                 _values.remove(idx);
78                 ShrinkIfNeeded();
79                 return true;
80         }
81         void Release()
82         {
83                 sq_delete(this,SQArray);
84         }
85         SQObjectPtrVec _values;
86 };
87 #endif //_SQARRAY_H_