sqarray.h 2.8 KB

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