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 | 1771x 1771x 1771x 1771x 1771x 1914x 2258x 1202x 19x 123x 1603x 330x 11x 194x 23x 23x 56x 263x 92x 96x 53x 53x 53x 186x 186x 205x 1x 1x 1x 1x 1x 145x 15x 15x 30x 30x 169x 35x 32x 15x 15x 138x 130x 153x 138x 140x 30x 45x | // A read-only list over another list. Indexes and ranges are checked under the read-only list's
// own name, then reads go to the source; sub-lists and reversed views are read-only in turn, and
// every change — on the list, its views and its cursors — is refused before its arguments are
// looked at.
import type { Comparator } from '../../../functions/comparators'
import type { List, ListCursor, ReadonlyList } from '../../../interfaces/list'
import { checkPosition } from '../../checks'
import { type HashFunction, hashCodeSymbol } from '../../equality'
import { apply } from '../../intrinsics'
import { AbstractList } from '../../skeletons/abstract-list'
import { ReadOnlyListCursor } from './read-only-cursor'
import { refuse } from './refuse'
import { registerReadOnly } from './registry'
import { hideSource, ReadOnlyIterator, SOURCE } from './source-key'
import type { ReadOnlyTags } from './tags'
/** The roles a read-only list can play. @internal */
export type ListRole = 'list' | 'subList' | 'reversedList'
/**
* A read-only view of `source`, printed with the tag of its `role`. Equality and hashing are the
* source's; iteration, `forEach` and cursors fail fast as the source's do.
*
* @internal
*/
export class ReadOnlyList<E> extends AbstractList<E> {
declare readonly [SOURCE]: ReadonlyList<E>
private readonly tags: ReadOnlyTags
private readonly role: ListRole
/**
* @param exact - The class whose instances register as a list; an instance of any other class,
* such as a subclass defined outside the library, is not registered.
*/
constructor(
source: ReadonlyList<E>,
tags: ReadOnlyTags,
role: ListRole = 'list',
exact: unknown = ReadOnlyList,
) {
super()
hideSource(this, source)
this.tags = tags
this.role = role
registerReadOnly(this, new.target === exact ? 'list' : undefined, source, tags[role])
}
get [Symbol.toStringTag](): string {
return this.tags[this.role]
}
get size(): number {
return this[SOURCE].size
}
override get revision(): number {
return this[SOURCE].revision
}
override isEmpty(): boolean {
return this[SOURCE].isEmpty()
}
getAt(index: number): E {
return this[SOURCE].get(index)
}
override [Symbol.iterator](): IterableIterator<E> {
return new ReadOnlyIterator(this[SOURCE][Symbol.iterator]())
}
override indexOf(element: E): number {
return this[SOURCE].indexOf(element)
}
override lastIndexOf(element: E): number {
return this[SOURCE].lastIndexOf(element)
}
override toArray(): E[] {
return this[SOURCE].toArray()
}
override forEach(
action: (element: E, index: number, list: List<E>) => void,
thisArg?: unknown,
): void {
this.requireFunction(action, 'forEach', 'action')
this[SOURCE].forEach((element, index) => {
apply(action, thisArg, [element, index, this])
})
}
override equals(other: unknown): boolean {
return other === this || this[SOURCE].equals(other)
}
override [hashCodeSymbol](hash: HashFunction): number {
return this[SOURCE][hashCodeSymbol](hash)
}
override cursor(): ListCursor<E> {
return new ReadOnlyListCursor(this[SOURCE].listCursor(0), `${this.className}.cursor`)
}
override listCursor(index?: number): ListCursor<E> {
const where = `${this.className}.listCursor`
const position = index === undefined ? 0 : checkPosition(index, this.size, where)
return new ReadOnlyListCursor(this[SOURCE].listCursor(position), where)
}
subList(from: number, to: number): List<E> {
this.checkSubList(from, to)
return new ReadOnlyList(this[SOURCE].subList(from, to), this.tags, 'subList')
}
reversed(): List<E> {
return new ReadOnlyList(this[SOURCE].reversed(), this.tags, 'reversedList')
}
// ── Every change is refused ──
setAt(_index: number, _element: E): void {
throw refuse(`${this.className}.set`)
}
insertAt(_index: number, _element: E): void {
throw refuse(`${this.className}.insert`)
}
insertItems(_index: number, _elements: readonly E[]): void {
throw refuse(`${this.className}.insertAll`)
}
removeAtIndex(_index: number): E {
throw refuse(`${this.className}.removeAt`)
}
removeRange(_from: number, _to: number): void {
throw refuse(`${this.className}.clear`)
}
override add(_element: E): this {
throw refuse(`${this.className}.add`)
}
override addFirst(_element: E): this {
throw refuse(`${this.className}.addFirst`)
}
override addLast(_element: E): this {
throw refuse(`${this.className}.addLast`)
}
override insert(_index: number, _element: E): this {
throw refuse(`${this.className}.insert`)
}
override insertAll(_index: number, _elements: Iterable<E>): boolean {
throw refuse(`${this.className}.insertAll`)
}
override addAll(_elements: Iterable<E>): boolean {
throw refuse(`${this.className}.addAll`)
}
override set(_index: number, _element: E): E {
throw refuse(`${this.className}.set`)
}
override removeAt(_index: number): E {
throw refuse(`${this.className}.removeAt`)
}
override removeFirst(): E {
throw refuse(`${this.className}.removeFirst`)
}
override removeLast(): E {
throw refuse(`${this.className}.removeLast`)
}
override delete(_element: E): boolean {
throw refuse(`${this.className}.delete`)
}
override clear(): void {
throw refuse(`${this.className}.clear`)
}
override removeAll(_elements: Iterable<E>): boolean {
throw refuse(`${this.className}.removeAll`)
}
override retainAll(_elements: Iterable<E>): boolean {
throw refuse(`${this.className}.retainAll`)
}
override removeIf(_predicate: (element: E, index: number, list: List<E>) => unknown): boolean {
throw refuse(`${this.className}.removeIf`)
}
override replaceAll(_op: (element: E, index: number, list: List<E>) => E): void {
throw refuse(`${this.className}.replaceAll`)
}
override sort(_comparator?: Comparator<E>): this {
throw refuse(`${this.className}.sort`)
}
}
|