3
\$\begingroup\$

So currently I have this function:

function loadPath(chunkSize, dataSize, depth, index) {
  let path = []
  let i = 0
  while (i < depth) {
    path.unshift(index % chunkSize)
    index = Math.floor(index / chunkSize)
    i++
  }
  return path
}

I would now like to pass in path as a variable from an object pool of short path arrays. So for example:

function loadPath(path, chunkSize, dataSize, depth, index) {
  
}

let path = new Array(3)
loadPath(path, 5, 100, 3, 41)
console.log(path) // 1, 3, 1
loadPath(path, 5, 100, 3, 50)
console.log(path) // ...

The question is, how can I do that without using push/unshift or reverse operations on the array? Basically just treating the array as a static block of memory, while at the same time not adding any unnecessary operations.

function loadPath(path, chunkSize, dataSize, depth, index) {
  let i = 0
  let x = depth - 1
  while (i < depth) {
    path[x] = index % chunkSize
    index = Math.floor(index / chunkSize)
    i++
    x--
  }
}

My question is, can this be done with any fewer temp variables? Some magic along the lines of this?

function loadPath(path, chunkSize, dataSize, depth, index) {
  let i = 0
  while (i < depth) {
    // something tricky perhaps here?
    // since x and i have a reciprocal relationship...
    // somehow fill it in backwards...
    path[depth - i] = index % chunkSize
    index = Math.floor(index / chunkSize)
    i++
  }
}

Or perhaps there is another way entirely to write this algorithm to be in as few steps and operations and temp variables as possible.

Could also use this floor implementation integrated somehow if it helps :)

function floor(n) {
  return ~~n - (~~n > n)
}
\$\endgroup\$
13
  • \$\begingroup\$ Your title short be short and concise while explaining what your code does. \$\endgroup\$ Commented Sep 25, 2020 at 12:19
  • \$\begingroup\$ You should be able to calculate the number of loops you need to do, so you can create an empty array of that size and fill it backwards. If I'm not mistaken it should be Math.log(chunkSize) / Math.log(index) (rounded up). \$\endgroup\$
    – RoToRa
    Commented Sep 25, 2020 at 13:44
  • \$\begingroup\$ Would you consider using concat? \$\endgroup\$
    – iAmOren
    Commented Sep 25, 2020 at 14:08
  • \$\begingroup\$ How about: path[depth - i - 1] = index % chunkSize - no need for x. \$\endgroup\$
    – iAmOren
    Commented Sep 25, 2020 at 14:18
  • \$\begingroup\$ What is this mysterious index and what is/why are you not using dataSize? \$\endgroup\$
    – iAmOren
    Commented Sep 25, 2020 at 14:20

2 Answers 2

2
\$\begingroup\$

Let's try the other way:

function loadPath(path, chunkSize, dataSize, depth, index) {
  for(var i = 0; i < depth; i++) {
    path[depth - i - 1] = index % chunkSize;
    index = Math.floor(index / chunkSize);
  };
};

var path = Array(3);
console.log("loadPath(path, 5, 100, 3, 41)");
loadPath(path, 5, 100, 3, 41);
console.log(path);
console.log("loadPath(path, 5, 100, 3, 50)");
loadPath(path, 5, 100, 3, 50);
console.log(path);
.as-console-wrapper { max-height: 100% !important; top: 0; }

What do you want to do if index >= chunkSize^3?

\$\endgroup\$
9
  • \$\begingroup\$ Thank you for the accept! Up-vote, please?!? :) Still: What do you want to do if index >= chunkSize^3? \$\endgroup\$
    – iAmOren
    Commented Sep 25, 2020 at 21:50
  • \$\begingroup\$ I can handle that outside of the function no problem. \$\endgroup\$ Commented Sep 25, 2020 at 22:03
  • \$\begingroup\$ Great! Thanks for the up-vote. I still have no clue as to what you are doing... :) \$\endgroup\$
    – iAmOren
    Commented Sep 25, 2020 at 22:06
  • \$\begingroup\$ I am working on a vm and this is an aspect of the memory allocation part. \$\endgroup\$ Commented Sep 25, 2020 at 22:17
  • 1
    \$\begingroup\$ Without a declaration for i using var or let a global variable would be created, which won't lead to an error but typically frowned upon \$\endgroup\$ Commented Sep 26, 2020 at 11:03
0
\$\begingroup\$

Same Answer to your own same/similar Question...
Hint: accept and up-vote both... :)

function recBucket(number, size) {
  if(number<size) return [number];
  else {
    var whole=Math.floor(number/size);
    return [recBucket(whole, size), number-whole*size].flat();
  }
};

function loadPath(chunkSize, dataSize, depth, index) {
  return recBucket(index, chunkSize);
};

console.log(loadPath(5, 100, 3, 41));
console.log(loadPath(5, 100, 3, 50));
.as-console-wrapper { max-height: 100% !important; top: 0; }

\$\endgroup\$
2
  • \$\begingroup\$ .flat feels a feels a bit hacky, just hiding implementation details to make things appear shorter. \$\endgroup\$ Commented Sep 25, 2020 at 16:04
  • \$\begingroup\$ Also recursion is less-than ideal. \$\endgroup\$ Commented Sep 25, 2020 at 16:18

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.