source: trip-planner-front/node_modules/async/internal/DoublyLinkedList.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.3 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = DLL;
7// Simple doubly linked list (https://en.wikipedia.org/wiki/Doubly_linked_list) implementation
8// used for queues. This implementation assumes that the node provided by the user can be modified
9// to adjust the next and last properties. We implement only the minimal functionality
10// for queue support.
11function DLL() {
12 this.head = this.tail = null;
13 this.length = 0;
14}
15
16function setInitial(dll, node) {
17 dll.length = 1;
18 dll.head = dll.tail = node;
19}
20
21DLL.prototype.removeLink = function (node) {
22 if (node.prev) node.prev.next = node.next;else this.head = node.next;
23 if (node.next) node.next.prev = node.prev;else this.tail = node.prev;
24
25 node.prev = node.next = null;
26 this.length -= 1;
27 return node;
28};
29
30DLL.prototype.empty = function () {
31 while (this.head) this.shift();
32 return this;
33};
34
35DLL.prototype.insertAfter = function (node, newNode) {
36 newNode.prev = node;
37 newNode.next = node.next;
38 if (node.next) node.next.prev = newNode;else this.tail = newNode;
39 node.next = newNode;
40 this.length += 1;
41};
42
43DLL.prototype.insertBefore = function (node, newNode) {
44 newNode.prev = node.prev;
45 newNode.next = node;
46 if (node.prev) node.prev.next = newNode;else this.head = newNode;
47 node.prev = newNode;
48 this.length += 1;
49};
50
51DLL.prototype.unshift = function (node) {
52 if (this.head) this.insertBefore(this.head, node);else setInitial(this, node);
53};
54
55DLL.prototype.push = function (node) {
56 if (this.tail) this.insertAfter(this.tail, node);else setInitial(this, node);
57};
58
59DLL.prototype.shift = function () {
60 return this.head && this.removeLink(this.head);
61};
62
63DLL.prototype.pop = function () {
64 return this.tail && this.removeLink(this.tail);
65};
66
67DLL.prototype.toArray = function () {
68 var arr = Array(this.length);
69 var curr = this.head;
70 for (var idx = 0; idx < this.length; idx++) {
71 arr[idx] = curr.data;
72 curr = curr.next;
73 }
74 return arr;
75};
76
77DLL.prototype.remove = function (testFn) {
78 var curr = this.head;
79 while (!!curr) {
80 var next = curr.next;
81 if (testFn(curr)) {
82 this.removeLink(curr);
83 }
84 curr = next;
85 }
86 return this;
87};
88module.exports = exports["default"];
Note: See TracBrowser for help on using the repository browser.