tuplelist.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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(std::initializer_list<PyObject*> list): Tuple(list.size()){
  31. int i = 0;
  32. for(PyObject* obj: list) _args[i++] = obj;
  33. }
  34. Tuple::~Tuple(){ if(!is_inlined()) pool64_dealloc(_args); }
  35. List ArgsView::to_list() const{
  36. List ret(size());
  37. for(int i=0; i<size(); i++) ret[i] = _begin[i];
  38. return ret;
  39. }
  40. Tuple ArgsView::to_tuple() const{
  41. Tuple ret(size());
  42. for(int i=0; i<size(); i++) ret[i] = _begin[i];
  43. return ret;
  44. }
  45. } // namespace pkpy