ScummVM API documentation
sqarray.h
1/* see copyright notice in squirrel.h */
2#ifndef _SQARRAY_H_
3#define _SQARRAY_H_
4
5struct SQArray : public CHAINABLE_OBJ
6{
7private:
8 SQArray(SQSharedState *ss,SQInteger nsize){_values.resize(nsize); INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);}
10 {
11 REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
12 }
13public:
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 SQObjectType GetType() {return OT_ARRAY;}
22#endif
23 void Finalize(){
24 _values.resize(0);
25 }
26 bool Get(const SQInteger nidx,SQObjectPtr &val)
27 {
28 if(nidx>=0 && nidx<(SQInteger)_values.size()){
29 SQObjectPtr &o = _values[nidx];
30 val = _realval(o);
31 return true;
32 }
33 else return false;
34 }
35 bool Set(const SQInteger nidx,const SQObjectPtr &val)
36 {
37 if(nidx>=0 && nidx<(SQInteger)_values.size()){
38 _values[nidx]=val;
39 return true;
40 }
41 else return false;
42 }
43 SQInteger Next(const SQObjectPtr &refpos,SQObjectPtr &outkey,SQObjectPtr &outval)
44 {
45 SQUnsignedInteger idx=TranslateIndex(refpos);
46 if(idx<_values.size()){
47 //first found
48 outkey=(SQInteger)idx;
49 SQObjectPtr &o = _values[idx];
50 outval = _realval(o);
51 //return idx for the next iteration
52 return ++idx;
53 }
54 //nothing to iterate anymore
55 return -1;
56 }
57 SQArray *Clone(){SQArray *anew=Create(_opt_ss(this),0); anew->_values.copy(_values); return anew; }
58 SQInteger Size() const {return _values.size();}
59 void Resize(SQInteger size)
60 {
61 SQObjectPtr _null;
62 Resize(size,_null);
63 }
64 void Resize(SQInteger size,SQObjectPtr &fill) { _values.resize(size,fill); ShrinkIfNeeded(); }
65 void Reserve(SQInteger size) { _values.reserve(size); }
66 void Append(const SQObject &o){_values.push_back(o);}
67 void Extend(const SQArray *a);
68 SQObjectPtr &Top(){return _values.top();}
69 void Pop(){_values.pop_back(); ShrinkIfNeeded(); }
70 bool Insert(SQInteger idx,const SQObject &val){
71 if(idx < 0 || idx > (SQInteger)_values.size())
72 return false;
73 _values.insert(idx,val);
74 return true;
75 }
76 void ShrinkIfNeeded() {
77 if(_values.size() <= _values.capacity()>>2) //shrink the array
78 _values.shrinktofit();
79 }
80 bool Remove(SQInteger idx){
81 if(idx < 0 || idx >= (SQInteger)_values.size())
82 return false;
83 _values.remove(idx);
84 ShrinkIfNeeded();
85 return true;
86 }
87 void Release()
88 {
89 sq_delete(this,SQArray);
90 }
91
92 SQObjectPtrVec _values;
93};
94#endif //_SQARRAY_H_
signed char * fill(signed char *first, signed char *last, Value val)
Definition: algorithm.h:168
Definition: sqarray.h:6
Definition: sqobject.h:313
Definition: sqobject.h:206
Definition: sqstate.h:60
Definition: squirrel.h:154