sqarray.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* see copyright notice in squirrel.h */
  2. #ifndef _SQARRAY_H_
  3. #define _SQARRAY_H_
  4. struct SQArray : public CHAINABLE_OBJ
  5. {
  6. private:
  7. SQArray(SQSharedState *ss,SQInteger nsize){_values.resize(nsize); INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);}
  8. ~SQArray()
  9. {
  10. REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
  11. }
  12. public:
  13. static SQArray* Create(SQSharedState *ss,SQInteger nInitialSize){
  14. SQArray *newarray=(SQArray*)SQ_MALLOC(sizeof(SQArray));
  15. new (newarray) SQArray(ss,nInitialSize);
  16. return newarray;
  17. }
  18. #ifndef NO_GARBAGE_COLLECTOR
  19. void Mark(SQCollectable **chain);
  20. SQObjectType GetType() {return OT_ARRAY;}
  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),0); anew->_values.copy(_values); return anew; }
  57. SQInteger Size() const {return _values.size();}
  58. void Resize(SQInteger size)
  59. {
  60. SQObjectPtr _null;
  61. Resize(size,_null);
  62. }
  63. void Resize(SQInteger size,SQObjectPtr &fill) { _values.resize(size,fill); ShrinkIfNeeded(); }
  64. void Reserve(SQInteger size) { _values.reserve(size); }
  65. void Append(const SQObject &o){_values.push_back(o);}
  66. void Extend(const SQArray *a);
  67. SQObjectPtr &Top(){return _values.top();}
  68. void Pop(){_values.pop_back(); ShrinkIfNeeded(); }
  69. bool Insert(SQInteger idx,const SQObject &val){
  70. if(idx < 0 || idx > (SQInteger)_values.size())
  71. return false;
  72. _values.insert(idx,val);
  73. return true;
  74. }
  75. void ShrinkIfNeeded() {
  76. if(_values.size() <= _values.capacity()>>2) //shrink the array
  77. _values.shrinktofit();
  78. }
  79. bool Remove(SQInteger idx){
  80. if(idx < 0 || idx >= (SQInteger)_values.size())
  81. return false;
  82. _values.remove(idx);
  83. ShrinkIfNeeded();
  84. return true;
  85. }
  86. void Release()
  87. {
  88. sq_delete(this,SQArray);
  89. }
  90. SQObjectPtrVec _values;
  91. };
  92. #endif //_SQARRAY_H_