Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int _kit_coreVectorSetUnitAxis(kit_coreVector** Vector_p, char axis, lens_t newLen){
- _IF_SDLERR(!newLen,;,"!newLen")
- kit_coreVector* Vector=*Vector_p;
- int isLowerCase = IS_LOWER(axis); //if axis was uppercase, keep oldSize set to 0
- SET_TO_LOWER(axis); //assuming ascii-like encoding
- _asize_t oldSize = 0;
- _asize_t newSize = Vector->unit*newLen;
- if(axis=='x'){ //implies vector is 1D
- _IF_SDLERR((Vector->_dims&3)!=1,;,"x && !1D")
- oldSize =sizeof(kit_coreVector);
- newSize+=sizeof(kit_coreVector);
- if(isLowerCase) oldSize += Vector->unit*Vector->x;
- if(oldSize == newSize) return 0;
- _IF_GOTO_ERROR(_kit_coreVectorRealloc(Vector_p, 'x', oldSize,newSize),;)
- Vector->x=newLen;
- *Vector_p=Vector;
- } else if(axis=='y'){ //implies vector is 2D
- _IF_SDLERR((Vector->_dims&3)!=2,;,"y && !2D")
- if(isLowerCase) oldSize = Vector->unit*Vector->y;
- void** p2d=Vector->p2d;
- lens_t x_len=Vector->x;
- for(lens_t xi=0; xi<x_len; ++xi)
- _IF_GOTO_ERROR(_kit_coreVectorRealloc(&p2d[xi], 'y', oldSize*(p2d[xi]!=NULL),newSize),;)
- if(x_len) Vector->y=newLen;
- } else if(axis=='z'){ //implies vector is 3D
- _IF_SDLERR((Vector->_dims&3)!=3,;,"z && !3D")
- if(isLowerCase) oldSize = Vector->unit*Vector->z;
- void*** p3d=Vector->p3d;
- lens_t x_len=Vector->x;
- lens_t y_len=Vector->y;
- for(lens_t xi=0; xi<x_len; ++xi){
- void** p2d=p3d[xi];
- for(lens_t yi=0; yi<y_len; ++yi)
- _IF_GOTO_ERROR(_kit_coreVectorRealloc(&p2d[yi], 'z', oldSize*(p2d[yi]!=NULL),newSize),;)
- }
- if(x_len && y_len) Vector->z=newLen;
- } else _IS_SDLERR(;,"axis=%c",axis)
- /*!err*/ return 0; // 0 on success
- _error_: return -1; //-1 on failure
- }
- //by "Add" i mean setting to a new length GREATER than the previous length
- int _kit_coreVectorAddPointerAxis(kit_coreVector** Vector_p, char axis, lens_t newLen){
- _IF_SDLERR(!newLen,;,"!newLen")
- kit_coreVector* Vector=*Vector_p;
- int isLowerCase = IS_LOWER(axis); //if axis was uppercase, keep oldSize set to 0
- SET_TO_LOWER(axis); //assuming ascii-like encoding
- _asize_t oldSize = 0;
- _asize_t newSize = sizeof(void*)*newLen;
- if(axis=='x'){
- //shift both forward by size of kit_coreVector struct
- newSize += oldSize=sizeof(kit_coreVector);
- if(isLowerCase) oldSize += sizeof(void*)*Vector->x;
- } else if(axis=='y') {
- //(= is the same as += if oldSize was 0)
- if(isLowerCase) oldSize = sizeof(void*)*Vector->y;
- } else _IS_SDLERR(;,"axis=%c",axis)
- void** p2d=Vector->p2d;
- int dims=Vector->_dims&3;
- switch((dims<<8)|axis){
- case (0x200|'x'): //2D, x axis
- if(Vector->x == newLen) break;
- _IF_SDLERR(newLen<Vector->x,;,"newLen<x");
- //extend x
- _IF_GOTO_ERROR(_kit_coreVectorRealloc(Vector_p, 'x', oldSize,newSize),;)
- Vector->x=newLen; //update x length to new value
- //fill in new x indices with additional y units
- _IF_GOTO_ERROR(_kit_coreVectorSetUnitAxis(Vector_p, 'y', Vector->y),;)
- break;
- case (0x300|'x'): //3D, x axis
- if(Vector->x == newLen) break;
- _IF_SDLERR(newLen<Vector->x,;,"newLen<x");
- //extend x
- _IF_GOTO_ERROR(_kit_coreVectorRealloc(Vector_p, 'x', oldSize,newSize),;)
- lens_t x_old = Vector->x;
- lens_t x_new = Vector->x=newLen; //(also updates x length to new value)
- //put additional y pointers in new x indices
- _asize_t y_size = sizeof(void*)*Vector->y;
- p2d=Vector->p2d;
- for(lens_t xi=x_old; xi<x_new; ++xi)
- _IF_GOTO_ERROR(_kit_coreVectorRealloc(&p2d[xi], 'y', 0,y_size),;)
- //fill in new y indices with additional z units
- _IF_GOTO_ERROR(_kit_coreVectorSetUnitAxis(Vector_p, 'z', Vector->z),;)
- break;
- case (0x300|'y'):; //3D, y axis
- if(Vector->y == newLen) break;
- _IF_SDLERR(newLen<Vector->y,;,"newLen<y");
- //extend y
- lens_t x_len = Vector->x;
- p2d=Vector->p2d;
- for(lens_t xi=0; xi<x_len; ++xi)
- _IF_GOTO_ERROR(_kit_coreVectorRealloc(&p2d[xi], 'y', oldSize,newSize),;)
- Vector->y=newLen; //update y length to new value
- //fill in new y indices with additional z units
- _IF_GOTO_ERROR(_kit_coreVectorSetUnitAxis(Vector_p, 'z', Vector->z),;)
- break;
- default: _IS_SDLERR(;,"%uD && %c", dims, axis)
- }
- /*!err*/ return 0; // 0 on success
- _error_: return -1; //-1 on failure
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement