All files / src/common queue.js

100% Statements 44/44
100% Branches 18/18
100% Functions 11/11
100% Lines 44/44
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176                                  18x               5x               13313x               20651x   5x       20646x 20646x                 1x   5x   1x       4x 4x 4x                   5x   4x 4x   1x 1x 1x 1x                   9x 9x   9x   5x   3x       2x         4x   1x       3x     6x   4x 4x 4x 4x                 1x   5x 5x 5x 5x 5x             2x                 2x                     2x   1x            
"use strict";
 
/**
 * @typedef {Array} Queue
 * @desc Queues are Arrays in this codebase.
 * 
 * Three types of queues are available:
 * <li> FIFO queues
 * <li> LIFO queues
 * <li> Priority queues
 */
 
/**
 * queue empty test
 */
function queueIsEmpty(queue)
{
  return !queue || !queue.length;
}
 
/**
 * array.pop
 */
function lifoQueuePop(queue)
{
  return queue.pop()
}
 
/**
 * array.shift
 */
function fifoQueuePop(queue)
{
  return queue.shift()
}
 
/**
 * array.push
 */
function queueInsert(element, queue)
{
  if (!queue)
  {
    return [element];
  }
  else
  {
    queue.push(element);
    return queue;
  }
}
 
/**
 * array.push + sift-up
 */
function priorityQueueInsert(isAbove)
{
  return function (element, queue)
  {
    if (!queue)
    {
      return [element];
    }
    else
    {
      queue.push(element);
      heapSiftUp(queue, queue.length - 1, isAbove);
      return queue;
    }
  }
}
 
/**
 * sift-up implementation
 */
function heapSiftUp(heap, index, isAbove)
{
  if (index !== 0)
  {
    const parentIndex = index >> 1;
    if (isAbove(heap[index], heap[parentIndex]))
    {
      const tmp = heap[parentIndex];
      heap[parentIndex] = heap[index];
      heap[index] = tmp;
      heapSiftUp(heap, parentIndex, isAbove);
    }
  }
}
 
/**
 * sift-down implementation
 */
function heapSiftDown(heap, index, isAbove)
{
  const leftIndex = index << 1;
  const rightIndex = leftIndex + 1;
  let maxIndex;
  if (rightIndex >= heap.length)
  {
    if (leftIndex >= heap.length)
    {
      return;
    }
    else
    {
      maxIndex = leftIndex;
    }
  }
  else
  {
    if (isAbove(heap[leftIndex], heap[rightIndex]))
    {
      maxIndex = leftIndex;
    }
    else
    {
      maxIndex = rightIndex;
    }
  }
  if (isAbove(heap[maxIndex], heap[index]))
  {
    const temp = heap[index];
    heap[index] = heap[maxIndex];
    heap[maxIndex] = temp;
    heapSiftDown(heap, maxIndex, isAbove)
  }
}
 
/**
 * array.shift + array.pop + array.unshift + shft-down
 */
function priorityQueuePop(isAbove)
{
  return function (queue)
  {
    let highest = queue[0];
    queue[0] = queue[queue.length - 1];
    queue.pop();
    heapSiftDown(queue, 0, isAbove);
    return highest;
  };
}
 
/**
 * plain array; uses array.pop for popping
 */
module.exports.lifoQueue = {
  queueIsEmpty: queueIsEmpty,
  queuePop: lifoQueuePop,
  queueInsert: queueInsert,
}
 
/**
 * plain array; uses array.shift for popping
 */
module.exports.fifoQueue = {
  queueIsEmpty: queueIsEmpty,
  queuePop: fifoQueuePop,
  queueInsert: queueInsert,
}
 
/**
 * binary max heap array
 * @param {function(element:object, aboveElement:object):boolean} isAbove order function for priority queue
 * @reutn {object} will return an object similar to lifo/fifoQueue
 */
module.exports.priorityQueue = function (isAbove)
{
  return {
    queueIsEmpty: queueIsEmpty,
    queuePop: priorityQueuePop(isAbove),
    queueInsert: priorityQueueInsert(isAbove),
  };
}