- More work on scripting interface
[supertux.git] / src / squirrel / squirrel / sqarray.h
1 /*      see copyright notice in squirrel.h */\r
2 #ifndef _SQARRAY_H_\r
3 #define _SQARRAY_H_\r
4 \r
5 struct SQArray : public CHAINABLE_OBJ\r
6 {\r
7 private:\r
8         SQArray(SQSharedState *ss,int nsize){_values.resize(nsize);_uiRef=0;INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);}\r
9         ~SQArray()\r
10         {\r
11                 REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);\r
12         }\r
13 public:\r
14         static SQArray* Create(SQSharedState *ss,int nInitialSize){\r
15                 SQArray *newarray=(SQArray*)SQ_MALLOC(sizeof(SQArray));\r
16                 new (newarray) SQArray(ss,nInitialSize);\r
17                 return newarray;\r
18         }\r
19 #ifndef NO_GARBAGE_COLLECTOR\r
20         void Mark(SQCollectable **chain);\r
21 #endif\r
22         void Finalize(){\r
23                 _values.resize(0);\r
24         }\r
25         bool Get(const int nidx,SQObjectPtr &val)\r
26         {\r
27                 if(nidx>=0 && nidx<(int)_values.size()){\r
28                         val=_values[nidx];\r
29                         return true;\r
30                 }\r
31                 else return false;\r
32         }\r
33         bool Set(const int nidx,const SQObjectPtr &val)\r
34         {\r
35                 if(nidx>=0 && nidx<(int)_values.size()){\r
36                         _values[nidx]=val;\r
37                         return true;\r
38                 }\r
39                 else return false;\r
40         }\r
41         int Next(const SQObjectPtr &refpos,SQObjectPtr &outkey,SQObjectPtr &outval)\r
42         {\r
43                 unsigned int idx=TranslateIndex(refpos);\r
44                 while(idx<_values.size()){\r
45                         //first found\r
46                         outkey=(SQInteger)idx;\r
47                         outval=_values[idx];\r
48                         //return idx for the next iteration\r
49                         return ++idx;\r
50                 }\r
51                 //nothing to iterate anymore\r
52                 return -1;\r
53         }\r
54         SQArray *Clone(){SQArray *anew=Create(_opt_ss(this),Size()); anew->_values.copy(_values); return anew; }\r
55         int Size() const {return _values.size();}\r
56         void Resize(int size,SQObjectPtr &fill = _null_) { _values.resize(size,fill); ShrinkIfNeeded(); }\r
57         void Reserve(int size) { _values.reserve(size); }\r
58         void Append(const SQObject &o){_values.push_back(o);}\r
59         void Extend(const SQArray *a);\r
60         SQObjectPtr &Top(){return _values.top();}\r
61         void Pop(){_values.pop_back(); ShrinkIfNeeded(); }\r
62         void Insert(const SQObject& idx,const SQObject &val){_values.insert((unsigned int)tointeger(idx),val);}\r
63         void ShrinkIfNeeded() {\r
64                 if(_values.size() <= _values.capacity()>>2) //shrink the array\r
65                         _values.shrinktofit();\r
66         }\r
67         void Remove(unsigned int idx){\r
68                 _values.remove(idx);\r
69                 ShrinkIfNeeded();\r
70         }\r
71         void Release()\r
72         {\r
73                 sq_delete(this,SQArray);\r
74         }\r
75         SQObjectPtrVec _values;\r
76 };\r
77 #endif //_SQARRAY_H_\r