sqarray.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 Exists(const SQInteger nidx)
  35. {
  36. return (nidx>=0 && nidx<(SQInteger)_values.size());
  37. }
  38. bool Set(const SQInteger nidx,const SQObjectPtr &val)
  39. {
  40. if(nidx>=0 && nidx<(SQInteger)_values.size()){
  41. _values[nidx]=val;
  42. return true;
  43. }
  44. else return false;
  45. }
  46. SQInteger Next(const SQObjectPtr &refpos,SQObjectPtr &outkey,SQObjectPtr &outval)
  47. {
  48. SQUnsignedInteger idx=TranslateIndex(refpos);
  49. while(idx<_values.size()){
  50. //first found
  51. outkey=(SQInteger)idx;
  52. SQObjectPtr &o = _values[idx];
  53. outval = _realval(o);
  54. //return idx for the next iteration
  55. return ++idx;
  56. }
  57. //nothing to iterate anymore
  58. return -1;
  59. }
  60. SQArray *Clone(){SQArray *anew=Create(_opt_ss(this),0); anew->_values.copy(_values); return anew; }
  61. SQInteger Size() const {return _values.size();}
  62. void Resize(SQInteger size)
  63. {
  64. SQObjectPtr _null;
  65. Resize(size,_null);
  66. }
  67. void Resize(SQInteger size,SQObjectPtr &fill) { _values.resize(size,fill); ShrinkIfNeeded(); }
  68. void Reserve(SQInteger size) { _values.reserve(size); }
  69. void Append(const SQObject &o){_values.push_back(o);}
  70. void Extend(const SQArray *a);
  71. SQObjectPtr &Top(){return _values.top();}
  72. void Pop(){_values.pop_back(); ShrinkIfNeeded(); }
  73. bool Insert(SQInteger idx,const SQObject &val){
  74. if(idx < 0 || idx > (SQInteger)_values.size())
  75. return false;
  76. _values.insert(idx,val);
  77. return true;
  78. }
  79. void ShrinkIfNeeded() {
  80. if(_values.size() <= _values.capacity()>>2) //shrink the array
  81. _values.shrinktofit();
  82. }
  83. bool Remove(SQInteger idx){
  84. if(idx < 0 || idx >= (SQInteger)_values.size())
  85. return false;
  86. _values.remove(idx);
  87. ShrinkIfNeeded();
  88. return true;
  89. }
  90. void Release()
  91. {
  92. sq_delete(this,SQArray);
  93. }
  94. SQObjectPtrVec _values;
  95. };
  96. #endif //_SQARRAY_H_