summaryrefslogtreecommitdiff
path: root/node_modules/dir-compare/src/fs/Queue.js
blob: 4d86dd5691e12447ff1f7c27d714c208d1445fbf (about) (plain)
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
/*

Queue.js

A function to represent a queue

Created by Kate Morley - http://code.iamkate.com/ - and released under the terms
of the CC0 1.0 Universal legal code:

http://creativecommons.org/publicdomain/zero/1.0/legalcode

*/

var MAX_UNUSED_ARRAY_SIZE = 10000

/* Creates a new queue. A queue is a first-in-first-out (FIFO) data structure -
 * items are added to the end of the queue and removed from the front.
 */
function Queue() {

  // initialise the queue and offset
  var queue = []
  var offset = 0

  // Returns the length of the queue.
  this.getLength = function () {
    return (queue.length - offset)
  }

  /* Enqueues the specified item. The parameter is:
   *
   * item - the item to enqueue
   */
  this.enqueue = function (item) {
    queue.push(item)
  }

  /* Dequeues an item and returns it. If the queue is empty, the value
   * 'undefined' is returned.
   */
  this.dequeue = function () {

    // if the queue is empty, return immediately
    if (queue.length === 0) {
      return undefined
    }

    // store the item at the front of the queue
    var item = queue[offset]

    // increment the offset and remove the free space if necessary
    if (++offset > MAX_UNUSED_ARRAY_SIZE) {
      queue = queue.slice(offset)
      offset = 0
    }

    // return the dequeued item
    return item

  }
}

module.exports = Queue