This repository has been archived by the owner on Dec 25, 2018. It is now read-only.
/
LinkedNodeList.js
214 lines (214 loc) · 8.95 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
214
/*!
* @author electricessence / https://github.com/electricessence/
* Licensing: MIT https://github.com/electricessence/TypeScript.NET/blob/master/LICENSE.md
*/
System.register(["../Text/Utility", "../Exceptions/InvalidOperationException", "../Exceptions/ArgumentException", "../Exceptions/ArgumentNullException"], function(exports_1, context_1) {
'use strict';
var __moduleName = context_1 && context_1.id;
var TextUtility, InvalidOperationException_1, ArgumentException_1, ArgumentNullException_1;
var LinkedNodeList;
function assertValidDetached(node, propName) {
if (propName === void 0) { propName = 'node'; }
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.");
}
return {
setters:[
function (TextUtility_1) {
TextUtility = TextUtility_1;
},
function (InvalidOperationException_1_1) {
InvalidOperationException_1 = InvalidOperationException_1_1;
},
function (ArgumentException_1_1) {
ArgumentException_1 = ArgumentException_1_1;
},
function (ArgumentNullException_1_1) {
ArgumentNullException_1 = ArgumentNullException_1_1;
}],
execute: function() {
LinkedNodeList = (function () {
function LinkedNodeList() {
this._first = null;
this._last = null;
}
Object.defineProperty(LinkedNodeList.prototype, "first", {
get: function () {
return this._first;
},
enumerable: true,
configurable: true
});
Object.defineProperty(LinkedNodeList.prototype, "last", {
get: function () {
return this._last;
},
enumerable: true,
configurable: true
});
Object.defineProperty(LinkedNodeList.prototype, "count", {
get: function () {
var next = this._first, i = 0;
while (next) {
i++;
next = next.next;
}
return i;
},
enumerable: true,
configurable: true
});
LinkedNodeList.prototype.forEach = function (action) {
var current = null, next = this.first, index = 0;
do {
current = next;
next = current && current.next;
} while (current
&& action(current, index++) !== false);
};
LinkedNodeList.prototype.clear = function () {
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;
};
LinkedNodeList.prototype.dispose = function () {
this.clear();
};
LinkedNodeList.prototype.contains = function (node) {
return this.indexOf(node) != -1;
};
LinkedNodeList.prototype.getNodeAt = function (index) {
if (index < 0)
return null;
var next = this._first, i = 0;
while (next && index < i++) {
next = next.next;
}
return next;
};
LinkedNodeList.prototype.indexOf = function (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;
};
LinkedNodeList.prototype.removeFirst = function () {
return this.removeNode(this._first);
};
LinkedNodeList.prototype.removeLast = function () {
return this.removeNode(this._last);
};
LinkedNodeList.prototype.removeNode = function (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;
};
LinkedNodeList.prototype.addNode = function (node) {
this.addNodeAfter(node);
};
LinkedNodeList.prototype.addNodeBefore = function (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;
}
};
LinkedNodeList.prototype.addNodeAfter = function (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;
}
};
LinkedNodeList.prototype.replace = function (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;
};
return LinkedNodeList;
}());
exports_1("default", LinkedNodeList);
}
}
});
//# sourceMappingURL=LinkedNodeList.js.map