HEX
Server: LiteSpeed
System: Linux houston.panomity.com 6.8.0-100-generic #100-Ubuntu SMP PREEMPT_DYNAMIC Tue Jan 13 16:40:06 UTC 2026 x86_64
User: nudepix (1011)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //opt/LC/node_modules/obliterator/permutations.js
/**
 * Obliterator Permutations Function
 * ==================================
 *
 * Iterator returning permutations of the given array.
 */
var Iterator = require('./iterator.js');

/**
 * Helper mapping indices to items.
 */
function indicesToItems(target, items, indices, r) {
  for (var i = 0; i < r; i++) target[i] = items[indices[i]];
}

/**
 * Permutations.
 *
 * @param  {array}    array - Target array.
 * @param  {number}   r     - Size of the subsequences.
 * @return {Iterator}
 */
module.exports = function permutations(array, r) {
  if (!Array.isArray(array))
    throw new Error(
      'obliterator/permutations: first argument should be an array.'
    );

  var n = array.length;

  if (arguments.length < 2) r = n;

  if (typeof r !== 'number')
    throw new Error(
      'obliterator/permutations: second argument should be omitted or a number.'
    );

  if (r > n)
    throw new Error(
      'obliterator/permutations: the size of the subsequences should not exceed the length of the array.'
    );

  var indices = new Uint32Array(n),
    subsequence = new Array(r),
    cycles = new Uint32Array(r),
    first = true,
    i;

  for (i = 0; i < n; i++) {
    indices[i] = i;

    if (i < r) cycles[i] = n - i;
  }

  i = r;

  return new Iterator(function next() {
    if (first) {
      first = false;
      indicesToItems(subsequence, array, indices, r);
      return {value: subsequence, done: false};
    }

    var tmp, j;

    i--;

    if (i < 0) return {done: true};

    cycles[i]--;

    if (cycles[i] === 0) {
      tmp = indices[i];

      for (j = i; j < n - 1; j++) indices[j] = indices[j + 1];

      indices[n - 1] = tmp;

      cycles[i] = n - i;
      return next();
    } else {
      j = cycles[i];
      tmp = indices[i];

      indices[i] = indices[n - j];
      indices[n - j] = tmp;

      i = r;

      indicesToItems(subsequence, array, indices, r);
      return {value: subsequence, done: false};
    }
  });
};