Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 | 105x 4x 9284x 9284x 8417x 8415x 96674x 8415x 16x 15x 13x 13x 13x 1053x 1053x 12x 12x 12x 448x 46x 44x 46x 175x 242x 17213x 200134x 5x 2884x 5253x 5253x 116520x 116520x 2854x 2854x 111684x 111684x 2947x 14767x 111690x 2874x 2889x 6496x 3477x 1461x 1437x 5667x 3881x 2901x 2932x 3276x 1070x 8533x 2876x 7527x 3638x 25562x 7625x 7732x 50x 87x 3120x 11683x 19x | /**
* `ArrayDeque`: a double-ended queue stored in a circular array.
*
* @packageDocumentation
*/
import type { Cursor } from '../interfaces/collection'
import type { Deque } from '../interfaces/queue'
import {
brandedHasInstance,
DEQUE_BRAND,
QUEUE_BRAND,
SEQUENCED_COLLECTION_BRAND,
} from '../internal/brand'
import { describeValue, isIterable, requireFunction } from '../internal/checks'
import { ArrayDequeEngine } from '../internal/engines/array-deque-engine'
import { equalsSymbol, type HashFunction, hashCodeSymbol } from '../internal/equality'
import { apply } from '../internal/intrinsics'
import { COLLECTION_BRAND, INSPECT } from '../internal/output'
const ARRAY_DEQUE_BRAND: unique symbol = /*#__PURE__*/ Symbol.for(
'javascript-collections-framework/1/ArrayDeque',
)
function notIterable(where: string, value: unknown): TypeError {
return new TypeError(`${where}: elements is not iterable (${describeValue(value)})`)
}
/**
* A double-ended queue stored in a circular array: adding at either end is amortised O(1) and
* removing is O(1), so
* it serves as a first-in-first-out queue (`add` + `poll`), as a stack (`push` + `pop`, at the
* end, as on an array) or as a buffer worked from both ends.
*
* `null` and `undefined` are not allowed as elements, so `poll()`, `peek()` and `pop()` returning
* `undefined` always means the deque is empty. Two deques are equal only when they are the same
* object; compare `toArray()` results to compare contents. Adding or removing while iterating
* throws `ConcurrentModificationError`; use a cursor to remove while iterating.
*
* The class is not designed to be subclassed.
*
* @example
* ```ts
* const queue = new ArrayDeque<string>()
* queue.add('a').add('b')
* queue.poll() // 'a'
*
* const stack = new ArrayDeque([1, 2])
* stack.push(3)
* stack.pop() // 3
* ```
*
* @sealed
*/
export class ArrayDeque<E> implements Deque<E> {
private readonly engine: ArrayDequeEngine<E>
/**
* Creates a deque holding the elements of `elements`, in order (the first is the front); empty
* when `elements` is omitted, `undefined` or `null` (as `new Set(null)` is).
*
* Time: O(m) for `m` elements.
*
* @param elements - The initial elements.
* @throws TypeError when `elements` is neither iterable nor `null`/`undefined`.
* @throws NullElementError when an element is `null` or `undefined`.
* @throws IllegalStateError when the elements would not fit (over 2^30 in total).
*
* @example
* ```ts
* new ArrayDeque(['a', 'b']) // ArrayDeque(2) ['a', 'b']
* ```
*/
constructor(elements?: Iterable<E> | null) {
this.engine = new ArrayDequeEngine<E>(this)
if (elements === undefined || elements === null) return
if (!isIterable(elements)) throw notIterable('ArrayDeque', elements)
const items: E[] = []
for (const element of elements) items.push(element)
this.engine.appendAll(items, 'ArrayDeque')
}
/**
* Returns a new deque of the elements of `elements`, in order, like `Array.from` but for
* iterables only.
*
* Time: O(m) for `m` elements.
*
* @param elements - The elements.
* @returns A new deque.
* @throws TypeError when `elements` is not iterable (`null`, `undefined` and array-likes
* included).
* @throws NullElementError when an element is `null` or `undefined`.
* @throws IllegalStateError when the elements would not fit (over 2^30 in total).
*
* @example
* ```ts
* ArrayDeque.from(new Set([1, 2])) // ArrayDeque(2) [1, 2]
* ```
*/
static from<T>(elements: Iterable<T>): ArrayDeque<T>
/**
* Returns a new deque of `mapFn(element, index)` for each element of `elements`, in order, with
* `this` set to `thisArg`, as `Array.from` does. `elements` is read while `mapFn` runs, so
* items `mapFn` appends to an array source are included.
*
* Time: O(m) plus the calls.
*
* @param elements - The elements.
* @param mapFn - Called with each element and its index; its result is stored.
* @param thisArg - The `this` value for `mapFn`.
* @returns A new deque.
* @throws TypeError when `mapFn` is not a function (checked first) or `elements` is not
* iterable.
* @throws NullElementError when `mapFn` returns `null` or `undefined`.
* @throws IllegalStateError when the elements would not fit (over 2^30 in total).
* @throws Error whatever `mapFn` throws; no collection is returned.
*
* @example
* ```ts
* ArrayDeque.from([1, 2], (n) => n * 10) // ArrayDeque(2) [10, 20]
* ```
*/
static from<T, U>(
elements: Iterable<T>,
mapFn: (element: T, index: number) => U,
thisArg?: unknown,
): ArrayDeque<U>
static from<T, U>(
elements: Iterable<T>,
mapFn?: (element: T, index: number) => U,
thisArg?: unknown,
): ArrayDeque<T | U> {
if (mapFn !== undefined) requireFunction(mapFn, 'ArrayDeque.from', 'mapFn')
if (!isIterable(elements)) throw notIterable('ArrayDeque.from', elements)
const items: Array<T | U> = []
let index = 0
for (const element of elements) {
items.push(mapFn === undefined ? element : (apply(mapFn, thisArg, [element, index]) as U))
index++
}
const deque = new ArrayDeque<T | U>()
deque.engine.appendAll(items, 'ArrayDeque.from')
return deque
}
/**
* Returns whether `value` is an `ArrayDeque`, including one created by another copy of the
* library with the same major version. A reversed view is not an `ArrayDeque`.
*
* Time: O(1).
*
* @param value - The value on the left of `instanceof`.
* @returns `true` when `value` is an `ArrayDeque`.
*
* @example
* ```ts
* new ArrayDeque() instanceof ArrayDeque // true
* ```
*/
static [Symbol.hasInstance](value: unknown): boolean {
// biome-ignore lint/complexity/noThisInStatic: `this` is the class on the right of `instanceof`
return brandedHasInstance(this, ArrayDeque, ARRAY_DEQUE_BRAND, value)
}
/**
* Marks instances of this class so `instanceof` recognises them across copies of the package.
* Its key is not exported, so the getter is not called directly. Time: O(1).
*/
get [ARRAY_DEQUE_BRAND](): true {
return true
}
/**
* Marks deques so `isDeque` recognises them across copies of the package. Its key is not
* exported, so the getter is not called directly. Time: O(1).
*/
get [DEQUE_BRAND](): true {
return true
}
/**
* Marks queues so `isQueue` recognises them across copies of the package. Its key is not
* exported, so the getter is not called directly. Time: O(1).
*/
get [QUEUE_BRAND](): true {
return true
}
/**
* Marks sequenced collections so `isSequencedCollection` recognises them across copies of the
* package. Its key is not exported, so the getter is not called directly. Time: O(1).
*/
get [SEQUENCED_COLLECTION_BRAND](): true {
return true
}
/**
* Marks collections so `isCollection` recognises them across copies of the package. Its key is
* not exported, so the getter is not called directly. Time: O(1).
*/
get [COLLECTION_BRAND](): true {
return true
}
/** The number of elements. Time: O(1). */
get size(): number {
return this.engine.size
}
/**
* A number that changes on every change to the contents and never on a read. Its exact values
* are unspecified. Time: O(1).
*/
get revision(): number {
return this.engine.revision
}
/** `'ArrayDeque'`, for `Object.prototype.toString`. Time: O(1). */
get [Symbol.toStringTag](): string {
return 'ArrayDeque'
}
/**
* Returns whether the deque has no elements.
*
* Time: O(1).
*
* @returns `size === 0`.
*
* @example
* ```ts
* new ArrayDeque().isEmpty() // true
* ```
*/
isEmpty(): boolean {
return this.engine.isEmpty()
}
/**
* Adds `element` at the end and returns the deque, so calls chain.
*
* Time: amortised O(1).
*
* @param element - The element to add.
* @returns The deque.
* @throws NullElementError when `element` is `null` or `undefined`.
* @throws IllegalStateError when the deque already holds the most elements it can.
*
* @example
* ```ts
* new ArrayDeque<number>().add(1).add(2) // ArrayDeque(2) [1, 2]
* ```
*/
add(element: E): this {
this.engine.add(element)
return this
}
/**
* Adds `element` at the end, as `add` does, and returns the deque. Unlike
* `Array.prototype.push`, it returns the deque, not the new length.
*
* Time: amortised O(1).
*
* @param element - The element to add.
* @returns The deque.
* @throws NullElementError when `element` is `null` or `undefined`.
* @throws IllegalStateError when the deque already holds the most elements it can.
*
* @example
* ```ts
* new ArrayDeque([1]).push(2) // ArrayDeque(2) [1, 2]
* ```
*/
push(element: E): this {
this.engine.push(element)
return this
}
/**
* Adds `element` at the end and returns the deque; the same as `add`.
*
* Time: amortised O(1).
*
* @param element - The element to add.
* @returns The deque.
* @throws NullElementError when `element` is `null` or `undefined`.
* @throws IllegalStateError when the deque already holds the most elements it can.
*
* @example
* ```ts
* new ArrayDeque([1]).addLast(2) // ArrayDeque(2) [1, 2]
* ```
*/
addLast(element: E): this {
this.engine.addLast(element)
return this
}
/**
* Adds `element` at the front and returns the deque.
*
* Time: amortised O(1).
*
* @param element - The element to add.
* @returns The deque.
* @throws NullElementError when `element` is `null` or `undefined`.
* @throws IllegalStateError when the deque already holds the most elements it can.
*
* @example
* ```ts
* new ArrayDeque([2]).addFirst(1) // ArrayDeque(2) [1, 2]
* ```
*/
addFirst(element: E): this {
this.engine.addFirst(element)
return this
}
/**
* Adds every element of `elements` at the end, in order, after reading and checking them all:
* if any is `null` or `undefined`, nothing is added.
*
* Time: amortised O(m) for `m` elements.
*
* @param elements - The elements to add.
* @returns `true` when any element was added.
* @throws TypeError when `elements` is not iterable.
* @throws NullElementError when an element is `null` or `undefined`.
* @throws IllegalStateError when the elements would not fit (over 2^30 in total).
*
* @example
* ```ts
* new ArrayDeque([1]).addAll([2, 3]) // true; [1, 2, 3]
* ```
*/
addAll(elements: Iterable<E>): boolean {
return this.engine.addAll(elements)
}
/**
* Removes and returns the first element, or returns `undefined` when the deque is empty.
*
* Time: O(1).
*
* @returns The removed element, or `undefined`.
*
* @example
* ```ts
* new ArrayDeque(['a', 'b']).poll() // 'a'
* ```
*/
poll(): E | undefined {
return this.engine.poll()
}
/**
* Removes and returns the last element, or returns `undefined` when the deque is empty, as
* `Array.prototype.pop` does.
*
* Time: O(1).
*
* @returns The removed element, or `undefined`.
*
* @example
* ```ts
* new ArrayDeque([1, 2]).pop() // 2
* ```
*/
pop(): E | undefined {
return this.engine.pop()
}
/**
* Removes and returns the first element.
*
* Time: O(1).
*
* @returns The removed element.
* @throws NoSuchElementError when the deque is empty.
*
* @example
* ```ts
* new ArrayDeque([1, 2]).removeFirst() // 1
* ```
*/
removeFirst(): E {
return this.engine.removeFirst()
}
/**
* Removes and returns the last element.
*
* Time: O(1).
*
* @returns The removed element.
* @throws NoSuchElementError when the deque is empty.
*
* @example
* ```ts
* new ArrayDeque([1, 2]).removeLast() // 2
* ```
*/
removeLast(): E {
return this.engine.removeLast()
}
/**
* Returns the first element without removing it, or `undefined` when the deque is empty.
*
* Time: O(1).
*
* @returns The first element, or `undefined`.
*
* @example
* ```ts
* new ArrayDeque([1, 2]).peek() // 1
* ```
*/
peek(): E | undefined {
return this.engine.peek()
}
/**
* Returns the last element without removing it, or `undefined` when the deque is empty.
*
* Time: O(1).
*
* @returns The last element, or `undefined`.
*
* @example
* ```ts
* new ArrayDeque([1, 2]).peekLast() // 2
* ```
*/
peekLast(): E | undefined {
return this.engine.peekLast()
}
/**
* Returns the first element.
*
* Time: O(1).
*
* @returns The first element.
* @throws NoSuchElementError when the deque is empty.
*
* @example
* ```ts
* new ArrayDeque([1, 2]).getFirst() // 1
* ```
*/
getFirst(): E {
return this.engine.getFirst()
}
/**
* Returns the last element.
*
* Time: O(1).
*
* @returns The last element.
* @throws NoSuchElementError when the deque is empty.
*
* @example
* ```ts
* new ArrayDeque([1, 2]).getLast() // 2
* ```
*/
getLast(): E {
return this.engine.getLast()
}
/**
* Returns whether some element is equal to `element` under SameValueZero; always `false` for
* `null` and `undefined`.
*
* Time: O(n).
*
* @param element - The value to look for.
* @returns `true` when present.
*
* @example
* ```ts
* new ArrayDeque([1, 2]).has(2) // true
* ```
*/
has(element: E): boolean {
return this.engine.has(element)
}
/**
* Removes the first element equal to `element` under SameValueZero, searching from the front.
* `reversed().delete(element)` removes the last one instead.
*
* Time: O(n).
*
* @param element - The value to remove.
* @returns `true` when an element was removed.
*
* @example
* ```ts
* new ArrayDeque([1, 2, 1]).delete(1) // true; [2, 1]
* ```
*/
delete(element: E): boolean {
return this.engine.delete(element)
}
/**
* Removes every element equal to one in `elements` (SameValueZero). Decides first, so an error
* while deciding removes nothing.
*
* Time: O(n + m) for `m` elements.
*
* @param elements - The elements to remove.
* @returns `true` when any element was removed.
* @throws TypeError when `elements` is not iterable.
*
* @example
* ```ts
* new ArrayDeque([1, 2, 1]).removeAll([1]) // true; [2]
* ```
*/
removeAll(elements: Iterable<E>): boolean {
return this.engine.removeAll(elements)
}
/**
* Removes every element not equal to one in `elements` (SameValueZero). Decides first, so an
* error while deciding removes nothing.
*
* Time: O(n + m) for `m` elements.
*
* @param elements - The elements to keep.
* @returns `true` when any element was removed.
* @throws TypeError when `elements` is not iterable.
*
* @example
* ```ts
* new ArrayDeque([1, 2, 3]).retainAll([2]) // true; [2]
* ```
*/
retainAll(elements: Iterable<E>): boolean {
return this.engine.retainAll(elements)
}
/**
* Calls `predicate(element)` on every element, then removes those for which it returned a truthy
* value; an error from `predicate` removes nothing.
*
* Time: O(n) plus the calls.
*
* @param predicate - Called with each element.
* @returns `true` when any element was removed.
* @throws TypeError when `predicate` is not a function.
* @throws ConcurrentModificationError when `predicate` adds or removes elements.
*
* @example
* ```ts
* new ArrayDeque([1, -2, 3]).removeIf((n) => n < 0) // true; [1, 3]
* ```
*/
removeIf(predicate: (element: E) => unknown): boolean {
return this.engine.removeIf(predicate)
}
/**
* Removes every element.
*
* Time: O(n).
*
* @example
* ```ts
* deque.clear() // deque.size === 0
* ```
*/
clear(): void {
this.engine.clear()
}
/**
* Returns a new array of the elements, from first to last.
*
* Time: O(n).
*
* @returns A new array.
*
* @example
* ```ts
* new ArrayDeque([1, 2]).toArray() // [1, 2]
* ```
*/
toArray(): E[] {
return this.engine.toArray()
}
/**
* Calls `action(element)` for each element from first to last, with `this` set to `thisArg`.
*
* Time: O(n) plus the calls.
*
* @param action - Called with each element.
* @param thisArg - The `this` value for `action`.
* @throws TypeError when `action` is not a function.
* @throws ConcurrentModificationError when `action` adds or removes elements.
*
* @example
* ```ts
* new ArrayDeque(['a', 'b']).forEach((s) => console.log(s))
* ```
*/
forEach(action: (element: E) => void, thisArg?: unknown): void {
this.engine.forEach(action, thisArg)
}
/**
* Returns an iterator over the elements from first to last.
*
* Time: O(1) to create; O(1) per step.
*
* @returns A new iterator.
* @throws ConcurrentModificationError (from `next()`) when elements are added or removed after
* the iterator is created, including through a cursor.
*
* @example
* ```ts
* for (const element of new ArrayDeque([1, 2])) console.log(element)
* ```
*/
[Symbol.iterator](): IterableIterator<E> {
return this.engine[Symbol.iterator]()
}
/**
* Returns a cursor positioned before the first element; `remove()` removes the element `next()`
* returned. `reversed().cursor()` walks from the last element to the first.
*
* Time: O(1) to create; `next` O(1); `remove` O(n).
*
* @returns A new cursor.
*
* @example
* ```ts
* const cursor = deque.cursor()
* while (cursor.hasNext()) if (cursor.next() < 0) cursor.remove()
* ```
*/
cursor(): Cursor<E> {
return this.engine.cursor()
}
/**
* Returns a live view of this deque in reverse order: its first element is this deque's last,
* and changes through either show in both (the view's `push` adds at this deque's front, its
* `poll` is this deque's `pop`). `reversed().reversed()` returns this deque.
*
* Time: O(1); each operation on the view costs what its mirror costs.
*
* @returns The reversed view.
*
* @example
* ```ts
* const deque = new ArrayDeque([1, 2, 3])
* deque.reversed().peek() // 3
* deque.reversed().delete(1) // removes the last 1
* ```
*/
reversed(): Deque<E> {
return this.engine.reversed()
}
/**
* Returns a new `ArrayDeque` with the same elements in the same order, independent of this one
* (the elements themselves are not copied).
*
* Time: O(n).
*
* @returns The copy.
*
* @example
* ```ts
* const copy = deque.copy()
* copy.poll() // deque is unchanged
* ```
*/
copy(): ArrayDeque<E> {
return new ArrayDeque(this.engine.toArray())
}
/**
* Returns whether `other` is this deque. Deques compare by identity; compare `toArray()` to
* compare contents. Called through a `Proxy` of the deque, it is `true` for that proxy too; the deque itself
* does not know its proxies, so `deque.equals(proxy)` is `false`.
*
* Time: O(1).
*
* @param other - Any value.
* @returns `true` when `other` is this deque.
*
* @example
* ```ts
* new ArrayDeque([1]).equals(new ArrayDeque([1])) // false
* ```
*/
equals(other: unknown): boolean {
return other === this || this.engine.equals(other)
}
/** The value-equality protocol; delegates to `equals` (identity). Time: O(1). */
[equalsSymbol](other: unknown): boolean {
return other === this || this.engine.equals(other)
}
/** The value-equality protocol: an identity hash, unspecified in value. Time: O(1). */
[hashCodeSymbol](hash: HashFunction): number {
return this.engine[hashCodeSymbol](hash)
}
/**
* Returns the class name, size and elements, e.g. `ArrayDeque(2) [1, 'a']`.
*
* Time: O(length of the output) for flat contents.
*
* @returns The text.
*
* @example
* ```ts
* String(new ArrayDeque([1, 'a'])) // "ArrayDeque(2) [1, 'a']"
* ```
*/
toString(): string {
return this.engine.toString()
}
/**
* Returns a new array of the elements, so `JSON.stringify` writes the deque as an array.
*
* Time: O(n).
*
* @returns A new array.
*
* @example
* ```ts
* JSON.stringify(new ArrayDeque([1, 2])) // '[1,2]'
* ```
*/
toJSON(): E[] {
return this.engine.toJSON()
}
/** Node's inspect hook: `ArrayDeque(2) [ 1, 2 ]` in `console.log`. Time: as `toString`. */
[INSPECT](
depth: number | null,
options: object,
inspect: (value: unknown, options: object) => string,
): string {
return this.engine[INSPECT](depth, options, inspect)
}
}
|