Advertisement
Kitomas

part of coreVector's first refactor attempt

Sep 10th, 2023
1,183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.31 KB | None | 0 0
  1. int _kit_coreVectorSetUnitAxis(kit_coreVector** Vector_p, char axis, lens_t newLen){
  2.   _IF_SDLERR(!newLen,;,"!newLen")
  3.   kit_coreVector* Vector=*Vector_p;
  4.   int isLowerCase = IS_LOWER(axis); //if axis was uppercase, keep oldSize set to 0
  5.   SET_TO_LOWER(axis); //assuming ascii-like encoding
  6.  
  7.   _asize_t oldSize = 0;
  8.   _asize_t newSize = Vector->unit*newLen;
  9.  
  10.   if(axis=='x'){ //implies vector is 1D
  11.     _IF_SDLERR((Vector->_dims&3)!=1,;,"x && !1D")
  12.  
  13.     oldSize =sizeof(kit_coreVector);
  14.     newSize+=sizeof(kit_coreVector);
  15.     if(isLowerCase) oldSize += Vector->unit*Vector->x;
  16.     if(oldSize == newSize) return 0;
  17.  
  18.     _IF_GOTO_ERROR(_kit_coreVectorRealloc(Vector_p, 'x', oldSize,newSize),;)
  19.  
  20.     Vector->x=newLen;
  21.     *Vector_p=Vector;
  22.  
  23.   } else if(axis=='y'){ //implies vector is 2D
  24.     _IF_SDLERR((Vector->_dims&3)!=2,;,"y && !2D")
  25.  
  26.     if(isLowerCase) oldSize = Vector->unit*Vector->y;
  27.  
  28.     void** p2d=Vector->p2d;
  29.     lens_t x_len=Vector->x;
  30.     for(lens_t xi=0; xi<x_len; ++xi)
  31.       _IF_GOTO_ERROR(_kit_coreVectorRealloc(&p2d[xi], 'y', oldSize*(p2d[xi]!=NULL),newSize),;)
  32.  
  33.     if(x_len) Vector->y=newLen;
  34.  
  35.   } else if(axis=='z'){ //implies vector is 3D
  36.     _IF_SDLERR((Vector->_dims&3)!=3,;,"z && !3D")
  37.  
  38.     if(isLowerCase) oldSize = Vector->unit*Vector->z;
  39.  
  40.     void*** p3d=Vector->p3d;
  41.     lens_t x_len=Vector->x;
  42.     lens_t y_len=Vector->y;
  43.     for(lens_t xi=0; xi<x_len; ++xi){
  44.       void** p2d=p3d[xi];
  45.       for(lens_t yi=0; yi<y_len; ++yi)
  46.         _IF_GOTO_ERROR(_kit_coreVectorRealloc(&p2d[yi], 'z', oldSize*(p2d[yi]!=NULL),newSize),;)
  47.     }
  48.  
  49.     if(x_len && y_len) Vector->z=newLen;
  50.  
  51.   } else _IS_SDLERR(;,"axis=%c",axis)
  52.  
  53.   /*!err*/ return  0; // 0 on success
  54.   _error_: return -1; //-1 on failure
  55. }
  56.  
  57. //by "Add" i mean setting to a new length GREATER than the previous length
  58. int _kit_coreVectorAddPointerAxis(kit_coreVector** Vector_p, char axis, lens_t newLen){
  59.   _IF_SDLERR(!newLen,;,"!newLen")
  60.   kit_coreVector* Vector=*Vector_p;
  61.   int isLowerCase = IS_LOWER(axis); //if axis was uppercase, keep oldSize set to 0
  62.   SET_TO_LOWER(axis); //assuming ascii-like encoding
  63.  
  64.   _asize_t oldSize = 0;
  65.   _asize_t newSize = sizeof(void*)*newLen;
  66.   if(axis=='x'){
  67.     //shift both forward by size of kit_coreVector struct
  68.     newSize += oldSize=sizeof(kit_coreVector);
  69.     if(isLowerCase) oldSize += sizeof(void*)*Vector->x;
  70.   } else if(axis=='y') {
  71.     //(= is the same as += if oldSize was 0)
  72.     if(isLowerCase) oldSize  = sizeof(void*)*Vector->y;
  73.   } else _IS_SDLERR(;,"axis=%c",axis)
  74.  
  75.   void** p2d=Vector->p2d;
  76.   int dims=Vector->_dims&3;
  77.   switch((dims<<8)|axis){
  78.   case (0x200|'x'): //2D, x axis
  79.     if(Vector->x == newLen) break;
  80.     _IF_SDLERR(newLen<Vector->x,;,"newLen<x");
  81.  
  82.     //extend x
  83.     _IF_GOTO_ERROR(_kit_coreVectorRealloc(Vector_p, 'x', oldSize,newSize),;)
  84.     Vector->x=newLen; //update x length to new value
  85.  
  86.     //fill in new x indices with additional y units
  87.     _IF_GOTO_ERROR(_kit_coreVectorSetUnitAxis(Vector_p, 'y', Vector->y),;)
  88.     break;
  89.  
  90.   case (0x300|'x'): //3D, x axis
  91.     if(Vector->x == newLen) break;
  92.     _IF_SDLERR(newLen<Vector->x,;,"newLen<x");
  93.  
  94.     //extend x
  95.     _IF_GOTO_ERROR(_kit_coreVectorRealloc(Vector_p, 'x', oldSize,newSize),;)
  96.     lens_t x_old = Vector->x;
  97.     lens_t x_new = Vector->x=newLen; //(also updates x length to new value)
  98.  
  99.     //put additional y pointers in new x indices
  100.     _asize_t y_size = sizeof(void*)*Vector->y;
  101.     p2d=Vector->p2d;
  102.     for(lens_t xi=x_old; xi<x_new; ++xi)
  103.       _IF_GOTO_ERROR(_kit_coreVectorRealloc(&p2d[xi], 'y', 0,y_size),;)
  104.  
  105.     //fill in new y indices with additional z units
  106.     _IF_GOTO_ERROR(_kit_coreVectorSetUnitAxis(Vector_p, 'z', Vector->z),;)
  107.     break;
  108.  
  109.   case (0x300|'y'):; //3D, y axis
  110.     if(Vector->y == newLen) break;
  111.     _IF_SDLERR(newLen<Vector->y,;,"newLen<y");
  112.  
  113.     //extend y
  114.     lens_t x_len = Vector->x;
  115.     p2d=Vector->p2d;
  116.     for(lens_t xi=0; xi<x_len; ++xi)
  117.       _IF_GOTO_ERROR(_kit_coreVectorRealloc(&p2d[xi], 'y', oldSize,newSize),;)
  118.     Vector->y=newLen; //update y length to new value
  119.  
  120.     //fill in new y indices with additional z units
  121.     _IF_GOTO_ERROR(_kit_coreVectorSetUnitAxis(Vector_p, 'z', Vector->z),;)
  122.     break;
  123.  
  124.   default: _IS_SDLERR(;,"%uD && %c", dims, axis)
  125.   }
  126.  
  127.   /*!err*/ return  0; // 0 on success
  128.   _error_: return -1; //-1 on failure
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement