-
Notifications
You must be signed in to change notification settings - Fork 0
/
range.ts
293 lines (260 loc) · 9.08 KB
/
range.ts
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
import { fEqOr } from "./bop"
import { also } from "./effect"
import { MathEx } from "./math"
import { Nums } from "./number"
declare const TheRange: unique symbol
interface RangeBase {
[TheRange]: true
}
class RangeBase { }
export type Ranges<T> = RangeAll | TRange<T> | TRangeTo<T> | TRangeFrom<T> | TRangeEq<T> | TRangeToEq<T>
export type NRanges = RangeAll | NRange | NRangeTo | NRangeFrom | NRangeEq | NRangeToEq
export type IRanges = RangeAll | IRange | IRangeTo | IRangeFrom | IRangeEq | IRangeToEq
/** `..` */
export class RangeAll extends RangeBase {
constructor() { super() }
}
/** `from..to` */
export class TRange<T> extends RangeBase {
constructor(public from: T, public to: T) { super() }
}
/** `..to` */
export class TRangeTo<T> extends RangeBase {
constructor(public to: T) { super() }
}
/** `from..` */
export class TRangeFrom<T> extends RangeBase {
constructor(public from: T) { super() }
}
/** `from..=to` */
export class TRangeEq<T> extends RangeBase {
constructor(public from: T, public to: T) { super() }
}
/** `..=to` */
export class TRangeToEq<T> extends RangeBase {
constructor(public to: T) { super() }
}
function rangeIter(step: number, from: number, to: number) {
if (from === -Infinity) from = Number.MIN_SAFE_INTEGER
if (to === Infinity) to = Number.MAX_SAFE_INTEGER
let c = -1
if (from > to) {
step = -step
c = 1
}
return {
[Symbol.iterator](): IterableIterator<number> {
return this
},
next(): IteratorResult<number> {
if (MathEx.cmp(from, to) === c)
return also({ done: false, value: from }, () => from += step)
return { done: true } as IteratorResult<number>
}
}
}
function rangeEqIter(step: number, from: number, to: number) {
if (from === -Infinity) from = Number.MIN_SAFE_INTEGER
if (to === Infinity) to = Number.MAX_SAFE_INTEGER
let c = -1
if (from > to) {
step = -step
c = 1
}
return {
[Symbol.iterator](): IterableIterator<number> {
return this
},
next(): IteratorResult<number> {
if (fEqOr(MathEx.cmp(from, to), c, 0))
return also({ done: false, value: from }, () => from += step)
return { done: true } as IteratorResult<number>
}
}
}
/** `from..to` */
export class NRange extends TRange<number> implements Iterable<number> {
constructor(from: number, to: number) { super(from, to) }
[Symbol.iterator]() {
return rangeIter(1, this.from, this.to)
}
}
/** `..to` */
export class NRangeTo extends TRangeTo<number> implements Iterable<number> {
constructor(to: number) { super(to) }
[Symbol.iterator]() {
return rangeIter(1, 0, this.to)
}
}
/** `from..` */
export class NRangeFrom extends TRangeFrom<number> implements Iterable<number> {
constructor(from: number) { super(from) }
[Symbol.iterator]() {
return rangeIter(1, this.from, Infinity)
}
}
/** `from..=to` */
export class NRangeEq extends TRangeEq<number> implements Iterable<number> {
constructor(from: number, to: number) { super(from, to) }
[Symbol.iterator]() {
return rangeEqIter(1, this.from, this.to)
}
}
/** `..=to` */
export class NRangeToEq extends TRangeToEq<number> implements Iterable<number>{
constructor(to: number) { super(to) }
[Symbol.iterator]() {
return rangeEqIter(1, 0, this.to)
}
}
function rangeIntIter(step: bigint, from: bigint, to?: bigint) {
if (to == null) {
return {
next(): IteratorResult<bigint> {
return also({ done: false, value: from }, () => from += step)
}
}
}
let c = -1n
if (from > to) {
step = -step
c = 1n
}
return {
[Symbol.iterator](): IterableIterator<bigint> {
return this
},
next(): IteratorResult<bigint> {
if (MathEx.cmp(from, to) === c)
return also({ done: false, value: from }, () => from += step)
return { done: true } as IteratorResult<bigint>
}
}
}
function rangeIntEqIter(step: bigint, from: bigint, to: bigint) {
let c = -1n
if (from > to) {
step = -step
c = 1n
}
return {
[Symbol.iterator](): IterableIterator<bigint> {
return this
},
next(): IteratorResult<bigint> {
if (fEqOr(MathEx.cmp(from, to), c, 0n))
return also({ done: false, value: from }, () => from += step)
return { done: true } as IteratorResult<bigint>
}
}
}
/** `from..to` */
export class IRange extends TRange<bigint> implements Iterable<bigint> {
constructor(from: bigint, to: bigint) { super(from, to) }
[Symbol.iterator]() {
return rangeIntIter(1n, this.from, this.to)
}
}
/** `..to` */
export class IRangeTo extends TRangeTo<bigint> {
constructor(to: bigint) { super(to) }
[Symbol.iterator]() {
return rangeIntIter(1n, 0n, this.to)
}
}
/** `from..` */
export class IRangeFrom extends TRangeFrom<bigint> implements Iterable<bigint>{
constructor(from: bigint) { super(from) }
[Symbol.iterator]() {
return rangeIntIter(1n, this.from)
}
}
/** `from..=to` */
export class IRangeEq extends TRangeEq<bigint> implements Iterable<bigint>{
constructor(from: bigint, to: bigint) { super(from, to) }
[Symbol.iterator]() {
return rangeIntEqIter(1n, this.from, this.to)
}
}
/** `..=to` */
export class IRangeToEq extends TRangeToEq<bigint> {
constructor(to: bigint) { super(to) }
[Symbol.iterator]() {
return rangeIntEqIter(1n, 0n, this.to)
}
}
const all = new RangeAll
export function rangeAll(): RangeAll {
return all
}
export function range(from: number, to: number): NRange
export function range(from: bigint, to: bigint): IRange
export function range<T>(from: T, to: T): TRange<T>
export function range(from: any, to: any): TRange<any> {
if (typeof from === 'number') return new NRange(from, to)
else if (typeof from === 'bigint') return new IRange(from, to)
return new TRange(from, to)
}
export function rangeTo(to: number): NRangeTo
export function rangeTo(to: bigint): IRangeTo
export function rangeTo<T>(to: T): TRangeTo<T>
export function rangeTo(to: any): TRangeTo<any> {
if (typeof to === 'number') return new NRangeTo(to)
else if (typeof to === 'bigint') return new IRangeTo(to)
return new TRangeTo(to)
}
export function rangeFrom(from: number): NRangeFrom
export function rangeFrom(from: bigint): IRangeFrom
export function rangeFrom<T>(from: T): TRangeFrom<T>
export function rangeFrom(from: any): TRangeFrom<any> {
if (typeof from === 'number') return new NRangeFrom(from)
else if (typeof from === 'bigint') return new IRangeFrom(from)
return new TRangeFrom(from)
}
export function rangeEq(from: number, to: number): NRangeEq
export function rangeEq(from: bigint, to: bigint): IRangeEq
export function rangeEq<T>(from: T, to: T): TRangeEq<T>
export function rangeEq(from: any, to: any): TRangeEq<any> {
if (typeof from === 'number') return new NRangeEq(from, to)
else if (typeof from === 'bigint') return new IRangeEq(from, to)
return new TRangeEq(from, to)
}
export function rangeToEq(to: number): NRangeToEq
export function rangeToEq(to: bigint): IRangeToEq
export function rangeToEq<T>(to: T): TRangeToEq<T>
export function rangeToEq(to: any): TRangeToEq<any> {
if (typeof to === 'number') return new NRangeToEq(to)
else if (typeof to === 'bigint') return new IRangeToEq(to)
return new TRangeToEq(to)
}
export function isRanges<T>(v: any): v is Ranges<T> {
return v instanceof RangeBase
}
export namespace Ranges {
export function toTuple(range: RangeAll): []
export function toTuple(range: TRange<number> | TRangeEq<number>): [from: number, to: number]
export function toTuple(range: TRange<bigint> | TRangeEq<bigint>): [from: bigint, to: bigint]
export function toTuple(range: TRangeFrom<number>): [from: number]
export function toTuple(range: TRangeFrom<bigint>): [from: bigint]
export function toTuple(range: TRangeTo<number> | TRangeToEq<number>): [from: 0, to: number]
export function toTuple(range: TRangeTo<bigint> | TRangeToEq<bigint>): [from: 0n, to: bigint]
export function toTuple(range: Ranges<number>): [from?: number, to?: number]
export function toTuple(range: Ranges<bigint>): [from?: bigint, to?: bigint]
export function toTuple(range: Ranges<any>): [from?: any, to?: any] {
if (range instanceof RangeAll) {
return []
} else if (range instanceof TRange) {
return [range.from, range.to]
} else if (range instanceof TRangeEq) {
return [range.from, range.to + Nums.one(range.to)]
} else if (range instanceof TRangeFrom) {
return [range.from]
} else if (range instanceof TRangeTo) {
return [Nums.zero(range.to), range.to]
} else if (range instanceof TRangeToEq) {
return [Nums.zero(range.to), range.to + Nums.one(range.to)]
} else {
throw new TypeError('Unsupported range')
}
}
}