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 | 23307x 32141x 254x 23053x 23053x 20308x 16268x 4485x 4485x 4472x 4342x 4342x 4318x 583x 583x 541x 4368x 4368x 4341x 4296x 4296x 4260x 4319x 4319x 276x 339x 339x 307x 257x 257x 220x 318x 318x 278x | /**
* Unmodifiable views: read-only windows onto collections their owner keeps changing.
*
* @packageDocumentation
*/
import type { ReadonlyCollection } from '../interfaces/collection'
import type { ReadonlyList, ReadonlySequencedCollection } from '../interfaces/list'
import type { ReadonlyMapCollection } from '../interfaces/map'
import type { ReadonlySequencedMap } from '../interfaces/sequenced-map'
import type { ReadonlySequencedSet } from '../interfaces/sequenced-set'
import type { ReadonlySetCollection } from '../interfaces/set'
import type { ReadonlySortedMap } from '../interfaces/sorted-map'
import type { ReadonlySortedSet } from '../interfaces/sorted-set'
import {
isBranded,
LIST_BRAND,
MAP_COLLECTION_BRAND,
SEQUENCED_COLLECTION_BRAND,
SEQUENCED_MAP_BRAND,
SET_COLLECTION_BRAND,
SORTED_MAP_BRAND,
SORTED_SET_BRAND,
} from '../internal/brand'
import { describeValue } from '../internal/checks'
import { COLLECTION_BRAND } from '../internal/output'
import {
ReadOnlyCollection,
ReadOnlySequencedCollection,
} from '../internal/views/read-only/read-only-collection'
import { ReadOnlyList } from '../internal/views/read-only/read-only-list'
import {
ReadOnlyMap,
ReadOnlySequencedMap,
ReadOnlySortedMap,
} from '../internal/views/read-only/read-only-map'
import {
ReadOnlySequencedSet,
ReadOnlySet,
ReadOnlySortedSet,
} from '../internal/views/read-only/read-only-set'
import { type ReadOnlyKind, readOnlyParts } from '../internal/views/read-only/registry'
import { UNMODIFIABLE_TAGS } from '../internal/views/read-only/tags'
/** Throws `TypeError` unless `value` carries every brand in `brands`. */
function requireFamily(
value: unknown,
brands: readonly symbol[],
where: string,
what: string,
family: string,
): void {
for (const brand of brands) {
if (!isBranded(value, brand)) {
throw new TypeError(
`${where}: ${what} is not ${family} from this library (${describeValue(value)})`,
)
}
}
}
/**
* `value` itself when it is a view the function of `kind` printing `tag` would return; otherwise a
* new view from `make`, over the source of any other read-only view this copy built (keeping the
* way that view hands out members), or over `value`.
*/
function unwrapOrWrap<T, M>(
value: T,
kind: ReadOnlyKind,
tag: string,
make: (source: T, reveal: ((member: M) => M) | undefined) => T,
): T {
const parts = readOnlyParts(value)
if (parts === undefined) return make(value, undefined)
if (parts.unmodifiable && parts.kind === kind && parts.printed === tag) return value
return make(parts.source as T, parts.reveal as ((member: M) => M) | undefined)
}
/**
* Returns a read-only view of `collection`: reads, iteration and cursors go to the collection, so
* the owner's changes show through, and every method that would change it throws
* `UnsupportedOperationError`. The view compares by identity (`equals` is `true` only for itself),
* because a collection of any family can be wrapped.
*
* When `collection` is already a view this function returns (printed as `UnmodifiableCollection`),
* it is returned unchanged. When it is any other unmodifiable view or immutable collection from
* this copy of the library, the new view reads that collection's source instead, and entries it
* hands out stay read-only, so read-only wrappers never nest.
*
* Time: O(1); each operation costs what the collection's costs.
*
* @param collection - A collection from this library.
* @returns `collection` itself, or an unmodifiable view printed as `UnmodifiableCollection`.
* @throws TypeError when `collection` is not a collection from this library.
*
* @example
* ```ts
* const queue = new ArrayDeque(['a'])
* const view = unmodifiableCollection(queue)
* queue.push('b')
* view.size // 2
* ```
*/
export function unmodifiableCollection<E>(
collection: ReadonlyCollection<E>,
): ReadonlyCollection<E> {
requireFamily(
collection,
[COLLECTION_BRAND],
'unmodifiableCollection',
'collection',
'a collection',
)
return unwrapOrWrap(
collection,
'collection',
UNMODIFIABLE_TAGS.collection,
(source, reveal: ((member: E) => E) | undefined) =>
new ReadOnlyCollection(source, UNMODIFIABLE_TAGS.collection, reveal),
)
}
/**
* Returns a read-only view of a sequenced collection: as {@link unmodifiableCollection}, with the
* ends (`getFirst`, `getLast`) and a reversed view. It compares by identity.
*
* When `collection` is already a view this function returns (printed as
* `UnmodifiableSequencedCollection`), it is returned unchanged. When it is any other unmodifiable
* view or immutable collection from this copy of the library, the new view reads that collection's
* source instead, and entries it hands out stay read-only, so read-only wrappers never nest.
*
* Time: O(1); each operation costs what the collection's costs.
*
* @param collection - A sequenced collection from this library (a list, deque or sequenced set).
* @returns `collection` itself, or an unmodifiable view printed as
* `UnmodifiableSequencedCollection`.
* @throws TypeError when `collection` is not a sequenced collection from this library.
*
* @example
* ```ts
* const deque = new ArrayDeque([1, 2, 3])
* unmodifiableSequencedCollection(deque).getLast() // 3
* ```
*/
export function unmodifiableSequencedCollection<E>(
collection: ReadonlySequencedCollection<E>,
): ReadonlySequencedCollection<E> {
requireFamily(
collection,
[COLLECTION_BRAND, SEQUENCED_COLLECTION_BRAND],
'unmodifiableSequencedCollection',
'collection',
'a sequenced collection',
)
return unwrapOrWrap(
collection,
'sequenced-collection',
UNMODIFIABLE_TAGS.sequencedCollection,
(source, reveal: ((member: E) => E) | undefined) =>
new ReadOnlySequencedCollection(source, UNMODIFIABLE_TAGS.sequencedCollection, reveal),
)
}
/**
* Returns a read-only view of `list`: indexes, `subList`, `reversed()` and cursors read the list,
* so the owner's changes show through, and every change — on the view, its sub-lists and reversed
* views, and its cursors — throws `UnsupportedOperationError` before its arguments are checked.
* The view equals, and hashes like, the list.
*
* When `list` is already a view this function returns (printed as `UnmodifiableList`, its sub-lists
* and reversed views included), it is returned unchanged. When it is any other unmodifiable view or
* immutable collection from this copy of the library, the new view reads that collection's source
* instead, and entries it hands out stay read-only, so read-only wrappers never nest.
*
* Time: O(1); each operation costs what the list's costs.
*
* @param list - A list from this library.
* @returns `list` itself, or an unmodifiable view printed as `UnmodifiableList`.
* @throws TypeError when `list` is not a list from this library.
*
* @example
* ```ts
* const items = new ArrayList(['a'])
* const view = unmodifiableList(items)
* items.add('b')
* view.get(1) // 'b'
* ```
*/
export function unmodifiableList<E>(list: ReadonlyList<E>): ReadonlyList<E> {
requireFamily(list, [LIST_BRAND], 'unmodifiableList', 'list', 'a list')
// Lists never register a `reveal`: their members are handed out as they are.
return unwrapOrWrap(
list,
'list',
UNMODIFIABLE_TAGS.list,
(source) => new ReadOnlyList(source, UNMODIFIABLE_TAGS),
)
}
/**
* Returns a read-only view of `set`: membership, iteration and cursors go to the set, so the
* owner's changes show through, and every method that would change it throws
* `UnsupportedOperationError`. The view equals, and hashes like, the set.
*
* When `set` is already a view this function returns (printed as `UnmodifiableSet`), it is returned
* unchanged. When it is any other unmodifiable view or immutable collection from this copy of the
* library, the new view reads that collection's source instead, and entries it hands out stay
* read-only, so read-only wrappers never nest.
*
* Time: O(1); each operation costs what the set's costs.
*
* @param set - A set from this library.
* @returns `set` itself, or an unmodifiable view printed as `UnmodifiableSet`.
* @throws TypeError when `set` is not a set from this library.
*
* @example
* ```ts
* const tags = new HashSet(['a'])
* unmodifiableSet(tags).has('a') // true
* ```
*/
export function unmodifiableSet<E>(set: ReadonlySetCollection<E>): ReadonlySetCollection<E> {
requireFamily(set, [SET_COLLECTION_BRAND], 'unmodifiableSet', 'set', 'a set')
return unwrapOrWrap(
set,
'set',
UNMODIFIABLE_TAGS.set,
(source, reveal: ((member: E) => E) | undefined) =>
new ReadOnlySet(source, UNMODIFIABLE_TAGS.set, reveal),
)
}
/**
* Returns a read-only view of a sequenced set: as {@link unmodifiableSet}, with the ends and a
* reversed view.
*
* When `set` is already a view this function returns (printed as `UnmodifiableSequencedSet`), it is
* returned unchanged. When it is any other unmodifiable view or immutable collection from this copy
* of the library, the new view reads that collection's source instead, and entries it hands out
* stay read-only, so read-only wrappers never nest.
*
* Time: O(1); each operation costs what the set's costs.
*
* @param set - A sequenced set from this library (a `LinkedHashSet`, a sorted set, …).
* @returns `set` itself, or an unmodifiable view printed as `UnmodifiableSequencedSet`.
* @throws TypeError when `set` is not a sequenced set from this library.
*
* @example
* ```ts
* unmodifiableSequencedSet(new LinkedHashSet(['a', 'b'])).getLast() // 'b'
* ```
*/
export function unmodifiableSequencedSet<E>(set: ReadonlySequencedSet<E>): ReadonlySequencedSet<E> {
requireFamily(
set,
[SET_COLLECTION_BRAND, SEQUENCED_COLLECTION_BRAND],
'unmodifiableSequencedSet',
'set',
'a sequenced set',
)
return unwrapOrWrap(
set,
'sequenced-set',
UNMODIFIABLE_TAGS.sequencedSet,
(source, reveal: ((member: E) => E) | undefined) =>
new ReadOnlySequencedSet(source, UNMODIFIABLE_TAGS.sequencedSet, reveal),
)
}
/**
* Returns a read-only view of a sorted set: as {@link unmodifiableSet}, with navigation
* (`floor`, `ceiling`, …), the comparator, and range and reversed views that are unmodifiable too.
*
* When `set` is already a view this function returns (printed as `UnmodifiableSortedSet`), it is
* returned unchanged. When it is any other unmodifiable view or immutable collection from this copy
* of the library, the new view reads that collection's source instead, and entries it hands out
* stay read-only, so read-only wrappers never nest.
*
* Time: O(1); each operation costs what the set's costs.
*
* @param set - A sorted set from this library.
* @returns `set` itself, or an unmodifiable view printed as `UnmodifiableSortedSet`.
* @throws TypeError when `set` is not a sorted set from this library.
*
* @example
* ```ts
* unmodifiableSortedSet(new TreeSet([10, 20, 30])).floor(25) // 20
* ```
*/
export function unmodifiableSortedSet<E>(set: ReadonlySortedSet<E>): ReadonlySortedSet<E> {
requireFamily(set, [SORTED_SET_BRAND], 'unmodifiableSortedSet', 'set', 'a sorted set')
return unwrapOrWrap(
set,
'sorted-set',
UNMODIFIABLE_TAGS.sortedSet,
(source, reveal: ((member: E) => E) | undefined) =>
new ReadOnlySortedSet(source, UNMODIFIABLE_TAGS.sortedSet, reveal),
)
}
/**
* Returns a read-only view of `map`: lookups, iteration and the key, entry and value views read
* the map, so the owner's changes show through, and every method that would change it, its views
* or its entries throws `UnsupportedOperationError`. Entries read the map's entries live. The view
* equals, and hashes like, the map. On an access-ordered `LinkedHashMap`, `get` through the view is
* an access, as on the map.
*
* When `map` is already a view this function returns (printed as `UnmodifiableMap`), it is returned
* unchanged. When it is any other unmodifiable view or immutable collection from this copy of the
* library, the new view reads that collection's source instead, and entries it hands out stay
* read-only, so read-only wrappers never nest.
*
* Time: O(1); each operation costs what the map's costs.
*
* @param map - A map from this library.
* @returns `map` itself, or an unmodifiable view printed as `UnmodifiableMap`.
* @throws TypeError when `map` is not a map from this library.
*
* @example
* ```ts
* const prices = new HashMap([['tea', 3]])
* const view = unmodifiableMap(prices)
* prices.set('tea', 4)
* view.get('tea') // 4
* ```
*/
export function unmodifiableMap<K, V>(
map: ReadonlyMapCollection<K, V>,
): ReadonlyMapCollection<K, V> {
requireFamily(map, [MAP_COLLECTION_BRAND], 'unmodifiableMap', 'map', 'a map')
// Maps never register a `reveal`: they wrap their entries themselves.
return unwrapOrWrap(
map,
'map',
UNMODIFIABLE_TAGS.map,
(source) => new ReadOnlyMap(source, UNMODIFIABLE_TAGS),
)
}
/**
* Returns a read-only view of a sequenced map: as {@link unmodifiableMap}, with the first and last
* entries, a reversed view, and sequenced key, entry and value views.
*
* When `map` is already a view this function returns (printed as `UnmodifiableSequencedMap`), it is
* returned unchanged. When it is any other unmodifiable view or immutable collection from this copy
* of the library, the new view reads that collection's source instead, and entries it hands out
* stay read-only, so read-only wrappers never nest.
*
* Time: O(1); each operation costs what the map's costs.
*
* @param map - A sequenced map from this library (a `LinkedHashMap`, a sorted map, …).
* @returns `map` itself, or an unmodifiable view printed as `UnmodifiableSequencedMap`.
* @throws TypeError when `map` is not a sequenced map from this library.
*
* @example
* ```ts
* unmodifiableSequencedMap(new LinkedHashMap([['a', 1]])).firstEntry()?.key // 'a'
* ```
*/
export function unmodifiableSequencedMap<K, V>(
map: ReadonlySequencedMap<K, V>,
): ReadonlySequencedMap<K, V> {
requireFamily(
map,
[MAP_COLLECTION_BRAND, SEQUENCED_MAP_BRAND],
'unmodifiableSequencedMap',
'map',
'a sequenced map',
)
// Maps never register a `reveal`: they wrap their entries themselves.
return unwrapOrWrap(
map,
'sequenced-map',
UNMODIFIABLE_TAGS.sequencedMap,
(source) => new ReadOnlySequencedMap(source, UNMODIFIABLE_TAGS),
)
}
/**
* Returns a read-only view of a sorted map: as {@link unmodifiableSequencedMap}, with nearest-entry
* navigation (`floorEntry`, `ceilingEntry`, …), the comparator, range views and a sorted key set,
* all unmodifiable too.
*
* When `map` is already a view this function returns (printed as `UnmodifiableSortedMap`), it is
* returned unchanged. When it is any other unmodifiable view or immutable collection from this copy
* of the library, the new view reads that collection's source instead, and entries it hands out
* stay read-only, so read-only wrappers never nest.
*
* Time: O(1); each operation costs what the map's costs.
*
* @param map - A sorted map from this library.
* @returns `map` itself, or an unmodifiable view printed as `UnmodifiableSortedMap`.
* @throws TypeError when `map` is not a sorted map from this library.
*
* @example
* ```ts
* const levels = new TreeMap([[10, 'low'], [50, 'high']])
* unmodifiableSortedMap(levels).floorEntry(30)?.value // 'low'
* ```
*/
export function unmodifiableSortedMap<K, V>(map: ReadonlySortedMap<K, V>): ReadonlySortedMap<K, V> {
requireFamily(map, [SORTED_MAP_BRAND], 'unmodifiableSortedMap', 'map', 'a sorted map')
// Maps never register a `reveal`: they wrap their entries themselves.
return unwrapOrWrap(
map,
'sorted-map',
UNMODIFIABLE_TAGS.sortedMap,
(source) => new ReadOnlySortedMap(source, UNMODIFIABLE_TAGS),
)
}
|