This repository has been archived by the owner on Dec 25, 2018. It is now read-only.
/
LinkedNodeList.js
213 lines (204 loc) · 7.27 KB
/
LinkedNodeList.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*!
* @author electricessence / https://github.com/electricessence/
* Licensing: MIT https://github.com/electricessence/TypeScript.NET/blob/master/LICENSE.md
*/
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var TextUtility = require("../Text/Utility");
var InvalidOperationException_1 = require("../Exceptions/InvalidOperationException");
var ArgumentException_1 = require("../Exceptions/ArgumentException");
var ArgumentNullException_1 = require("../Exceptions/ArgumentNullException");
var LinkedNodeList = function () {
function LinkedNodeList() {
_classCallCheck(this, LinkedNodeList);
this._first = null;
this._last = null;
}
_createClass(LinkedNodeList, [{
key: "forEach",
value: function forEach(action) {
var current = null,
next = this.first,
index = 0;
do {
current = next;
next = current && current.next;
} while (current && action(current, index++) !== false);
}
}, {
key: "clear",
value: function clear() {
var _ = this,
n,
cF = 0,
cL = 0;
n = _._first;
_._first = null;
while (n) {
cF++;
var current = n;
n = n.next;
current.next = null;
}
n = _._last;
_._last = null;
while (n) {
cL++;
var _current = n;
n = n.previous;
_current.previous = null;
}
if (cF !== cL) console.warn('LinkedNodeList: Forward versus reverse count does not match when clearing. Forward: ' + cF + ", Reverse: " + cL);
return cF;
}
}, {
key: "dispose",
value: function dispose() {
this.clear();
}
}, {
key: "contains",
value: function contains(node) {
return this.indexOf(node) != -1;
}
}, {
key: "getNodeAt",
value: function getNodeAt(index) {
if (index < 0) return null;
var next = this._first,
i = 0;
while (next && index < i++) {
next = next.next;
}
return next;
}
}, {
key: "indexOf",
value: function indexOf(node) {
if (node && (node.previous || node.next)) {
var index = 0;
var c,
n = this._first;
do {
c = n;
if (c === node) return index;
index++;
} while (n = c && c.next);
}
return -1;
}
}, {
key: "removeFirst",
value: function removeFirst() {
return this.removeNode(this._first);
}
}, {
key: "removeLast",
value: function removeLast() {
return this.removeNode(this._last);
}
}, {
key: "removeNode",
value: function removeNode(node) {
if (node == null) throw new ArgumentNullException_1.default('node');
var _ = this;
var prev = node.previous,
next = node.next,
a = false,
b = false;
if (prev) prev.next = next;else if (_._first == node) _._first = next;else a = true;
if (next) next.previous = prev;else if (_._last == node) _._last = prev;else b = true;
if (a !== b) {
throw new ArgumentException_1.default('node', TextUtility.format("Provided node is has no {0} reference but is not the {1} node!", a ? "previous" : "next", a ? "first" : "last"));
}
return !a && !b;
}
}, {
key: "addNode",
value: function addNode(node) {
this.addNodeAfter(node);
}
}, {
key: "addNodeBefore",
value: function addNodeBefore(node, before) {
assertValidDetached(node);
var _ = this;
if (!before) {
before = _._first;
}
if (before) {
var prev = before.previous;
node.previous = prev;
node.next = before;
before.previous = node;
if (prev) prev.next = node;
if (before == _._first) _._last = node;
} else {
_._first = _._last = node;
}
}
}, {
key: "addNodeAfter",
value: function addNodeAfter(node, after) {
assertValidDetached(node);
var _ = this;
if (!after) {
after = _._last;
}
if (after) {
var next = after.next;
node.next = next;
node.previous = after;
after.next = node;
if (next) next.previous = node;
if (after == _._last) _._last = node;
} else {
_._first = _._last = node;
}
}
}, {
key: "replace",
value: function replace(node, replacement) {
if (node == null) throw new ArgumentNullException_1.default('node');
assertValidDetached(replacement, 'replacement');
var _ = this;
replacement.previous = node.previous;
replacement.next = node.next;
if (node.previous) node.previous.next = replacement;
if (node.next) node.next.previous = replacement;
if (node == _._first) _._first = replacement;
if (node == _._last) _._last = replacement;
}
}, {
key: "first",
get: function get() {
return this._first;
}
}, {
key: "last",
get: function get() {
return this._last;
}
}, {
key: "count",
get: function get() {
var next = this._first,
i = 0;
while (next) {
i++;
next = next.next;
}
return i;
}
}]);
return LinkedNodeList;
}();
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = LinkedNodeList;
function assertValidDetached(node) {
var propName = arguments.length <= 1 || arguments[1] === undefined ? 'node' : arguments[1];
if (node == null) throw new ArgumentNullException_1.default(propName);
if (node.next || node.previous) throw new InvalidOperationException_1.default("Cannot add a node to a LinkedNodeList that is already linked.");
}
//# sourceMappingURL=LinkedNodeList.js.map