The Squirrel interpreter. See http://www.squirrel-lang.org/
squirrel/sqarray.h@3:7268a3ceaffc, 2014-12-16 (annotated)
- Committer:
- jhnwkmn
- Date:
- Tue Dec 16 11:39:42 2014 +0000
- Revision:
- 3:7268a3ceaffc
- Parent:
- 0:97a4f8cc534c
Accepts \r as line terminator as well.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jhnwkmn | 0:97a4f8cc534c | 1 | /* see copyright notice in squirrel.h */ |
jhnwkmn | 0:97a4f8cc534c | 2 | #ifndef _SQARRAY_H_ |
jhnwkmn | 0:97a4f8cc534c | 3 | #define _SQARRAY_H_ |
jhnwkmn | 0:97a4f8cc534c | 4 | |
jhnwkmn | 0:97a4f8cc534c | 5 | struct SQArray : public CHAINABLE_OBJ |
jhnwkmn | 0:97a4f8cc534c | 6 | { |
jhnwkmn | 0:97a4f8cc534c | 7 | private: |
jhnwkmn | 0:97a4f8cc534c | 8 | SQArray(SQSharedState *ss,SQInteger nsize){_values.resize(nsize); INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);} |
jhnwkmn | 0:97a4f8cc534c | 9 | ~SQArray() |
jhnwkmn | 0:97a4f8cc534c | 10 | { |
jhnwkmn | 0:97a4f8cc534c | 11 | REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this); |
jhnwkmn | 0:97a4f8cc534c | 12 | } |
jhnwkmn | 0:97a4f8cc534c | 13 | public: |
jhnwkmn | 0:97a4f8cc534c | 14 | static SQArray* Create(SQSharedState *ss,SQInteger nInitialSize){ |
jhnwkmn | 0:97a4f8cc534c | 15 | SQArray *newarray=(SQArray*)SQ_MALLOC(sizeof(SQArray)); |
jhnwkmn | 0:97a4f8cc534c | 16 | new (newarray) SQArray(ss,nInitialSize); |
jhnwkmn | 0:97a4f8cc534c | 17 | return newarray; |
jhnwkmn | 0:97a4f8cc534c | 18 | } |
jhnwkmn | 0:97a4f8cc534c | 19 | #ifndef NO_GARBAGE_COLLECTOR |
jhnwkmn | 0:97a4f8cc534c | 20 | void Mark(SQCollectable **chain); |
jhnwkmn | 0:97a4f8cc534c | 21 | SQObjectType GetType() {return OT_ARRAY;} |
jhnwkmn | 0:97a4f8cc534c | 22 | #endif |
jhnwkmn | 0:97a4f8cc534c | 23 | void Finalize(){ |
jhnwkmn | 0:97a4f8cc534c | 24 | _values.resize(0); |
jhnwkmn | 0:97a4f8cc534c | 25 | } |
jhnwkmn | 0:97a4f8cc534c | 26 | bool Get(const SQInteger nidx,SQObjectPtr &val) |
jhnwkmn | 0:97a4f8cc534c | 27 | { |
jhnwkmn | 0:97a4f8cc534c | 28 | if(nidx>=0 && nidx<(SQInteger)_values.size()){ |
jhnwkmn | 0:97a4f8cc534c | 29 | SQObjectPtr &o = _values[nidx]; |
jhnwkmn | 0:97a4f8cc534c | 30 | val = _realval(o); |
jhnwkmn | 0:97a4f8cc534c | 31 | return true; |
jhnwkmn | 0:97a4f8cc534c | 32 | } |
jhnwkmn | 0:97a4f8cc534c | 33 | else return false; |
jhnwkmn | 0:97a4f8cc534c | 34 | } |
jhnwkmn | 0:97a4f8cc534c | 35 | bool Set(const SQInteger nidx,const SQObjectPtr &val) |
jhnwkmn | 0:97a4f8cc534c | 36 | { |
jhnwkmn | 0:97a4f8cc534c | 37 | if(nidx>=0 && nidx<(SQInteger)_values.size()){ |
jhnwkmn | 0:97a4f8cc534c | 38 | _values[nidx]=val; |
jhnwkmn | 0:97a4f8cc534c | 39 | return true; |
jhnwkmn | 0:97a4f8cc534c | 40 | } |
jhnwkmn | 0:97a4f8cc534c | 41 | else return false; |
jhnwkmn | 0:97a4f8cc534c | 42 | } |
jhnwkmn | 0:97a4f8cc534c | 43 | SQInteger Next(const SQObjectPtr &refpos,SQObjectPtr &outkey,SQObjectPtr &outval) |
jhnwkmn | 0:97a4f8cc534c | 44 | { |
jhnwkmn | 0:97a4f8cc534c | 45 | SQUnsignedInteger idx=TranslateIndex(refpos); |
jhnwkmn | 0:97a4f8cc534c | 46 | while(idx<_values.size()){ |
jhnwkmn | 0:97a4f8cc534c | 47 | //first found |
jhnwkmn | 0:97a4f8cc534c | 48 | outkey=(SQInteger)idx; |
jhnwkmn | 0:97a4f8cc534c | 49 | SQObjectPtr &o = _values[idx]; |
jhnwkmn | 0:97a4f8cc534c | 50 | outval = _realval(o); |
jhnwkmn | 0:97a4f8cc534c | 51 | //return idx for the next iteration |
jhnwkmn | 0:97a4f8cc534c | 52 | return ++idx; |
jhnwkmn | 0:97a4f8cc534c | 53 | } |
jhnwkmn | 0:97a4f8cc534c | 54 | //nothing to iterate anymore |
jhnwkmn | 0:97a4f8cc534c | 55 | return -1; |
jhnwkmn | 0:97a4f8cc534c | 56 | } |
jhnwkmn | 0:97a4f8cc534c | 57 | SQArray *Clone(){SQArray *anew=Create(_opt_ss(this),0); anew->_values.copy(_values); return anew; } |
jhnwkmn | 0:97a4f8cc534c | 58 | SQInteger Size() const {return _values.size();} |
jhnwkmn | 0:97a4f8cc534c | 59 | void Resize(SQInteger size) |
jhnwkmn | 0:97a4f8cc534c | 60 | { |
jhnwkmn | 0:97a4f8cc534c | 61 | SQObjectPtr _null; |
jhnwkmn | 0:97a4f8cc534c | 62 | Resize(size,_null); |
jhnwkmn | 0:97a4f8cc534c | 63 | } |
jhnwkmn | 0:97a4f8cc534c | 64 | void Resize(SQInteger size,SQObjectPtr &fill) { _values.resize(size,fill); ShrinkIfNeeded(); } |
jhnwkmn | 0:97a4f8cc534c | 65 | void Reserve(SQInteger size) { _values.reserve(size); } |
jhnwkmn | 0:97a4f8cc534c | 66 | void Append(const SQObject &o){_values.push_back(o);} |
jhnwkmn | 0:97a4f8cc534c | 67 | void Extend(const SQArray *a); |
jhnwkmn | 0:97a4f8cc534c | 68 | SQObjectPtr &Top(){return _values.top();} |
jhnwkmn | 0:97a4f8cc534c | 69 | void Pop(){_values.pop_back(); ShrinkIfNeeded(); } |
jhnwkmn | 0:97a4f8cc534c | 70 | bool Insert(SQInteger idx,const SQObject &val){ |
jhnwkmn | 0:97a4f8cc534c | 71 | if(idx < 0 || idx > (SQInteger)_values.size()) |
jhnwkmn | 0:97a4f8cc534c | 72 | return false; |
jhnwkmn | 0:97a4f8cc534c | 73 | _values.insert(idx,val); |
jhnwkmn | 0:97a4f8cc534c | 74 | return true; |
jhnwkmn | 0:97a4f8cc534c | 75 | } |
jhnwkmn | 0:97a4f8cc534c | 76 | void ShrinkIfNeeded() { |
jhnwkmn | 0:97a4f8cc534c | 77 | if(_values.size() <= _values.capacity()>>2) //shrink the array |
jhnwkmn | 0:97a4f8cc534c | 78 | _values.shrinktofit(); |
jhnwkmn | 0:97a4f8cc534c | 79 | } |
jhnwkmn | 0:97a4f8cc534c | 80 | bool Remove(SQInteger idx){ |
jhnwkmn | 0:97a4f8cc534c | 81 | if(idx < 0 || idx >= (SQInteger)_values.size()) |
jhnwkmn | 0:97a4f8cc534c | 82 | return false; |
jhnwkmn | 0:97a4f8cc534c | 83 | _values.remove(idx); |
jhnwkmn | 0:97a4f8cc534c | 84 | ShrinkIfNeeded(); |
jhnwkmn | 0:97a4f8cc534c | 85 | return true; |
jhnwkmn | 0:97a4f8cc534c | 86 | } |
jhnwkmn | 0:97a4f8cc534c | 87 | void Release() |
jhnwkmn | 0:97a4f8cc534c | 88 | { |
jhnwkmn | 0:97a4f8cc534c | 89 | sq_delete(this,SQArray); |
jhnwkmn | 0:97a4f8cc534c | 90 | } |
jhnwkmn | 0:97a4f8cc534c | 91 | |
jhnwkmn | 0:97a4f8cc534c | 92 | SQObjectPtrVec _values; |
jhnwkmn | 0:97a4f8cc534c | 93 | }; |
jhnwkmn | 0:97a4f8cc534c | 94 | #endif //_SQARRAY_H_ |