tuplelist.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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(){ if(!is_inlined()) pool64_dealloc(_args); }
  40. List ArgsView::to_list() const{
  41. List ret(size());
  42. for(int i=0; i<size(); i++) ret[i] = _begin[i];
  43. return ret;
  44. }
  45. Tuple ArgsView::to_tuple() const{
  46. Tuple ret(size());
  47. for(int i=0; i<size(); i++) ret[i] = _begin[i];
  48. return ret;
  49. }
  50. } // namespace pkpy