async-promise
Version:
Asynchronous coordination primitives for JavaScript and TypeScript
152 lines (149 loc) • 5.04 kB
JavaScript
/*! *****************************************************************************
Copyright (C) Ron A. Buckton. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************** */
var LinkedListNode = (function () {
function LinkedListNode(value) {
this.value = value;
}
Object.defineProperty(LinkedListNode.prototype, "list", {
/** Gets the LinkedList for this node */
get: function () {
return this._list;
},
enumerable: true,
configurable: true
});
Object.defineProperty(LinkedListNode.prototype, "previous", {
/** Gets the previous node in the list */
get: function () {
if (this._previous && this !== this._list.first) {
return this._previous;
}
return undefined;
},
enumerable: true,
configurable: true
});
Object.defineProperty(LinkedListNode.prototype, "next", {
/** Gets the next node in the list */
get: function () {
if (this._next && this._next !== this._list.first) {
return this._next;
}
return undefined;
},
enumerable: true,
configurable: true
});
return LinkedListNode;
})();
exports.LinkedListNode = LinkedListNode;
var LinkedList = (function () {
function LinkedList() {
}
Object.defineProperty(LinkedList.prototype, "first", {
/** Gets the first node in the list */
get: function () {
return this._head;
},
enumerable: true,
configurable: true
});
Object.defineProperty(LinkedList.prototype, "last", {
/** Gets the last node in the list */
get: function () {
if (this._head) {
return this._head._previous;
}
return undefined;
},
enumerable: true,
configurable: true
});
Object.defineProperty(LinkedList.prototype, "size", {
/** Gets the size of the list */
get: function () {
return this._size;
},
enumerable: true,
configurable: true
});
LinkedList.prototype.insert = function (afterNode, newNode) {
if (newNode == undefined)
throw new TypeError();
if (afterNode && afterNode.list !== this)
throw new Error("Wrong list.");
if (newNode.list)
throw new Error("Node is already attached to a list.");
newNode._list = this;
if (this._head == undefined) {
newNode._next = newNode;
newNode._previous = newNode;
this._head = newNode;
}
else {
if (afterNode == undefined) {
afterNode = this.last;
this._head = newNode;
}
newNode._previous = afterNode;
newNode._next = afterNode._next;
afterNode._next._previous = newNode;
afterNode._next = newNode;
}
this._size++;
};
LinkedList.prototype.push = function (newNode) {
this.insert(this.last, newNode);
};
LinkedList.prototype.pop = function () {
var node = this.last;
if (this.delete(node)) {
return node;
}
};
LinkedList.prototype.shift = function () {
var node = this.first;
if (this.delete(node)) {
return node;
}
};
LinkedList.prototype.unshift = function (newNode) {
this.insert(undefined, newNode);
};
LinkedList.prototype.delete = function (node) {
if (node == undefined || node.list !== this) {
return false;
}
if (node.next == undefined) {
this._head = undefined;
}
else {
node._next._previous = node._previous;
node._previous._next = node._next;
if (this._head === node) {
this._head = node._next;
}
}
node._list = undefined;
node._next = undefined;
node._previous = undefined;
this._size--;
return true;
};
LinkedList.prototype.clear = function () {
while (this.size > 0) {
this.delete(this.last);
}
};
return LinkedList;
})();
exports.LinkedList = LinkedList;