array.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #ifndef ARRAY_H
  2. #define ARRAY_H
  3. #include "../utils/types.h"
  4. #include "../utils/var_mem_mngr.h"
  5. namespace binom {
  6. class Array : VarMemoryManager<Array> {
  7. friend class VarMemoryManager<Array>;
  8. friend class Variable;
  9. union types {
  10. void* ptr;
  11. VarType* type;
  12. byte* bytes;
  13. types(void* ptr) : ptr(ptr) {}
  14. } data;
  15. inline ui64& length() const {return *reinterpret_cast<ui64*>(data.bytes + 1);}
  16. inline byte*& ptrImpl() {return data.bytes;}
  17. inline ui64 msizeImpl() const {return 9 + length() * PTR_SZ;}
  18. void* cloneImpl() const;
  19. void destroyImpl();
  20. public:
  21. Array();
  22. Array(varr array);
  23. Array(const Array& other);
  24. Array(Array&& other);
  25. ~Array() {destroy ();}
  26. ByteArray serialize() const;
  27. static Array deserialize(ByteArray::iterator& it);
  28. inline bool isEmpty() const {return !length();}
  29. inline ui64 getMemberCount() const {return *reinterpret_cast<ui64*>(data.bytes + 1);}
  30. inline bool inRange(ui64 index) const {return length() > index;}
  31. Variable& getVariable(ui64 index) const;
  32. Variable& insert(ui64 index, Variable var);
  33. Variable& pushBack(Variable var);
  34. Variable& pushFront(Variable var);
  35. Array subarr(ui64 index, ui64 n);
  36. void remove(ui64 index, ui64 n = 1);
  37. void popBack(ui64 n = 1);
  38. void popFront(ui64 n = 1);
  39. void clear();
  40. inline Variable& operator[](ui64 index) const {return getVariable(index);}
  41. Array& operator+=(Variable var);
  42. Array& operator=(Array other);
  43. bool operator==(Array other) const;
  44. bool operator!=(Array other) const;
  45. bool operator<(Array other) const;
  46. bool operator<=(Array other) const;
  47. bool operator>(Array other) const;
  48. bool operator>=(Array other) const;
  49. ui8 getCompare(Array other) const;
  50. ArrayIterator begin() const {return reinterpret_cast<ArrayIterator>(data.bytes + 9);}
  51. ArrayIterator end() const {return reinterpret_cast<ArrayIterator>(data.bytes + msize());}
  52. Variable& asVar() {return *reinterpret_cast<Variable*>(this);}
  53. };
  54. }
  55. #endif