-
Notifications
You must be signed in to change notification settings - Fork 30
/
SortedDictFast.ts
237 lines (203 loc) · 6.06 KB
/
SortedDictFast.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
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable generator-star-spacing */
import { SortedListFast } from './SortedListFast'
/**
* 有序字典.
* 模拟python的`sortedcontainers.SortedDict`.
*/
class SortedDictFast<K = number, V = unknown> {
private readonly _sl: SortedListFast<K> = new SortedListFast()
private readonly _dict: Map<K, V> = new Map()
constructor()
constructor(iterable: Iterable<readonly [K, V]>)
constructor(compareFn: (a: K, b: K) => number)
constructor(iterable: Iterable<readonly [K, V]>, compareFn: (a: K, b: K) => number)
constructor(compareFn: (a: K, b: K) => number, iterable: Iterable<readonly [K, V]>)
constructor(
arg1?: Iterable<readonly [K, V]> | ((a: K, b: K) => number),
arg2?: Iterable<readonly [K, V]> | ((a: K, b: K) => number)
) {
let defaultCompareFn = (a: any, b: any) => a - b
let defaultData: (readonly [K, V])[] = []
if (arg1 !== void 0) {
if (typeof arg1 === 'function') {
defaultCompareFn = arg1
} else {
defaultData = [...arg1]
}
}
if (arg2 !== void 0) {
if (typeof arg2 === 'function') {
defaultCompareFn = arg2
} else {
defaultData = [...arg2]
}
}
this._sl = new SortedListFast(defaultCompareFn)
defaultData.forEach(([k, v]) => {
this.set(k, v)
})
}
set(key: K, value: V): this {
if (!this._dict.has(key)) this._sl.add(key)
this._dict.set(key, value)
return this
}
setDefault(key: K, defaultValue: V): V {
if (this._dict.has(key)) return this._dict.get(key)!
this._sl.add(key)
this._dict.set(key, defaultValue)
return defaultValue
}
has(key: K): boolean {
return this._dict.has(key)
}
get(key: K): V | undefined {
return this._dict.get(key)
}
delete(key: K, equals?: (a: K, b: K) => boolean): boolean {
if (!this._dict.has(key)) return false
this._sl.discard(key, equals)
this._dict.delete(key)
return true
}
pop(key: K, equals?: (a: K, b: K) => boolean): V | undefined {
if (!this._dict.has(key)) return void 0
this._sl.discard(key, equals)
const value = this._dict.get(key)!
this._dict.delete(key)
return value
}
popItem(index = -1): [K, V] | undefined {
if (!this._dict.size) return void 0
const key = this._sl.pop(index)!
const value = this._dict.get(key)!
this._dict.delete(key)
return [key, value]
}
peekItem(index = -1): [K, V] | undefined {
if (!this._dict.size) return void 0
const key = this._sl.at(index)!
const value = this._dict.get(key)!
return [key, value]
}
peekMinItem(): [K, V] | undefined {
if (!this._dict.size) return void 0
const key = this._sl.min!
const value = this._dict.get(key)!
return [key, value]
}
peekMaxItem(): [K, V] | undefined {
if (!this._dict.size) return void 0
const key = this._sl.max!
const value = this._dict.get(key)!
return [key, value]
}
forEach(callbackfn: (value: V, key: K) => void): void {
this._sl.forEach(key => {
callbackfn(this._dict.get(key)!, key)
})
}
enumerate(start: number, end: number, f: (key: K, value: V) => void, erase = false): void {
this._sl.enumerate(
start,
end,
key => {
f(key, this._dict.get(key)!)
if (erase) this._dict.delete(key)
},
erase
)
}
/**
* 返回`key`在有序字典中的位置.
*/
bisectLeft(key: K): number {
return this._sl.bisectLeft(key)
}
/**
* 返回`key`在有序字典中的位置.
*/
bisectRight(key: K): number {
return this._sl.bisectRight(key)
}
floor(key: K): [K, V] | undefined {
const floorKey = this._sl.floor(key)
if (floorKey === void 0) return void 0
return [floorKey, this._dict.get(floorKey)!]
}
ceiling(key: K): [K, V] | undefined {
const ceilingKey = this._sl.ceiling(key)
if (ceilingKey === void 0) return void 0
return [ceilingKey, this._dict.get(ceilingKey)!]
}
lower(key: K): [K, V] | undefined {
const lowerKey = this._sl.lower(key)
if (lowerKey === void 0) return void 0
return [lowerKey, this._dict.get(lowerKey)!]
}
higher(key: K): [K, V] | undefined {
const higherKey = this._sl.higher(key)
if (higherKey === void 0) return void 0
return [higherKey, this._dict.get(higherKey)!]
}
clear(): void {
this._sl.clear()
this._dict.clear()
}
toString(): string {
const sb: string[] = [`SortedDict(${this.size}) {`]
this.forEach((v, k) => {
sb.push(` ${k} => ${v},`)
})
sb.push('}')
return sb.join('\n')
}
/**
* 返回一个迭代器,用于遍历键的范围在 `[start, end)` 内的所有键值对.
*/
*islice(start: number, end: number, reverse = false): IterableIterator<[K, V]> {
const keys = this._sl.iSlice(start, end, reverse)
for (const key of keys) {
yield [key, this._dict.get(key)!]
}
}
/**
* 返回一个迭代器,用于遍历键的范围在 `[min, max] 闭区间`内的所有键值对.
*/
*irange(min: K, max: K, reverse = false): IterableIterator<[K, V]> {
const keys = this._sl.iRange(min, max, reverse)
for (const key of keys) {
yield [key, this._dict.get(key)!]
}
}
*keys(): IterableIterator<K> {
yield* this._sl
}
*values(): IterableIterator<V> {
for (const key of this._sl) {
yield this._dict.get(key)!
}
}
*entries(): IterableIterator<[K, V]> {
for (const key of this._sl) {
yield [key, this._dict.get(key)!]
}
}
*[Symbol.iterator](): IterableIterator<[K, V]> {
for (const key of this._sl) {
yield [key, this._dict.get(key)!]
}
}
get size(): number {
return this._dict.size
}
}
export { SortedDictFast }
if (require.main === module) {
const mp = new SortedDictFast<number, number>()
mp.set(1, 1)
mp.set(2, 2)
mp.set(3, 3)
console.log(mp.toString())
}