| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
/*----------------------------------------------------------------------------- |
| 4 |
| Copyright (c) 2014-2017, PhosphorJS Contributors |
| 5 |
| |
| 6 |
| Distributed under the terms of the BSD 3-Clause License. |
| 7 |
| |
| 8 |
| The full license is in the file LICENSE, distributed with this software. |
| 9 |
|----------------------------------------------------------------------------*/ |
| 10 |
import { IRetroable } from '@lumino/algorithm'; |
| 11 |
|
| 12 |
/** |
| 13 |
* A generic doubly-linked list. |
| 14 |
*/ |
| 15 |
export class LinkedList<T> implements Iterable<T>, IRetroable<T> { |
| 16 |
/** |
| 17 |
* Whether the list is empty. |
| 18 |
* |
| 19 |
* #### Complexity |
| 20 |
* Constant. |
| 21 |
*/ |
| 22 |
get isEmpty(): boolean { |
| 23 |
return this._size === 0; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* The size of the list. |
| 28 |
* |
| 29 |
* #### Complexity |
| 30 |
* `O(1)` |
| 31 |
* |
| 32 |
* #### Notes |
| 33 |
* This is equivalent to `length`. |
| 34 |
*/ |
| 35 |
get size(): number { |
| 36 |
return this._size; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* The length of the list. |
| 41 |
* |
| 42 |
* #### Complexity |
| 43 |
* Constant. |
| 44 |
* |
| 45 |
* #### Notes |
| 46 |
* This is equivalent to `size`. |
| 47 |
* |
| 48 |
* This property is deprecated. |
| 49 |
*/ |
| 50 |
get length(): number { |
| 51 |
return this._size; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* The first value in the list. |
| 56 |
* |
| 57 |
* This is `undefined` if the list is empty. |
| 58 |
* |
| 59 |
* #### Complexity |
| 60 |
* Constant. |
| 61 |
*/ |
| 62 |
get first(): T | undefined { |
| 63 |
return this._first ? this._first.value : undefined; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* The last value in the list. |
| 68 |
* |
| 69 |
* This is `undefined` if the list is empty. |
| 70 |
* |
| 71 |
* #### Complexity |
| 72 |
* Constant. |
| 73 |
*/ |
| 74 |
get last(): T | undefined { |
| 75 |
return this._last ? this._last.value : undefined; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* The first node in the list. |
| 80 |
* |
| 81 |
* This is `null` if the list is empty. |
| 82 |
* |
| 83 |
* #### Complexity |
| 84 |
* Constant. |
| 85 |
*/ |
| 86 |
get firstNode(): LinkedList.INode<T> | null { |
| 87 |
return this._first; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* The last node in the list. |
| 92 |
* |
| 93 |
* This is `null` if the list is empty. |
| 94 |
* |
| 95 |
* #### Complexity |
| 96 |
* Constant. |
| 97 |
*/ |
| 98 |
get lastNode(): LinkedList.INode<T> | null { |
| 99 |
return this._last; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Create an iterator over the values in the list. |
| 104 |
* |
| 105 |
* @returns A new iterator starting with the first value. |
| 106 |
* |
| 107 |
* #### Complexity |
| 108 |
* Constant. |
| 109 |
*/ |
| 110 |
*[Symbol.iterator](): IterableIterator<T> { |
| 111 |
let node = this._first; |
| 112 |
while (node) { |
| 113 |
yield node.value; |
| 114 |
node = node.next; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Create a reverse iterator over the values in the list. |
| 120 |
* |
| 121 |
* @returns A new iterator starting with the last value. |
| 122 |
* |
| 123 |
* #### Complexity |
| 124 |
* Constant. |
| 125 |
*/ |
| 126 |
*retro(): IterableIterator<T> { |
| 127 |
let node = this._last; |
| 128 |
while (node) { |
| 129 |
yield node.value; |
| 130 |
node = node.prev; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Create an iterator over the nodes in the list. |
| 136 |
* |
| 137 |
* @returns A new iterator starting with the first node. |
| 138 |
* |
| 139 |
* #### Complexity |
| 140 |
* Constant. |
| 141 |
*/ |
| 142 |
*nodes(): IterableIterator<LinkedList.INode<T>> { |
| 143 |
let node = this._first; |
| 144 |
while (node) { |
| 145 |
yield node; |
| 146 |
node = node.next; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Create a reverse iterator over the nodes in the list. |
| 152 |
* |
| 153 |
* @returns A new iterator starting with the last node. |
| 154 |
* |
| 155 |
* #### Complexity |
| 156 |
* Constant. |
| 157 |
*/ |
| 158 |
*retroNodes(): IterableIterator<LinkedList.INode<T>> { |
| 159 |
let node = this._last; |
| 160 |
while (node) { |
| 161 |
yield node; |
| 162 |
node = node.prev; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Assign new values to the list, replacing all current values. |
| 168 |
* |
| 169 |
* @param values - The values to assign to the list. |
| 170 |
* |
| 171 |
* #### Complexity |
| 172 |
* Linear. |
| 173 |
*/ |
| 174 |
assign(values: Iterable<T>): void { |
| 175 |
this.clear(); |
| 176 |
for (const value of values) { |
| 177 |
this.addLast(value); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Add a value to the end of the list. |
| 183 |
* |
| 184 |
* @param value - The value to add to the end of the list. |
| 185 |
* |
| 186 |
* #### Complexity |
| 187 |
* Constant. |
| 188 |
* |
| 189 |
* #### Notes |
| 190 |
* This is equivalent to `addLast`. |
| 191 |
*/ |
| 192 |
push(value: T): void { |
| 193 |
this.addLast(value); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Remove and return the value at the end of the list. |
| 198 |
* |
| 199 |
* @returns The removed value, or `undefined` if the list is empty. |
| 200 |
* |
| 201 |
* #### Complexity |
| 202 |
* Constant. |
| 203 |
* |
| 204 |
* #### Notes |
| 205 |
* This is equivalent to `removeLast`. |
| 206 |
*/ |
| 207 |
pop(): T | undefined { |
| 208 |
return this.removeLast(); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Add a value to the beginning of the list. |
| 213 |
* |
| 214 |
* @param value - The value to add to the beginning of the list. |
| 215 |
* |
| 216 |
* #### Complexity |
| 217 |
* Constant. |
| 218 |
* |
| 219 |
* #### Notes |
| 220 |
* This is equivalent to `addFirst`. |
| 221 |
*/ |
| 222 |
shift(value: T): void { |
| 223 |
this.addFirst(value); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Remove and return the value at the beginning of the list. |
| 228 |
* |
| 229 |
* @returns The removed value, or `undefined` if the list is empty. |
| 230 |
* |
| 231 |
* #### Complexity |
| 232 |
* Constant. |
| 233 |
* |
| 234 |
* #### Notes |
| 235 |
* This is equivalent to `removeFirst`. |
| 236 |
*/ |
| 237 |
unshift(): T | undefined { |
| 238 |
return this.removeFirst(); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Add a value to the beginning of the list. |
| 243 |
* |
| 244 |
* @param value - The value to add to the beginning of the list. |
| 245 |
* |
| 246 |
* @returns The list node which holds the value. |
| 247 |
* |
| 248 |
* #### Complexity |
| 249 |
* Constant. |
| 250 |
*/ |
| 251 |
addFirst(value: T): LinkedList.INode<T> { |
| 252 |
let node = new Private.LinkedListNode<T>(this, value); |
| 253 |
if (!this._first) { |
| 254 |
this._first = node; |
| 255 |
this._last = node; |
| 256 |
} else { |
| 257 |
node.next = this._first; |
| 258 |
this._first.prev = node; |
| 259 |
this._first = node; |
| 260 |
} |
| 261 |
this._size++; |
| 262 |
return node; |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Add a value to the end of the list. |
| 267 |
* |
| 268 |
* @param value - The value to add to the end of the list. |
| 269 |
* |
| 270 |
* @returns The list node which holds the value. |
| 271 |
* |
| 272 |
* #### Complexity |
| 273 |
* Constant. |
| 274 |
*/ |
| 275 |
addLast(value: T): LinkedList.INode<T> { |
| 276 |
let node = new Private.LinkedListNode<T>(this, value); |
| 277 |
if (!this._last) { |
| 278 |
this._first = node; |
| 279 |
this._last = node; |
| 280 |
} else { |
| 281 |
node.prev = this._last; |
| 282 |
this._last.next = node; |
| 283 |
this._last = node; |
| 284 |
} |
| 285 |
this._size++; |
| 286 |
return node; |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Insert a value before a specific node in the list. |
| 291 |
* |
| 292 |
* @param value - The value to insert before the reference node. |
| 293 |
* |
| 294 |
* @param ref - The reference node of interest. If this is `null`, |
| 295 |
* the value will be added to the beginning of the list. |
| 296 |
* |
| 297 |
* @returns The list node which holds the value. |
| 298 |
* |
| 299 |
* #### Notes |
| 300 |
* The reference node must be owned by the list. |
| 301 |
* |
| 302 |
* #### Complexity |
| 303 |
* Constant. |
| 304 |
*/ |
| 305 |
insertBefore(value: T, ref: LinkedList.INode<T> | null): LinkedList.INode<T> { |
| 306 |
if (!ref || ref === this._first) { |
| 307 |
return this.addFirst(value); |
| 308 |
} |
| 309 |
if (!(ref instanceof Private.LinkedListNode) || ref.list !== this) { |
| 310 |
throw new Error('Reference node is not owned by the list.'); |
| 311 |
} |
| 312 |
let node = new Private.LinkedListNode<T>(this, value); |
| 313 |
let _ref = ref as Private.LinkedListNode<T>; |
| 314 |
let prev = _ref.prev!; |
| 315 |
node.next = _ref; |
| 316 |
node.prev = prev; |
| 317 |
_ref.prev = node; |
| 318 |
prev.next = node; |
| 319 |
this._size++; |
| 320 |
return node; |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Insert a value after a specific node in the list. |
| 325 |
* |
| 326 |
* @param value - The value to insert after the reference node. |
| 327 |
* |
| 328 |
* @param ref - The reference node of interest. If this is `null`, |
| 329 |
* the value will be added to the end of the list. |
| 330 |
* |
| 331 |
* @returns The list node which holds the value. |
| 332 |
* |
| 333 |
* #### Notes |
| 334 |
* The reference node must be owned by the list. |
| 335 |
* |
| 336 |
* #### Complexity |
| 337 |
* Constant. |
| 338 |
*/ |
| 339 |
insertAfter(value: T, ref: LinkedList.INode<T> | null): LinkedList.INode<T> { |
| 340 |
if (!ref || ref === this._last) { |
| 341 |
return this.addLast(value); |
| 342 |
} |
| 343 |
if (!(ref instanceof Private.LinkedListNode) || ref.list !== this) { |
| 344 |
throw new Error('Reference node is not owned by the list.'); |
| 345 |
} |
| 346 |
let node = new Private.LinkedListNode<T>(this, value); |
| 347 |
let _ref = ref as Private.LinkedListNode<T>; |
| 348 |
let next = _ref.next!; |
| 349 |
node.next = next; |
| 350 |
node.prev = _ref; |
| 351 |
_ref.next = node; |
| 352 |
next.prev = node; |
| 353 |
this._size++; |
| 354 |
return node; |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* Remove and return the value at the beginning of the list. |
| 359 |
* |
| 360 |
* @returns The removed value, or `undefined` if the list is empty. |
| 361 |
* |
| 362 |
* #### Complexity |
| 363 |
* Constant. |
| 364 |
*/ |
| 365 |
removeFirst(): T | undefined { |
| 366 |
let node = this._first; |
| 367 |
if (!node) { |
| 368 |
return undefined; |
| 369 |
} |
| 370 |
if (node === this._last) { |
| 371 |
this._first = null; |
| 372 |
this._last = null; |
| 373 |
} else { |
| 374 |
this._first = node.next; |
| 375 |
this._first!.prev = null; |
| 376 |
} |
| 377 |
node.list = null; |
| 378 |
node.next = null; |
| 379 |
node.prev = null; |
| 380 |
this._size--; |
| 381 |
return node.value; |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* Remove and return the value at the end of the list. |
| 386 |
* |
| 387 |
* @returns The removed value, or `undefined` if the list is empty. |
| 388 |
* |
| 389 |
* #### Complexity |
| 390 |
* Constant. |
| 391 |
*/ |
| 392 |
removeLast(): T | undefined { |
| 393 |
let node = this._last; |
| 394 |
if (!node) { |
| 395 |
return undefined; |
| 396 |
} |
| 397 |
if (node === this._first) { |
| 398 |
this._first = null; |
| 399 |
this._last = null; |
| 400 |
} else { |
| 401 |
this._last = node.prev; |
| 402 |
this._last!.next = null; |
| 403 |
} |
| 404 |
node.list = null; |
| 405 |
node.next = null; |
| 406 |
node.prev = null; |
| 407 |
this._size--; |
| 408 |
return node.value; |
| 409 |
} |
| 410 |
|
| 411 |
/** |
| 412 |
* Remove a specific node from the list. |
| 413 |
* |
| 414 |
* @param node - The node to remove from the list. |
| 415 |
* |
| 416 |
* #### Complexity |
| 417 |
* Constant. |
| 418 |
* |
| 419 |
* #### Notes |
| 420 |
* The node must be owned by the list. |
| 421 |
*/ |
| 422 |
removeNode(node: LinkedList.INode<T>): void { |
| 423 |
if (!(node instanceof Private.LinkedListNode) || node.list !== this) { |
| 424 |
throw new Error('Node is not owned by the list.'); |
| 425 |
} |
| 426 |
let _node = node as Private.LinkedListNode<T>; |
| 427 |
if (_node === this._first && _node === this._last) { |
| 428 |
this._first = null; |
| 429 |
this._last = null; |
| 430 |
} else if (_node === this._first) { |
| 431 |
this._first = _node.next; |
| 432 |
this._first!.prev = null; |
| 433 |
} else if (_node === this._last) { |
| 434 |
this._last = _node.prev; |
| 435 |
this._last!.next = null; |
| 436 |
} else { |
| 437 |
_node.next!.prev = _node.prev; |
| 438 |
_node.prev!.next = _node.next; |
| 439 |
} |
| 440 |
_node.list = null; |
| 441 |
_node.next = null; |
| 442 |
_node.prev = null; |
| 443 |
this._size--; |
| 444 |
} |
| 445 |
|
| 446 |
/** |
| 447 |
* Remove all values from the list. |
| 448 |
* |
| 449 |
* #### Complexity |
| 450 |
* Linear. |
| 451 |
*/ |
| 452 |
clear(): void { |
| 453 |
let node = this._first; |
| 454 |
while (node) { |
| 455 |
let next = node.next; |
| 456 |
node.list = null; |
| 457 |
node.prev = null; |
| 458 |
node.next = null; |
| 459 |
node = next; |
| 460 |
} |
| 461 |
this._first = null; |
| 462 |
this._last = null; |
| 463 |
this._size = 0; |
| 464 |
} |
| 465 |
|
| 466 |
private _first: Private.LinkedListNode<T> | null = null; |
| 467 |
private _last: Private.LinkedListNode<T> | null = null; |
| 468 |
private _size = 0; |
| 469 |
} |
| 470 |
|
| 471 |
/** |
| 472 |
* The namespace for the `LinkedList` class statics. |
| 473 |
*/ |
| 474 |
export namespace LinkedList { |
| 475 |
/** |
| 476 |
* An object which represents a node in a linked list. |
| 477 |
* |
| 478 |
* #### Notes |
| 479 |
* User code will not create linked list nodes directly. Nodes |
| 480 |
* are created automatically when values are added to a list. |
| 481 |
*/ |
| 482 |
export interface INode<T> { |
| 483 |
/** |
| 484 |
* The linked list which created and owns the node. |
| 485 |
* |
| 486 |
* This will be `null` when the node is removed from the list. |
| 487 |
*/ |
| 488 |
readonly list: LinkedList<T> | null; |
| 489 |
|
| 490 |
/** |
| 491 |
* The next node in the list. |
| 492 |
* |
| 493 |
* This will be `null` when the node is the last node in the list |
| 494 |
* or when the node is removed from the list. |
| 495 |
*/ |
| 496 |
readonly next: INode<T> | null; |
| 497 |
|
| 498 |
/** |
| 499 |
* The previous node in the list. |
| 500 |
* |
| 501 |
* This will be `null` when the node is the first node in the list |
| 502 |
* or when the node is removed from the list. |
| 503 |
*/ |
| 504 |
readonly prev: INode<T> | null; |
| 505 |
|
| 506 |
/** |
| 507 |
* The user value stored in the node. |
| 508 |
*/ |
| 509 |
readonly value: T; |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* Create a linked list from an iterable of values. |
| 514 |
* |
| 515 |
* @param values - The iterable object of interest. |
| 516 |
* |
| 517 |
* @returns A new linked list initialized with the given values. |
| 518 |
* |
| 519 |
* #### Complexity |
| 520 |
* Linear. |
| 521 |
*/ |
| 522 |
export function from<T>(values: Iterable<T>): LinkedList<T> { |
| 523 |
let list = new LinkedList<T>(); |
| 524 |
list.assign(values); |
| 525 |
return list; |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
/** |
| 530 |
* The namespace for the module implementation details. |
| 531 |
*/ |
| 532 |
namespace Private { |
| 533 |
/** |
| 534 |
* The internal linked list node implementation. |
| 535 |
*/ |
| 536 |
export class LinkedListNode<T> { |
| 537 |
/** |
| 538 |
* The linked list which created and owns the node. |
| 539 |
*/ |
| 540 |
list: LinkedList<T> | null = null; |
| 541 |
|
| 542 |
/** |
| 543 |
* The next node in the list. |
| 544 |
*/ |
| 545 |
next: LinkedListNode<T> | null = null; |
| 546 |
|
| 547 |
/** |
| 548 |
* The previous node in the list. |
| 549 |
*/ |
| 550 |
prev: LinkedListNode<T> | null = null; |
| 551 |
|
| 552 |
/** |
| 553 |
* The user value stored in the node. |
| 554 |
*/ |
| 555 |
readonly value: T; |
| 556 |
|
| 557 |
/** |
| 558 |
* Construct a new linked list node. |
| 559 |
* |
| 560 |
* @param list - The list which owns the node. |
| 561 |
* |
| 562 |
* @param value - The value for the link. |
| 563 |
*/ |
| 564 |
constructor(list: LinkedList<T>, value: T) { |
| 565 |
this.list = list; |
| 566 |
this.value = value; |
| 567 |
} |
| 568 |
} |
| 569 |
} |