-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
275 lines (245 loc) · 6.87 KB
/
index.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
* Creates a rope data structure
*
* @param {String} str - String to populate the rope.
* @api public
*/
function Rope(str) {
// allow usage without `new`
if (!(this instanceof Rope)) return new Rope(str);
this._value = str;
this.length = str.length;
adjust.call(this);
}
/**
* The threshold used to split a leaf node into two child nodes.
*
* @api public
*/
Rope.SPLIT_LENGTH = 1000;
/**
* The threshold used to join two child nodes into one leaf node.
*
* @api public
*/
Rope.JOIN_LENGTH = 500;
/**
* The threshold used to trigger a tree node rebuild when rebalancing the rope.
*
* @api public
*/
Rope.REBALANCE_RATIO = 1.2;
/**
* Adjusts the tree structure, so that very long nodes are split
* and short ones are joined
*
* @api private
*/
function adjust() {
if (typeof this._value != 'undefined') {
if (this.length > Rope.SPLIT_LENGTH) {
var divide = Math.floor(this.length / 2);
this._left = new Rope(this._value.substring(0, divide));
this._right = new Rope(this._value.substring(divide));
delete this._value;
}
} else {
if (this.length < Rope.JOIN_LENGTH) {
this._value = this._left.toString() + this._right.toString();
delete this._left;
delete this._right;
}
}
}
/**
* Converts the rope to a JavaScript String.
*
* @api public
*/
Rope.prototype.toString = function() {
if (typeof this._value != 'undefined') {
return this._value;
} else {
return this._left.toString() + this._right.toString();
}
}
/**
* Removes text from the rope between the `start` and `end` positions.
* The character at `start` gets removed, but the character at `end` is
* not removed.
*
* @param {Number} start - Initial position (inclusive)
* @param {Number} end - Final position (not-inclusive)
* @api public
*/
Rope.prototype.remove = function(start, end) {
if (start < 0 || start > this.length) throw new RangeError('Start is not within rope bounds.');
if (end < 0 || end > this.length) throw new RangexError('End is not within rope bounds.');
if (start > end) throw new RangexError('Start is greater than end.');
if (typeof this._value != 'undefined') {
this._value = this._value.substring(0, start) + this._value.substring(end);
this.length = this._value.length;
} else {
var leftLength = this._left.length;
var leftStart = Math.min(start, leftLength);
var leftEnd = Math.min(end, leftLength);
var rightLength = this._right.length;
var rightStart = Math.max(0, Math.min(start - leftLength, rightLength));
var rightEnd = Math.max(0, Math.min(end - leftLength, rightLength));
if (leftStart < leftLength) {
this._left.remove(leftStart, leftEnd);
}
if (rightEnd > 0) {
this._right.remove(rightStart, rightEnd);
}
this.length = this._left.length + this._right.length;
}
adjust.call(this);
}
/**
* Inserts text into the rope on the specified position.
*
* @param {Number} position - Where to insert the text
* @param {String} value - Text to be inserted on the rope
* @api public
*/
Rope.prototype.insert = function(position, value) {
if (typeof value != 'string') {
value = value.toString();
}
if (position < 0 || position > this.length) throw new RangeError('position is not within rope bounds.');
if (typeof this._value != 'undefined') {
this._value = this._value.substring(0, position) + value.toString() + this._value.substring(position);
this.length = this._value.length;
} else {
var leftLength = this._left.length;
if (position < leftLength) {
this._left.insert(position, value);
this.length = this._left.length + this._right.length;
} else {
this._right.insert(position - leftLength, value);
}
}
adjust.call(this);
}
/**
* Rebuilds the entire rope structure, producing a balanced tree.
*
* @api public
*/
Rope.prototype.rebuild = function() {
if (typeof this._value == 'undefined') {
this._value = this._left.toString() + this._right.toString();
delete this._left;
delete this._right;
adjust.call(this);
}
}
/**
* Finds unbalanced nodes in the tree and rebuilds them.
*
* @api public
*/
Rope.prototype.rebalance = function() {
if (typeof this._value == 'undefined') {
if (this._left.length / this._right.length > Rope.REBALANCE_RATIO ||
this._right.length / this._left.length > Rope.REBALANCE_RATIO) {
this.rebuild();
} else {
this._left.rebalance();
this._right.rebalance();
}
}
}
/**
* Returns text from the rope between the `start` and `end` positions.
* The character at `start` gets returned, but the character at `end` is
* not returned.
*
* @param {Number} start - Initial position (inclusive)
* @param {Number} end - Final position (not-inclusive)
* @api public
*/
Rope.prototype.substring = function(start, end) {
if (typeof end == 'undefined') {
end = this.length;
}
if (start < 0 || isNaN(start)) {
start = 0;
} else if (start > this.length) {
start = this.length;
}
if (end < 0 || isNaN(end)) {
end = 0;
} else if (end > this.length) {
end = this.length;
}
if (typeof this._value != 'undefined') {
return this._value.substring(start, end);
} else {
var leftLength = this._left.length;
var leftStart = Math.min(start, leftLength);
var leftEnd = Math.min(end, leftLength);
var rightLength = this._right.length;
var rightStart = Math.max(0, Math.min(start - leftLength, rightLength));
var rightEnd = Math.max(0, Math.min(end - leftLength, rightLength));
if (leftStart != leftEnd) {
if (rightStart != rightEnd) {
return this._left.substring(leftStart, leftEnd) + this._right.substring(rightStart, rightEnd);
} else {
return this._left.substring(leftStart, leftEnd);
}
} else {
if (rightStart != rightEnd) {
return this._right.substring(rightStart, rightEnd);
} else {
return '';
}
}
}
}
/**
* Returns a string of `length` characters from the rope, starting
* at the `start` position.
*
* @param {Number} start - Initial position (inclusive)
* @param {Number} length - Size of the string to return
* @api public
*/
Rope.prototype.substr = function(start, length) {
var end;
if (start < 0) {
start = this.length + start;
if (start < 0) {
start = 0;
}
}
if (typeof length == 'undefined') {
end = this.length;
} else {
if (length < 0) {
length = 0;
}
end = start + length;
}
return this.substring(start, end);
}
/**
* Returns the character at `position`
*
* @param {Number} position
* @api public
*/
Rope.prototype.charAt = function(position) {
return this.substring(position, position + 1);
}
/**
* Returns the code of the character at `position`
*
* @param {Number} position
* @api public
*/
Rope.prototype.charCodeAt = function(position) {
return this.substring(position, position + 1).charCodeAt(0);
}
module.exports = Rope;