fixed warnings in squirrel
[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,int nsize){_values.resize(nsize);_uiRef=0;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,int 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 int nidx,SQObjectPtr &val)
26         {
27                 if(nidx>=0 && nidx<(int)_values.size()){
28                         val=_values[nidx];
29                         return true;
30                 }
31                 else return false;
32         }
33         bool Set(const int nidx,const SQObjectPtr &val)
34         {
35                 if(nidx>=0 && nidx<(int)_values.size()){
36                         _values[nidx]=val;
37                         return true;
38                 }
39                 else return false;
40         }
41         int Next(const SQObjectPtr &refpos,SQObjectPtr &outkey,SQObjectPtr &outval)
42         {
43                 unsigned int idx=TranslateIndex(refpos);
44                 while(idx<_values.size()){
45                         //first found
46                         outkey=(SQInteger)idx;
47                         outval=_values[idx];
48                         //return idx for the next iteration
49                         return ++idx;
50                 }
51                 //nothing to iterate anymore
52                 return -1;
53         }
54         SQArray *Clone(){SQArray *anew=Create(_opt_ss(this),Size()); anew->_values.copy(_values); return anew; }
55         int Size() const {return _values.size();}
56         void Resize(int size,SQObjectPtr &fill = _null_) { _values.resize(size,fill); ShrinkIfNeeded(); }
57         void Reserve(int size) { _values.reserve(size); }
58         void Append(const SQObject &o){_values.push_back(o);}
59         void Extend(const SQArray *a);
60         SQObjectPtr &Top(){return _values.top();}
61         void Pop(){_values.pop_back(); ShrinkIfNeeded(); }
62         void Insert(const SQObject& idx,const SQObject &val){_values.insert((unsigned int)tointeger(idx),val);}
63         void ShrinkIfNeeded() {
64                 if(_values.size() <= _values.capacity()>>2) //shrink the array
65                         _values.shrinktofit();
66         }
67         void Remove(unsigned int idx){
68                 _values.remove(idx);
69                 ShrinkIfNeeded();
70         }
71         void Release()
72         {
73                 sq_delete(this,SQArray);
74         }
75         SQObjectPtrVec _values;
76 };
77 #endif //_SQARRAY_H_