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 | /**
* The sorted-set interfaces: sets kept in the order of a comparator, with nearest-match queries
* and live range views.
*
* @packageDocumentation
*/
import type { Comparator } from '../functions/comparators'
import type { Cursor } from './collection'
import type { ReadonlySequencedSet, SequencedSet } from './sequenced-set'
import type { ReadonlySetCollection, SetCollection } from './set'
/**
* The inclusive flags of `subSet` and `subMap`.
*
* @example
* ```ts
* set.subSet(1, 5, { fromInclusive: false, toInclusive: true }) // members in (1, 5]
* map.subMap(1, 5, { toInclusive: true }) // mappings with keys in [1, 5]
* ```
*/
export interface SubSetOptions {
/** Whether `from` itself is in range. Default `true`. */
fromInclusive?: boolean | undefined
/** Whether `to` itself is in range. Default `false`. */
toInclusive?: boolean | undefined
}
/**
* The inclusive flag of `headSet`, `tailSet`, `headMap` and `tailMap`.
*
* @example
* ```ts
* set.headSet(5, { inclusive: true }) // members up to and including 5
* map.tailMap(5, { inclusive: false }) // mappings with keys after 5
* ```
*/
export interface BoundOptions {
/**
* Whether the bound itself is in range. Default `false` for `headSet` and `headMap`, `true` for
* `tailSet` and `tailMap`.
*/
inclusive?: boolean | undefined
}
/**
* The read-only view of a sorted set: a set whose members are kept in the order of its comparator,
* with the ends, nearest-match queries and live range views. Two members are the same member when
* the comparator returns `0` for them.
*
* "Before" and "after" follow the set's own order, which is descending on a reversed view.
*
* @example
* ```ts
* function nextSlot(slots: ReadonlySortedSet<number>, at: number): number | undefined {
* return slots.ceiling(at)
* }
* ```
*/
export interface ReadonlySortedSet<E> extends ReadonlySequencedSet<E> {
/**
* The number of members. Time: O(1) on a set; O(k + log n) on a range view holding k of the
* set's n members, which counts the members in range.
*/
readonly size: number
/**
* Returns whether there are no members.
*
* Time: O(1) on a set; O(log n) on a range view, which looks for one member in range.
*
* @returns `size === 0`.
*/
isEmpty(): boolean
/**
* Returns an iterator over the members in this set's order.
*
* Time: O(log n) to create; O(1) amortised per step.
*
* @returns A new iterator.
* @throws ConcurrentModificationError (from `next()`) when members are added or removed after
* the iterator is created, other than through its own cursor.
*/
[Symbol.iterator](): IterableIterator<E>
/**
* Returns a cursor positioned before the first member.
*
* Time: O(log n) to create; O(1) amortised per step.
*
* @returns A new cursor.
*/
cursor(): Cursor<E>
/**
* Returns an iterator over the members, as `values()` does.
*
* Time: O(log n) to create; O(1) amortised per step.
*
* @returns A new iterator.
*/
keys(): IterableIterator<E>
/**
* Returns an iterator over the members in this set's order.
*
* Time: O(log n) to create; O(1) amortised per step.
*
* @returns A new iterator.
*/
values(): IterableIterator<E>
/**
* Returns an iterator over `[member, member]` pairs in this set's order.
*
* Time: O(log n) to create; O(1) amortised per step.
*
* @returns A new iterator.
*/
entries(): IterableIterator<[E, E]>
/**
* Calls `action(member, member, set)` for each member in order, with `this` set to `thisArg`.
*
* Time: O(n) plus the calls.
*
* @param action - Called with each member twice and the set.
* @param thisArg - The `this` value for `action`.
* @throws TypeError when `action` is not a function.
*/
forEach(
action: (element: E, same: E, set: ReadonlySetCollection<E>) => void,
thisArg?: unknown,
): void
/**
* Returns the comparator that orders the set, or `undefined` for natural order; a reversed view
* returns a function that orders in reverse.
*
* Time: O(1).
*
* @returns The comparator, or `undefined`.
*/
comparator(): Comparator<E> | undefined
/**
* Returns the greatest member before `element`, or `undefined` when there is none.
*
* Time: O(log n).
*
* @param element - The value to search from; it need not be a member.
* @returns The member, or `undefined`.
*/
lower(element: E): E | undefined
/**
* Returns the greatest member equal to or before `element`, or `undefined` when there is none.
*
* Time: O(log n).
*
* @param element - The value to search from; it need not be a member.
* @returns The member, or `undefined`.
*/
floor(element: E): E | undefined
/**
* Returns the least member equal to or after `element`, or `undefined` when there is none.
*
* Time: O(log n).
*
* @param element - The value to search from; it need not be a member.
* @returns The member, or `undefined`.
*/
ceiling(element: E): E | undefined
/**
* Returns the least member after `element`, or `undefined` when there is none.
*
* Time: O(log n).
*
* @param element - The value to search from; it need not be a member.
* @returns The member, or `undefined`.
*/
higher(element: E): E | undefined
/**
* Returns a live view of the members from `from` (inclusive by default) to `to` (exclusive by
* default).
*
* Time: O(log n).
*
* @param from - The low end, in this set's order.
* @param to - The high end, in this set's order.
* @param options - The inclusive flags.
* @returns The view.
*/
subSet(from: E, to: E, options?: SubSetOptions | null): ReadonlySortedSet<E>
/**
* Returns a live view of the members before `to` (exclusive by default).
*
* Time: O(log n).
*
* @param to - The high end, in this set's order.
* @param options - The inclusive flag.
* @returns The view.
*/
headSet(to: E, options?: BoundOptions | null): ReadonlySortedSet<E>
/**
* Returns a live view of the members from `from` on (inclusive by default).
*
* Time: O(log n).
*
* @param from - The low end, in this set's order.
* @param options - The inclusive flag.
* @returns The view.
*/
tailSet(from: E, options?: BoundOptions | null): ReadonlySortedSet<E>
/**
* Returns a live view of this set in the opposite order.
*
* Time: O(1).
*
* @returns The reversed view.
*/
reversed(): ReadonlySortedSet<E>
}
/**
* A set kept in the order of a comparator, with the ends, nearest-match queries, removal from
* either end and live range views. Two members are the same member when the comparator returns
* `0` for them. `addFirst` and `addLast` throw `UnsupportedOperationError`: the order decides
* where a member goes.
*
* @example
* ```ts
* function takeUpTo<E>(set: SortedSet<E>, limit: E): E[] {
* const taken = set.headSet(limit, { inclusive: true }).toArray()
* set.headSet(limit, { inclusive: true }).clear()
* return taken
* }
* ```
*/
export interface SortedSet<E> extends SequencedSet<E>, ReadonlySortedSet<E> {
/**
* Calls `action(member, member, set)` for each member in order, with `this` set to `thisArg`.
*
* Time: O(n) plus the calls.
*
* @param action - Called with each member twice and the set.
* @param thisArg - The `this` value for `action`.
* @throws TypeError when `action` is not a function.
*/
forEach(action: (element: E, same: E, set: SetCollection<E>) => void, thisArg?: unknown): void
/**
* Removes and returns the first member, or returns `undefined` when the set is empty.
*
* Time: O(log n).
*
* @returns The removed member, or `undefined`.
*/
pollFirst(): E | undefined
/**
* Removes and returns the last member, or returns `undefined` when the set is empty.
*
* Time: O(log n).
*
* @returns The removed member, or `undefined`.
*/
pollLast(): E | undefined
/**
* Returns a live view of the members from `from` (inclusive by default) to `to` (exclusive by
* default); changes through the view reach this set.
*
* Time: O(log n).
*
* @param from - The low end, in this set's order.
* @param to - The high end, in this set's order.
* @param options - The inclusive flags.
* @returns The view.
*/
subSet(from: E, to: E, options?: SubSetOptions | null): SortedSet<E>
/**
* Returns a live view of the members before `to` (exclusive by default); changes through the
* view reach this set.
*
* Time: O(log n).
*
* @param to - The high end, in this set's order.
* @param options - The inclusive flag.
* @returns The view.
*/
headSet(to: E, options?: BoundOptions | null): SortedSet<E>
/**
* Returns a live view of the members from `from` on (inclusive by default); changes through the
* view reach this set.
*
* Time: O(log n).
*
* @param from - The low end, in this set's order.
* @param options - The inclusive flag.
* @returns The view.
*/
tailSet(from: E, options?: BoundOptions | null): SortedSet<E>
/**
* Returns a live view of this set in the opposite order; changes through the view reach this
* set.
*
* Time: O(1).
*
* @returns The reversed view.
*/
reversed(): SortedSet<E>
}
|