tuplelist.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "pocketpy/tuplelist.h"
  2. namespace pkpy {
  3. Tuple::Tuple(int n){
  4. if(n <= 3){
  5. this->_args = _inlined;
  6. }else{
  7. this->_args = (PyObject**)pool64_alloc(n * sizeof(void*));
  8. }
  9. this->_size = n;
  10. }
  11. Tuple::Tuple(const Tuple& other): Tuple(other._size){
  12. for(int i=0; i<_size; i++) _args[i] = other._args[i];
  13. }
  14. Tuple::Tuple(Tuple&& other) noexcept {
  15. _size = other._size;
  16. if(other.is_inlined()){
  17. _args = _inlined;
  18. for(int i=0; i<_size; i++) _args[i] = other._args[i];
  19. }else{
  20. _args = other._args;
  21. other._args = other._inlined;
  22. other._size = 0;
  23. }
  24. }
  25. Tuple::Tuple(List&& other) noexcept {
  26. _size = other.size();
  27. _args = other._data;
  28. other._data = nullptr;
  29. }
  30. Tuple::Tuple(PyObject* _0, PyObject* _1): Tuple(2){
  31. _args[0] = _0;
  32. _args[1] = _1;
  33. }
  34. Tuple::Tuple(PyObject* _0, PyObject* _1, PyObject* _2): Tuple(3){
  35. _args[0] = _0;
  36. _args[1] = _1;
  37. _args[2] = _2;
  38. }
  39. Tuple::Tuple(PyObject* _0, PyObject* _1, PyObject* _2, PyObject* _3): Tuple(4){
  40. _args[0] = _0;
  41. _args[1] = _1;
  42. _args[2] = _2;
  43. _args[3] = _3;
  44. }
  45. Tuple::~Tuple(){ if(!is_inlined()) pool64_dealloc(_args); }
  46. List ArgsView::to_list() const{
  47. List ret(size());
  48. for(int i=0; i<size(); i++) ret[i] = _begin[i];
  49. return ret;
  50. }
  51. Tuple ArgsView::to_tuple() const{
  52. Tuple ret(size());
  53. for(int i=0; i<size(); i++) ret[i] = _begin[i];
  54. return ret;
  55. }
  56. } // namespace pkpy