Advertisement
Kitomas

kit_sdl2_coreVector.c as of 2023/9/13

Sep 15th, 2023
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.69 KB | None | 0 0
  1. //my own primitive form of contiguous dynamic arrays
  2. #include "../include/kit_sdl2/kit_macroconst.h"
  3. #include "../include/kit_sdl2/kit_core.h"
  4. #include "../_private/include/_kit_privmacro.h"
  5.  
  6.  
  7.  
  8.  
  9. static inline int _getDimensions(Uint32 x, Uint32 y, Uint32 z){
  10.   int     dims = 1;
  11.   if(y>1) dims = 2;
  12.   if(z>1) dims = 3;
  13.   return dims;
  14. }
  15.  
  16.  
  17. static inline void _truncateLens(Uint32* lens, Uint64 lenslen,
  18.                                  Uint32 x, Uint32 y, Uint32 z)
  19. {
  20.   Uint32 lens_max=_getLens_max(x,y,z);
  21.   //kit_coreLog("lens_max=%u, lenslen=%u, xyz=%u,%u,%u",
  22.   //             lens_max,    lenslen,         x, y, z);
  23.   for(Uint32 i; i<lenslen; ++i)
  24.     if(lens[i]>lens_max) lens[i]=lens_max;
  25. }
  26.  
  27.  
  28. static inline Uint64 _getLenslen(Uint32 x, Uint32 y, Uint32 z){
  29.   Uint64  lenslen  = 1;
  30.   if(y>1) lenslen *= x;
  31.   if(z>1) lenslen *= y;
  32.   //kit_coreLog("lenslen=%u, xyz=%u,%u,%u", lenslen, x,y,z);
  33.   return lenslen;
  34. }
  35. static inline Uint32 _getLens_max(Uint32 x, Uint32 y, Uint32 z){
  36.   Uint32 lens_max;
  37.   if(     z>1) lens_max = z;
  38.   else if(y>1) lens_max = y;
  39.   else  /*x>1*/lens_max = x;
  40.   return lens_max;
  41. }
  42. int _resizeLens(kit_coreVector* Vector){ //also truncates
  43.   Uint32 x=Vector->x;
  44.   Uint32 y=Vector->y;
  45.   Uint32 z=Vector->z;
  46.  
  47.   Uint32 lenslen_old = Vector->lenslen;
  48.   Uint32 lenslen_new = _getLenslen(x,y,z);
  49.  
  50.   Uint32 lensize_old = lenslen_old*sizeof(Uint32);
  51.   Uint32 lensize_new = lenslen_new*sizeof(Uint32);
  52.  
  53.   _IF_SDLERR(kit_coreRealloc(&Vector->lens, lensize_old,lensize_new),;,"!lengths")
  54.  
  55.   /*!err*/ return  0;
  56.   _error_: return -1;
  57. }
  58.  
  59.  
  60.  
  61.  
  62. //called when SetFast isn't applicable
  63.  //(probably) slower than SetFast, hence the name
  64. int _kit_coreVectorSetSlow(kit_coreVector** Vector_p, Uint32 x_new, Uint32 y_new, Uint32 z_new){
  65.   kit_coreLog("SetSlow");
  66.   kit_coreVector* Vector=*Vector_p;
  67.   kit_coreVector vin=*Vector; //v(ector) in
  68.  
  69.   kit_coreVector* VectorOut=kit_coreVectorCreate(x_new,y_new,z_new, vin.unit, vin.type.n);
  70.   _IF_GOTO_ERROR(VectorOut==NULL,;)
  71.   kit_coreVector vout=*VectorOut; //v(ector) out
  72.  
  73.  
  74.   //copy input data to output
  75.    //the size of each row, in bytes, to be fed into memcpy
  76.   Uint32 dataRow_size = MIN(vin.unit*vin.x, vout.unit*vout.x);
  77.  
  78.   Uint32 idata_inc = vin.unit*vin.x;   //input data increase
  79.   Uint32 odata_inc = vout.unit*vout.x; //output data increase
  80.  
  81.   Uint32 yi_max = MIN(vin.y, vout.y);
  82.   Uint32 zi_max = MIN(vin.z, vout.z);
  83.   Sint32 y_difference = vout.y-vin.y;
  84.  
  85.   for(Uint32 zi=0; zi<zi_max; ++zi){
  86.     for(Uint32 yi=0; yi<yi_max; ++yi){
  87.       SDL_memcpy(vout.data, vin.data, dataRow_size);
  88.       vin.data +=idata_inc;
  89.       vout.data+=odata_inc;
  90.     }
  91.     //skip output forward by any empty space left
  92.     if(y_difference>0) vout.data += odata_inc*y_difference;
  93.   }
  94.  
  95.  
  96.  
  97.   kit_coreVectorDestroy(Vector_p); //destroy original
  98.   *Vector_p=VectorOut; //set vector pointer to the new one
  99.  
  100.   /*!err*/ return  0; // 0 on success
  101.   _error_: return -1; //-1 on error
  102. }
  103.  
  104.  
  105.  
  106. //should be called only when resizing is done on the same axis as its maximum,
  107.  //like when only x is being resized on a currently 1D vector, or only the y on a 2D vector, etc.
  108. int _kit_coreVectorSetFast(kit_coreVector** Vector_p, Uint32 x_new, Uint32 y_new, Uint32 z_new){
  109.   kit_coreLog("SetFast");
  110.   kit_coreVector* Vector=*Vector_p;
  111.  
  112.   Uint64 totalSize_old = sizeof(kit_coreVector) + Vector->datasize;
  113.   Uint64 lengthsSize_old = Vector->lensize;
  114.  
  115.   Uint64 totalSize_new = sizeof(kit_coreVector) + Vector->unit*x_new*y_new*z_new;
  116.   Uint64 lengthsSize_new = _getLensize(x_new,y_new,z_new);
  117.  
  118.  
  119.   _IF_SDLERR(kit_coreRealloc(&Vector, totalSize_old, totalSize_new),;,"!Vector")
  120.   Vector->datasize = totalSize_new-sizeof(kit_coreVector); //size of vector struct not counted
  121.   Vector->data = (void*)Vector+sizeof(kit_coreVector);
  122.  
  123.  
  124.   /*!err*/ *Vector_p=Vector; return  0;
  125.   _error_: *Vector_p=Vector; return -1;
  126. }
  127.  
  128.  
  129.  
  130. //set new size (if <x,y,z>_new == 0, then leave that axis unchanged)
  131. int kit_coreVectorSet(kit_coreVector** Vector_p, Uint32 x_new, Uint32 y_new, Uint32 z_new){
  132.   _IF_SDLERR(Vector_p==NULL,;,"!Vector_p")
  133.   kit_coreVector* Vector=*Vector_p;
  134.   _IF_SDLERR(Vector==NULL,;,"!*Vector_p")
  135.  
  136.   if(!x_new && !y_new && !z_new) goto _noerr_;
  137.   _IF_SDLERR(x_new&U32_MSb,;,"x<0")
  138.   _IF_SDLERR(y_new&U32_MSb,;,"y<0")
  139.   _IF_SDLERR(z_new&U32_MSb,;,"z<0")
  140.  
  141.  
  142.   Uint32 x_old=Vector->x;
  143.   Uint32 y_old=Vector->y;
  144.   Uint32 z_old=Vector->z;
  145.  
  146.   if(!x_new) x_new=x_old;
  147.   if(!y_new) y_new=y_old;
  148.   if(!z_new) z_new=z_old;
  149.  
  150.   int dimensions_old = _getDimensions(x_old,y_old,z_old);
  151.  
  152.  
  153.   int decision;
  154.   decision  = dimensions_old<<12; //dimensions on 3rd nybble
  155.   decision |= (x_old!=x_new)<< 8; //'did x axis change?' on 2nd nybble
  156.   decision |= (y_old!=y_new)<< 4; //'did y axis change?' on 1st nybble
  157.   decision |= (z_old!=z_new)    ; //'did z axis change?' on 0th nybble
  158.   if(!(decision&0xFFF)) goto _noerr_; //return early if everything is the same
  159.   switch(decision){
  160.   case 0x1100: SDL_FALLTHROUGH;
  161.   case 0x2010: SDL_FALLTHROUGH;
  162.   case 0x3001: _IF_GOTO_ERROR(_kit_coreVectorSetFast(Vector_p, x_new,y_new,z_new),;) break;
  163.   default: _IF_GOTO_ERROR(_kit_coreVectorSetSlow(Vector_p, x_new,y_new,z_new),;)
  164.   }
  165.  
  166.   Vector=*Vector_p;
  167.   Vector->z_block = x_new*y_new;
  168.   Vector->x=x_new;
  169.   Vector->y=y_new;
  170.   Vector->z=z_new;
  171.  
  172.   _noerr_: return  0; // 0 on success
  173.   _error_: return -1; //-1 on failure
  174. }
  175.  
  176.  
  177.  
  178. //add to or subtract from size
  179. int kit_coreVectorAdd(kit_coreVector** Vector_p, Sint32 x_add, Sint32 y_add, Sint32 z_add){
  180.   _IF_SDLERR(Vector_p==NULL,;,"!Vector_p")
  181.   kit_coreVector* Vector=*Vector_p;
  182.   _IF_SDLERR(Vector==NULL,;,"!*Vector_p")
  183.  
  184.  
  185.   Uint32 x_new = Vector->x+x_add;
  186.   Uint32 y_new = Vector->y+y_add;
  187.   Uint32 z_new = Vector->z+z_add;
  188.  
  189.   //==0 error is done here, since its behavior is different in VectorSet
  190.   _IF_SDLERR(x_new==0,;,"x==0")
  191.   _IF_SDLERR(y_new==0,;,"y==0")
  192.   _IF_SDLERR(z_new==0,;,"z==0")
  193.  
  194.  
  195.   _IF_GOTO_ERROR(kit_coreVectorSet(Vector_p, x_new,y_new,z_new),;)
  196.  
  197.  
  198.   /*!err*/ return  0; // 0 on success
  199.   _error_: return -1; //-1 on failure
  200. }
  201.  
  202.  
  203.  
  204. Uint32 kit_coreVectorAppend(kit_coreVector** Vector_p, void* src, Uint32 x_pos, Uint32 y_pos){
  205.   Uint32 newIndex=-1; //0xffffffff
  206.   _IF_SDLERR(Vector_p==NULL,;,"!Vector_p")
  207.   kit_coreVector* Vector=*Vector_p;
  208.   _IF_SDLERR(Vector==NULL,;,"!*Vector_p")
  209.  
  210.   //TBD
  211.  
  212.   _error_:
  213.   return newIndex;
  214. }
  215.  
  216.  
  217.  
  218.  
  219. int kit_coreVectorDestroy(kit_coreVector** Vector_p){
  220.   _IF_SDLERR(Vector_p==NULL,;,"!Vector_p")
  221.   kit_coreVector* Vector=*Vector_p;
  222.   _IF_GOTO(Vector==NULL,_noerr_,;) //exit early if vector is already destroyed
  223.  
  224.   if(Vector->lens != NULL) SDL_free(Vector->lens);
  225.   SDL_free(Vector);
  226.   *Vector_p=NULL;
  227.  
  228.   _noerr_: return  0; // 0 on success
  229.   _error_: return -1; //-1 on failure
  230. }
  231.  
  232.  
  233.  
  234. kit_coreVector* kit_coreVectorCreate(Uint32 x, Uint32 y, Uint32 z, Uint32 unit, Uint64 type_n){
  235.   kit_coreVector* Vector=NULL;
  236.   _IF_SDLERR(x==0,;,"x==0")
  237.   _IF_SDLERR(y==0,;,"y==0")
  238.   _IF_SDLERR(z==0,;,"z==0")
  239.   _IF_SDLERR(unit==0,;,"unit==0")
  240.  
  241.   _IF_SDLERR(x&U32_MSb,;,"x<0")
  242.   _IF_SDLERR(y&U32_MSb,;,"y<0")
  243.   _IF_SDLERR(z&U32_MSb,;,"z<0")
  244.   _IF_SDLERR(unit&U32_MSb,;,"unit<0")
  245.  
  246.  
  247.   //allocate memory for vector
  248.   Uint64 VectorSize = unit*x*y*z;
  249.   _IF_SDLERR(kit_coreRealloc(&Vector, 0,sizeof(kit_coreVector)+VectorSize) ,;,"!Vector")
  250.   Vector->data = (void*)Vector+sizeof(kit_coreVector);
  251.   Vector->unit=-1;
  252.  
  253.  
  254.   //allocate memory for lengths
  255.    //(z is not used here, because even 3D vectors only need a 2D array of Uint32)
  256.   //Uint64 lengthsSize = _getLensize(x,y,z);
  257.   //_IF_SDLERR(kit_coreRealloc(&Vector->lens, 0,lengthsSize),;,"!lengths")
  258.  
  259.  
  260.   //set the rest of the struct's values
  261.    //set last byte of type.n to 0 so it can always be interpreted as a string (type.s)
  262.   Vector->type.n = type_n&U64_MSBC;
  263.   Vector->z_block = x*y;
  264.   Vector->lensize=lengthsSize;
  265.   Vector->datasize=VectorSize;
  266.   Vector->x=x, Vector->y=y, Vector->z=z;
  267.   Vector->unit=unit;
  268.  
  269.  
  270.   _error_: //Vector->unit will remain -1 on error
  271.   if(Vector!=NULL && Vector->unit==-1) kit_coreVectorDestroy(&Vector);
  272.   return Vector;
  273. }
  274.  
  275.  
  276.  
  277. kit_coreVector* kit_coreVectorCopy(kit_coreVector* Vector){
  278.   kit_coreVector* VectorOut=NULL;
  279.   _IF_SDLERR(Vector==NULL,;,"!Vector")
  280.  
  281.   Uint64   type_n = Vector->type.n;
  282.   Uint64  z_block = Vector->z_block;
  283.   Uint64  lensize = Vector->lensize;
  284.   Uint64 datasize = Vector->datasize;
  285.   Uint32        x = Vector->x;
  286.   Uint32        y = Vector->y;
  287.   Uint32        z = Vector->z;
  288.   Uint32     unit = Vector->unit;
  289.   Uint32*    lens = Vector->lens;
  290.   void*      data = Vector->data;
  291.  
  292.   VectorOut=kit_coreVectorCreate(x,y,z, unit, type_n);
  293.   _IF_GOTO_ERROR(VectorOut==NULL,;)
  294.  
  295.   VectorOut->type.n   = type_n;
  296.   VectorOut->z_block  = z_block;
  297.   VectorOut->lensize  = lensize;
  298.   VectorOut->datasize = datasize;
  299.   VectorOut->x        = x;
  300.   VectorOut->y        = y;
  301.   VectorOut->z        = z;
  302.   VectorOut->unit     = unit;
  303.   SDL_memcpy(VectorOut->lens, lens, lensize);
  304.   SDL_memcpy(VectorOut->data, data, datasize);
  305.  
  306.   _error_:
  307.   return VectorOut;
  308. }
  309.  
  310.  
  311.  
  312.  
  313. #if defined(_KIT_CORE_DEBUG) || defined(_KIT_ALL_DEBUG)
  314. extern int printf(const char*,...);
  315. void kit_coreVectorPrintInt(kit_coreVector* Vector,const char* prefix){
  316.   char msg[]="PrintInt";
  317.   const char* _prefix=prefix;
  318.   if(prefix==NULL || prefix==(void*)1) _prefix=msg;
  319.   if(Vector==NULL){ kit_coreLog("%s: Vector cannot be NULL", _prefix); return; }
  320.   if(Vector->unit != sizeof(Uint32)){
  321.     kit_coreLog("%s: Vector->unit != sizeof(Uint32)", _prefix); return;
  322.   }
  323.   Uint32 x_s=Vector->x, y_s=Vector->y, z_s=Vector->z; Sint32* i32=Vector->data;
  324.   Uint32 index=-1;
  325.   if(prefix==(void*)1) goto print_results; //skip test fill if _prefix==1
  326.   //test fill
  327.   for(Uint32 zi=0; zi<z_s; ++zi){
  328.     for(Uint32 yi=0; yi<y_s; ++yi){
  329.       for(Uint32 xi=0; xi<x_s; ++xi) i32[++index] = ((xi+zi)<<4) | (yi+zi);
  330.     }
  331.   }
  332.   //print results
  333.   if(prefix==NULL) return; //now this part is optional!
  334.   print_results:;
  335.   index=-1;
  336.   printf("%s: %u,%u,%u\n", _prefix, x_s,y_s,z_s);
  337.   for(Uint32 zi=0; zi<z_s; ++zi){
  338.     printf("zi=%u:\n",zi);
  339.     for(Uint32 yi=0; yi<y_s; ++yi){
  340.       for(Uint32 xi=0; xi<x_s; ++xi) printf("%02X,",i32[++index]);
  341.       printf("\n");
  342.     }
  343.     printf("\n");
  344.   }
  345. }
  346. void kit_coreVectorPrintLens(kit_coreVector* Vector,const char* prefix){
  347.   char msg[]="PrintLens";
  348.   const char* _prefix=prefix;
  349.   if(prefix==NULL || prefix==(void*)1) _prefix=msg;
  350.   if(Vector==NULL){ kit_coreLog("%s: Vector cannot be NULL", _prefix); return; }
  351.  
  352.   Uint32 x_s=Vector->x, y_s=Vector->y; Uint32* u32=Vector->lens;
  353.   Uint32 index=-1;
  354.   if(prefix==(void*)1) goto print_results; //skip test fill if _prefix==1
  355.   //test fill
  356.   for(Uint32 yi=0; yi<y_s; ++yi){
  357.     for(Uint32 xi=0; xi<x_s; ++xi)
  358.       u32[++index] = (xi<<4) | yi;
  359.   }
  360.   //print results
  361.   if(prefix==NULL) return;
  362.   print_results:;
  363.   index=-1;
  364.   printf("%s: %u,%u\n", _prefix, x_s,y_s);
  365.   for(Uint32 yi=0; yi<y_s; ++yi){
  366.     for(Uint32 xi=0; xi<x_s; ++xi)
  367.       printf("%02X,",u32[++index]);
  368.     printf("\n");
  369.   }
  370. }
  371. #define _vectorLogT(...) kit_coreLog(__VA_ARGS__)
  372. //#define _vectorLogT(...) ;
  373. int kit_coreVectorTest(){
  374.   int returnStatus=0;
  375.   kit_coreVector* Vector=NULL; SDL_SetError("(no error)");
  376.  
  377.   ++returnStatus;
  378.   //_error_:
  379.   --returnStatus;
  380.   if(returnStatus){
  381.     _vectorLogT("failed; error = \"%s\"",SDL_GetError());
  382.   } else if(Vector != NULL){
  383.     _vectorLogT("attempting to destroy vector");
  384.     if(kit_coreVectorDestroy(&Vector))
  385.       _vectorLogT("failed; error = \"%s\"",SDL_GetError());
  386.   }
  387.   return returnStatus;
  388. }
  389. #else
  390. int kit_coreVectorTest(){
  391.   kit_coreLog("\"_KIT_CORE_DEBUG\" was not defined during compilation of kit_core");
  392.   SDL_SetError("!defined(_KIT_CORE_DEBUG)");
  393.   return 1;
  394. }
  395. #endif
  396.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement