Advertisement
Kitomas

kit_sdl2_coreVector.c as of 2023-9-7

Sep 8th, 2023 (edited)
1,008
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 36.65 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. //(so i can choose whether or not to actually print here)
  8. #define _vectorLogOP(...) kit_coreLog(__VA_ARGS__)
  9. //#define _vectorLogOP(...) ;
  10. //#define _vectorLogT(...) kit_coreLog(__VA_ARGS__)
  11. #define _vectorLogT(...) ;
  12.  
  13. extern void _kit_coreVectorPrintInt(kit_coreVector* Vector, const char* prefix);
  14. extern void _kit_coreVectorPrintLens(kit_coreVector* Vector, const char* prefix);
  15.  
  16.  
  17. //(like kit_coreRealloc, and added memory is initialized to 0)
  18. int _kit_coreVectorRealloc(void* ptr_p, char axis,
  19.                            lens_t osize, lens_t nsize)
  20. {
  21.   if(osize==nsize) return 0;
  22.   void* _ptr=SDL_realloc(*(void**)ptr_p,nsize);
  23.   if(_ptr==NULL){
  24.     if(axis!=0) SDL_SetError("%c realloc",axis);
  25.     return -1;
  26.   }
  27.   if(nsize>osize) SDL_memset(_ptr+osize, 0, /*size_difference=*/nsize-osize);
  28.   *(void**)ptr_p=_ptr;
  29.   return 0;
  30. }
  31.  
  32.  
  33. void _kit_coreVectorFreeZinY(kit_coreVector* Vector,
  34.                              lens_t x_start, lens_t x_end,
  35.                              lens_t y_start, lens_t y_end)
  36. {
  37.   if((Vector->_dims&3)<3) return; //z doesn't exist in 1d or 2d array
  38.   void*** p3d=Vector->p3d;
  39.   for(lens_t xi=x_start; xi<x_end; ++xi){ void** p2d=p3d[xi];
  40.     if(p2d==NULL) continue;
  41.     for(lens_t yi=y_start; yi<y_end; ++yi){ void* p1d=p2d[yi];
  42.       if(p1d==NULL) continue;
  43.       SDL_free(p1d); p2d[yi]=NULL;
  44.     }
  45.   }
  46. }
  47.  
  48. void _kit_coreVectorFreeYinX(kit_coreVector* Vector,
  49.                              lens_t x_start, lens_t x_end)
  50. {
  51.   if((Vector->_dims&3)<2) return; //y doesn't exist in 1d array (nor does z)
  52.   void** p2d=Vector->p2d;
  53.   for(lens_t xi=x_start; xi<x_end; ++xi){ void* p1d=p2d[xi];
  54.     if(p1d==NULL) continue;
  55.     SDL_free(p1d); p2d[xi]=NULL;
  56.   }
  57. }
  58.  
  59.  
  60. int _kit_coreVectorResizeX(kit_coreVector** Vector_p,
  61.                            lens_t x_osize, lens_t x_nsize, lens_t x_new)
  62. {
  63.   //(x axis is contiguous with the vector struct itself)
  64.   x_osize+=sizeof(kit_coreVector); x_nsize+=sizeof(kit_coreVector);
  65.   if(_kit_coreVectorRealloc(Vector_p,'x', x_osize,x_nsize)) return -12;
  66.   kit_coreVector* Vector=*Vector_p;
  67.   Vector->ptr=(void*)Vector+sizeof(kit_coreVector);
  68.   Vector->x=x_new; *Vector_p=Vector;
  69.   return 0;
  70. }
  71.  
  72. int _kit_coreVectorResizeY(kit_coreVector* Vector,
  73.                            lens_t x_start, lens_t x_end,
  74.                            lens_t y_osize, lens_t y_nsize, lens_t y_new)
  75. {
  76.   if((Vector->_dims&3)<2) return 0; //y doesn't exist in 1d array (nor does z)
  77.   void** p2d=Vector->p2d;
  78.   for(lens_t xi=x_start; xi<x_end; ++xi)
  79.     if(_kit_coreVectorRealloc(&p2d[xi],'y', y_osize,y_nsize)) return -13;
  80.   Vector->y=y_new;
  81.   return 0;
  82. }
  83.  
  84. int _kit_coreVectorResizeZ(kit_coreVector* Vector,
  85.                            lens_t x_start, lens_t x_end,
  86.                            lens_t y_start, lens_t y_end,
  87.                            lens_t z_osize, lens_t z_nsize, lens_t z_new)
  88. {
  89.   if((Vector->_dims&3)<3) return 0; //z doesn't exist in 1d or 2d array
  90.   void*** p3d=Vector->p3d;
  91.   for(lens_t xi=x_start; xi<x_end; ++xi){ void** p2d=p3d[xi];
  92.     for(lens_t yi=y_start; yi<y_end; ++yi)
  93.       if(_kit_coreVectorRealloc(&p2d[yi],'z', z_osize,z_nsize)) return -14;
  94.   }
  95.   Vector->z=z_new;
  96.   return 0;
  97. }
  98.  
  99.  
  100. void _kit_coreVectorFreeLensYinX(kit_coreVector* Vector,
  101.                                  lens_t x_start, lens_t x_end)
  102. {
  103.   //since this is freeing heap memory,
  104.    //y is only allocated memory when vector is 3d
  105.   if((Vector->_dims&3)<3) return;
  106.   lens_t** r3D=Vector->lens.r3D;
  107.   if(r3D == NULL) return;
  108.   for(lens_t xi=x_start; xi<x_end; ++xi){ lens_t* r2D=r3D[xi];
  109.     if(r2D==NULL) continue;
  110.     SDL_free(r2D); r3D[xi]=NULL;
  111.   }
  112. }
  113.  
  114.  
  115. int _kit_coreVectorResizeLensX(kit_coreVector* Vector,
  116.                                lens_t x_old, lens_t x_new)
  117. {
  118.   int dims=Vector->_dims&3;
  119.   if(Vector->lens.r3D==NULL) return 0;
  120.   if(dims<=1){
  121.     //real length must be <= allocated length
  122.     if(Vector->lens.r1D>x_new) Vector->lens.r1D=x_new;
  123.     return 0;
  124.   } else if(dims==2){
  125.     x_old*=sizeof(lens_t);
  126.     x_new*=sizeof(lens_t);
  127.   } else if(dims==3){
  128.     x_old*=sizeof(lens_t*);
  129.     x_new*=sizeof(lens_t*);
  130.   }
  131.   if(_kit_coreVectorRealloc(&Vector->lens.r3D,'x', x_old,x_new)) return -12;
  132.   return 0;
  133. }
  134.  
  135. int _kit_coreVectorResizeLensY(kit_coreVector* Vector,
  136.                                lens_t x_start, lens_t x_end,
  137.                                lens_t   y_old, lens_t y_new)
  138. {
  139.   int dims=Vector->_dims&3;
  140.   if(dims<2) return 0; //y doesn't exist in 1d array (nor does z)
  141.   if(Vector->lens.r3D==NULL) return 0;
  142.   else if(dims==2){
  143.     lens_t* r2D=Vector->lens.r2D;
  144.     for(lens_t xi=x_start; xi<x_end; ++xi)
  145.       if(r2D[xi]>y_new) r2D[xi]=y_new; //real length must be <= allocated length
  146.   } else if(dims==3){
  147.     kit_coreLog("xs,xe=%u,%u, yo,yn=%u,%u", x_start,x_end, y_old,y_new);
  148.     y_old*=sizeof(lens_t*);
  149.     y_new*=sizeof(lens_t*);
  150.     lens_t** r3D=Vector->lens.r3D;
  151.     for(lens_t xi=x_start; xi<x_end; ++xi)
  152.       if(_kit_coreVectorRealloc(&r3D[xi],'y', y_old,y_new)) return -13;
  153.   }
  154.   return 0;
  155. }
  156.  
  157. void _kit_coreVectorResizeLensZ(kit_coreVector* Vector,
  158.                                 lens_t x_start, lens_t x_end,
  159.                                 lens_t y_start, lens_t y_end,
  160.                                 lens_t z_new)
  161. {
  162.   if((Vector->_dims&3)<3) return; //z doesn't exist in 1d or 2d array
  163.   if(Vector->lens.r3D==NULL) return;
  164.   lens_t** r3D=Vector->lens.r3D;
  165.   for(lens_t xi=x_start; xi<x_end; ++xi){ lens_t* r2D=r3D[xi];
  166.     for(lens_t yi=y_start; yi<y_end; ++yi)
  167.       if(r2D[yi]>z_new) r2D[yi]=z_new; //real length must be <= allocated length
  168.   }
  169. }
  170.  
  171.  
  172. //(remember, <xyz>_old and <xyz>_new are sometimes flipped when used as a parameter when freeing)
  173. static inline int _kit_coreVectorSubX(kit_coreVector** Vector_p,
  174.                                       lens_t x_new,   lens_t x_old, lens_t y_new,
  175.                                       lens_t x_osize, lens_t x_nsize)
  176. { _vectorLogOP("  SubX");
  177.   kit_coreVector* Vector=*Vector_p; int rS; //(r)eturn(S)tatus
  178.   _kit_coreVectorFreeZinY(Vector, x_new,x_old, 0,y_new);
  179.   _kit_coreVectorFreeYinX(Vector, x_new,x_old);
  180.   if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
  181.   //lens
  182.   _kit_coreVectorFreeLensYinX(Vector, x_new,x_old);
  183.   if((rS=_kit_coreVectorResizeLensX(Vector, x_old,x_new))) return rS;
  184.   return 0;
  185. }
  186.  
  187. static inline int _kit_coreVectorSubY(kit_coreVector* Vector,
  188.                                       lens_t x_new,   lens_t y_new, lens_t y_old,
  189.                                       lens_t y_osize, lens_t y_nsize)
  190. { _vectorLogOP("  SubY");
  191.   int rS; //(r)eturn(S)tatus
  192.   _kit_coreVectorFreeZinY(Vector, 0,x_new, y_new,y_old);
  193.   if((rS=_kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new))) return rS;
  194.   //lens
  195.   if((rS=_kit_coreVectorResizeLensY(Vector, 0,x_new, y_old,y_new))) return rS;
  196.   return 0;
  197. }
  198.  
  199. static inline int _kit_coreVectorSubZ(kit_coreVector* Vector,
  200.                                       lens_t x_new,   lens_t y_new, lens_t z_osize,
  201.                                       lens_t z_nsize, lens_t z_new)
  202. { _vectorLogOP("  SubZ");
  203.   int rS; //(r)eturn(S)tatus
  204.   if((rS=_kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new))) return rS;
  205.   //lens
  206.   _kit_coreVectorResizeLensZ(Vector, 0,x_new, 0,y_new, z_new); //returns void
  207.   return 0;
  208. }
  209.  
  210. static inline int _kit_coreVectorSubXY(kit_coreVector** Vector_p,
  211.                                        lens_t x_new,   lens_t x_old, lens_t y_old,
  212.                                        lens_t x_osize, lens_t x_nsize,
  213.                                        lens_t y_new,   lens_t y_osize, lens_t y_nsize)
  214. { _vectorLogOP("SubXY");
  215.   kit_coreVector* Vector=*Vector_p; int rS; //(r)eturn(S)tatus
  216.   _kit_coreVectorFreeZinY(Vector, x_new,x_old,     0,y_old);
  217.   _kit_coreVectorFreeYinX(Vector, x_new,x_old);
  218.   if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
  219.   _kit_coreVectorFreeZinY(Vector,     0,x_new, y_new,y_old);
  220.   return _kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new);
  221. }
  222.  
  223. static inline int _kit_coreVectorSubYZ(kit_coreVector* Vector,
  224.                                        lens_t x_new,   lens_t y_new,   lens_t y_old,
  225.                                        lens_t y_osize, lens_t y_nsize,
  226.                                        lens_t z_osize, lens_t z_nsize, lens_t z_new)
  227. { _vectorLogOP("SubYZ");
  228.   int rS; //(r)eturn(S)tatus
  229.   _kit_coreVectorFreeZinY(Vector, 0,x_new, y_new,y_old);
  230.   if((rS=_kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new))) return rS;
  231.   return _kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new);
  232. }
  233.  
  234. static inline int _kit_coreVectorSubXZ(kit_coreVector** Vector_p,
  235.                                        lens_t x_new,   lens_t x_old,   lens_t y_new,
  236.                                        lens_t x_osize, lens_t x_nsize,
  237.                                        lens_t z_osize, lens_t z_nsize, lens_t z_new)
  238. { _vectorLogOP("SubXZ");
  239.   kit_coreVector* Vector=*Vector_p; int rS; //(r)eturn(S)tatus
  240.   _kit_coreVectorFreeZinY(Vector, x_new,x_old, 0,y_new);
  241.   _kit_coreVectorFreeYinX(Vector, x_new,x_old);
  242.   if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
  243.   return _kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new);
  244. }
  245.  
  246. static inline int _kit_coreVectorSubXYZ(kit_coreVector** Vector_p,
  247.                                         lens_t x_new,   lens_t x_old,   lens_t y_old,
  248.                                         lens_t x_osize, lens_t x_nsize,
  249.                                         lens_t y_new,   lens_t y_osize, lens_t y_nsize,
  250.                                         lens_t z_osize, lens_t z_nsize, lens_t z_new)
  251. { _vectorLogOP("SubXYZ");
  252.   kit_coreVector* Vector=*Vector_p; int rS; //(r)eturn(S)tatus
  253.   _kit_coreVectorFreeZinY(Vector, x_new,x_old,     0,y_old);
  254.   _kit_coreVectorFreeYinX(Vector, x_new,x_old);
  255.   if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
  256.   _kit_coreVectorFreeZinY(Vector,     0,x_new, y_new,y_old);
  257.   if((rS=_kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new))) return rS;
  258.   return _kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new);
  259. }
  260.  
  261.  
  262. static inline int _kit_coreVectorAddX(kit_coreVector** Vector_p,
  263.                                       lens_t x_old,   lens_t x_new, lens_t x_osize, lens_t x_nsize,
  264.                                                       lens_t y_new,                 lens_t y_nsize,
  265.                                                       lens_t z_new,                 lens_t z_nsize)
  266. { _vectorLogOP("  AddX");
  267.   int rS; //(r)eturn(S)tatus
  268.   if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
  269.   kit_coreVector* Vector=*Vector_p;
  270.   if((rS=_kit_coreVectorResizeY(Vector, x_old,x_new, 0,y_nsize, y_new))) return rS;
  271.   if((rS=_kit_coreVectorResizeZ(Vector, x_old,x_new, 0,y_new, 0,z_nsize, z_new))) return rS;
  272.   //lens
  273.   if((rS=_kit_coreVectorResizeLensX(Vector, x_old,x_new))) return rS;
  274.   if((rS=_kit_coreVectorResizeLensY(Vector, x_old,x_new, 0,y_new))) return rS;
  275.   //(lens z resize unnecessary for addition)
  276.   return 0;
  277. }
  278.  
  279. static inline int _kit_coreVectorAddY(kit_coreVector* Vector,
  280.                                                     lens_t x_new,
  281.                                       lens_t y_old, lens_t y_new, lens_t y_osize, lens_t y_nsize,
  282.                                                     lens_t z_new,                 lens_t z_nsize)
  283. { _vectorLogOP("  AddY");
  284.   int rS; //(r)eturn(S)tatus
  285.   if((rS=_kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new))) return rS;
  286.   if((rS=_kit_coreVectorResizeZ(Vector, 0,x_new, y_old,y_new, 0,z_nsize, z_new))) return rS;
  287.   //lens
  288.   if((rS=_kit_coreVectorResizeLensY(Vector, 0,x_new, y_old,y_new))) return rS;
  289.   //(lens z resize unnecessary for addition)
  290.   return 0;
  291. }
  292.  
  293. static inline int _kit_coreVectorAddZ(kit_coreVector* Vector,
  294.                                       lens_t x_new,
  295.                                       lens_t y_new,
  296.                                       lens_t z_new, lens_t z_osize, lens_t z_nsize)
  297. { _vectorLogOP("  AddZ");
  298.   int rS; //(r)eturn(S)tatus
  299.   if((rS=_kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new))) return rS;
  300.   //(lens z resize unnecessary for addition)
  301.   return 0;
  302. }
  303.  
  304. static inline int _kit_coreVectorAddXY(kit_coreVector** Vector_p,
  305.                                        lens_t x_old, lens_t x_new, lens_t x_osize, lens_t x_nsize,
  306.                                        lens_t y_old, lens_t y_new, lens_t y_osize, lens_t y_nsize,
  307.                                                      lens_t z_new,                 lens_t z_nsize)
  308. { _vectorLogOP("AddXY");
  309.   kit_coreVector* Vector=*Vector_p; int rS; //(r)eturn(S)tatus
  310.   if((rS=_kit_coreVectorAddY(Vector, x_new, y_osize,y_nsize,
  311.                              y_new,y_old, z_nsize, z_new))) return rS;
  312.   if((rS=_kit_coreVectorAddX(Vector_p, x_osize,x_nsize, x_new,x_old,
  313.                              y_nsize,y_new, z_nsize, z_new))) return rS;
  314.   return 0;
  315. }
  316.  
  317. static inline int _kit_coreVectorAddYZ(kit_coreVector* Vector,
  318.                                                      lens_t x_new,
  319.                                        lens_t y_old, lens_t y_new, lens_t y_osize, lens_t y_nsize,
  320.                                                      lens_t z_new, lens_t z_osize, lens_t z_nsize)
  321. { _vectorLogOP("AddYZ");
  322.   int rS; //(r)eturn(S)tatus
  323.   if((rS=_kit_coreVectorAddY(Vector, x_new, y_osize,y_nsize,
  324.                              y_new,y_old, z_nsize, z_new))) return rS;
  325.   if((rS=_kit_coreVectorAddZ(Vector, x_new, y_new, z_osize,z_nsize, z_new))) return rS;
  326.   return 0;
  327. }
  328.  
  329. static inline int _kit_coreVectorAddXZ(kit_coreVector** Vector_p,
  330.                                        lens_t x_old, lens_t x_new, lens_t x_osize, lens_t x_nsize,
  331.                                        lens_t y_new,                               lens_t y_nsize,
  332.                                                      lens_t z_new, lens_t z_osize, lens_t z_nsize)
  333. { _vectorLogOP("AddXZ");
  334.   kit_coreVector* Vector=*Vector_p; int rS; //(r)eturn(S)tatus
  335.   if((rS=_kit_coreVectorAddX(Vector_p, x_osize,x_nsize, x_new,x_old,
  336.                              y_nsize,y_new, z_nsize, z_new))) return rS;
  337.   if((rS=_kit_coreVectorAddZ(Vector, x_new, y_new, z_osize,z_nsize, z_new))) return rS;
  338.   return 0;
  339. }
  340.  
  341. static inline int _kit_coreVectorAddXYZ(kit_coreVector** Vector_p,
  342.                                         lens_t x_old, lens_t x_new, lens_t x_osize, lens_t x_nsize,
  343.                                         lens_t y_old, lens_t y_new, lens_t y_osize, lens_t y_nsize,
  344.                                                       lens_t z_new, lens_t z_osize, lens_t z_nsize)
  345. { _vectorLogOP("AddXYZ");
  346.   kit_coreVector* Vector=*Vector_p; int rS; //(r)eturn(S)tatus
  347.   if((rS=_kit_coreVectorAddX(Vector_p, x_osize,x_nsize, x_new,x_old,
  348.                              y_nsize,y_new, z_nsize, z_new))) return rS;
  349.   if((rS=_kit_coreVectorAddY(Vector, x_new, y_osize,y_nsize,
  350.                              y_new,y_old, z_nsize, z_new))) return rS;
  351.   if((rS=_kit_coreVectorAddZ(Vector, x_new, y_new, z_osize,z_nsize, z_new))) return rS;
  352. }
  353.  
  354.  
  355. //_<0(unchanged)/N(egative)/P(ositive)><X/Y/Z>
  356. #define _0X (0x0000)
  357. #define _NX (0x1000)
  358. #define _PX (0x3000)
  359. #define _0Y (0x0000)
  360. #define _NY (0x0100)
  361. #define _PY (0x0300)
  362. #define _0Z (0x0000)
  363. #define _NZ (0x0010)
  364. #define _PZ (0x0030)
  365. #define _VADECISION(_action) \
  366.   if((returnStatus=_action)){ goto _error_; }
  367. #define _VADECISION_B(_action) _VADECISION(_action) break;
  368. //add to or subtract from size
  369. int kit_coreVectorAdd(kit_coreVector** Vector_p, diffs_t x_add, diffs_t y_add, diffs_t z_add){
  370.   if(!x_add && !y_add && !z_add) return 0;
  371.   int returnStatus=0;
  372.   _IF_SDLERR_I(Vector_p==NULL,-1,;,"!Vector_p")
  373.   kit_coreVector* Vector=*Vector_p;
  374.   _IF_SDLERR_I(Vector==NULL,-2,;,"!*Vector_p")
  375.  
  376.   lens_t unit=Vector->unit;
  377.   lens_t x_old=Vector->x,   y_old=Vector->y,   z_old=Vector->z;
  378.        _IF_SDLERR_I( (!z_old)&&(z_add!=0) ,-3,;,"z!=0&& !3D")
  379.   else _IF_SDLERR_I( (!y_old)&&(y_add!=0) ,-4,;,"y!=0 && 1D")
  380.   lens_t x_new=x_old+x_add, y_new=y_old+y_add, z_new=z_old+z_add;
  381.        _IF_SDLERR_I( (!z_old)!=(!z_new) ,-5,;,"z axis flp")
  382.   else _IF_SDLERR_I( (!y_old)!=(!y_new) ,-6,;,"y axis flp")
  383.  
  384.   //check for any underflows and overflows respectively (and some dimension error checks)
  385.    //x
  386.   _IF_SDLERR_I( ((x_add<0)&&(x_new>x_old)) || !x_new ,-7,;,"x==0")
  387.   _IF_SDLERR_I( (x_add>0)&&(x_new<x_old) ,-8,;,"x overflow")
  388.    //y
  389.   if(y_add<0 && (y_new>y_old)) y_new=0; //set to 0 in event of underflow
  390.   else _IF_SDLERR_I( (y_add>0)&&(y_new<y_old) ,-9,;,"y overflow")
  391.   else _IF_SDLERR_I( (y_new==0)&&(z_new>0) ,-10,;,"y==0&& z>0")
  392.    //z
  393.   if((z_add<0) && (z_new>z_old)) z_new=0; //set to 0 in event of underflow
  394.   else _IF_SDLERR_I( (z_add>0)&&(z_new<z_old) ,-11,;,"z overflow")
  395.  
  396.   lens_t x_unit, y_unit, z_unit;
  397.   x_unit=y_unit=z_unit = sizeof(void*);
  398.   int dims=(x_new!=0)+(y_new!=0)+(z_new!=0); //dim(ension)s
  399.        if(dims==1)   x_unit=unit;
  400.   else if(dims==2)   y_unit=unit;
  401.   else /*(dims==3)*/ z_unit=unit;
  402.   lens_t x_osize=x_unit*x_old, y_osize=y_unit*y_old, z_osize=z_unit*z_old;
  403.   lens_t x_nsize=x_unit*x_new, y_nsize=y_unit*y_new, z_nsize=z_unit*z_new;
  404.  
  405.   int decision;
  406.   decision =( ((x_add>0)<<1)|(x_add!=0) )<<12; //0x0000 -> 0x3000
  407.   decision|=( ((y_add>0)<<1)|(y_add!=0) )<< 8; //0x0000 -> 0x0300
  408.   decision|=( ((z_add>0)<<1)|(z_add!=0) )<< 4; //0x0000 -> 0x0030
  409.   switch(decision){
  410.   //SUB
  411.   case (_NX|_0Y|_0Z): //_NX_0Y_0Z; technically a superset of: (n/a)
  412.     _VADECISION_B(_kit_coreVectorSubX(&Vector,x_new,x_old,y_new,x_osize,x_nsize))
  413.   case (_0X|_NY|_0Z): //_0X_NY_0Z; technically a superset of: (n/a)
  414.     _VADECISION_B(_kit_coreVectorSubY(Vector,x_new,y_new,y_old,y_osize,y_nsize))
  415.   case (_0X|_0Y|_NZ): //_0X_0Y_NZ; technically a superset of: (n/a)
  416.     _VADECISION_B(_kit_coreVectorSubZ(Vector,x_new,y_new,z_osize,z_nsize,z_new))
  417.   /*
  418.   case (_NX|_NY|_0Z): //_NX_NY_0Z; technically a superset of: (_NX_0Y_0Z,_0X_NY_0Z)
  419.     _VADECISION_B(_kit_coreVectorSubXY(&Vector,x_new,x_old,y_old,x_osize,x_nsize,y_new,y_osize,y_nsize))
  420.   case (_0X|_NY|_NZ): //_0X_NY_NZ; technically a superset of: (_0X_NY_0Z,_0X_0Y_NZ)
  421.     _VADECISION_B(_kit_coreVectorSubYZ(Vector,x_new,y_new,y_old,y_osize,y_nsize,z_osize,z_nsize,z_new))
  422.   case (_NX|_0Y|_NZ): //_NX_0Y_NZ; technically a superset of: (_NX_0Y_0Z,_0X_0Y_NZ)
  423.     _VADECISION_B(_kit_coreVectorSubXZ(&Vector,x_new,x_old,y_new,x_osize,x_nsize,z_osize,z_nsize,z_new))
  424.   case (_NX|_NY|_NZ): //_NX_NY_NZ; technically a superset of: (_NX_NY_0Z,_0X_0Y_NZ)
  425.     _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))
  426.   */
  427.   //ADD
  428.   case (_PX|_0Y|_0Z): //_PX_0Y_0Z; technically a superset of: (n/a)
  429.     _VADECISION_B(_kit_coreVectorAddX(&Vector, x_old,x_new,x_osize,x_nsize, y_new,y_nsize, z_new,z_nsize))
  430.   case (_0X|_PY|_0Z): //_0X_PY_0Z; technically a superset of: (n/a)
  431.     _VADECISION_B(_kit_coreVectorAddY(Vector, x_new, y_old,y_new, y_osize,y_nsize, z_new,z_nsize))
  432.   case (_0X|_0Y|_PZ): //_0X_0Y_PZ; technically a superset of: (n/a)
  433.     _VADECISION_B(_kit_coreVectorAddZ(Vector, x_new, y_new, z_new,z_osize,z_nsize))
  434.   case (_PX|_PY|_0Z): //_PX_PY_0Z; technically a superset of: (_PX_0Y_0Z,_0X_PY_0Z)
  435.     _VADECISION_B(_kit_coreVectorAddXY(&Vector, x_old,x_new,x_osize,x_nsize, y_old,y_new,y_osize,y_nsize, z_new,z_nsize))
  436.   /*
  437.   case (_0X|_PY|_PZ): //_0X_PY_PZ; technically a superset of: (_0X_PY_0Z,_0X_0Y_PZ)
  438.     _VADECISION_B(_kit_coreVectorAddYZ())
  439.   case (_PX|_0Y|_PZ): //_PX_0Y_PZ; technically a superset of: (_PX_0Y_0Z,_0X_0Y_PZ)
  440.     _VADECISION_B(_kit_coreVectorAddXZ())
  441.   case (_PX|_PY|_PZ): //_PX_PY_PZ; technically a superset of: (_PX_PY_0Z,_0X_0Y_PZ)
  442.     _VADECISION_B(_kit_coreVectorAddXYZ())
  443.   //SUBX ADDYZ
  444.   case (_NX|_PY|_0Z): //_NX_PY_0Z; technically a superset of: (_NX_0Y_0Z, _0X_PY_0Z)
  445.     _VADECISION(  _kit_coreVectorSubX())
  446.     _VADECISION_B(_kit_coreVectorAddY())
  447.   case (_NX|_0Y|_PZ): //_NX_0Y_PZ; technically a superset of: (_NX_0Y_0Z, _0X_0Y_PZ)
  448.     _VADECISION(  _kit_coreVectorSubX())
  449.     _VADECISION_B(_kit_coreVectorAddZ())
  450.   case (_NX|_PY|_PZ): //_NX_PY_PZ; technically a superset of: (_NX_0Y_0Z, _0X_PY_PZ)
  451.     _VADECISION(  _kit_coreVectorSubX())
  452.     _VADECISION_B(_kit_coreVectorAddYZ())
  453.   //SUBY ADDXZ
  454.   case (_PX|_NY|_0Z): //_PX_NY_0Z; technically a superset of: (_0X_NY_0Z, _PX_0Y_0Z)
  455.     //x_new is swapped for x_old here (see line 5)
  456.     _VADECISION(  _kit_coreVectorSubY())
  457.     _VADECISION_B(_kit_coreVectorAddX())
  458.   case (_0X|_NY|_PZ): //_0X_NY_PZ; technically a superset of: (_0X_NY_0Z, _0X_0Y_PZ)
  459.     _VADECISION(  _kit_coreVectorSubY())
  460.     _VADECISION_B(_kit_coreVectorAddZ())
  461.   case (_PX|_NY|_PZ): //_PX_NY_PZ; technically a superset of: (_0X_NY_0Z, _PX_0Y_PZ)
  462.     //x_new is swapped for x_old here (see line 5)
  463.     _VADECISION(  _kit_coreVectorSubY())
  464.     _VADECISION_B(_kit_coreVectorAddXZ())
  465.   //SUBZ ADDXY
  466.   case (_PX|_0Y|_NZ): //_PX_0Y_NZ; technically a superset of: (_0X_0Y_NZ, _PX_0Y_0Z)
  467.     //x_new is swapped for x_old here (see line 5)
  468.     _VADECISION(  _kit_coreVectorSubZ())
  469.     _VADECISION_B(_kit_coreVectorAddX())
  470.   case (_0X|_PY|_NZ): //_0X_PY_NZ; technically a superset of: (_0X_0Y_NZ, _0X_PY_0Z)
  471.     //y_new is swapped for y_old here (see line 5)
  472.     _VADECISION(  _kit_coreVectorSubZ())
  473.     _VADECISION_B(_kit_coreVectorAddY())
  474.   case (_PX|_PY|_NZ): //_PX_PY_NZ; technically a superset of: (_0X_0Y_NZ, _PX_PY_0Z)
  475.     //x_new,y_new is swapped for x_old,y_old here respectively (see line 5)
  476.     _VADECISION(  _kit_coreVectorSubZ())
  477.     _VADECISION_B(_kit_coreVectorAddXY())
  478.   //SUBXYZ ADDXYZ
  479.   case (_NX|_NY|_PZ): //_NX_NY_PZ; technically a superset of: (_NX_NY_0Z, _0X_0Y_PZ)
  480.     _VADECISION(  _kit_coreVectorSubXY())
  481.     _VADECISION_B(_kit_coreVectorAddZ())
  482.   case (_PX|_NY|_NZ): //_PX_NY_NZ; technically a superset of: (_0X_NY_NZ, _PX_0Y_0Z)
  483.     //x_new is swapped for x_old here (see line 5)
  484.     _VADECISION(  _kit_coreVectorSubYZ())
  485.     _VADECISION_B(_kit_coreVectorAddX())
  486.   case (_NX|_PY|_NZ): //_NX_PY_NZ; technically a superset of: (_NX_0Y_NZ, _0X_PY_0Z)
  487.     //y_new is swapped for y_old here (see line 5)
  488.     _VADECISION(  _kit_coreVectorSubXZ())
  489.     _VADECISION_B(_kit_coreVectorAddY())
  490.   */
  491.   default: _IS_SDLERR_I(-15,;,"deflt:%X",decision)
  492.   } *Vector_p=Vector;
  493.   _error_:
  494.   return returnStatus;
  495. }
  496.  
  497. //set new size (if an axis == 0, then leave that axis unchanged)
  498.  //also, if the new axis length is the same as the old one, leave it unchanged also
  499. int kit_coreVectorSet(kit_coreVector** Vector_p, lens_t x_new, lens_t y_new, lens_t z_new){
  500.   int returnStatus=-1;
  501.   _IF_SDLERR(Vector_p==NULL,;,"!Vector_p")
  502.   kit_coreVector* Vector=*Vector_p;
  503.   _IF_SDLERR(Vector==NULL,;,"!*Vector_p")
  504.  
  505.   diffs_t x_diff=(x_new==0) ? 0 : x_new-Vector->x;
  506.   diffs_t y_diff=(y_new==0) ? 0 : y_new-Vector->y;
  507.   diffs_t z_diff=(z_new==0) ? 0 : z_new-Vector->z;
  508.  
  509.   returnStatus=kit_coreVectorAdd(Vector_p, x_diff,y_diff,z_diff);
  510.  
  511.   _error_:
  512.   return returnStatus;
  513. }
  514.  
  515.  
  516. lens_t kit_coreVectorAppend1D(kit_coreVector** Vector_p, void* src){ //TBD
  517.   lens_t newIndex=-1;
  518.   _IF_SDLERR(Vector_p==NULL,;,"!Vector_p")
  519.   kit_coreVector* Vector=*Vector_p;
  520.   _IF_SDLERR(Vector==NULL,;,"!*Vector_p")
  521.   _IF_SDLERR((Vector->_dims&3)!=1,;,"vector!=1D")
  522.  
  523.   _IF_GOTO_ERROR(kit_coreVectorAdd(&Vector, 1,0,0),;)
  524.   newIndex=Vector->x-1; lens_t unitSize=Vector->unit;
  525.   void* dst=Vector->p1d + newIndex*unitSize;
  526.  
  527.   SDL_memcpy(dst, src, unitSize);
  528.   *Vector_p=Vector;
  529.  
  530.   _error_:
  531.   return newIndex;
  532. }
  533.  
  534. lens_t kit_coreVectorAppend2D(kit_coreVector** Vector_p, void* src, lens_t x_pos){ //TBD
  535.   lens_t newIndex=-1;
  536.   _IF_SDLERR(Vector_p==NULL,;,"!Vector_p")
  537.   kit_coreVector* Vector=*Vector_p;
  538.   _IF_SDLERR(Vector==NULL,;,"!*Vector_p")
  539.   _IF_SDLERR((Vector->_dims&3)!=2,;,"vector!=2D")
  540. /*
  541.   newIndex=y_lengths[x_pos]; lens_t unitSize=Vector->unit;
  542.   if(newIndex > (Vector->y-1)) _IF_GOTO_ERROR(kit_coreVectorAdd(&Vector, 0,1,0),newIndex=-1)
  543.   void* dst=Vector->p2d[x_pos] + newIndex*unitSize;
  544.  
  545.   SDL_memcpy(dst, src, unitSize);
  546.   ++y_lengths[x_pos];
  547.   *Vector_p=Vector;
  548. */
  549.   _error_:
  550.   return newIndex;
  551. }
  552.  
  553. lens_t kit_coreVectorAppend3D(kit_coreVector** Vector_p, void* src, lens_t x_pos, lens_t y_pos){ //TBD
  554.   lens_t newIndex=-1;
  555.   _IF_SDLERR(Vector_p==NULL,;,"!Vector_p")
  556.   kit_coreVector* Vector=*Vector_p;
  557.   _IF_SDLERR(Vector==NULL,;,"!*Vector_p")
  558.   _IF_SDLERR((Vector->_dims&3)!=3,;,"vector!=3D")
  559. /*
  560.   newIndex=yz_lengths[x_pos][y_pos]; lens_t unitSize=Vector->unit;
  561.   if(newIndex > (Vector->z-1)) _IF_GOTO_ERROR(kit_coreVectorAdd(&Vector, 0,0,1),newIndex=-1)
  562.   void* dst=Vector->p3d[x_pos][y_pos] + newIndex*unitSize;
  563.  
  564.   SDL_memcpy(dst, src, unitSize);
  565.   ++yz_lengths[x_pos][y_pos];
  566.   *Vector_p=Vector;
  567. */
  568.   _error_:
  569.   return newIndex;
  570. }
  571.  
  572. lens_t kit_coreVectorAppend(kit_coreVector** Vector_p, void* src, lens_t x_pos, lens_t y_pos){ //TBD
  573.   lens_t newIndex=-1;
  574.   _IF_SDLERR(Vector_p==NULL,;,"!Vector_p")
  575.   kit_coreVector* Vector=*Vector_p;
  576.   _IF_SDLERR(Vector==NULL,;,"!*Vector_p")
  577.   _IF_SDLERR((Vector->_dims&3)==0,;,"vector==0D")
  578.  
  579.   lens_t dimensions=Vector->_dims&3;
  580.   if(     dimensions==1) newIndex=kit_coreVectorAppend1D(Vector_p, src);
  581.   else if(dimensions==2) newIndex=kit_coreVectorAppend2D(Vector_p, src, x_pos);
  582.   else if(dimensions==3) newIndex=kit_coreVectorAppend3D(Vector_p, src, x_pos, y_pos);
  583.  
  584.   _error_:
  585.   return newIndex;
  586. }
  587.  
  588.  
  589. int kit_coreVectorDestroy(kit_coreVector** Vector_p){
  590.   int returnStatus=0;
  591.   _IF_SDLERR_I(Vector_p==NULL,-1,;,"!Vector_p")
  592.   kit_coreVector* Vector=*Vector_p;
  593.   _IF_SDLERR_I(Vector==NULL,-2,;,"!*Vector_p")
  594.   int x=Vector->x, y=Vector->y; //z=Vector->z;
  595.   int condition = Vector->_dims&=3;
  596.   if(!condition) condition = (x!=0)+(y!=0)+(Vector->z!=0);
  597.   _IF_SDLERR_I(!condition,-3,;,"vector=0D")
  598.  
  599.   //condition must be equal to the number of dimensions (1=1d,2=2d,3=3d)
  600.   switch(condition){
  601.   case 3: _kit_coreVectorFreeZinY(Vector, 0,x, 0,y);
  602.           _kit_coreVectorFreeLensYinX(Vector, 0,x);                SDL_FALLTHROUGH;
  603.   case 2: _kit_coreVectorFreeYinX(Vector, 0,x);
  604.           if(Vector->lens.r2D != NULL) SDL_free(Vector->lens.r2D); SDL_FALLTHROUGH;
  605.   case 1: SDL_free(Vector); //frees Vector->p1d too
  606.   }       *Vector_p=NULL;
  607.  
  608.   _error_: return returnStatus;
  609. }
  610.  
  611.  
  612. kit_coreVector* kit_coreVectorCreate(lens_t x, lens_t y, lens_t z, lens_t unit, Uint64 type_n){
  613.   kit_coreVector* Vector=NULL;
  614.   _IF_SDLERR(x==0,;,"x==0")
  615.   _IF_SDLERR(unit==0,;,"unit==0")
  616.  
  617.   //x axis
  618.   size_t x_size=(!y && !z) ? unit*x : sizeof(void*)*x;
  619.   _IF_SDLERR(_kit_coreVectorRealloc(&Vector,0, 0,sizeof(kit_coreVector)+x_size),;,"!x axis")
  620.   Vector->p1d=(void*)Vector + sizeof(kit_coreVector); //actual array is ahead by this many bytes
  621.   Vector->_dims=U64_MSb; //sign bit will be unset on success
  622.   ++Vector->_dims; //vector now contains an x axis
  623.  
  624.   //y axis
  625.   _IF_SDLERR(y==0 && z>0,;,"y==0&& z>0")
  626.   if(y>0){ ++Vector->_dims; //vector now contains a y axis
  627.     size_t y_size=(!z) ? unit*y : sizeof(void*)*y;
  628.     _IF_SDLERR(_kit_coreVectorResizeY(Vector, 0,x, 0,y_size, y),;,"!y axis")
  629.   }
  630.  
  631.   //z axis
  632.   if(z>0){ ++Vector->_dims; //vector now contains a z axis
  633.     _IF_SDLERR(_kit_coreVectorResizeZ(Vector, 0,x, 0,y, 0,unit*z, z),;,"!z axis")
  634.   }
  635.  
  636. /*
  637.   //tbd: test for 1d and 2d VectorAdd ops
  638.   //allocate lens
  639.    //(if vector is 1D, lens.r1D is already set to 0 with the previous memset)
  640.   int dims=Vector->_dims&3;
  641.   if(dims==2){ //2D
  642.     _IF_SDLERR(_kit_coreVectorRealloc(&Vector->lens.r2D,0, 0,x*sizeof(lens_t)),;,"!r2D")
  643.   } else if(dims==3){ //3D
  644.     _IF_SDLERR(_kit_coreVectorRealloc(&Vector->lens.r3D,0, 0,x*sizeof(lens_t*)),;,"!r3D")
  645.     lens_t** r3D=Vector->lens.r3D;
  646.     for(lens_t xi=0; xi<x; ++xi){
  647.       _IF_SDLERR(_kit_coreVectorRealloc(&r3D[xi],0, 0,y*sizeof(lens_t)),;,"!r2D")
  648.     }
  649.   }*/
  650.  
  651.   Vector->_dims&=U64_MSbC; //unset sign bit to indicate success
  652.   Vector->type.n=type_n&U64_MSBC; //set last byte to 0 for interpretation as string
  653.   Vector->x=x, Vector->y=y, Vector->z=z;
  654.   Vector->unit=unit;
  655.   _error_:
  656.   if((Vector!=NULL) && (Vector->_dims<0)) kit_coreVectorDestroy(&Vector);
  657.   return Vector;
  658. }
  659.  
  660.  
  661.  
  662.  
  663. #if defined(_KIT_CORE_DEBUG) || defined(_KIT_ALL_DEBUG)
  664. extern int printf(const char*,...);
  665. void _kit_coreVectorPrintInt(kit_coreVector* Vector,const char* prefix){
  666.   if(Vector==NULL && prefix!=NULL){ kit_coreLog("%s: Vector cannot be NULL", prefix); return; }
  667.   lens_t x_s=Vector->x, y_s=Vector->y, z_s=Vector->z; void* ptr=Vector->ptr;
  668.   Sint32 *i1d=ptr, **i2d=ptr, ***i3d=ptr, dims=Vector->_dims&3;
  669.   lens_t xi=0, yi=0, zi=0;
  670.   //test fill
  671.   if(dims!=3) goto _n3d_fs;
  672.   for(zi=0; zi<z_s; ++zi){ _n3d_fs: if(dims==1) goto _n2d_fs;
  673.     for(yi=0; yi<y_s; ++yi){ _n2d_fs:
  674.       for(xi=0; xi<x_s; ++xi){
  675.         switch(dims){
  676.         case 1: i1d[xi]        =xi;                   break;
  677.         case 2: i2d[xi][yi]    =(xi<<4)|yi;           break;
  678.         case 3: i3d[xi][yi][zi]=((xi+zi)<<4)|(yi+zi); }
  679.       } if(dims==1) goto _n2d_fe;
  680.     } _n2d_fe: if(dims!=3) goto _n3d_fe;
  681.   } _n3d_fe:
  682.   //print
  683.   if(prefix==NULL) return; //now printing is optional!
  684.   printf("%s: %u,%u,%u (%uD)\n", prefix, x_s,y_s,z_s, dims);
  685.   if(dims!=3) goto _n3d_ps;
  686.   for(zi=0; zi<z_s; ++zi){ _n3d_ps: if(dims==1) goto _n2d_ps;
  687.     for(yi=0; yi<y_s; ++yi){ _n2d_ps: if(!zi) printf("0");
  688.       for(xi=0; xi<x_s; ++xi){
  689.         switch(dims){
  690.         case 1: printf("%X,",i1d[xi]);         break;
  691.         case 2: printf("%X,",i2d[xi][yi]);     break;
  692.         case 3: printf("%X,",i3d[xi][yi][zi]); }
  693.       } if(dims==1) goto _n2d_pe;
  694.       printf("\n");
  695.     } _n2d_pe: if(dims!=3) goto _n3d_pe;
  696.     printf("\n");
  697.   } _n3d_pe: printf("\n");
  698. }
  699. void _kit_coreVectorPrintLens(kit_coreVector* Vector, const char* prefix){
  700.   if(Vector==NULL && prefix!=NULL){ kit_coreLog("%s: Vector cannot be NULL", prefix); return; }
  701.   lens_t x_s=Vector->x, y_s=Vector->y; void* r_D=Vector->lens.r3D;
  702.   if(r_D == NULL) return;
  703.   int dims=Vector->_dims&3;
  704.   if(dims<=1) return;
  705.   if(dims==2){
  706.     //test fill
  707.     lens_t* r2D=r_D;
  708.     for(lens_t xi=0; xi<y_s; ++xi) r2D[xi]=xi;
  709.     //print
  710.     if(prefix==NULL) return;
  711.     printf("%s: %u (%uD)\n", prefix, x_s, dims);
  712.     for(lens_t xi=0; xi<y_s; ++xi) printf("%02X,",r2D[xi]);
  713.   } else {
  714.     //test fill
  715.     lens_t** r3D=r_D;
  716.     for(lens_t xi=0; xi<x_s; ++xi){
  717.       for(lens_t yi=0; yi<y_s; ++yi){
  718.         r3D[xi][yi]=(xi<<4)|yi;
  719.       }
  720.     }
  721.     //print
  722.     if(prefix==NULL) return;
  723.     printf("%s: %u,%u (%uD)\n", prefix, x_s,y_s, dims);
  724.     for(lens_t xi=0; xi<x_s; ++xi){
  725.       for(lens_t yi=0; yi<y_s; ++yi){
  726.         printf("%02X,",r3D[xi][yi]);
  727.       } printf("\n");
  728.     }
  729.   }
  730.   printf("\n");
  731. }
  732. int kit_coreVectorTest(){
  733.   int returnStatus=0;
  734.   kit_coreVector* Vector=NULL; SDL_SetError("(no error)");
  735.   //(x,y,z) test creation and destruction
  736.    //0,0,0 (should error)
  737.   _vectorLogT("testing creation of vector of size 0,0,0 (should error):");
  738.   Vector=kit_coreVectorCreate(0,0,0, sizeof(Uint32),STR_U64("U32"));
  739.   _IF_GOTO_ERROR(Vector!=NULL,;)
  740.   _vectorLogT("  error = \"%s\"",SDL_GetError());
  741.    //0,0,2 (should error)
  742.   _vectorLogT("testing creation of vector of size 0,0,2 (should error):");
  743.   Vector=kit_coreVectorCreate(0,0,2, sizeof(Uint32),STR_U64("U32"));
  744.   _IF_GOTO_ERROR(Vector!=NULL,;)
  745.   _vectorLogT("  error = \"%s\"",SDL_GetError());
  746.    //0,2,0 (should error)
  747.   _vectorLogT("testing creation of vector of size 0,2,0 (should error):");
  748.   Vector=kit_coreVectorCreate(0,2,0, sizeof(Uint32),STR_U64("U32"));
  749.   _IF_GOTO_ERROR(Vector!=NULL,;)
  750.   _vectorLogT("  error = \"%s\"",SDL_GetError());
  751.    //0,2,2 (should error)
  752.   _vectorLogT("testing creation of vector of size 0,2,2 (should error):");
  753.   Vector=kit_coreVectorCreate(0,2,2, sizeof(Uint32),STR_U64("U32"));
  754.   _IF_GOTO_ERROR(Vector!=NULL,;)
  755.   _vectorLogT("  error = \"%s\"",SDL_GetError());
  756.    //2,0,0
  757.   _vectorLogT("testing creation of vector of size 2,0,0:");
  758.   Vector=kit_coreVectorCreate(2,0,0, sizeof(Uint32),STR_U64("U32"));
  759.   _IF_GOTO_ERROR(Vector==NULL,;)
  760.   _kit_coreVectorPrintInt(Vector,NULL); //test fill
  761.   _vectorLogT("  attempting to destroy vector");
  762.   _IF_GOTO_ERROR(kit_coreVectorDestroy(&Vector),;)
  763.    //2,0,2 (should error)
  764.   _vectorLogT("testing creation of vector of size 2,0,2 (should error):");
  765.   Vector=kit_coreVectorCreate(2,0,2, sizeof(Uint32),STR_U64("U32"));
  766.   _IF_GOTO_ERROR(Vector!=NULL,;)
  767.   _vectorLogT("  error = \"%s\"",SDL_GetError());
  768.    //2,2,0
  769.   _vectorLogT("testing creation of vector of size 2,2,0:");
  770.   Vector=kit_coreVectorCreate(2,2,0, sizeof(Uint32),STR_U64("U32"));
  771.   _IF_GOTO_ERROR(Vector==NULL,;)
  772.   _kit_coreVectorPrintInt(Vector,NULL); //test fill
  773.   _vectorLogT("  attempting to destroy vector");
  774.   _IF_GOTO_ERROR(kit_coreVectorDestroy(&Vector),;)
  775.    //2,2,2
  776.   _vectorLogT("testing creation of vector of size 2,2,2:");
  777.   Vector=kit_coreVectorCreate(2,2,2, sizeof(Uint32),STR_U64("U32"));
  778.   _IF_GOTO_ERROR(Vector==NULL,;)
  779.   _kit_coreVectorPrintInt(Vector,NULL); //test fill
  780.   _vectorLogT("  attempting to destroy vector");
  781.   _IF_GOTO_ERROR(kit_coreVectorDestroy(&Vector),;)
  782.  
  783.   //test array 1D->3D accesses
  784.   for(Uint32 dims=1; dims<=3; ++dims){
  785.     _vectorLogT("testing %uD array accesses...",dims);
  786.     //test multiple times each to (hopefully) catch any undefined behavior
  787.     Vector=kit_coreVectorCreate(4,4*(dims>=2),4*(dims==3), sizeof(Uint32),STR_U64("U32"));
  788.     _IF_GOTO_ERROR(Vector==NULL,;)
  789.      //(will in theory throw something like an access violation error on failure)
  790.     for(Uint32 i=0; i<20; ++i) _kit_coreVectorPrintInt(Vector,NULL);
  791.     _vectorLogT("  attempting to destroy vector");
  792.     _IF_GOTO_ERROR(kit_coreVectorDestroy(&Vector),;)
  793.   }
  794.  
  795.   //test 1D -> 3D VectorAdd resizing
  796.   for(Uint32 dims=1; dims<=3; ++dims){
  797.     for(Sint32 x0=-1; x0<=1; ++x0){
  798.     for(Sint32 y0=-1; y0<=1; ++y0){
  799.     for(Sint32 z0=-1; z0<=1; ++z0){
  800.       for(Sint32 x1=-1; x1<=1; ++x1){
  801.       for(Sint32 y1=-1; y1<=1; ++y1){
  802.       for(Sint32 z1=-1; z1<=1; ++z1){
  803.         _vectorLogT("(%u,%u,%u) (%uD):", 5,5*(dims>=2),5*(dims==3), dims);
  804.         kit_coreVector* v=kit_coreVectorCreate(5,5*(dims>=2),5*(dims==3),sizeof(Uint32),0);
  805.           //first x0
  806.           if(kit_coreVectorAdd(&v, x0,y0,z0)){
  807.             _vectorLogT("0: (%2i,%2i,%2i) -> (%2i,%2i,%2i): \"%s\"",
  808.                        x0,y0,z0, x1,y1,z1, SDL_GetError());
  809.           } else {
  810.             _vectorLogT("0: (%2i,%2i,%2i) -> (%2i,%2i,%2i)", x0,y0,z0, x1,y1,z1);
  811.             _kit_coreVectorPrintInt(v,NULL);
  812.             _kit_coreVectorPrintLens(v,NULL);
  813.           }
  814.           //second x0
  815.           if(kit_coreVectorAdd(&v, x0,y0,z0)){
  816.             _vectorLogT("1:  (%2i,%2i,%2i) -> (%2i,%2i,%2i): \"%s\"",
  817.                        x0,y0,z0, x1,y1,z1, SDL_GetError());
  818.           } else {
  819.             _vectorLogT("1:  (%2i,%2i,%2i) -> (%2i,%2i,%2i)", x0,y0,z0, x1,y1,z1);
  820.             _kit_coreVectorPrintInt(v,NULL);
  821.             _kit_coreVectorPrintLens(v,NULL);
  822.           }
  823.           //first x1
  824.           if(kit_coreVectorAdd(&v, x1,y1,z1)){
  825.             _vectorLogT("0: (%2i,%2i,%2i) -> (%2i,%2i,%2i): \"%s\"",
  826.                        x0,y0,z0, x1,y1,z1, SDL_GetError());
  827.           } else {
  828.             _vectorLogT("0: (%2i,%2i,%2i) -> (%2i,%2i,%2i)", x0,y0,z0, x1,y1,z1);
  829.             _kit_coreVectorPrintInt(v,NULL);
  830.             _kit_coreVectorPrintLens(v,NULL);
  831.           }
  832.           //second x1
  833.           if(kit_coreVectorAdd(&v, x1,y1,z1)){
  834.             _vectorLogT("1:   (%2i,%2i,%2i) -> (%2i,%2i,%2i): \"%s\"",
  835.                        x0,y0,z0, x1,y1,z1, SDL_GetError());
  836.           } else {
  837.             _vectorLogT("1:   (%2i,%2i,%2i) -> (%2i,%2i,%2i)", x0,y0,z0, x1,y1,z1);
  838.             _kit_coreVectorPrintInt(v,NULL);
  839.             _kit_coreVectorPrintLens(v,NULL);
  840.           }
  841.         kit_coreVectorDestroy(&v);
  842.       }}}
  843.     }}}
  844.   }
  845.   //test 1D -> 3D VectorSet resizing (maybe)
  846.  
  847.   //test copy (TBD)
  848.  
  849.   //test append (TBD)
  850.    //1D
  851.    //2D
  852.    //3D
  853.  
  854.   ++returnStatus;
  855.   _error_:
  856.   --returnStatus;
  857.   if(returnStatus){
  858.     _vectorLogT("failed; error = \"%s\"",SDL_GetError());
  859.   } else if(Vector != NULL){
  860.     _vectorLogT("attempting to destroy vector");
  861.     if(kit_coreVectorDestroy(&Vector))
  862.       _vectorLogT("failed; error = \"%s\"",SDL_GetError());
  863.   }
  864.   return returnStatus;
  865. }
  866. #else
  867. void _kit_coreVectorPrintInt(kit_coreVector* Vector,const char* prefix){
  868.   kit_coreLog("%s: \"_KIT_CORE_DEBUG\" was not defined during compilation of kit_core",prefix);
  869. }
  870. int kit_coreVectorTest(){
  871.   kit_coreLog("\"_KIT_CORE_DEBUG\" was not defined during compilation of kit_core");
  872.   SDL_SetError("!defined(_KIT_CORE_DEBUG)");
  873.   return -1;
  874. }
  875. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement