Old 02-18-2024, 06:09 AM   #1
SaschArt
Human being with feelings
 
SaschArt's Avatar
 
Join Date: Aug 2013
Posts: 236
Default Multiple arrays in gmem[]

I'm trying to make a set of functions that will merge several arrays into one. We have shared a single gmem[] global array and many times more arrays are needed. Here's what I did:

Code:
function alloc(sz) global() (
  memset((this.top+=sz)-sz,0,sz);
);
alloc.top=0; 
arr0=alloc(22); // array index 0 - maximum allocated dimension, we never exceed it with setArrLen()
arr1=alloc(22); // array index 1
arr2=alloc(22); // array index 2

function getI(i_arr) local(i_a, j) (
  i_a=0; j=1;
  while(i_a<i_arr) ( // scan all arrays length to get the position of index
    j+=gmem[j]+1;
    i_a+=1;
  );
  j;
);
function getArr(i_arr, i) (
  gmem[getI(i_arr)+i+1];
);
function setArr(i_arr, i, val) (
  gmem[getI(i_arr)+i+1]=val;
);
function setArrLen(i_arr, val) local(i, j) (
  i=getI(i_arr);
  last=gmem[i];
  val!=last && last ? (
    val<last ? (
      j=i+last;
      while(j<=gmem[0]) ( // move the old data to new position
        gmem[j]=j+last-val<=gmem[0] ? gmem[j+last-val] : 0;
        j+=1;
      );
    ) : (
      j=gmem[0];
      while(j>=i+last) (
        gmem[j+val-last]=j<i+val ? 0:gmem[j];
        j-=1;
      );
    );  
  );
  gmem[i]=val; // store array length on the first position for each array
  gmem[0]+=val-last; // store all array length
);
gmem[0]=3; // alloc arrays number
setArrLen(0, 5);
setArrLen(1, 7);
setArrLen(2, 7);


arr0[1]+=1;
arr1[2]+=2;
arr2[0]+=2;
arr2[1]+=3;
arr2[6]+=4;
setArr(0, 1, arr0[1]);
setArr(1, 2, arr1[2]);
setArr(2, 0, arr2[0]);
setArr(2, 1, arr2[1]);
setArr(2, 6, arr2[6]);

res=getArr(0, 1); // good result
res=getArr(1, 2); // good result
res=getArr(2, 1); // good result
setArrLen(0, gmem[getI(0)]+1);
res=getArr(0, 1); // good result
res=getArr(1, 2); // good result
res=getArr(2, 1); // good result
setArrLen(0, gmem[getI(1)]+1);
res=getArr(1, 2); // wrong result
As you can see in the code, I want to be able to change the size of the arrays. It only works if I increment or decrement the size of first array by one. How to improve to can I set any size I want to any array (up to maximum allocated dimension)?
__________________
Audio plugins | BrainWaveProducer | EmRysRa

Last edited by SaschArt; 02-18-2024 at 06:48 AM.
SaschArt is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 10:43 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.