| 1 | 1 | module.exports = function () { |
| 2 | 22 | Object.defineProperty(this, '_head', { |
| 3 | | value: undefined, |
| 4 | | writable: true, |
| 5 | | enumerable: false, |
| 6 | | configurable: false |
| 7 | | }) |
| 8 | 22 | Object.defineProperty(this, '_tail', { |
| 9 | | value: undefined, |
| 10 | | writable: true, |
| 11 | | enumerable: false, |
| 12 | | configurable: false |
| 13 | | }) |
| 14 | 22 | Object.defineProperty(this, '_next', { |
| 15 | | value: undefined, |
| 16 | | writable: true, |
| 17 | | enumerable: false, |
| 18 | | configurable: false |
| 19 | | }) |
| 20 | 22 | Object.defineProperty(this, '_length', { |
| 21 | | value: 0, |
| 22 | | writable: true, |
| 23 | | enumerable: false, |
| 24 | | configurable: false |
| 25 | | }) |
| 26 | | } |
| 27 | | |
| 28 | 1 | module.exports.prototype.__defineGetter__('head', function () { |
| 29 | 21 | return this._head && this._head.data |
| 30 | | }) |
| 31 | | |
| 32 | 1 | module.exports.prototype.__defineGetter__('tail', function () { |
| 33 | 21 | return this._tail && this._tail.data |
| 34 | | }) |
| 35 | | |
| 36 | 1 | module.exports.prototype.__defineGetter__('current', function () { |
| 37 | 27 | return this._current && this._current.data |
| 38 | | }) |
| 39 | | |
| 40 | 1 | module.exports.prototype.__defineGetter__('length', function () { |
| 41 | 24 | return this._length |
| 42 | | }) |
| 43 | | |
| 44 | 1 | module.exports.prototype.push = function (data) { |
| 45 | 131 | this._tail = new Item(data, this._tail) |
| 46 | 131 | if (this._length === 0) { |
| 47 | 16 | this._head = this._tail |
| 48 | 16 | this._current = this._head |
| 49 | 16 | this._next = this._head |
| 50 | | } |
| 51 | 131 | this._length++ |
| 52 | | } |
| 53 | | |
| 54 | 1 | module.exports.prototype.pop = function () { |
| 55 | 15 | var tail = this._tail |
| 56 | 15 | if (this._length === 0) { |
| 57 | 3 | return |
| 58 | | } |
| 59 | 12 | this._length-- |
| 60 | 12 | if (this._length === 0) { |
| 61 | 1 | this._head = this._tail = this._current = this._next = undefined |
| 62 | 1 | return tail.data |
| 63 | | } |
| 64 | 11 | this._tail = tail.prev |
| 65 | 11 | this._tail.next = undefined |
| 66 | 11 | if (this._current === tail) { |
| 67 | 2 | this._current = this._tail |
| 68 | 2 | this._next = undefined |
| 69 | | } |
| 70 | 11 | return tail.data |
| 71 | | } |
| 72 | | |
| 73 | 1 | module.exports.prototype.shift = function () { |
| 74 | 14 | var head = this._head |
| 75 | 14 | if (this._length === 0) { |
| 76 | 3 | return |
| 77 | | } |
| 78 | 11 | this._length-- |
| 79 | 11 | if (this._length === 0) { |
| 80 | 1 | this._head = this._tail = this._current = this._next = undefined |
| 81 | 1 | return head.data |
| 82 | | } |
| 83 | 10 | this._head = this._head.next |
| 84 | 10 | if (this._current === head) { |
| 85 | 5 | this._current = this._head |
| 86 | 5 | this._next = this._current.next |
| 87 | | } |
| 88 | 10 | return head.data |
| 89 | | } |
| 90 | | |
| 91 | 1 | module.exports.prototype.unshift = function (data) { |
| 92 | 2 | this._head = new Item(data, undefined, this._head) |
| 93 | 2 | if (this._length === 0) { |
| 94 | 1 | this._tail = this._head |
| 95 | 1 | this._next = this._head |
| 96 | | } |
| 97 | 2 | this._length++ |
| 98 | | } |
| 99 | | |
| 100 | 1 | module.exports.prototype.unshiftCurrent = function () { |
| 101 | 24 | var current = this._current |
| 102 | 24 | if (current === this._head || this._length < 2) { |
| 103 | 2 | return current && current.data |
| 104 | | } |
| 105 | | // remove |
| 106 | 22 | if (current === this._tail) { |
| 107 | 21 | this._tail = current.prev |
| 108 | 21 | this._tail.next = undefined |
| 109 | 21 | this._current = this._tail |
| 110 | | } else { |
| 111 | 1 | current.next.prev = current.prev |
| 112 | 1 | current.prev.next = current.next |
| 113 | 1 | this._current = current.prev |
| 114 | | } |
| 115 | 22 | this._next = this._current.next |
| 116 | | // unshift |
| 117 | 22 | current.next = this._head |
| 118 | 22 | current.prev = undefined |
| 119 | 22 | this._head.prev = current |
| 120 | 22 | this._head = current |
| 121 | 22 | return current.data |
| 122 | | } |
| 123 | | |
| 124 | 1 | module.exports.prototype.removeCurrent = function (data) { |
| 125 | 34 | var current = this._current |
| 126 | 34 | if (this._length === 0) { |
| 127 | 4 | return |
| 128 | | } |
| 129 | 30 | this._length-- |
| 130 | 30 | if (this._length === 0) { |
| 131 | 3 | this._head = this._tail = this._current = this._next = undefined |
| 132 | 3 | return current.data |
| 133 | | } |
| 134 | 27 | if (current === this._tail) { |
| 135 | 9 | this._tail = current.prev |
| 136 | 9 | this._tail.next = undefined |
| 137 | 9 | this._current = this._tail |
| 138 | 18 | } else if (current === this._head) { |
| 139 | 12 | this._head = current.next |
| 140 | 12 | this._head.prev = undefined |
| 141 | 12 | this._current = this._head |
| 142 | | } else { |
| 143 | 6 | current.next.prev = current.prev |
| 144 | 6 | current.prev.next = current.next |
| 145 | 6 | this._current = current.prev |
| 146 | | } |
| 147 | 27 | this._next = this._current.next |
| 148 | 27 | return current.data |
| 149 | | } |
| 150 | | |
| 151 | 1 | module.exports.prototype.next = function () { |
| 152 | 84 | var next = this._next |
| 153 | 84 | if (next !== undefined) { |
| 154 | 69 | this._next = next.next |
| 155 | 69 | this._current = next |
| 156 | 69 | return next.data |
| 157 | | } |
| 158 | | } |
| 159 | | |
| 160 | 1 | module.exports.prototype.resetCursor = function () { |
| 161 | 1 | this._current = this._next = this._head |
| 162 | 1 | return this |
| 163 | | } |
| 164 | | |
| 165 | 1 | function Item (data, prev, next) { |
| 166 | 133 | this.next = next |
| 167 | 134 | if (next) next.prev = this |
| 168 | 133 | this.prev = prev |
| 169 | 248 | if (prev) prev.next = this |
| 170 | 133 | this.data = data |
| 171 | | } |