src/utils/queue.js   A
last analyzed

Complexity

Total Complexity 16
Complexity/F 2

Size

Lines of Code 120
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 40
mnd 8
bc 8
fnc 8
dl 0
loc 120
rs 10
bpm 1
cpm 2
noi 0
c 0
b 0
f 0
1
/**
2
 * Class: jaxon.utils.queue
3
 *
4
 * global: jaxon
5
 */
6
7
(function(self) {
8
    /**
9
     * Construct and return a new queue object.
10
     *
11
     * @param {integer} size The number of entries the queue will be able to hold.
12
     *
13
     * @returns {object}
14
     */
15
    self.create = size => ({
16
        start: 0,
17
        count: 0,
18
        size: size,
19
        end: 0,
20
        elements: [],
21
        paused: false,
22
    });
23
24
    /**
25
     * Check id a queue is empty.
26
     *
27
     * @param {object} oQueue The queue to check.
28
     *
29
     * @returns {boolean}
30
     */
31
    self.empty = oQueue => oQueue.count <= 0;
32
33
    /**
34
     * Check id a queue is empty.
35
     *
36
     * @param {object} oQueue The queue to check.
37
     *
38
     * @returns {boolean}
39
     */
40
    self.full = oQueue => oQueue.count >= oQueue.size;
41
42
    /**
43
     * Push a new object into the tail of the buffer maintained by the specified queue object.
44
     *
45
     * @param {object} oQueue The queue in which you would like the object stored.
46
     * @param {object} obj    The object you would like stored in the queue.
47
     *
48
     * @returns {integer} The number of entries in the queue.
49
     */
50
    self.push = (oQueue, obj) => {
51
        // No push if the queue is full.
52
        if(self.full(oQueue)) {
53
            throw { code: 10003 };
54
        }
55
56
        oQueue.elements[oQueue.end] = obj;
57
        if(++oQueue.end >= oQueue.size) {
58
            oQueue.end = 0;
59
        }
60
        return ++oQueue.count;
61
    };
62
63
    /**
64
     * Push a new object into the head of the buffer maintained by the specified queue object.
65
     *
66
     * This effectively pushes an object to the front of the queue... it will be processed first.
67
     *
68
     * @param {object} oQueue The queue in which you would like the object stored.
69
     * @param {object} obj    The object you would like stored in the queue.
70
     *
71
     * @returns {integer} The number of entries in the queue.
72
     */
73
    self.pushFront = (oQueue, obj) => {
74
        // No push if the queue is full.
75
        if(self.full(oQueue)) {
76
            throw { code: 10003 };
77
        }
78
79
        // Simply push if the queue is empty
80
        if(self.empty(oQueue)) {
81
            return self.push(oQueue, obj);
82
        }
83
84
        // Put the object one position back.
85
        if(--oQueue.start < 0) {
86
            oQueue.start = oQueue.size - 1;
87
        }
88
        oQueue.elements[oQueue.start] = obj;
89
        return ++oQueue.count;
90
    };
91
92
    /**
93
     * Attempt to pop an object off the head of the queue.
94
     *
95
     * @param {object} oQueue The queue object you would like to modify.
96
     *
97
     * @returns {object|null}
98
     */
99
    self.pop = (oQueue) => {
100
        if(self.empty(oQueue)) {
101
            return null;
102
        }
103
104
        let obj = oQueue.elements[oQueue.start];
105
        delete oQueue.elements[oQueue.start];
106
        if(++oQueue.start >= oQueue.size) {
107
            oQueue.start = 0;
108
        }
109
        oQueue.count--;
110
        return obj;
111
    };
112
113
    /**
114
     * Attempt to pop an object off the head of the queue.
115
     *
116
     * @param {object} oQueue The queue object you would like to modify.
117
     *
118
     * @returns {object|null}
119
     */
120
    self.peek = (oQueue) => {
121
        if(self.empty(oQueue)) {
122
            return null;
123
        }
124
        return oQueue.elements[oQueue.start];
125
    };
126
})(jaxon.utils.queue);
127