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 | 32729x 32729x 834061x 6894x 6894x 1415x 847x 44x 67760x 31413x 5175x 111x 50962x 104x 89x 87x 3623x 3623x 1638x 1638x 2328x 2328x 2244x 2244x 6936x 6936x 6936x 207975x 21534x 15393x 459297x 2522x 2473x 6804x 4447x 6743x 4390x 9891x 9657x 9657x 53957x 2348x 2348x 7309x 2538x 821x 8552x 12010x 11974x 3473x 2248x 3410x 2179x 16593x 16210x 98454x 14160x 86484x 86484x 805615x 86482x 32729x 6894x 7113x 9833x 9601x 7409x 7166x | // The skeleton every deque extends: every `Queue`, `Deque` and `SequencedCollection` operation
// written once over a few end operations and a one-direction walker. Nothing here works by
// position, so a linked engine keeps every walk O(n); the reversed view swaps the ends and the
// walking direction of its source.
import { NoSuchElementError } from '../../errors/errors'
import type { Cursor } from '../../interfaces/collection'
import type { Deque } from '../../interfaces/queue'
import { DEQUE_BRAND, QUEUE_BRAND, SEQUENCED_COLLECTION_BRAND } from '../brand'
import { requireElement, type Where } from '../checks'
import { sameValueZero } from '../equality'
import { done, FailFastCursor, FailFastIterator } from '../iteration'
import type { ChangeCounter } from '../revision'
import { AbstractCollection } from './abstract-collection'
/**
* Walks a deque in one direction, one element at a time. `next()` is called only after `hasNext()`
* returned `true`; `remove()` removes the element `next()` last returned, as a structural change.
*
* @internal
*/
export interface DequeWalker<E> {
hasNext(): boolean
next(): E
remove(): void
}
/** A fail-fast iterator over a deque, from its first element to its last. */
class DequeIterator<E> extends FailFastIterator<E> {
private readonly walker: DequeWalker<E>
constructor(walker: DequeWalker<E>, changes: ChangeCounter, where: string) {
super(changes, where)
this.walker = walker
}
protected step(): IteratorResult<E> {
return this.walker.hasNext() ? { done: false, value: this.walker.next() } : done()
}
}
/** A fail-fast cursor over a deque; `remove` removes the element `next` returned. */
class DequeCursor<E> extends FailFastCursor<E> {
private readonly walker: DequeWalker<E>
constructor(walker: DequeWalker<E>, changes: ChangeCounter, where: string) {
super(changes, where)
this.walker = walker
}
protected hasMore(): boolean {
return this.walker.hasNext()
}
protected advance(): E {
return this.walker.next()
}
protected removeLast(): void {
this.walker.remove()
}
}
/**
* Base class of every deque and deque view.
*
* Subclasses implement `size`, `[Symbol.toStringTag]`, `reversed()` and the operations below;
* changing operations record their change in the shared counter. Elements reaching them are
* already checked, and the deque is non-empty where an operation says so.
*
* @internal
*/
export abstract class AbstractDeque<E> extends AbstractCollection<E> implements Deque<E> {
protected readonly iterationMode = 'fail-fast' as const
/** Returns the front element of a non-empty deque. */
abstract peekFront(): E
/** Returns the back element of a non-empty deque. */
abstract peekBack(): E
/** A walker from the front to the back, or from the back to the front. */
abstract walker(fromBack: boolean): DequeWalker<E>
/** Adds `element` at the front; throws before any change when the deque is full. */
abstract insertFront(element: E, where: Where, method?: string): void
/** Adds `element` at the back; throws before any change when the deque is full. */
abstract insertBack(element: E, where: Where, method?: string): void
/** Removes and returns the front element of a non-empty deque. */
abstract takeFront(): E
/** Removes and returns the back element of a non-empty deque. */
abstract takeBack(): E
/** Keeps the elements whose flag is not `true`, in order, in one pass. */
abstract compact(flags: readonly boolean[]): void
/** Removes every element of a non-empty deque. */
abstract clearAll(): void
/** Returns spare storage of an empty deque to its smallest size; not a structural change. */
releaseStorage(): void {}
/** Throws `IllegalStateError` when `count` more elements would not fit; changes nothing. */
abstract ensureRoom(count: number, where: string): void
abstract reversed(): Deque<E>
/** Whether `null` and `undefined` are stored (`true`) or rejected with `NullElementError`. */
get storesEmpty(): boolean {
return false
}
/** The word error messages use for this deque: `the deque is empty`. */
protected get noun(): string {
return 'deque'
}
/** The deque the caller holds, used for identity: the public object for the engine. */
get owner(): Deque<E> {
return this
}
/** The change counter, shared with the reversed view. */
get changeCounter(): ChangeCounter {
return this.changes
}
/** Marks every sequenced collection for `isSequencedCollection`, across loaded copies. */
get [SEQUENCED_COLLECTION_BRAND](): true {
return true
}
/** Marks every queue for `isQueue`, across loaded copies. */
get [QUEUE_BRAND](): true {
return true
}
/** Marks every deque for `isDeque`, across loaded copies. */
get [DEQUE_BRAND](): true {
return true
}
// ── Adding ──
add(element: E): this {
this.insertChecked(element, 'add', false)
return this
}
addLast(element: E): this {
this.insertChecked(element, 'addLast', false)
return this
}
push(element: E): this {
this.insertChecked(element, 'push', false)
return this
}
addFirst(element: E): this {
this.insertChecked(element, 'addFirst', true)
return this
}
override addAll(elements: Iterable<E>): boolean {
const items = this.readAll(elements, 'addAll')
this.appendAll(items, `${this.className}.addAll`)
return items.length > 0
}
/** Checks every element and the room for all of them, then adds them at the back in order. */
appendAll(items: readonly E[], where: string): void {
if (!this.storesEmpty) for (let i = 0; i < items.length; i++) requireElement(items[i], where)
if (items.length === 0) return
this.ensureRoom(items.length, where)
for (let i = 0; i < items.length; i++) this.insertBack(items[i] as E, where)
}
// ── Removing ──
poll(): E | undefined {
return this.size === 0 ? undefined : this.takeFront()
}
pop(): E | undefined {
return this.size === 0 ? undefined : this.takeBack()
}
removeFirst(): E {
if (this.size === 0) throw this.empty('removeFirst')
return this.takeFront()
}
removeLast(): E {
if (this.size === 0) throw this.empty('removeLast')
return this.takeBack()
}
override delete(element: E): boolean {
if (!this.storesEmpty && (element === null || element === undefined)) return false
const walker = this.walker(false)
while (walker.hasNext()) {
if (sameValueZero(walker.next(), element)) {
walker.remove()
return true
}
}
return false
}
override clear(): void {
if (this.size > 0) this.clearAll()
else this.releaseStorage()
}
protected override removeMarked(flags: readonly boolean[]): void {
this.compact(flags)
}
// ── Reading ──
peek(): E | undefined {
return this.size === 0 ? undefined : this.peekFront()
}
peekLast(): E | undefined {
return this.size === 0 ? undefined : this.peekBack()
}
getFirst(): E {
if (this.size === 0) throw this.empty('getFirst')
return this.peekFront()
}
getLast(): E {
if (this.size === 0) throw this.empty('getLast')
return this.peekBack()
}
override has(element: E): boolean {
if (!this.storesEmpty && (element === null || element === undefined)) return false
const walker = this.walker(false)
while (walker.hasNext()) if (sameValueZero(walker.next(), element)) return true
return false
}
override toArray(): E[] {
const walker = this.walker(false)
const out: E[] = []
while (walker.hasNext()) out.push(walker.next())
return out
}
// ── Iteration and equality ──
[Symbol.iterator](): IterableIterator<E> {
return new DequeIterator(
this.walker(false),
this.changes,
`${this.className}[Symbol.iterator]().next`,
)
}
cursor(): Cursor<E> {
return new DequeCursor(this.walker(false), this.changes, `${this.className}.cursor`)
}
override equals(other: unknown): boolean {
return other === this || other === this.owner
}
private insertChecked(element: E, method: string, front: boolean): void {
if (!this.storesEmpty) requireElement(element, this, method)
if (front) this.insertFront(element, this, method)
else this.insertBack(element, this, method)
}
private empty(method: string): NoSuchElementError {
return new NoSuchElementError(`${this.className}.${method}: the ${this.noun} is empty`)
}
}
|