Advertisement
Kitomas

kit_sdl2_coreVector.c as of 8-18-23

Aug 18th, 2023
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 27.07 KB | None | 0 0
  1. //my own primitive form of contiguous dynamic arrays
  2. #include "../include/kit_sdl2/kit_core.h"
  3. #include "../_private/include/_kit_privmacro.h"
  4.  
  5. //(MAYBE at some point replace <x/y/z>_new with <x/y/z>_old when axes aren't updated)
  6.  
  7. //for debug
  8. extern int printf(const char*,...);
  9. void _kit_coreVectorPrintInt(kit_coreVector* Vector,const char* prefix){
  10.   if(Vector==NULL){ printf("%s: Vector cannot be NULL\n", prefix); return; }
  11.   Uint32 x_s=Vector->x, y_s=Vector->y, z_s=Vector->z; void* ptr=Vector->ptr;
  12.   Sint32 *i1d=ptr, **i2d=ptr, ***i3d=ptr, dims=Vector->_dims;
  13.   Uint32 xi=0, yi=0, zi=0;
  14.   //test fill
  15.   if(dims!=3) goto _n3d_fs;
  16.   for(zi=0; zi<z_s; ++zi){ _n3d_fs: if(dims==1) goto _n2d_fs;
  17.     for(yi=0; yi<y_s; ++yi){ _n2d_fs:
  18.       for(xi=0; xi<x_s; ++xi){
  19.         switch(dims){
  20.         case 1: i1d[xi]        =xi;                   break;
  21.         case 2: i2d[xi][yi]    =(xi<<4)|yi;           break;
  22.         case 3: i3d[xi][yi][zi]=((xi+zi)<<4)|(yi+zi); }
  23.       } if(dims==1) goto _n2d_fe;
  24.     } _n2d_fe: if(dims!=3) goto _n3d_fe;
  25.   } _n3d_fe:
  26.   //print
  27.   printf("%s: %u,%u,%u (%uD)\n", prefix, x_s,y_s,z_s, dims);
  28.   if(dims!=3) goto _n3d_ps;
  29.   for(zi=0; zi<z_s; ++zi){ _n3d_ps: if(dims==1) goto _n2d_ps;
  30.     for(yi=0; yi<y_s; ++yi){ _n2d_ps: if(!zi) printf("0");
  31.       for(xi=0; xi<x_s; ++xi){
  32.         switch(dims){
  33.         case 1: printf("%X,",i1d[xi]);         break;
  34.         case 2: printf("%X,",i2d[xi][yi]);     break;
  35.         case 3: printf("%X,",i3d[xi][yi][zi]); }
  36.       } if(dims==1) goto _n2d_pe;
  37.       printf("\n");
  38.     } _n2d_pe: if(dims!=3) goto _n3d_pe;
  39.     printf("\n");
  40.   } _n3d_pe: printf("\n");
  41. }
  42.  
  43.  
  44. //initializes any added memory if difference is positive
  45. int _kit_coreVectorRealloc(void* ptr_p, char axis,
  46.                            Uint32 osize, Uint32 nsize)
  47. {
  48.   if(osize==nsize) return 0;
  49.   void* _ptr=SDL_realloc(*(void**)ptr_p,nsize);
  50.   if(_ptr==NULL){
  51.     if(axis!=0) SDL_SetError("SDL_realloc() of %c axis failed",axis);
  52.     return -1;
  53.   }
  54.   if(nsize>osize) SDL_memset(_ptr+osize, 0, /*size_difference=*/nsize-osize);
  55.   *(void**)ptr_p=_ptr;
  56.   return 0;
  57. }
  58.  
  59.  
  60. void _kit_coreVectorFreeZinY(kit_coreVector* Vector,
  61.                              Uint32 x_start, Uint32 x_end,
  62.                              Uint32 y_start, Uint32 y_end)
  63. {
  64.   if((Vector->_dims&3)<3) return; //z doesn't exist in 1d or 2d array
  65.   void*** p3d=Vector->p3d;
  66.   for(Uint32 xi=x_start; xi<x_end; ++xi){ void** p2d=p3d[xi];
  67.     if(p2d==NULL) break;
  68.     for(Uint32 yi=y_start; yi<y_end; ++yi){ void* p1d=p2d[yi];
  69.       if(p1d==NULL) break;
  70.       SDL_free(p1d); p2d[yi]=NULL;
  71.     }
  72.   }
  73. }
  74.  
  75. void _kit_coreVectorFreeYinX(kit_coreVector* Vector,
  76.                              Uint32 x_start, Uint32 x_end)
  77. {
  78.   if((Vector->_dims&3)<2) return; //y doesn't exist in 1d array (nor does z)
  79.   void** p2d=Vector->p2d;
  80.   for(Uint32 xi=x_start; xi<x_end; ++xi){ void* p1d=p2d[xi];
  81.     if(p1d==NULL) break;
  82.     SDL_free(p1d); p2d[xi]=NULL;
  83.   }
  84. }
  85.  
  86.  
  87. int _kit_coreVectorResizeX(kit_coreVector** Vector_p,
  88.                            Uint32 x_osize, Uint32 x_nsize, Uint32 x_new)
  89. {
  90.   //x axis is contiguous with the vector struct itself
  91.   x_osize+=sizeof(kit_coreVector); x_nsize+=sizeof(kit_coreVector);
  92.   if(_kit_coreVectorRealloc(Vector_p,'x', x_osize,x_nsize)) return -12;
  93.   kit_coreVector* Vector=*Vector_p;
  94.   Vector->ptr=(void*)Vector+sizeof(kit_coreVector);
  95.   Vector->x=x_new; *Vector_p=Vector;
  96.   return 0;
  97. }
  98.  
  99. int _kit_coreVectorResizeY(kit_coreVector* Vector,
  100.                            Uint32 x_start, Uint32 x_end,
  101.                            Uint32 y_osize, Uint32 y_nsize, Uint32 y_new)
  102. {
  103.   if((Vector->_dims&3)<2) return 0; //y doesn't exist in 1d array (nor does z)
  104.   void** p2d=Vector->p2d;
  105.   for(Uint32 xi=x_start; xi<x_end; ++xi)
  106.     if(_kit_coreVectorRealloc(&p2d[xi],'y', y_osize,y_nsize)) return -13;
  107.   Vector->y=y_new;
  108.   return 0;
  109. }
  110.  
  111. int _kit_coreVectorResizeZ(kit_coreVector* Vector,
  112.                            Uint32 x_start, Uint32 x_end,
  113.                            Uint32 y_start, Uint32 y_end,
  114.                            Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
  115. {
  116.   if((Vector->_dims&3)<3) return 0; //z doesn't exist in 1d or 2d array
  117.   void*** p3d=Vector->p3d;
  118.   for(Uint32 xi=x_start; xi<x_end; ++xi){ void** p2d=p3d[xi];
  119.     for(Uint32 yi=y_start; yi<y_end; ++yi)
  120.       if(_kit_coreVectorRealloc(&p2d[yi],'z', z_osize,z_nsize)) return -14;
  121.   }
  122.   Vector->z=z_new;
  123.   return 0;
  124. }
  125.  
  126. //uninitialize rS where it isn't needed
  127. //un-inline these if binary size becomes unacceptable!
  128. //(remember, <xyz>_old and <xyz>_new are sometimes flipped when used as a parameter when freeing)
  129. static inline int _kit_coreVectorSubX(kit_coreVector** Vector_p,
  130.                                       Uint32 x_new,   Uint32 x_old, Uint32 y_new,
  131.                                       Uint32 x_osize, Uint32 x_nsize)
  132. { //printf("SubX\n");
  133.   kit_coreVector* Vector=*Vector_p;
  134.   _kit_coreVectorFreeZinY(Vector, x_new,x_old, 0,y_new);
  135.   _kit_coreVectorFreeYinX(Vector, x_new,x_old);
  136.   return _kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new);
  137. }
  138.  
  139. static inline int _kit_coreVectorSubY(kit_coreVector* Vector,
  140.                                       Uint32 x_new,   Uint32 y_new, Uint32 y_old,
  141.                                       Uint32 y_osize, Uint32 y_nsize)
  142. { //printf("SubY\n");
  143.   _kit_coreVectorFreeZinY(Vector, 0,x_new, y_new,y_old);
  144.   return _kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new);
  145. }
  146.  
  147. static inline int _kit_coreVectorSubZ(kit_coreVector* Vector,
  148.                                       Uint32 x_new,   Uint32 y_new, Uint32 z_osize,
  149.                                       Uint32 z_nsize, Uint32 z_new)
  150. { //printf("SubZ\n");
  151.   return _kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new);
  152. }
  153.  
  154. static inline int _kit_coreVectorSubXY(kit_coreVector** Vector_p,
  155.                                        Uint32 x_new,   Uint32 x_old, Uint32 y_old,
  156.                                        Uint32 x_osize, Uint32 x_nsize,
  157.                                        Uint32 y_new,   Uint32 y_osize, Uint32 y_nsize)
  158. { //printf("SubXY\n");
  159.   kit_coreVector* Vector=*Vector_p; int rS; //(r)eturn(S)tatus
  160.   _kit_coreVectorFreeZinY(Vector, x_new,x_old,     0,y_old);
  161.   _kit_coreVectorFreeYinX(Vector, x_new,x_old);
  162.   if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
  163.   _kit_coreVectorFreeZinY(Vector,     0,x_new, y_new,y_old);
  164.   return _kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new);
  165. }
  166.  
  167. static inline int _kit_coreVectorSubYZ(kit_coreVector* Vector,
  168.                                        Uint32 x_new,   Uint32 y_new,   Uint32 y_old,
  169.                                        Uint32 y_osize, Uint32 y_nsize,
  170.                                        Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
  171. { //printf("SubYZ\n");
  172.   int rS; //(r)eturn(S)tatus
  173.   _kit_coreVectorFreeZinY(Vector, 0,x_new, y_new,y_old);
  174.   if((rS=_kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new))) return rS;
  175.   return _kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new);
  176. }
  177.  
  178. static inline int _kit_coreVectorSubXZ(kit_coreVector** Vector_p,
  179.                                        Uint32 x_new,   Uint32 x_old,   Uint32 y_new,
  180.                                        Uint32 x_osize, Uint32 x_nsize,
  181.                                        Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
  182. { //printf("SubXZ\n");
  183.   kit_coreVector* Vector=*Vector_p; int rS; //(r)eturn(S)tatus
  184.   _kit_coreVectorFreeZinY(Vector, x_new,x_old, 0,y_new);
  185.   _kit_coreVectorFreeYinX(Vector, x_new,x_old);
  186.   if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
  187.   return _kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new);
  188. }
  189.  
  190. static inline int _kit_coreVectorSubXYZ(kit_coreVector** Vector_p,
  191.                                         Uint32 x_new,   Uint32 x_old,   Uint32 y_old,
  192.                                         Uint32 x_osize, Uint32 x_nsize,
  193.                                         Uint32 y_new,   Uint32 y_osize, Uint32 y_nsize,
  194.                                         Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
  195. { //printf("SubXYZ\n");
  196.   kit_coreVector* Vector=*Vector_p; int rS; //(r)eturn(S)tatus
  197.   _kit_coreVectorFreeZinY(Vector, x_new,x_old,     0,y_old);
  198.   _kit_coreVectorFreeYinX(Vector, x_new,x_old);
  199.   if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
  200.   _kit_coreVectorFreeZinY(Vector,     0,x_new, y_new,y_old);
  201.   if((rS=_kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new))) return rS;
  202.   return _kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new);
  203. }
  204.  
  205.  
  206. static inline int _kit_coreVectorAddX(kit_coreVector** Vector_p,
  207.                                       Uint32 x_osize, Uint32 x_nsize, Uint32 x_new,
  208.                                       Uint32 x_old,   Uint32 y_nsize, Uint32 y_new,
  209.                                       Uint32 z_nsize, Uint32 z_new)
  210. { //printf("AddX\n");
  211.   int rS; //(r)eturn(S)tatus
  212.   if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
  213.   kit_coreVector* Vector=*Vector_p;
  214.   if((rS=_kit_coreVectorResizeY(Vector, x_old,x_new, 0,y_nsize, y_new))) return rS;
  215.   return _kit_coreVectorResizeZ(Vector, x_old,x_new, 0,y_new, 0,z_nsize, z_new);
  216. }
  217.  
  218. static inline int _kit_coreVectorAddY(kit_coreVector* Vector,
  219.                                       Uint32 x_new, Uint32 y_osize, Uint32 y_nsize, Uint32 y_new,
  220.                                       Uint32 y_old, Uint32 z_nsize, Uint32 z_new)
  221. { //printf("AddY\n");
  222.   int rS; //(r)eturn(S)tatus
  223.   if((rS=_kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new))) return rS;
  224.   return _kit_coreVectorResizeZ(Vector, 0,x_new, y_old,y_new, 0,z_nsize, z_new);
  225. }
  226.  
  227. static inline int _kit_coreVectorAddZ(kit_coreVector* Vector,
  228.                                       Uint32 x_new,   Uint32 y_new,
  229.                                       Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
  230. { //printf("AddZ\n");
  231.   return _kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new);
  232. }
  233.  
  234. static inline int _kit_coreVectorAddXY(kit_coreVector** Vector_p,
  235.                                        Uint32 x_osize, Uint32 x_nsize, Uint32 x_new,
  236.                                        Uint32 x_old,   Uint32 y_osize, Uint32 y_nsize, Uint32 y_new,
  237.                                        Uint32 y_old,   Uint32 z_nsize, Uint32 z_new)
  238. { //printf("AddXY\n");
  239.   int rS; //(r)eturn(S)tatus
  240.   if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
  241.   kit_coreVector* Vector=*Vector_p;
  242.   if((rS=_kit_coreVectorResizeY(Vector,     0,x_old, y_osize,y_nsize, y_new))) return rS;
  243.   if((rS=_kit_coreVectorResizeY(Vector, x_old,x_new,       0,y_nsize, y_new))) return rS;
  244.   if((rS=_kit_coreVectorResizeZ(Vector,     0,x_old, y_old,y_new, 0,z_nsize, z_new))) return rS;
  245.   return _kit_coreVectorResizeZ(Vector, x_old,x_new,     0,y_new, 0,z_nsize, z_new);
  246. }
  247.  
  248. static inline int _kit_coreVectorAddYZ(kit_coreVector* Vector,
  249.                                        Uint32 x_new, Uint32 y_osize, Uint32 y_nsize, Uint32 y_new,
  250.                                        Uint32 y_old, Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
  251. { //printf("AddYZ\n");
  252.   int rS; //(r)eturn(S)tatus
  253.   if((rS=_kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new))) return rS;
  254.   if((rS=_kit_coreVectorResizeZ(Vector, 0,x_new,     0,y_old, z_osize,z_nsize, z_new))) return rS;
  255.   return _kit_coreVectorResizeZ(Vector, 0,x_new, y_old,y_new,       0,z_nsize, z_new);
  256. }
  257.  
  258. static inline int _kit_coreVectorAddXZ(kit_coreVector** Vector_p,
  259.                                        Uint32 x_osize, Uint32 x_nsize, Uint32 x_new,
  260.                                        Uint32 x_old,   Uint32 y_nsize, Uint32 y_new,
  261.                                        Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
  262. { //printf("AddXZ\n");
  263.   int rS; //(r)eturn(S)tatus
  264.   if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
  265.   kit_coreVector* Vector=*Vector_p;
  266.   if((rS=_kit_coreVectorResizeY(Vector, x_old,x_new, 0,y_nsize, y_new))) return rS;
  267.   if((rS=_kit_coreVectorResizeZ(Vector,     0,x_old, 0,y_new, z_osize,z_nsize, z_new))) return rS;
  268.   return _kit_coreVectorResizeZ(Vector, x_old,x_new, 0,y_new,       0,z_nsize, z_new);
  269. }
  270.  
  271. static inline int _kit_coreVectorAddXYZ(kit_coreVector** Vector_p,
  272.                                         Uint32 x_osize, Uint32 x_nsize, Uint32 x_new,
  273.                                         Uint32 x_old,   Uint32 y_osize, Uint32 y_nsize, Uint32 y_new,
  274.                                         Uint32 y_old,   Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
  275. { //printf("AddXYZ\n");
  276.   int rS; //(r)eturn(S)tatus
  277.   if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
  278.   kit_coreVector* Vector=*Vector_p;
  279.   if((rS=_kit_coreVectorResizeY(Vector,     0,x_old, y_osize,y_nsize, y_new))) return rS;
  280.   if((rS=_kit_coreVectorResizeY(Vector, x_old,x_new,       0,y_nsize, y_new))) return rS;
  281.   if((rS=_kit_coreVectorResizeZ(Vector,     0,x_old,     0,y_old, z_osize,z_nsize, z_new))) return rS;
  282.   if((rS=_kit_coreVectorResizeZ(Vector,     0,x_old, y_old,y_new,       0,z_nsize, z_new))) return rS;
  283.   return _kit_coreVectorResizeZ(Vector, x_old,x_new,     0,y_new,       0,z_nsize, z_new);
  284. }
  285.  
  286.  
  287. //_<0(unchanged)/N(egative)/P(ositive)><X/Y/Z>
  288. #define _0X (0x0000)
  289. #define _NX (0x1000)
  290. #define _PX (0x3000)
  291. #define _0Y (0x0000)
  292. #define _NY (0x0100)
  293. #define _PY (0x0300)
  294. #define _0Z (0x0000)
  295. #define _NZ (0x0010)
  296. #define _PZ (0x0030)
  297. #define _VADECISION(_action) \
  298.   if((returnStatus=_action)){ goto _error_; }
  299. #define _VADECISION_B(_action) _VADECISION(_action) break;
  300. //add to or subtract from size
  301. int kit_coreVectorAdd(kit_coreVector** Vector_p, Sint32 x_add, Sint32 y_add, Sint32 z_add){
  302.   if(!x_add && !y_add && !z_add) return 0;
  303.   int returnStatus=0;
  304.   _IF_SDLERR_I(Vector_p==NULL,-1,;,"Vector_p cannot be NULL")
  305.   kit_coreVector* Vector=*Vector_p;
  306.   _IF_SDLERR_I(Vector==NULL,-2,;,"*Vector_p cannot be NULL")
  307.  
  308.   Uint32 unit=Vector->unit;
  309.   Uint32 x_old=Vector->x,   y_old=Vector->y,   z_old=Vector->z;
  310.        _IF_SDLERR_I( (!z_old)&&(z_add!=0) ,-3,;,"cannot change z on non-3d vector")
  311.   else _IF_SDLERR_I( (!y_old)&&(y_add!=0) ,-4,;,"cannot change y on 1d vector")
  312.   Uint32 x_new=x_old+x_add, y_new=y_old+y_add, z_new=z_old+z_add;
  313.        _IF_SDLERR_I( (!z_old)!=(!z_new) ,-5,;,"resulting z cannot have its axis status flipped")
  314.   else _IF_SDLERR_I( (!y_old)!=(!y_new) ,-6,;,"resulting y cannot have its axis status flipped")
  315.  
  316.   //check for any underflows and overflows respectively (and some dimension error checks)
  317.    //x
  318.   _IF_SDLERR_I( ((x_add<0)&&(x_new>x_old)) || !x_new ,-7,;,"resulting x cannot be 0")
  319.   _IF_SDLERR_I( (x_add>0)&&(x_new<x_old) ,-8,;,"applying x_add caused an overflow")
  320.    //y
  321.   if(y_add<0 && (y_new>y_old)) y_new=0;
  322.   else _IF_SDLERR_I( (y_add>0)&&(y_new<y_old) ,-9,;,"applying y_add caused an overflow")
  323.   else _IF_SDLERR_I( (y_new==0)&&(z_new>0) ,-10,;,"resulting y cannot be 0 if z>0")
  324.    //z
  325.   if((z_add<0) && (z_new>z_old)) z_new=0;
  326.   else _IF_SDLERR_I( (z_add>0)&&(z_new<z_old) ,-11,;,"applying z_add caused an overflow")
  327.  
  328.   Uint32 x_unit, y_unit, z_unit;
  329.   x_unit=y_unit=z_unit = sizeof(void*);
  330.   int decision, dims=(x_new!=0)+(y_new!=0)+(z_new!=0); //dim(ension)s
  331.        if(dims==1)   x_unit=unit;
  332.   else if(dims==2)   y_unit=unit;
  333.   else /*(dims==3)*/ z_unit=unit;
  334.   Uint32 x_osize=x_unit*x_old, y_osize=y_unit*y_old, z_osize=z_unit*z_old;
  335.   Uint32 x_nsize=x_unit*x_new, y_nsize=y_unit*y_new, z_nsize=z_unit*z_new;
  336.  
  337.   decision =( ((x_add>0)<<1)|(x_add!=0) )<<12; //0x0000 -> 0x3000
  338.   decision|=( ((y_add>0)<<1)|(y_add!=0) )<< 8; //0x0000 -> 0x0300
  339.   decision|=( ((z_add>0)<<1)|(z_add!=0) )<< 4; //0x0000 -> 0x0030
  340.   switch(decision){
  341.   //SUB
  342.   case (_NX|_0Y|_0Z): //_NX_0Y_0Z; technically a superset of: (n/a)
  343.     _VADECISION_B(_kit_coreVectorSubX(&Vector,x_new,x_old,y_new,x_osize,x_nsize))
  344.   case (_0X|_NY|_0Z): //_0X_NY_0Z; technically a superset of: (n/a)
  345.     _VADECISION_B(_kit_coreVectorSubY(Vector,x_new,y_new,y_old,y_osize,y_nsize))
  346.   case (_0X|_0Y|_NZ): //_0X_0Y_NZ; technically a superset of: (n/a)
  347.     _VADECISION_B(_kit_coreVectorSubZ(Vector,x_new,y_new,z_osize,z_nsize,z_new))
  348.   case (_NX|_NY|_0Z): //_NX_NY_0Z; technically a superset of: (_NX_0Y_0Z,_0X_NY_0Z)
  349.     _VADECISION_B(_kit_coreVectorSubXY(&Vector,x_new,x_old,y_old,x_osize,x_nsize,y_new,y_osize,y_nsize))
  350.   case (_0X|_NY|_NZ): //_0X_NY_NZ; technically a superset of: (_0X_NY_0Z,_0X_0Y_NZ)
  351.     _VADECISION_B(_kit_coreVectorSubYZ(Vector,x_new,y_new,y_old,y_osize,y_nsize,z_osize,z_nsize,z_new))
  352.   case (_NX|_0Y|_NZ): //_NX_0Y_NZ; technically a superset of: (_NX_0Y_0Z,_0X_0Y_NZ)
  353.     _VADECISION_B(_kit_coreVectorSubXZ(&Vector,x_new,x_old,y_new,x_osize,x_nsize,z_osize,z_nsize,z_new))
  354.   case (_NX|_NY|_NZ): //_NX_NY_NZ; technically a superset of: (_NX_NY_0Z,_0X_0Y_NZ)
  355.     _VADECISION_B(_kit_coreVectorSubXYZ(&Vector,x_new,x_old,y_old,x_osize,x_nsize,y_new,y_osize,y_nsize,z_osize,z_nsize,z_new))
  356.   //ADD
  357.   case (_PX|_0Y|_0Z): //_PX_0Y_0Z; technically a superset of: (n/a)
  358.     _VADECISION_B(_kit_coreVectorAddX(&Vector,x_osize,x_nsize,x_new,x_old,y_nsize,y_new,z_nsize,z_new))
  359.   case (_0X|_PY|_0Z): //_0X_PY_0Z; technically a superset of: (n/a)
  360.     _VADECISION_B(_kit_coreVectorAddY(Vector,x_new,y_osize,y_nsize,y_new,y_old,z_nsize,z_new))
  361.   case (_0X|_0Y|_PZ): //_0X_0Y_PZ; technically a superset of: (n/a)
  362.     _VADECISION_B(_kit_coreVectorAddZ(Vector,x_new,y_new,z_osize,z_nsize,z_new))
  363.   case (_PX|_PY|_0Z): //_PX_PY_0Z; technically a superset of: (_PX_0Y_0Z,_0X_PY_0Z)
  364.     _VADECISION_B(_kit_coreVectorAddXY(&Vector,x_osize,x_nsize,x_new,x_old,y_osize,y_nsize,y_new,y_old,z_nsize,z_new))
  365.   case (_0X|_PY|_PZ): //_0X_PY_PZ; technically a superset of: (_0X_PY_0Z,_0X_0Y_PZ)
  366.     _VADECISION_B(_kit_coreVectorAddYZ(Vector,x_new,y_osize,y_nsize,y_new,y_old,z_osize,z_nsize,z_new))
  367.   case (_PX|_0Y|_PZ): //_PX_0Y_PZ; technically a superset of: (_PX_0Y_0Z,_0X_0Y_PZ)
  368.     _VADECISION_B(_kit_coreVectorAddXZ(&Vector,x_osize,x_nsize,x_new,x_old,y_nsize,y_new,z_osize,z_nsize,z_new))
  369.   case (_PX|_PY|_PZ): //_PX_PY_PZ; technically a superset of: (_PX_PY_0Z,_0X_0Y_PZ)
  370.     _VADECISION_B(_kit_coreVectorAddXYZ(&Vector,x_osize,x_nsize,x_new,x_old,y_osize,y_nsize,y_new,y_old,z_osize,z_nsize,z_new))
  371.   //SUBX ADDYZ
  372.   case (_NX|_PY|_0Z): //_NX_PY_0Z; technically a superset of: (_NX_0Y_0Z, _0X_PY_0Z)
  373.     _VADECISION(  _kit_coreVectorSubX(&Vector,x_new,x_old,y_new,x_osize,x_nsize))
  374.     _VADECISION_B(_kit_coreVectorAddY(Vector,x_new,y_osize,y_nsize,y_new,y_old,z_nsize,z_new))
  375.   case (_NX|_0Y|_PZ): //_NX_0Y_PZ; technically a superset of: (_NX_0Y_0Z, _0X_0Y_PZ)
  376.     _VADECISION(  _kit_coreVectorSubX(&Vector,x_new,x_old,y_new,x_osize,x_nsize))
  377.     _VADECISION_B(_kit_coreVectorAddZ(Vector,x_new,y_new,z_osize,z_nsize,z_new))
  378.   case (_NX|_PY|_PZ): //_NX_PY_PZ; technically a superset of: (_NX_0Y_0Z, _0X_PY_PZ)
  379.     _VADECISION(  _kit_coreVectorSubX(&Vector,x_new,x_old,y_new,x_osize,x_nsize))
  380.     _VADECISION_B(_kit_coreVectorAddYZ(Vector,x_new,y_osize,y_nsize,y_new,y_old,z_osize,z_nsize,z_new))
  381.   //SUBY ADDXZ
  382.   case (_PX|_NY|_0Z): //_PX_NY_0Z; technically a superset of: (_0X_NY_0Z, _PX_0Y_0Z)
  383.     //x_new is swapped for x_old here (see line 5)
  384.     _VADECISION(  _kit_coreVectorSubY(Vector,x_old,y_new,y_old,y_osize,y_nsize))
  385.     _VADECISION_B(_kit_coreVectorAddX(&Vector,x_osize,x_nsize,x_new,x_old,y_nsize,y_new,z_nsize,z_new))
  386.   case (_0X|_NY|_PZ): //_0X_NY_PZ; technically a superset of: (_0X_NY_0Z, _0X_0Y_PZ)
  387.     _VADECISION(  _kit_coreVectorSubY(Vector,x_new,y_new,y_old,y_osize,y_nsize))
  388.     _VADECISION_B(_kit_coreVectorAddZ(Vector,x_new,y_new,z_osize,z_nsize,z_new))
  389.   case (_PX|_NY|_PZ): //_PX_NY_PZ; technically a superset of: (_0X_NY_0Z, _PX_0Y_PZ)
  390.     //x_new is swapped for x_old here (see line 5)
  391.     _VADECISION(  _kit_coreVectorSubY(Vector,x_old,y_new,y_old,y_osize,y_nsize))
  392.     _VADECISION_B(_kit_coreVectorAddXZ(&Vector,x_osize,x_nsize,x_new,x_old,y_nsize,y_new,z_osize,z_nsize,z_new))
  393.   //SUBZ ADDXY
  394.   case (_PX|_0Y|_NZ): //_PX_0Y_NZ; technically a superset of: (_0X_0Y_NZ, _PX_0Y_0Z)
  395.     //x_new is swapped for x_old here (see line 5)
  396.     _VADECISION(  _kit_coreVectorSubZ(Vector,x_old,y_new,z_osize,z_nsize,z_new))
  397.     _VADECISION_B(_kit_coreVectorAddX(&Vector,x_osize,x_nsize,x_new,x_old,y_nsize,y_new,z_nsize,z_new))
  398.   case (_0X|_PY|_NZ): //_0X_PY_NZ; technically a superset of: (_0X_0Y_NZ, _0X_PY_0Z)
  399.     //y_new is swapped for y_old here (see line 5)
  400.     _VADECISION(  _kit_coreVectorSubZ(Vector,x_new,y_old,z_osize,z_nsize,z_new))
  401.     _VADECISION_B(_kit_coreVectorAddY(Vector,x_new,y_osize,y_nsize,y_new,y_old,z_nsize,z_new))
  402.   case (_PX|_PY|_NZ): //_PX_PY_NZ; technically a superset of: (_0X_0Y_NZ, _PX_PY_0Z)
  403.     //x_new,y_new is swapped for x_old,y_old here respectively (see line 5)
  404.     _VADECISION(  _kit_coreVectorSubZ(Vector,x_old,y_old,z_osize,z_nsize,z_new))
  405.     _VADECISION_B(_kit_coreVectorAddXY(&Vector,x_osize,x_nsize,x_new,x_old,y_osize,y_nsize,y_new,y_old,z_nsize,z_new))
  406.   //SUBXYZ ADDXYZ
  407.   case (_NX|_NY|_PZ): //_NX_NY_PZ; technically a superset of: (_NX_NY_0Z, _0X_0Y_PZ)
  408.     _VADECISION(  _kit_coreVectorSubXY(&Vector,x_new,x_old,y_old,x_osize,x_nsize,y_new,y_osize,y_nsize))
  409.     _VADECISION_B(_kit_coreVectorAddZ(Vector,x_new,y_new,z_osize,z_nsize,z_new))
  410.   case (_PX|_NY|_NZ): //_PX_NY_NZ; technically a superset of: (_0X_NY_NZ, _PX_0Y_0Z)
  411.     //x_new is swapped for x_old here (see line 5)
  412.     _VADECISION(  _kit_coreVectorSubYZ(Vector,x_old,y_new,y_old,y_osize,y_nsize,z_osize,z_nsize,z_new))
  413.     _VADECISION_B(_kit_coreVectorAddX(&Vector,x_osize,x_nsize,x_new,x_old,y_nsize,y_new,z_nsize,z_new))
  414.   case (_NX|_PY|_NZ): //_NX_PY_NZ; technically a superset of: (_NX_0Y_NZ, _0X_PY_0Z)
  415.     //y_new is swapped for y_old here (see line 5)
  416.     _VADECISION(  _kit_coreVectorSubXZ(&Vector,x_new,x_old,y_old,x_osize,x_nsize,z_osize,z_nsize,z_new))
  417.     _VADECISION_B(_kit_coreVectorAddY(Vector,x_new,y_osize,y_nsize,y_new,y_old,z_nsize,z_new))
  418.   default: _IS_SDLERR_I(-15,;,"decision switch defaulted (this shouldn't be possible); decision=0x%X",decision)
  419.   } *Vector_p=Vector;
  420.   _error_:
  421.   return returnStatus;
  422. }
  423. //set new size (if an axis == 0, then leave that axis unchanged)
  424.  //also, if the new axis length is the same as the old one, leave it unchanged also
  425. int kit_coreVectorSet(kit_coreVector** Vector_p, Uint32 x_new, Uint32 y_new, Uint32 z_new){
  426.   int returnStatus=0;
  427.   _IF_SDLERR_I(Vector_p==NULL,-1,;,"Vector_p cannot be NULL")
  428.   kit_coreVector* Vector=*Vector_p;
  429.   _IF_SDLERR_I(Vector==NULL,-2,;,"*Vector_p cannot be NULL")
  430.  
  431.   Sint32 x_diff=(x_new==0) ? 0 : x_new-Vector->x;
  432.   Sint32 y_diff=(y_new==0) ? 0 : y_new-Vector->y;
  433.   Sint32 z_diff=(z_new==0) ? 0 : z_new-Vector->z;
  434.  
  435.   returnStatus=kit_coreVectorAdd(Vector_p, x_diff,y_diff,z_diff);
  436.  
  437.   _error_:
  438.   return returnStatus;
  439. }
  440.  
  441.  
  442. int kit_coreVectorDestroy(kit_coreVector** Vector_p){
  443.   int returnStatus=0;
  444.   _IF_SDLERR_I(Vector_p==NULL,-1,;,"Vector_p cannot be NULL")
  445.   kit_coreVector* Vector=*Vector_p;
  446.   _IF_SDLERR_I(Vector==NULL,-2,;,"*Vector_p cannot be NULL")
  447.  
  448.   int x=Vector->x, y=Vector->y; //z=Vector->z;
  449.   Sint32 condition=Vector->_dims&3;
  450.   if(!condition) Vector->_dims=condition = (x!=0)+(y!=0)+(Vector->z!=0);
  451.  
  452.   //condition must be equal to the number of dimensions (1=1d,2=2d,3=3d)
  453.   switch(condition){
  454.   case 3: _kit_coreVectorFreeZinY(Vector, 0,x, 0,y); SDL_FALLTHROUGH;
  455.   case 2: _kit_coreVectorFreeYinX(Vector, 0,x);      SDL_FALLTHROUGH;
  456.   case 1: SDL_free(Vector); //frees Vector->p1d too
  457.   }       *Vector_p=NULL;
  458.   _error_:
  459.   return returnStatus;
  460. }
  461.  
  462.  
  463. kit_coreVector* kit_coreVectorCreate(Uint32 x, Uint32 y, Uint32 z,
  464.                                      Uint32 unit, Uint32 type_n,
  465.                                      int* returnStatus_p)
  466. {
  467.   kit_coreVector* Vector=NULL;
  468.   int returnStatus=0;
  469.   _IF_SDLERR_I(x==0,-1,;,"variable x cannot be 0")
  470.  
  471.   //x axis
  472.   size_t x_size=(!y && !z) ? unit*x : sizeof(void*)*x;
  473.   returnStatus=_kit_coreVectorRealloc(&Vector,0, 0,sizeof(kit_coreVector)+x_size);
  474.   _IF_SDLERR_I(returnStatus,-2,;,"SDL_malloc() for x axis failed")
  475.   Vector->p1d=(void*)Vector + sizeof(kit_coreVector); //array is ahead by 32B
  476.   Vector->_dims=0x80000000; //sign bit will be unset on success
  477.   ++Vector->_dims; //vector now contains an x axis
  478.  
  479.   //y axis
  480.   _IF_SDLERR_I(y==0 && z>0,-3,;,"variable y cannot be 0 if z>0")
  481.   if(y>0){ ++Vector->_dims; //vector now contains a y axis
  482.     size_t y_size=(!z) ? unit*y : sizeof(void*)*y;
  483.     returnStatus=_kit_coreVectorResizeY(Vector, 0,x, 0,y_size, y);
  484.     _IF_SDLERR_I(returnStatus,-4,;,"SDL_malloc() for y axis failed")
  485.   }
  486.  
  487.   //z axis
  488.   if(z>0){ ++Vector->_dims; //vector now contains a z axis
  489.     returnStatus=_kit_coreVectorResizeZ(Vector, 0,x, 0,y, 0,unit*z, z);
  490.     _IF_SDLERR_I(returnStatus,-5,;,"SDL_malloc() for z axis failed")
  491.   }
  492.  
  493.   Vector->_dims&=0x7fffffff; //unset sign bit to indicate success
  494.   Vector->type.n=type_n;
  495.   Vector->x=x, Vector->y=y, Vector->z=z;
  496.   Vector->unit=unit;
  497.   _error_:
  498.   if((Vector!=NULL) && (Vector->_dims<0))
  499.     returnStatus+=kit_coreVectorDestroy(&Vector); //-=0 -> 2
  500.   if(returnStatus_p!=NULL) *returnStatus_p=returnStatus;
  501.   return Vector;
  502. }
  503.  
  504.  
  505. /* notes
  506. (in practice, redundant operations are supposed to be trimmed)
  507. sub z:
  508.   if  !3d: error "cannot change z on non-3d vector"
  509.   -realloc z
  510. sub y:
  511.   if   1d: error "cannot change y on 1d vector"
  512.   if   3d: free z in old y
  513.   -realloc y
  514. sub x:
  515.   if   3d: free z in y in old x
  516.   if >=2d: free y in old x
  517.   -realloc x
  518. add x:
  519.   +realloc x
  520.   if >=2d: malloc y in new x
  521.   if   3d: malloc z in new y in new x
  522. add y:
  523.   if   1d: error "cannot change y on 1d vector"
  524.   +realloc y
  525.   if   3d: malloc z in new y
  526. add z:
  527.   if  !3d: error "cannot change z on non-3d vector"
  528.   +realloc z
  529.  
  530.  
  531. (priority is sub x,y,z then add x,y,z)
  532.  
  533. _0x_0y_0z: (RETURN W/O ACTION)
  534.  
  535. SUB
  536. _Nx_0y_0z: (atomic)
  537.   sub x
  538. _0x_Ny_0z: (atomic)
  539.   sub y
  540. _0x_0y_Nz: (atomic)
  541.   sub z
  542. _Nx_Ny_0z: (_Nx_0y_0z,_0x_Ny_0z)
  543.   sub x
  544.   sub y
  545. _0x_Ny_Nz: (_0x_Ny_0z,_0x_0y_Nz)
  546.   sub y
  547.   sub z
  548. _Nx_0y_Nz: (_Nx_0y_0z,_0x_0y_Nz)
  549.   sub x
  550.   sub z
  551. _Nx_Ny_Nz: (_Nx_Ny_0z,_0x_0y_Nz)
  552.   sub x
  553.   sub y
  554.   sub z
  555.  
  556. ADD
  557. _Px_0y_0z: (atomic)
  558.   add x
  559. _0x_Py_0z: (atomic)
  560.   add y
  561. _0x_0y_Pz: (atomic)
  562.   add z
  563. _Px_Py_0z: (_Px_0y_0z,_0x_Py_0z)
  564.   add x
  565.   add y
  566. _0x_Py_Pz: (_0x_Py_0z,_0x_0y_Pz)
  567.   add y
  568.   add z
  569. _Px_0y_Pz: (_Px_0y_0z,_0x_0y_Pz)
  570.   add x
  571.   add z
  572. _Px_Py_Pz: (_Px_Py_0z,_0x_0y_Pz)
  573.   add x
  574.   add y
  575.   add z
  576.  
  577. SUBX ADDYZ
  578. _Nx_Py_0z: (_Nx_0y_0z, _0x_Py_0z)
  579.   sub x
  580.     add y
  581. _Nx_0y_Pz: (_Nx_0y_0z, _0x_0y_Pz)
  582.   sub x
  583.     add z
  584. _Nx_Py_Pz: (_Nx_0y_0z, _0x_Py_Pz)
  585.   sub x
  586.     add y
  587.     add z
  588.  
  589. SUBY ADDXZ
  590. _Px_Ny_0z: (_0x_Ny_0z, _Px_0y_0z)
  591.   sub y
  592.     add x
  593. _0x_Ny_Pz: (_0x_Ny_0z, _0x_0y_Pz)
  594.   sub y
  595.     add z
  596. _Px_Ny_Pz: (_0x_Ny_0z, _Px_0y_Pz)
  597.   sub y
  598.     add x
  599.     add z
  600.  
  601. SUBZ ADDXY
  602. _Px_0y_Nz: (_0x_0y_Nz, _Px_0y_0z)
  603.   sub z
  604.     add x
  605. _0x_Py_Nz: (_0x_0y_Nz, _0x_Py_0z)
  606.   sub z
  607.     add y
  608. _Px_Py_Nz: (_0x_0y_Nz, _Px_Py_0z)
  609.   sub z
  610.     add x
  611.     add y
  612.  
  613. SUBXYZ ADDXYZ
  614. _Nx_Ny_Pz: (_Nx_Ny_0z, _0x_0y_Pz)
  615.   sub x
  616.   sub y
  617.     add z
  618. _Px_Ny_Nz: (_0x_Ny_Nz, _Px_0y_0z)
  619.   sub y
  620.   sub z
  621.     add x
  622. _Nx_Py_Nz: (_Nx_0y_Nz, _0x_Py_0z)
  623.   sub x
  624.   sub z
  625.     add y
  626. */
  627.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement