@clayroach/effect 3.19.14-source-capture.6 → 3.19.14-source-capture.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/Utils.js +1 -1
- package/dist/cjs/Utils.js.map +1 -1
- package/dist/cjs/internal/clock.js +1 -1
- package/dist/cjs/internal/clock.js.map +1 -1
- package/dist/esm/Utils.js +1 -1
- package/dist/esm/Utils.js.map +1 -1
- package/dist/esm/internal/clock.js +1 -1
- package/dist/esm/internal/clock.js.map +1 -1
- package/package.json +1 -1
- package/src/Arbitrary.ts +0 -1101
- package/src/Array.ts +0 -3589
- package/src/BigDecimal.ts +0 -1349
- package/src/BigInt.ts +0 -643
- package/src/Boolean.ts +0 -287
- package/src/Brand.ts +0 -360
- package/src/Cache.ts +0 -281
- package/src/Cause.ts +0 -1555
- package/src/Channel.ts +0 -2355
- package/src/ChildExecutorDecision.ts +0 -146
- package/src/Chunk.ts +0 -1495
- package/src/Clock.ts +0 -111
- package/src/Config.ts +0 -542
- package/src/ConfigError.ts +0 -270
- package/src/ConfigProvider.ts +0 -333
- package/src/ConfigProviderPathPatch.ts +0 -100
- package/src/Console.ts +0 -226
- package/src/Context.ts +0 -585
- package/src/Cron.ts +0 -706
- package/src/Data.ts +0 -596
- package/src/DateTime.ts +0 -1686
- package/src/DefaultServices.ts +0 -34
- package/src/Deferred.ts +0 -301
- package/src/Differ.ts +0 -450
- package/src/Duration.ts +0 -1000
- package/src/Effect.ts +0 -14839
- package/src/Effectable.ts +0 -107
- package/src/Either.ts +0 -1040
- package/src/Encoding.ts +0 -195
- package/src/Equal.ts +0 -98
- package/src/Equivalence.ts +0 -235
- package/src/ExecutionPlan.ts +0 -308
- package/src/ExecutionStrategy.ts +0 -119
- package/src/Exit.ts +0 -467
- package/src/FastCheck.ts +0 -9
- package/src/Fiber.ts +0 -744
- package/src/FiberHandle.ts +0 -540
- package/src/FiberId.ts +0 -195
- package/src/FiberMap.ts +0 -656
- package/src/FiberRef.ts +0 -431
- package/src/FiberRefs.ts +0 -204
- package/src/FiberRefsPatch.ts +0 -105
- package/src/FiberSet.ts +0 -491
- package/src/FiberStatus.ts +0 -108
- package/src/Function.ts +0 -1222
- package/src/GlobalValue.ts +0 -53
- package/src/Graph.ts +0 -3732
- package/src/GroupBy.ts +0 -103
- package/src/HKT.ts +0 -45
- package/src/Hash.ts +0 -195
- package/src/HashMap.ts +0 -519
- package/src/HashRing.ts +0 -317
- package/src/HashSet.ts +0 -2346
- package/src/Inspectable.ts +0 -287
- package/src/Iterable.ts +0 -1119
- package/src/JSONSchema.ts +0 -1044
- package/src/KeyedPool.ts +0 -167
- package/src/Layer.ts +0 -1251
- package/src/LayerMap.ts +0 -436
- package/src/List.ts +0 -977
- package/src/LogLevel.ts +0 -285
- package/src/LogSpan.ts +0 -25
- package/src/Logger.ts +0 -702
- package/src/Mailbox.ts +0 -268
- package/src/ManagedRuntime.ts +0 -180
- package/src/Match.ts +0 -1477
- package/src/MergeDecision.ts +0 -95
- package/src/MergeState.ts +0 -172
- package/src/MergeStrategy.ts +0 -107
- package/src/Metric.ts +0 -780
- package/src/MetricBoundaries.ts +0 -69
- package/src/MetricHook.ts +0 -151
- package/src/MetricKey.ts +0 -224
- package/src/MetricKeyType.ts +0 -262
- package/src/MetricLabel.ts +0 -47
- package/src/MetricPair.ts +0 -71
- package/src/MetricPolling.ts +0 -148
- package/src/MetricRegistry.ts +0 -48
- package/src/MetricState.ts +0 -257
- package/src/Micro.ts +0 -4405
- package/src/ModuleVersion.ts +0 -18
- package/src/MutableHashMap.ts +0 -411
- package/src/MutableHashSet.ts +0 -706
- package/src/MutableList.ts +0 -297
- package/src/MutableQueue.ts +0 -227
- package/src/MutableRef.ts +0 -202
- package/src/NonEmptyIterable.ts +0 -32
- package/src/Number.ts +0 -1071
- package/src/Option.ts +0 -2170
- package/src/Order.ts +0 -373
- package/src/Ordering.ts +0 -111
- package/src/ParseResult.ts +0 -2031
- package/src/PartitionedSemaphore.ts +0 -200
- package/src/Pipeable.ts +0 -566
- package/src/Pool.ts +0 -204
- package/src/Predicate.ts +0 -1405
- package/src/Pretty.ts +0 -205
- package/src/PrimaryKey.ts +0 -23
- package/src/PubSub.ts +0 -182
- package/src/Queue.ts +0 -644
- package/src/Random.ts +0 -204
- package/src/RateLimiter.ts +0 -138
- package/src/RcMap.ts +0 -141
- package/src/RcRef.ts +0 -122
- package/src/Readable.ts +0 -93
- package/src/Record.ts +0 -1274
- package/src/RedBlackTree.ts +0 -421
- package/src/Redacted.ts +0 -144
- package/src/Ref.ts +0 -180
- package/src/RegExp.ts +0 -38
- package/src/Reloadable.ts +0 -127
- package/src/Request.ts +0 -347
- package/src/RequestBlock.ts +0 -118
- package/src/RequestResolver.ts +0 -366
- package/src/Resource.ts +0 -119
- package/src/Runtime.ts +0 -383
- package/src/RuntimeFlags.ts +0 -368
- package/src/RuntimeFlagsPatch.ts +0 -183
- package/src/STM.ts +0 -2045
- package/src/Schedule.ts +0 -2219
- package/src/ScheduleDecision.ts +0 -62
- package/src/ScheduleInterval.ts +0 -151
- package/src/ScheduleIntervals.ts +0 -122
- package/src/Scheduler.ts +0 -353
- package/src/Schema.ts +0 -10914
- package/src/SchemaAST.ts +0 -3043
- package/src/Scope.ts +0 -204
- package/src/ScopedCache.ts +0 -151
- package/src/ScopedRef.ts +0 -117
- package/src/Secret.ts +0 -88
- package/src/SingleProducerAsyncInput.ts +0 -67
- package/src/Sink.ts +0 -1461
- package/src/SortedMap.ts +0 -287
- package/src/SortedSet.ts +0 -390
- package/src/Stream.ts +0 -6468
- package/src/StreamEmit.ts +0 -136
- package/src/StreamHaltStrategy.ts +0 -123
- package/src/Streamable.ts +0 -45
- package/src/String.ts +0 -778
- package/src/Struct.ts +0 -243
- package/src/Subscribable.ts +0 -100
- package/src/SubscriptionRef.ts +0 -298
- package/src/Supervisor.ts +0 -240
- package/src/Symbol.ts +0 -29
- package/src/SynchronizedRef.ts +0 -270
- package/src/TArray.ts +0 -495
- package/src/TDeferred.ts +0 -100
- package/src/TMap.ts +0 -515
- package/src/TPriorityQueue.ts +0 -223
- package/src/TPubSub.ts +0 -200
- package/src/TQueue.ts +0 -432
- package/src/TRandom.ts +0 -129
- package/src/TReentrantLock.ts +0 -224
- package/src/TRef.ts +0 -178
- package/src/TSemaphore.ts +0 -129
- package/src/TSet.ts +0 -365
- package/src/TSubscriptionRef.ts +0 -192
- package/src/Take.ts +0 -258
- package/src/TestAnnotation.ts +0 -158
- package/src/TestAnnotationMap.ts +0 -119
- package/src/TestAnnotations.ts +0 -117
- package/src/TestClock.ts +0 -556
- package/src/TestConfig.ts +0 -47
- package/src/TestContext.ts +0 -36
- package/src/TestLive.ts +0 -53
- package/src/TestServices.ts +0 -390
- package/src/TestSized.ts +0 -55
- package/src/Tracer.ts +0 -198
- package/src/Trie.ts +0 -840
- package/src/Tuple.ts +0 -305
- package/src/Types.ts +0 -353
- package/src/Unify.ts +0 -113
- package/src/UpstreamPullRequest.ts +0 -117
- package/src/UpstreamPullStrategy.ts +0 -121
- package/src/Utils.ts +0 -809
- package/src/index.ts +0 -1561
- package/src/internal/array.ts +0 -8
- package/src/internal/blockedRequests.ts +0 -520
- package/src/internal/cache.ts +0 -733
- package/src/internal/cause.ts +0 -1050
- package/src/internal/channel/channelExecutor.ts +0 -1200
- package/src/internal/channel/channelState.ts +0 -134
- package/src/internal/channel/childExecutorDecision.ts +0 -96
- package/src/internal/channel/continuation.ts +0 -200
- package/src/internal/channel/mergeDecision.ts +0 -113
- package/src/internal/channel/mergeState.ts +0 -120
- package/src/internal/channel/mergeStrategy.ts +0 -72
- package/src/internal/channel/singleProducerAsyncInput.ts +0 -259
- package/src/internal/channel/subexecutor.ts +0 -229
- package/src/internal/channel/upstreamPullRequest.ts +0 -84
- package/src/internal/channel/upstreamPullStrategy.ts +0 -87
- package/src/internal/channel.ts +0 -2603
- package/src/internal/clock.ts +0 -95
- package/src/internal/completedRequestMap.ts +0 -9
- package/src/internal/concurrency.ts +0 -54
- package/src/internal/config.ts +0 -716
- package/src/internal/configError.ts +0 -304
- package/src/internal/configProvider/pathPatch.ts +0 -97
- package/src/internal/configProvider.ts +0 -799
- package/src/internal/console.ts +0 -153
- package/src/internal/context.ts +0 -337
- package/src/internal/core-effect.ts +0 -2293
- package/src/internal/core-stream.ts +0 -998
- package/src/internal/core.ts +0 -3273
- package/src/internal/data.ts +0 -36
- package/src/internal/dataSource.ts +0 -327
- package/src/internal/dateTime.ts +0 -1277
- package/src/internal/defaultServices/console.ts +0 -100
- package/src/internal/defaultServices.ts +0 -163
- package/src/internal/deferred.ts +0 -46
- package/src/internal/differ/chunkPatch.ts +0 -211
- package/src/internal/differ/contextPatch.ts +0 -232
- package/src/internal/differ/hashMapPatch.ts +0 -220
- package/src/internal/differ/hashSetPatch.ts +0 -176
- package/src/internal/differ/orPatch.ts +0 -311
- package/src/internal/differ/readonlyArrayPatch.ts +0 -210
- package/src/internal/differ.ts +0 -200
- package/src/internal/doNotation.ts +0 -80
- package/src/internal/effect/circular.ts +0 -905
- package/src/internal/effectable.ts +0 -131
- package/src/internal/either.ts +0 -110
- package/src/internal/encoding/base64.ts +0 -286
- package/src/internal/encoding/base64Url.ts +0 -29
- package/src/internal/encoding/common.ts +0 -51
- package/src/internal/encoding/hex.ts +0 -315
- package/src/internal/errors.ts +0 -7
- package/src/internal/executionPlan.ts +0 -114
- package/src/internal/executionStrategy.ts +0 -74
- package/src/internal/fiber.ts +0 -388
- package/src/internal/fiberId.ts +0 -267
- package/src/internal/fiberMessage.ts +0 -82
- package/src/internal/fiberRefs/patch.ts +0 -144
- package/src/internal/fiberRefs.ts +0 -297
- package/src/internal/fiberRuntime.ts +0 -3915
- package/src/internal/fiberScope.ts +0 -71
- package/src/internal/fiberStatus.ts +0 -119
- package/src/internal/groupBy.ts +0 -530
- package/src/internal/hashMap/array.ts +0 -49
- package/src/internal/hashMap/bitwise.ts +0 -32
- package/src/internal/hashMap/config.ts +0 -14
- package/src/internal/hashMap/keySet.ts +0 -8
- package/src/internal/hashMap/node.ts +0 -391
- package/src/internal/hashMap.ts +0 -586
- package/src/internal/hashSet.ts +0 -323
- package/src/internal/keyedPool.ts +0 -244
- package/src/internal/layer/circular.ts +0 -228
- package/src/internal/layer.ts +0 -1487
- package/src/internal/logSpan.ts +0 -20
- package/src/internal/logger-circular.ts +0 -24
- package/src/internal/logger.ts +0 -485
- package/src/internal/mailbox.ts +0 -561
- package/src/internal/managedRuntime/circular.ts +0 -6
- package/src/internal/managedRuntime.ts +0 -134
- package/src/internal/matcher.ts +0 -652
- package/src/internal/metric/boundaries.ts +0 -75
- package/src/internal/metric/hook.ts +0 -483
- package/src/internal/metric/key.ts +0 -167
- package/src/internal/metric/keyType.ts +0 -238
- package/src/internal/metric/label.ts +0 -41
- package/src/internal/metric/pair.ts +0 -48
- package/src/internal/metric/polling.ts +0 -149
- package/src/internal/metric/registry.ts +0 -187
- package/src/internal/metric/state.ts +0 -290
- package/src/internal/metric.ts +0 -577
- package/src/internal/opCodes/cause.ts +0 -35
- package/src/internal/opCodes/channel.ts +0 -83
- package/src/internal/opCodes/channelChildExecutorDecision.ts +0 -17
- package/src/internal/opCodes/channelMergeDecision.ts +0 -11
- package/src/internal/opCodes/channelMergeState.ts +0 -17
- package/src/internal/opCodes/channelMergeStrategy.ts +0 -11
- package/src/internal/opCodes/channelState.ts +0 -23
- package/src/internal/opCodes/channelUpstreamPullRequest.ts +0 -11
- package/src/internal/opCodes/channelUpstreamPullStrategy.ts +0 -11
- package/src/internal/opCodes/config.ts +0 -65
- package/src/internal/opCodes/configError.ts +0 -35
- package/src/internal/opCodes/continuation.ts +0 -11
- package/src/internal/opCodes/deferred.ts +0 -11
- package/src/internal/opCodes/effect.ts +0 -89
- package/src/internal/opCodes/layer.ts +0 -59
- package/src/internal/opCodes/streamHaltStrategy.ts +0 -23
- package/src/internal/option.ts +0 -80
- package/src/internal/pool.ts +0 -432
- package/src/internal/pubsub.ts +0 -1762
- package/src/internal/query.ts +0 -204
- package/src/internal/queue.ts +0 -766
- package/src/internal/random.ts +0 -161
- package/src/internal/rateLimiter.ts +0 -93
- package/src/internal/rcMap.ts +0 -285
- package/src/internal/rcRef.ts +0 -192
- package/src/internal/redBlackTree/iterator.ts +0 -200
- package/src/internal/redBlackTree/node.ts +0 -68
- package/src/internal/redBlackTree.ts +0 -1245
- package/src/internal/redacted.ts +0 -73
- package/src/internal/ref.ts +0 -171
- package/src/internal/reloadable.ts +0 -140
- package/src/internal/request.ts +0 -177
- package/src/internal/resource.ts +0 -76
- package/src/internal/ringBuffer.ts +0 -68
- package/src/internal/runtime.ts +0 -558
- package/src/internal/runtimeFlags.ts +0 -188
- package/src/internal/runtimeFlagsPatch.ts +0 -103
- package/src/internal/schedule/decision.ts +0 -47
- package/src/internal/schedule/interval.ts +0 -101
- package/src/internal/schedule/intervals.ts +0 -180
- package/src/internal/schedule.ts +0 -2199
- package/src/internal/schema/errors.ts +0 -191
- package/src/internal/schema/schemaId.ts +0 -106
- package/src/internal/schema/util.ts +0 -50
- package/src/internal/scopedCache.ts +0 -644
- package/src/internal/scopedRef.ts +0 -118
- package/src/internal/secret.ts +0 -89
- package/src/internal/singleShotGen.ts +0 -35
- package/src/internal/sink.ts +0 -2120
- package/src/internal/stack.ts +0 -10
- package/src/internal/stm/core.ts +0 -817
- package/src/internal/stm/entry.ts +0 -59
- package/src/internal/stm/journal.ts +0 -123
- package/src/internal/stm/opCodes/stm.ts +0 -71
- package/src/internal/stm/opCodes/stmState.ts +0 -17
- package/src/internal/stm/opCodes/strategy.ts +0 -17
- package/src/internal/stm/opCodes/tExit.ts +0 -29
- package/src/internal/stm/opCodes/tryCommit.ts +0 -11
- package/src/internal/stm/stm.ts +0 -1453
- package/src/internal/stm/stmState.ts +0 -136
- package/src/internal/stm/tArray.ts +0 -550
- package/src/internal/stm/tDeferred.ts +0 -81
- package/src/internal/stm/tExit.ts +0 -190
- package/src/internal/stm/tMap.ts +0 -824
- package/src/internal/stm/tPriorityQueue.ts +0 -267
- package/src/internal/stm/tPubSub.ts +0 -551
- package/src/internal/stm/tQueue.ts +0 -393
- package/src/internal/stm/tRandom.ts +0 -140
- package/src/internal/stm/tReentrantLock.ts +0 -352
- package/src/internal/stm/tRef.ts +0 -195
- package/src/internal/stm/tSemaphore.ts +0 -113
- package/src/internal/stm/tSet.ts +0 -259
- package/src/internal/stm/tSubscriptionRef.ts +0 -286
- package/src/internal/stm/tryCommit.ts +0 -34
- package/src/internal/stm/txnId.ts +0 -14
- package/src/internal/stm/versioned.ts +0 -4
- package/src/internal/stream/debounceState.ts +0 -57
- package/src/internal/stream/emit.ts +0 -123
- package/src/internal/stream/haltStrategy.ts +0 -94
- package/src/internal/stream/handoff.ts +0 -187
- package/src/internal/stream/handoffSignal.ts +0 -59
- package/src/internal/stream/pull.ts +0 -34
- package/src/internal/stream/sinkEndReason.ts +0 -30
- package/src/internal/stream/zipAllState.ts +0 -88
- package/src/internal/stream/zipChunksState.ts +0 -56
- package/src/internal/stream.ts +0 -8801
- package/src/internal/string-utils.ts +0 -107
- package/src/internal/subscriptionRef.ts +0 -138
- package/src/internal/supervisor/patch.ts +0 -190
- package/src/internal/supervisor.ts +0 -303
- package/src/internal/synchronizedRef.ts +0 -114
- package/src/internal/take.ts +0 -199
- package/src/internal/testing/sleep.ts +0 -27
- package/src/internal/testing/suspendedWarningData.ts +0 -85
- package/src/internal/testing/warningData.ts +0 -94
- package/src/internal/tracer.ts +0 -293
- package/src/internal/trie.ts +0 -722
- package/src/internal/version.ts +0 -7
|
@@ -1,1245 +0,0 @@
|
|
|
1
|
-
import * as Chunk from "../Chunk.js"
|
|
2
|
-
import * as Equal from "../Equal.js"
|
|
3
|
-
import { dual, pipe } from "../Function.js"
|
|
4
|
-
import * as Hash from "../Hash.js"
|
|
5
|
-
import { format, NodeInspectSymbol, toJSON } from "../Inspectable.js"
|
|
6
|
-
import * as Option from "../Option.js"
|
|
7
|
-
import type * as Order from "../Order.js"
|
|
8
|
-
import type * as Ordering from "../Ordering.js"
|
|
9
|
-
import { pipeArguments } from "../Pipeable.js"
|
|
10
|
-
import { hasProperty } from "../Predicate.js"
|
|
11
|
-
import type * as RBT from "../RedBlackTree.js"
|
|
12
|
-
import { Direction, RedBlackTreeIterator } from "./redBlackTree/iterator.js"
|
|
13
|
-
import * as Node from "./redBlackTree/node.js"
|
|
14
|
-
import * as Stack from "./stack.js"
|
|
15
|
-
|
|
16
|
-
const RedBlackTreeSymbolKey = "effect/RedBlackTree"
|
|
17
|
-
|
|
18
|
-
/** @internal */
|
|
19
|
-
export const RedBlackTreeTypeId: RBT.TypeId = Symbol.for(RedBlackTreeSymbolKey) as RBT.TypeId
|
|
20
|
-
|
|
21
|
-
/** @internal */
|
|
22
|
-
export interface RedBlackTreeImpl<in out K, out V> extends RBT.RedBlackTree<K, V> {
|
|
23
|
-
readonly _ord: Order.Order<K>
|
|
24
|
-
readonly _root: Node.Node<K, V> | undefined
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const redBlackTreeVariance = {
|
|
28
|
-
/* c8 ignore next */
|
|
29
|
-
_Key: (_: any) => _,
|
|
30
|
-
/* c8 ignore next */
|
|
31
|
-
_Value: (_: never) => _
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const RedBlackTreeProto: RBT.RedBlackTree<unknown, unknown> = {
|
|
35
|
-
[RedBlackTreeTypeId]: redBlackTreeVariance,
|
|
36
|
-
[Hash.symbol](this: RBT.RedBlackTree<unknown, unknown>): number {
|
|
37
|
-
let hash = Hash.hash(RedBlackTreeSymbolKey)
|
|
38
|
-
for (const item of this) {
|
|
39
|
-
hash ^= pipe(Hash.hash(item[0]), Hash.combine(Hash.hash(item[1])))
|
|
40
|
-
}
|
|
41
|
-
return Hash.cached(this, hash)
|
|
42
|
-
},
|
|
43
|
-
[Equal.symbol]<K, V>(this: RedBlackTreeImpl<K, V>, that: unknown): boolean {
|
|
44
|
-
if (isRedBlackTree(that)) {
|
|
45
|
-
if ((this._root?.count ?? 0) !== ((that as RedBlackTreeImpl<K, V>)._root?.count ?? 0)) {
|
|
46
|
-
return false
|
|
47
|
-
}
|
|
48
|
-
const entries = Array.from(that)
|
|
49
|
-
return Array.from(this).every((itemSelf, i) => {
|
|
50
|
-
const itemThat = entries[i]
|
|
51
|
-
return Equal.equals(itemSelf[0], itemThat[0]) && Equal.equals(itemSelf[1], itemThat[1])
|
|
52
|
-
})
|
|
53
|
-
}
|
|
54
|
-
return false
|
|
55
|
-
},
|
|
56
|
-
[Symbol.iterator]<K, V>(this: RedBlackTreeImpl<K, V>): RedBlackTreeIterator<K, V> {
|
|
57
|
-
const stack: Array<Node.Node<K, V>> = []
|
|
58
|
-
let n = this._root
|
|
59
|
-
while (n != null) {
|
|
60
|
-
stack.push(n)
|
|
61
|
-
n = n.left
|
|
62
|
-
}
|
|
63
|
-
return new RedBlackTreeIterator(this, stack, Direction.Forward)
|
|
64
|
-
},
|
|
65
|
-
toString() {
|
|
66
|
-
return format(this.toJSON())
|
|
67
|
-
},
|
|
68
|
-
toJSON() {
|
|
69
|
-
return {
|
|
70
|
-
_id: "RedBlackTree",
|
|
71
|
-
values: Array.from(this).map(toJSON)
|
|
72
|
-
}
|
|
73
|
-
},
|
|
74
|
-
[NodeInspectSymbol]() {
|
|
75
|
-
return this.toJSON()
|
|
76
|
-
},
|
|
77
|
-
pipe() {
|
|
78
|
-
return pipeArguments(this, arguments)
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const makeImpl = <K, V>(ord: Order.Order<K>, root: Node.Node<K, V> | undefined): RedBlackTreeImpl<K, V> => {
|
|
83
|
-
const tree = Object.create(RedBlackTreeProto)
|
|
84
|
-
tree._ord = ord
|
|
85
|
-
tree._root = root
|
|
86
|
-
return tree
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/** @internal */
|
|
90
|
-
export const isRedBlackTree: {
|
|
91
|
-
<K, V>(u: Iterable<readonly [K, V]>): u is RBT.RedBlackTree<K, V>
|
|
92
|
-
(u: unknown): u is RBT.RedBlackTree<unknown, unknown>
|
|
93
|
-
} = (u: unknown): u is RBT.RedBlackTree<unknown, unknown> => hasProperty(u, RedBlackTreeTypeId)
|
|
94
|
-
|
|
95
|
-
/** @internal */
|
|
96
|
-
export const empty = <K, V = never>(ord: Order.Order<K>): RBT.RedBlackTree<K, V> => makeImpl<K, V>(ord, undefined)
|
|
97
|
-
|
|
98
|
-
/** @internal */
|
|
99
|
-
export const fromIterable = dual<
|
|
100
|
-
<B>(ord: Order.Order<B>) => <K extends B, V>(entries: Iterable<readonly [K, V]>) => RBT.RedBlackTree<K, V>,
|
|
101
|
-
<K extends B, V, B>(entries: Iterable<readonly [K, V]>, ord: Order.Order<B>) => RBT.RedBlackTree<K, V>
|
|
102
|
-
>(2, <K extends B, V, B>(entries: Iterable<readonly [K, V]>, ord: Order.Order<B>) => {
|
|
103
|
-
let tree = empty<K, V>(ord)
|
|
104
|
-
for (const [key, value] of entries) {
|
|
105
|
-
tree = insert(tree, key, value)
|
|
106
|
-
}
|
|
107
|
-
return tree
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
/** @internal */
|
|
111
|
-
export const make =
|
|
112
|
-
<K>(ord: Order.Order<K>) =>
|
|
113
|
-
<Entries extends Array<readonly [K, any]>>(...entries: Entries): RBT.RedBlackTree<
|
|
114
|
-
K,
|
|
115
|
-
Entries[number] extends readonly [any, infer V] ? V : never
|
|
116
|
-
> => {
|
|
117
|
-
return fromIterable(entries, ord)
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/** @internal */
|
|
121
|
-
export const atBackwards = dual<
|
|
122
|
-
(index: number) => <K, V>(self: RBT.RedBlackTree<K, V>) => Iterable<[K, V]>,
|
|
123
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, index: number) => Iterable<[K, V]>
|
|
124
|
-
>(2, (self, index) => at(self, index, Direction.Backward))
|
|
125
|
-
|
|
126
|
-
/** @internal */
|
|
127
|
-
export const atForwards = dual<
|
|
128
|
-
(index: number) => <K, V>(self: RBT.RedBlackTree<K, V>) => Iterable<[K, V]>,
|
|
129
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, index: number) => Iterable<[K, V]>
|
|
130
|
-
>(2, (self, index) => at(self, index, Direction.Forward))
|
|
131
|
-
|
|
132
|
-
const at = <K, V>(
|
|
133
|
-
self: RBT.RedBlackTree<K, V>,
|
|
134
|
-
index: number,
|
|
135
|
-
direction: RBT.RedBlackTree.Direction
|
|
136
|
-
): Iterable<[K, V]> => {
|
|
137
|
-
return {
|
|
138
|
-
[Symbol.iterator]: () => {
|
|
139
|
-
if (index < 0) {
|
|
140
|
-
return new RedBlackTreeIterator(self, [], direction)
|
|
141
|
-
}
|
|
142
|
-
let node = (self as RedBlackTreeImpl<K, V>)._root
|
|
143
|
-
const stack: Array<Node.Node<K, V>> = []
|
|
144
|
-
while (node !== undefined) {
|
|
145
|
-
stack.push(node)
|
|
146
|
-
if (node.left !== undefined) {
|
|
147
|
-
if (index < node.left.count) {
|
|
148
|
-
node = node.left
|
|
149
|
-
continue
|
|
150
|
-
}
|
|
151
|
-
index -= node.left.count
|
|
152
|
-
}
|
|
153
|
-
if (!index) {
|
|
154
|
-
return new RedBlackTreeIterator(self, stack, direction)
|
|
155
|
-
}
|
|
156
|
-
index -= 1
|
|
157
|
-
if (node.right !== undefined) {
|
|
158
|
-
if (index >= node.right.count) {
|
|
159
|
-
break
|
|
160
|
-
}
|
|
161
|
-
node = node.right
|
|
162
|
-
} else {
|
|
163
|
-
break
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
return new RedBlackTreeIterator(self, [], direction)
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/** @internal */
|
|
172
|
-
export const findAll = dual<
|
|
173
|
-
<K>(key: K) => <V>(self: RBT.RedBlackTree<K, V>) => Chunk.Chunk<V>,
|
|
174
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, key: K) => Chunk.Chunk<V>
|
|
175
|
-
>(2, <K, V>(self: RBT.RedBlackTree<K, V>, key: K) => {
|
|
176
|
-
const stack: Array<Node.Node<K, V>> = []
|
|
177
|
-
let node = (self as RedBlackTreeImpl<K, V>)._root
|
|
178
|
-
let result = Chunk.empty<V>()
|
|
179
|
-
while (node !== undefined || stack.length > 0) {
|
|
180
|
-
if (node) {
|
|
181
|
-
stack.push(node)
|
|
182
|
-
node = node.left
|
|
183
|
-
} else {
|
|
184
|
-
const current = stack.pop()!
|
|
185
|
-
if (Equal.equals(key, current.key)) {
|
|
186
|
-
result = Chunk.prepend(current.value)(result)
|
|
187
|
-
}
|
|
188
|
-
node = current.right
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
return result
|
|
192
|
-
})
|
|
193
|
-
|
|
194
|
-
/** @internal */
|
|
195
|
-
export const findFirst = dual<
|
|
196
|
-
<K>(key: K) => <V>(self: RBT.RedBlackTree<K, V>) => Option.Option<V>,
|
|
197
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, key: K) => Option.Option<V>
|
|
198
|
-
>(2, <K, V>(self: RBT.RedBlackTree<K, V>, key: K) => {
|
|
199
|
-
const cmp = (self as RedBlackTreeImpl<K, V>)._ord
|
|
200
|
-
let node = (self as RedBlackTreeImpl<K, V>)._root
|
|
201
|
-
while (node !== undefined) {
|
|
202
|
-
const d = cmp(key, node.key)
|
|
203
|
-
if (Equal.equals(key, node.key)) {
|
|
204
|
-
return Option.some(node.value)
|
|
205
|
-
}
|
|
206
|
-
if (d <= 0) {
|
|
207
|
-
node = node.left
|
|
208
|
-
} else {
|
|
209
|
-
node = node.right
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
return Option.none()
|
|
213
|
-
})
|
|
214
|
-
|
|
215
|
-
/** @internal */
|
|
216
|
-
export const first = <K, V>(self: RBT.RedBlackTree<K, V>): Option.Option<[K, V]> => {
|
|
217
|
-
let node: Node.Node<K, V> | undefined = (self as RedBlackTreeImpl<K, V>)._root
|
|
218
|
-
let current: Node.Node<K, V> | undefined = (self as RedBlackTreeImpl<K, V>)._root
|
|
219
|
-
while (node !== undefined) {
|
|
220
|
-
current = node
|
|
221
|
-
node = node.left
|
|
222
|
-
}
|
|
223
|
-
return current ? Option.some([current.key, current.value]) : Option.none()
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/** @internal */
|
|
227
|
-
export const getAt = dual<
|
|
228
|
-
(index: number) => <K, V>(self: RBT.RedBlackTree<K, V>) => Option.Option<[K, V]>,
|
|
229
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, index: number) => Option.Option<[K, V]>
|
|
230
|
-
>(2, <K, V>(self: RBT.RedBlackTree<K, V>, index: number) => {
|
|
231
|
-
if (index < 0) {
|
|
232
|
-
return Option.none()
|
|
233
|
-
}
|
|
234
|
-
let root = (self as RedBlackTreeImpl<K, V>)._root
|
|
235
|
-
let node: Node.Node<K, V> | undefined = undefined
|
|
236
|
-
while (root !== undefined) {
|
|
237
|
-
node = root
|
|
238
|
-
if (root.left) {
|
|
239
|
-
if (index < root.left.count) {
|
|
240
|
-
root = root.left
|
|
241
|
-
continue
|
|
242
|
-
}
|
|
243
|
-
index -= root.left.count
|
|
244
|
-
}
|
|
245
|
-
if (!index) {
|
|
246
|
-
return Option.some([node.key, node.value])
|
|
247
|
-
}
|
|
248
|
-
index -= 1
|
|
249
|
-
if (root.right) {
|
|
250
|
-
if (index >= root.right.count) {
|
|
251
|
-
break
|
|
252
|
-
}
|
|
253
|
-
root = root.right
|
|
254
|
-
} else {
|
|
255
|
-
break
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
return Option.none()
|
|
259
|
-
})
|
|
260
|
-
|
|
261
|
-
/** @internal */
|
|
262
|
-
export const getOrder = <K, V>(tree: RBT.RedBlackTree<K, V>): Order.Order<K> => (tree as RedBlackTreeImpl<K, V>)._ord
|
|
263
|
-
|
|
264
|
-
/** @internal */
|
|
265
|
-
export const has = dual<
|
|
266
|
-
<K>(key: K) => <V>(self: RBT.RedBlackTree<K, V>) => boolean,
|
|
267
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, key: K) => boolean
|
|
268
|
-
>(2, (self, key) => Option.isSome(findFirst(self, key)))
|
|
269
|
-
|
|
270
|
-
/** @internal */
|
|
271
|
-
export const insert = dual<
|
|
272
|
-
<K, V>(key: K, value: V) => (self: RBT.RedBlackTree<K, V>) => RBT.RedBlackTree<K, V>,
|
|
273
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, key: K, value: V) => RBT.RedBlackTree<K, V>
|
|
274
|
-
>(3, <K, V>(self: RBT.RedBlackTree<K, V>, key: K, value: V) => {
|
|
275
|
-
const cmp = (self as RedBlackTreeImpl<K, V>)._ord
|
|
276
|
-
// Find point to insert new node at
|
|
277
|
-
let n: Node.Node<K, V> | undefined = (self as RedBlackTreeImpl<K, V>)._root
|
|
278
|
-
const n_stack: Array<Node.Node<K, V>> = []
|
|
279
|
-
const d_stack: Array<Ordering.Ordering> = []
|
|
280
|
-
while (n != null) {
|
|
281
|
-
const d = cmp(key, n.key)
|
|
282
|
-
n_stack.push(n)
|
|
283
|
-
d_stack.push(d)
|
|
284
|
-
if (d <= 0) {
|
|
285
|
-
n = n.left
|
|
286
|
-
} else {
|
|
287
|
-
n = n.right
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
// Rebuild path to leaf node
|
|
291
|
-
n_stack.push({
|
|
292
|
-
color: Node.Color.Red,
|
|
293
|
-
key,
|
|
294
|
-
value,
|
|
295
|
-
left: undefined,
|
|
296
|
-
right: undefined,
|
|
297
|
-
count: 1
|
|
298
|
-
})
|
|
299
|
-
for (let s = n_stack.length - 2; s >= 0; --s) {
|
|
300
|
-
const n2 = n_stack[s]!
|
|
301
|
-
if (d_stack[s]! <= 0) {
|
|
302
|
-
n_stack[s] = {
|
|
303
|
-
color: n2.color,
|
|
304
|
-
key: n2.key,
|
|
305
|
-
value: n2.value,
|
|
306
|
-
left: n_stack[s + 1],
|
|
307
|
-
right: n2.right,
|
|
308
|
-
count: n2.count + 1
|
|
309
|
-
}
|
|
310
|
-
} else {
|
|
311
|
-
n_stack[s] = {
|
|
312
|
-
color: n2.color,
|
|
313
|
-
key: n2.key,
|
|
314
|
-
value: n2.value,
|
|
315
|
-
left: n2.left,
|
|
316
|
-
right: n_stack[s + 1],
|
|
317
|
-
count: n2.count + 1
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
// Rebalance tree using rotations
|
|
322
|
-
for (let s = n_stack.length - 1; s > 1; --s) {
|
|
323
|
-
const p = n_stack[s - 1]!
|
|
324
|
-
const n3 = n_stack[s]!
|
|
325
|
-
if (p.color === Node.Color.Black || n3.color === Node.Color.Black) {
|
|
326
|
-
break
|
|
327
|
-
}
|
|
328
|
-
const pp = n_stack[s - 2]!
|
|
329
|
-
if (pp.left === p) {
|
|
330
|
-
if (p.left === n3) {
|
|
331
|
-
const y = pp.right
|
|
332
|
-
if (y && y.color === Node.Color.Red) {
|
|
333
|
-
p.color = Node.Color.Black
|
|
334
|
-
pp.right = Node.repaint(y, Node.Color.Black)
|
|
335
|
-
pp.color = Node.Color.Red
|
|
336
|
-
s -= 1
|
|
337
|
-
} else {
|
|
338
|
-
pp.color = Node.Color.Red
|
|
339
|
-
pp.left = p.right
|
|
340
|
-
p.color = Node.Color.Black
|
|
341
|
-
p.right = pp
|
|
342
|
-
n_stack[s - 2] = p
|
|
343
|
-
n_stack[s - 1] = n3
|
|
344
|
-
Node.recount(pp)
|
|
345
|
-
Node.recount(p)
|
|
346
|
-
if (s >= 3) {
|
|
347
|
-
const ppp = n_stack[s - 3]!
|
|
348
|
-
if (ppp.left === pp) {
|
|
349
|
-
ppp.left = p
|
|
350
|
-
} else {
|
|
351
|
-
ppp.right = p
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
break
|
|
355
|
-
}
|
|
356
|
-
} else {
|
|
357
|
-
const y = pp.right
|
|
358
|
-
if (y && y.color === Node.Color.Red) {
|
|
359
|
-
p.color = Node.Color.Black
|
|
360
|
-
pp.right = Node.repaint(y, Node.Color.Black)
|
|
361
|
-
pp.color = Node.Color.Red
|
|
362
|
-
s -= 1
|
|
363
|
-
} else {
|
|
364
|
-
p.right = n3.left
|
|
365
|
-
pp.color = Node.Color.Red
|
|
366
|
-
pp.left = n3.right
|
|
367
|
-
n3.color = Node.Color.Black
|
|
368
|
-
n3.left = p
|
|
369
|
-
n3.right = pp
|
|
370
|
-
n_stack[s - 2] = n3
|
|
371
|
-
n_stack[s - 1] = p
|
|
372
|
-
Node.recount(pp)
|
|
373
|
-
Node.recount(p)
|
|
374
|
-
Node.recount(n3)
|
|
375
|
-
if (s >= 3) {
|
|
376
|
-
const ppp = n_stack[s - 3]!
|
|
377
|
-
if (ppp.left === pp) {
|
|
378
|
-
ppp.left = n3
|
|
379
|
-
} else {
|
|
380
|
-
ppp.right = n3
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
break
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
} else {
|
|
387
|
-
if (p.right === n3) {
|
|
388
|
-
const y = pp.left
|
|
389
|
-
if (y && y.color === Node.Color.Red) {
|
|
390
|
-
p.color = Node.Color.Black
|
|
391
|
-
pp.left = Node.repaint(y, Node.Color.Black)
|
|
392
|
-
pp.color = Node.Color.Red
|
|
393
|
-
s -= 1
|
|
394
|
-
} else {
|
|
395
|
-
pp.color = Node.Color.Red
|
|
396
|
-
pp.right = p.left
|
|
397
|
-
p.color = Node.Color.Black
|
|
398
|
-
p.left = pp
|
|
399
|
-
n_stack[s - 2] = p
|
|
400
|
-
n_stack[s - 1] = n3
|
|
401
|
-
Node.recount(pp)
|
|
402
|
-
Node.recount(p)
|
|
403
|
-
if (s >= 3) {
|
|
404
|
-
const ppp = n_stack[s - 3]!
|
|
405
|
-
if (ppp.right === pp) {
|
|
406
|
-
ppp.right = p
|
|
407
|
-
} else {
|
|
408
|
-
ppp.left = p
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
break
|
|
412
|
-
}
|
|
413
|
-
} else {
|
|
414
|
-
const y = pp.left
|
|
415
|
-
if (y && y.color === Node.Color.Red) {
|
|
416
|
-
p.color = Node.Color.Black
|
|
417
|
-
pp.left = Node.repaint(y, Node.Color.Black)
|
|
418
|
-
pp.color = Node.Color.Red
|
|
419
|
-
s -= 1
|
|
420
|
-
} else {
|
|
421
|
-
p.left = n3.right
|
|
422
|
-
pp.color = Node.Color.Red
|
|
423
|
-
pp.right = n3.left
|
|
424
|
-
n3.color = Node.Color.Black
|
|
425
|
-
n3.right = p
|
|
426
|
-
n3.left = pp
|
|
427
|
-
n_stack[s - 2] = n3
|
|
428
|
-
n_stack[s - 1] = p
|
|
429
|
-
Node.recount(pp)
|
|
430
|
-
Node.recount(p)
|
|
431
|
-
Node.recount(n3)
|
|
432
|
-
if (s >= 3) {
|
|
433
|
-
const ppp = n_stack[s - 3]!
|
|
434
|
-
if (ppp.right === pp) {
|
|
435
|
-
ppp.right = n3
|
|
436
|
-
} else {
|
|
437
|
-
ppp.left = n3
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
break
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
// Return new tree
|
|
446
|
-
n_stack[0]!.color = Node.Color.Black
|
|
447
|
-
return makeImpl((self as RedBlackTreeImpl<K, V>)._ord, n_stack[0])
|
|
448
|
-
})
|
|
449
|
-
|
|
450
|
-
/** @internal */
|
|
451
|
-
export const keysForward = <K, V>(self: RBT.RedBlackTree<K, V>): IterableIterator<K> => keys(self, Direction.Forward)
|
|
452
|
-
|
|
453
|
-
/** @internal */
|
|
454
|
-
export const keysBackward = <K, V>(self: RBT.RedBlackTree<K, V>): IterableIterator<K> => keys(self, Direction.Backward)
|
|
455
|
-
|
|
456
|
-
const keys = <K, V>(
|
|
457
|
-
self: RBT.RedBlackTree<K, V>,
|
|
458
|
-
direction: RBT.RedBlackTree.Direction
|
|
459
|
-
): IterableIterator<K> => {
|
|
460
|
-
const begin: RedBlackTreeIterator<K, V> = self[Symbol.iterator]() as RedBlackTreeIterator<K, V>
|
|
461
|
-
let count = 0
|
|
462
|
-
return {
|
|
463
|
-
[Symbol.iterator]: () => keys(self, direction),
|
|
464
|
-
next: (): IteratorResult<K, number> => {
|
|
465
|
-
count++
|
|
466
|
-
const entry = begin.key
|
|
467
|
-
if (direction === Direction.Forward) {
|
|
468
|
-
begin.moveNext()
|
|
469
|
-
} else {
|
|
470
|
-
begin.movePrev()
|
|
471
|
-
}
|
|
472
|
-
switch (entry._tag) {
|
|
473
|
-
case "None": {
|
|
474
|
-
return { done: true, value: count }
|
|
475
|
-
}
|
|
476
|
-
case "Some": {
|
|
477
|
-
return { done: false, value: entry.value }
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
/** @internal */
|
|
485
|
-
export const last = <K, V>(self: RBT.RedBlackTree<K, V>): Option.Option<[K, V]> => {
|
|
486
|
-
let node: Node.Node<K, V> | undefined = (self as RedBlackTreeImpl<K, V>)._root
|
|
487
|
-
let current: Node.Node<K, V> | undefined = (self as RedBlackTreeImpl<K, V>)._root
|
|
488
|
-
while (node !== undefined) {
|
|
489
|
-
current = node
|
|
490
|
-
node = node.right
|
|
491
|
-
}
|
|
492
|
-
return current ? Option.some([current.key, current.value]) : Option.none()
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
/** @internal */
|
|
496
|
-
export const reversed = <K, V>(self: RBT.RedBlackTree<K, V>): Iterable<[K, V]> => {
|
|
497
|
-
return {
|
|
498
|
-
[Symbol.iterator]: () => {
|
|
499
|
-
const stack: Array<Node.Node<K, V>> = []
|
|
500
|
-
let node = (self as RedBlackTreeImpl<K, V>)._root
|
|
501
|
-
while (node !== undefined) {
|
|
502
|
-
stack.push(node)
|
|
503
|
-
node = node.right
|
|
504
|
-
}
|
|
505
|
-
return new RedBlackTreeIterator(self, stack, Direction.Backward)
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
/** @internal */
|
|
511
|
-
export const greaterThanBackwards = dual<
|
|
512
|
-
<K>(key: K) => <V>(self: RBT.RedBlackTree<K, V>) => Iterable<[K, V]>,
|
|
513
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, key: K) => Iterable<[K, V]>
|
|
514
|
-
>(2, (self, key) => greaterThan(self, key, Direction.Backward))
|
|
515
|
-
|
|
516
|
-
/** @internal */
|
|
517
|
-
export const greaterThanForwards = dual<
|
|
518
|
-
<K>(key: K) => <V>(self: RBT.RedBlackTree<K, V>) => Iterable<[K, V]>,
|
|
519
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, key: K) => Iterable<[K, V]>
|
|
520
|
-
>(2, (self, key) => greaterThan(self, key, Direction.Forward))
|
|
521
|
-
|
|
522
|
-
const greaterThan = <K, V>(
|
|
523
|
-
self: RBT.RedBlackTree<K, V>,
|
|
524
|
-
key: K,
|
|
525
|
-
direction: RBT.RedBlackTree.Direction
|
|
526
|
-
): Iterable<[K, V]> => {
|
|
527
|
-
return {
|
|
528
|
-
[Symbol.iterator]: () => {
|
|
529
|
-
const cmp = (self as RedBlackTreeImpl<K, V>)._ord
|
|
530
|
-
let node = (self as RedBlackTreeImpl<K, V>)._root
|
|
531
|
-
const stack = []
|
|
532
|
-
let last_ptr = 0
|
|
533
|
-
while (node !== undefined) {
|
|
534
|
-
const d = cmp(key, node.key)
|
|
535
|
-
stack.push(node)
|
|
536
|
-
if (d < 0) {
|
|
537
|
-
last_ptr = stack.length
|
|
538
|
-
}
|
|
539
|
-
if (d < 0) {
|
|
540
|
-
node = node.left
|
|
541
|
-
} else {
|
|
542
|
-
node = node.right
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
stack.length = last_ptr
|
|
546
|
-
return new RedBlackTreeIterator(self, stack, direction)
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
/** @internal */
|
|
552
|
-
export const greaterThanEqualBackwards = dual<
|
|
553
|
-
<K>(key: K) => <V>(self: RBT.RedBlackTree<K, V>) => Iterable<[K, V]>,
|
|
554
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, key: K) => Iterable<[K, V]>
|
|
555
|
-
>(2, (self, key) => greaterThanEqual(self, key, Direction.Backward))
|
|
556
|
-
|
|
557
|
-
/** @internal */
|
|
558
|
-
export const greaterThanEqualForwards = dual<
|
|
559
|
-
<K>(key: K) => <V>(self: RBT.RedBlackTree<K, V>) => Iterable<[K, V]>,
|
|
560
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, key: K) => Iterable<[K, V]>
|
|
561
|
-
>(2, (self, key) => greaterThanEqual(self, key, Direction.Forward))
|
|
562
|
-
|
|
563
|
-
const greaterThanEqual = <K, V>(
|
|
564
|
-
self: RBT.RedBlackTree<K, V>,
|
|
565
|
-
key: K,
|
|
566
|
-
direction: RBT.RedBlackTree.Direction = Direction.Forward
|
|
567
|
-
): Iterable<[K, V]> => {
|
|
568
|
-
return {
|
|
569
|
-
[Symbol.iterator]: () => {
|
|
570
|
-
const cmp = (self as RedBlackTreeImpl<K, V>)._ord
|
|
571
|
-
let node = (self as RedBlackTreeImpl<K, V>)._root
|
|
572
|
-
const stack = []
|
|
573
|
-
let last_ptr = 0
|
|
574
|
-
while (node !== undefined) {
|
|
575
|
-
const d = cmp(key, node.key)
|
|
576
|
-
stack.push(node)
|
|
577
|
-
if (d <= 0) {
|
|
578
|
-
last_ptr = stack.length
|
|
579
|
-
}
|
|
580
|
-
if (d <= 0) {
|
|
581
|
-
node = node.left
|
|
582
|
-
} else {
|
|
583
|
-
node = node.right
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
stack.length = last_ptr
|
|
587
|
-
return new RedBlackTreeIterator(self, stack, direction)
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
/** @internal */
|
|
593
|
-
export const lessThanBackwards = dual<
|
|
594
|
-
<K>(key: K) => <V>(self: RBT.RedBlackTree<K, V>) => Iterable<[K, V]>,
|
|
595
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, key: K) => Iterable<[K, V]>
|
|
596
|
-
>(2, (self, key) => lessThan(self, key, Direction.Backward))
|
|
597
|
-
|
|
598
|
-
/** @internal */
|
|
599
|
-
export const lessThanForwards = dual<
|
|
600
|
-
<K>(key: K) => <V>(self: RBT.RedBlackTree<K, V>) => Iterable<[K, V]>,
|
|
601
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, key: K) => Iterable<[K, V]>
|
|
602
|
-
>(2, (self, key) => lessThan(self, key, Direction.Forward))
|
|
603
|
-
|
|
604
|
-
const lessThan = <K, V>(
|
|
605
|
-
self: RBT.RedBlackTree<K, V>,
|
|
606
|
-
key: K,
|
|
607
|
-
direction: RBT.RedBlackTree.Direction
|
|
608
|
-
): Iterable<[K, V]> => {
|
|
609
|
-
return {
|
|
610
|
-
[Symbol.iterator]: () => {
|
|
611
|
-
const cmp = (self as RedBlackTreeImpl<K, V>)._ord
|
|
612
|
-
let node = (self as RedBlackTreeImpl<K, V>)._root
|
|
613
|
-
const stack = []
|
|
614
|
-
let last_ptr = 0
|
|
615
|
-
while (node !== undefined) {
|
|
616
|
-
const d = cmp(key, node.key)
|
|
617
|
-
stack.push(node)
|
|
618
|
-
if (d > 0) {
|
|
619
|
-
last_ptr = stack.length
|
|
620
|
-
}
|
|
621
|
-
if (d <= 0) {
|
|
622
|
-
node = node.left
|
|
623
|
-
} else {
|
|
624
|
-
node = node.right
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
stack.length = last_ptr
|
|
628
|
-
return new RedBlackTreeIterator(self, stack, direction)
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
/** @internal */
|
|
634
|
-
export const lessThanEqualBackwards = dual<
|
|
635
|
-
<K>(key: K) => <V>(self: RBT.RedBlackTree<K, V>) => Iterable<[K, V]>,
|
|
636
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, key: K) => Iterable<[K, V]>
|
|
637
|
-
>(2, (self, key) => lessThanEqual(self, key, Direction.Backward))
|
|
638
|
-
|
|
639
|
-
/** @internal */
|
|
640
|
-
export const lessThanEqualForwards = dual<
|
|
641
|
-
<K>(key: K) => <V>(self: RBT.RedBlackTree<K, V>) => Iterable<[K, V]>,
|
|
642
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, key: K) => Iterable<[K, V]>
|
|
643
|
-
>(2, (self, key) => lessThanEqual(self, key, Direction.Forward))
|
|
644
|
-
|
|
645
|
-
const lessThanEqual = <K, V>(
|
|
646
|
-
self: RBT.RedBlackTree<K, V>,
|
|
647
|
-
key: K,
|
|
648
|
-
direction: RBT.RedBlackTree.Direction
|
|
649
|
-
): Iterable<[K, V]> => {
|
|
650
|
-
return {
|
|
651
|
-
[Symbol.iterator]: () => {
|
|
652
|
-
const cmp = (self as RedBlackTreeImpl<K, V>)._ord
|
|
653
|
-
let node = (self as RedBlackTreeImpl<K, V>)._root
|
|
654
|
-
const stack = []
|
|
655
|
-
let last_ptr = 0
|
|
656
|
-
while (node !== undefined) {
|
|
657
|
-
const d = cmp(key, node.key)
|
|
658
|
-
stack.push(node)
|
|
659
|
-
if (d >= 0) {
|
|
660
|
-
last_ptr = stack.length
|
|
661
|
-
}
|
|
662
|
-
if (d < 0) {
|
|
663
|
-
node = node.left
|
|
664
|
-
} else {
|
|
665
|
-
node = node.right
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
|
-
stack.length = last_ptr
|
|
669
|
-
return new RedBlackTreeIterator(self, stack, direction)
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
/** @internal */
|
|
675
|
-
export const forEach = dual<
|
|
676
|
-
<K, V>(f: (key: K, value: V) => void) => (self: RBT.RedBlackTree<K, V>) => void,
|
|
677
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, f: (key: K, value: V) => void) => void
|
|
678
|
-
>(2, <K, V>(self: RBT.RedBlackTree<K, V>, f: (key: K, value: V) => void) => {
|
|
679
|
-
const root = (self as RedBlackTreeImpl<K, V>)._root
|
|
680
|
-
if (root !== undefined) {
|
|
681
|
-
visitFull(root, (key, value) => {
|
|
682
|
-
f(key, value)
|
|
683
|
-
return Option.none()
|
|
684
|
-
})
|
|
685
|
-
}
|
|
686
|
-
})
|
|
687
|
-
|
|
688
|
-
/** @internal */
|
|
689
|
-
export const forEachGreaterThanEqual = dual<
|
|
690
|
-
<K, V>(min: K, f: (key: K, value: V) => void) => (self: RBT.RedBlackTree<K, V>) => void,
|
|
691
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, min: K, f: (key: K, value: V) => void) => void
|
|
692
|
-
>(3, <K, V>(self: RBT.RedBlackTree<K, V>, min: K, f: (key: K, value: V) => void) => {
|
|
693
|
-
const root = (self as RedBlackTreeImpl<K, V>)._root
|
|
694
|
-
const ord = (self as RedBlackTreeImpl<K, V>)._ord
|
|
695
|
-
if (root !== undefined) {
|
|
696
|
-
visitGreaterThanEqual(root, min, ord, (key, value) => {
|
|
697
|
-
f(key, value)
|
|
698
|
-
return Option.none()
|
|
699
|
-
})
|
|
700
|
-
}
|
|
701
|
-
})
|
|
702
|
-
|
|
703
|
-
/** @internal */
|
|
704
|
-
export const forEachLessThan = dual<
|
|
705
|
-
<K, V>(max: K, f: (key: K, value: V) => void) => (self: RBT.RedBlackTree<K, V>) => void,
|
|
706
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, max: K, f: (key: K, value: V) => void) => void
|
|
707
|
-
>(3, <K, V>(self: RBT.RedBlackTree<K, V>, max: K, f: (key: K, value: V) => void) => {
|
|
708
|
-
const root = (self as RedBlackTreeImpl<K, V>)._root
|
|
709
|
-
const ord = (self as RedBlackTreeImpl<K, V>)._ord
|
|
710
|
-
if (root !== undefined) {
|
|
711
|
-
visitLessThan(root, max, ord, (key, value) => {
|
|
712
|
-
f(key, value)
|
|
713
|
-
return Option.none()
|
|
714
|
-
})
|
|
715
|
-
}
|
|
716
|
-
})
|
|
717
|
-
|
|
718
|
-
/** @internal */
|
|
719
|
-
export const forEachBetween = dual<
|
|
720
|
-
<K, V>(options: {
|
|
721
|
-
readonly min: K
|
|
722
|
-
readonly max: K
|
|
723
|
-
readonly body: (key: K, value: V) => void
|
|
724
|
-
}) => (self: RBT.RedBlackTree<K, V>) => void,
|
|
725
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, options: {
|
|
726
|
-
readonly min: K
|
|
727
|
-
readonly max: K
|
|
728
|
-
readonly body: (key: K, value: V) => void
|
|
729
|
-
}) => void
|
|
730
|
-
>(2, <K, V>(self: RBT.RedBlackTree<K, V>, { body, max, min }: {
|
|
731
|
-
readonly min: K
|
|
732
|
-
readonly max: K
|
|
733
|
-
readonly body: (key: K, value: V) => void
|
|
734
|
-
}) => {
|
|
735
|
-
const root = (self as RedBlackTreeImpl<K, V>)._root
|
|
736
|
-
const ord = (self as RedBlackTreeImpl<K, V>)._ord
|
|
737
|
-
if (root) {
|
|
738
|
-
visitBetween(root, min, max, ord, (key, value) => {
|
|
739
|
-
body(key, value)
|
|
740
|
-
return Option.none()
|
|
741
|
-
})
|
|
742
|
-
}
|
|
743
|
-
})
|
|
744
|
-
|
|
745
|
-
/** @internal */
|
|
746
|
-
export const reduce = dual<
|
|
747
|
-
<Z, V, K>(
|
|
748
|
-
zero: Z,
|
|
749
|
-
f: (accumulator: Z, value: V, key: K) => Z
|
|
750
|
-
) => (self: RBT.RedBlackTree<K, V>) => Z,
|
|
751
|
-
<Z, V, K>(self: RBT.RedBlackTree<K, V>, zero: Z, f: (accumulator: Z, value: V, key: K) => Z) => Z
|
|
752
|
-
>(3, (self, zero, f) => {
|
|
753
|
-
let accumulator = zero
|
|
754
|
-
for (const entry of self) {
|
|
755
|
-
accumulator = f(accumulator, entry[1], entry[0])
|
|
756
|
-
}
|
|
757
|
-
return accumulator
|
|
758
|
-
})
|
|
759
|
-
|
|
760
|
-
/** @internal */
|
|
761
|
-
export const removeFirst = dual<
|
|
762
|
-
<K>(key: K) => <V>(self: RBT.RedBlackTree<K, V>) => RBT.RedBlackTree<K, V>,
|
|
763
|
-
<K, V>(self: RBT.RedBlackTree<K, V>, key: K) => RBT.RedBlackTree<K, V>
|
|
764
|
-
>(2, <K, V>(self: RBT.RedBlackTree<K, V>, key: K) => {
|
|
765
|
-
if (!has(self, key)) {
|
|
766
|
-
return self
|
|
767
|
-
}
|
|
768
|
-
const ord = (self as RedBlackTreeImpl<K, V>)._ord
|
|
769
|
-
const cmp = ord
|
|
770
|
-
let node: Node.Node<K, V> | undefined = (self as RedBlackTreeImpl<K, V>)._root
|
|
771
|
-
const stack = []
|
|
772
|
-
while (node !== undefined) {
|
|
773
|
-
const d = cmp(key, node.key)
|
|
774
|
-
stack.push(node)
|
|
775
|
-
if (Equal.equals(key, node.key)) {
|
|
776
|
-
node = undefined
|
|
777
|
-
} else if (d <= 0) {
|
|
778
|
-
node = node.left
|
|
779
|
-
} else {
|
|
780
|
-
node = node.right
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
if (stack.length === 0) {
|
|
784
|
-
return self
|
|
785
|
-
}
|
|
786
|
-
const cstack = new Array<Node.Node<K, V>>(stack.length)
|
|
787
|
-
let n = stack[stack.length - 1]!
|
|
788
|
-
cstack[cstack.length - 1] = {
|
|
789
|
-
color: n.color,
|
|
790
|
-
key: n.key,
|
|
791
|
-
value: n.value,
|
|
792
|
-
left: n.left,
|
|
793
|
-
right: n.right,
|
|
794
|
-
count: n.count
|
|
795
|
-
}
|
|
796
|
-
for (let i = stack.length - 2; i >= 0; --i) {
|
|
797
|
-
n = stack[i]!
|
|
798
|
-
if (n.left === stack[i + 1]) {
|
|
799
|
-
cstack[i] = {
|
|
800
|
-
color: n.color,
|
|
801
|
-
key: n.key,
|
|
802
|
-
value: n.value,
|
|
803
|
-
left: cstack[i + 1],
|
|
804
|
-
right: n.right,
|
|
805
|
-
count: n.count
|
|
806
|
-
}
|
|
807
|
-
} else {
|
|
808
|
-
cstack[i] = {
|
|
809
|
-
color: n.color,
|
|
810
|
-
key: n.key,
|
|
811
|
-
value: n.value,
|
|
812
|
-
left: n.left,
|
|
813
|
-
right: cstack[i + 1],
|
|
814
|
-
count: n.count
|
|
815
|
-
}
|
|
816
|
-
}
|
|
817
|
-
}
|
|
818
|
-
// Get node
|
|
819
|
-
n = cstack[cstack.length - 1]!
|
|
820
|
-
// If not leaf, then swap with previous node
|
|
821
|
-
if (n.left !== undefined && n.right !== undefined) {
|
|
822
|
-
// First walk to previous leaf
|
|
823
|
-
const split = cstack.length
|
|
824
|
-
n = n.left
|
|
825
|
-
while (n.right != null) {
|
|
826
|
-
cstack.push(n)
|
|
827
|
-
n = n.right
|
|
828
|
-
}
|
|
829
|
-
// Copy path to leaf
|
|
830
|
-
const v = cstack[split - 1]
|
|
831
|
-
cstack.push({
|
|
832
|
-
color: n.color,
|
|
833
|
-
key: v!.key,
|
|
834
|
-
value: v!.value,
|
|
835
|
-
left: n.left,
|
|
836
|
-
right: n.right,
|
|
837
|
-
count: n.count
|
|
838
|
-
})
|
|
839
|
-
cstack[split - 1]!.key = n.key
|
|
840
|
-
cstack[split - 1]!.value = n.value
|
|
841
|
-
// Fix up stack
|
|
842
|
-
for (let i = cstack.length - 2; i >= split; --i) {
|
|
843
|
-
n = cstack[i]!
|
|
844
|
-
cstack[i] = {
|
|
845
|
-
color: n.color,
|
|
846
|
-
key: n.key,
|
|
847
|
-
value: n.value,
|
|
848
|
-
left: n.left,
|
|
849
|
-
right: cstack[i + 1],
|
|
850
|
-
count: n.count
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
cstack[split - 1]!.left = cstack[split]
|
|
854
|
-
}
|
|
855
|
-
|
|
856
|
-
// Remove leaf node
|
|
857
|
-
n = cstack[cstack.length - 1]!
|
|
858
|
-
if (n.color === Node.Color.Red) {
|
|
859
|
-
// Easy case: removing red leaf
|
|
860
|
-
const p = cstack[cstack.length - 2]!
|
|
861
|
-
if (p.left === n) {
|
|
862
|
-
p.left = undefined
|
|
863
|
-
} else if (p.right === n) {
|
|
864
|
-
p.right = undefined
|
|
865
|
-
}
|
|
866
|
-
cstack.pop()
|
|
867
|
-
for (let i = 0; i < cstack.length; ++i) {
|
|
868
|
-
cstack[i]!.count--
|
|
869
|
-
}
|
|
870
|
-
return makeImpl(ord, cstack[0])
|
|
871
|
-
} else {
|
|
872
|
-
if (n.left !== undefined || n.right !== undefined) {
|
|
873
|
-
// Second easy case: Single child black parent
|
|
874
|
-
if (n.left !== undefined) {
|
|
875
|
-
Node.swap(n, n.left)
|
|
876
|
-
} else if (n.right !== undefined) {
|
|
877
|
-
Node.swap(n, n.right)
|
|
878
|
-
}
|
|
879
|
-
// Child must be red, so repaint it black to balance color
|
|
880
|
-
n.color = Node.Color.Black
|
|
881
|
-
for (let i = 0; i < cstack.length - 1; ++i) {
|
|
882
|
-
cstack[i]!.count--
|
|
883
|
-
}
|
|
884
|
-
return makeImpl(ord, cstack[0])
|
|
885
|
-
} else if (cstack.length === 1) {
|
|
886
|
-
// Third easy case: root
|
|
887
|
-
return makeImpl(ord, undefined)
|
|
888
|
-
} else {
|
|
889
|
-
// Hard case: Repaint n, and then do some nasty stuff
|
|
890
|
-
for (let i = 0; i < cstack.length; ++i) {
|
|
891
|
-
cstack[i]!.count--
|
|
892
|
-
}
|
|
893
|
-
const parent = cstack[cstack.length - 2]
|
|
894
|
-
fixDoubleBlack(cstack)
|
|
895
|
-
// Fix up links
|
|
896
|
-
if (parent!.left === n) {
|
|
897
|
-
parent!.left = undefined
|
|
898
|
-
} else {
|
|
899
|
-
parent!.right = undefined
|
|
900
|
-
}
|
|
901
|
-
}
|
|
902
|
-
}
|
|
903
|
-
return makeImpl(ord, cstack[0])
|
|
904
|
-
})
|
|
905
|
-
|
|
906
|
-
/** @internal */
|
|
907
|
-
export const size = <K, V>(self: RBT.RedBlackTree<K, V>): number => (self as RedBlackTreeImpl<K, V>)._root?.count ?? 0
|
|
908
|
-
|
|
909
|
-
/** @internal */
|
|
910
|
-
export const valuesForward = <K, V>(self: RBT.RedBlackTree<K, V>): IterableIterator<V> =>
|
|
911
|
-
values(self, Direction.Forward)
|
|
912
|
-
|
|
913
|
-
/** @internal */
|
|
914
|
-
export const valuesBackward = <K, V>(self: RBT.RedBlackTree<K, V>): IterableIterator<V> =>
|
|
915
|
-
values(self, Direction.Backward)
|
|
916
|
-
|
|
917
|
-
/** @internal */
|
|
918
|
-
const values = <K, V>(
|
|
919
|
-
self: RBT.RedBlackTree<K, V>,
|
|
920
|
-
direction: RBT.RedBlackTree.Direction
|
|
921
|
-
): IterableIterator<V> => {
|
|
922
|
-
const begin: RedBlackTreeIterator<K, V> = self[Symbol.iterator]() as RedBlackTreeIterator<K, V>
|
|
923
|
-
let count = 0
|
|
924
|
-
return {
|
|
925
|
-
[Symbol.iterator]: () => values(self, direction),
|
|
926
|
-
next: (): IteratorResult<V, number> => {
|
|
927
|
-
count++
|
|
928
|
-
const entry = begin.value
|
|
929
|
-
if (direction === Direction.Forward) {
|
|
930
|
-
begin.moveNext()
|
|
931
|
-
} else {
|
|
932
|
-
begin.movePrev()
|
|
933
|
-
}
|
|
934
|
-
switch (entry._tag) {
|
|
935
|
-
case "None": {
|
|
936
|
-
return { done: true, value: count }
|
|
937
|
-
}
|
|
938
|
-
case "Some": {
|
|
939
|
-
return { done: false, value: entry.value }
|
|
940
|
-
}
|
|
941
|
-
}
|
|
942
|
-
}
|
|
943
|
-
}
|
|
944
|
-
}
|
|
945
|
-
|
|
946
|
-
const visitFull = <K, V, A>(
|
|
947
|
-
node: Node.Node<K, V>,
|
|
948
|
-
visit: (key: K, value: V) => Option.Option<A>
|
|
949
|
-
): Option.Option<A> => {
|
|
950
|
-
let current: Node.Node<K, V> | undefined = node
|
|
951
|
-
let stack: Stack.Stack<Node.Node<K, V>> | undefined = undefined
|
|
952
|
-
let done = false
|
|
953
|
-
while (!done) {
|
|
954
|
-
if (current != null) {
|
|
955
|
-
stack = Stack.make(current, stack)
|
|
956
|
-
current = current.left
|
|
957
|
-
} else if (stack != null) {
|
|
958
|
-
const value = visit(stack.value.key, stack.value.value)
|
|
959
|
-
if (Option.isSome(value)) {
|
|
960
|
-
return value
|
|
961
|
-
}
|
|
962
|
-
current = stack.value.right
|
|
963
|
-
stack = stack.previous
|
|
964
|
-
} else {
|
|
965
|
-
done = true
|
|
966
|
-
}
|
|
967
|
-
}
|
|
968
|
-
return Option.none()
|
|
969
|
-
}
|
|
970
|
-
|
|
971
|
-
const visitGreaterThanEqual = <K, V, A>(
|
|
972
|
-
node: Node.Node<K, V>,
|
|
973
|
-
min: K,
|
|
974
|
-
ord: Order.Order<K>,
|
|
975
|
-
visit: (key: K, value: V) => Option.Option<A>
|
|
976
|
-
): Option.Option<A> => {
|
|
977
|
-
let current: Node.Node<K, V> | undefined = node
|
|
978
|
-
let stack: Stack.Stack<Node.Node<K, V>> | undefined = undefined
|
|
979
|
-
let done = false
|
|
980
|
-
while (!done) {
|
|
981
|
-
if (current !== undefined) {
|
|
982
|
-
stack = Stack.make(current, stack)
|
|
983
|
-
if (ord(min, current.key) <= 0) {
|
|
984
|
-
current = current.left
|
|
985
|
-
} else {
|
|
986
|
-
current = undefined
|
|
987
|
-
}
|
|
988
|
-
} else if (stack !== undefined) {
|
|
989
|
-
if (ord(min, stack.value.key) <= 0) {
|
|
990
|
-
const value = visit(stack.value.key, stack.value.value)
|
|
991
|
-
if (Option.isSome(value)) {
|
|
992
|
-
return value
|
|
993
|
-
}
|
|
994
|
-
}
|
|
995
|
-
current = stack.value.right
|
|
996
|
-
stack = stack.previous
|
|
997
|
-
} else {
|
|
998
|
-
done = true
|
|
999
|
-
}
|
|
1000
|
-
}
|
|
1001
|
-
return Option.none()
|
|
1002
|
-
}
|
|
1003
|
-
|
|
1004
|
-
const visitLessThan = <K, V, A>(
|
|
1005
|
-
node: Node.Node<K, V>,
|
|
1006
|
-
max: K,
|
|
1007
|
-
ord: Order.Order<K>,
|
|
1008
|
-
visit: (key: K, value: V) => Option.Option<A>
|
|
1009
|
-
): Option.Option<A> => {
|
|
1010
|
-
let current: Node.Node<K, V> | undefined = node
|
|
1011
|
-
let stack: Stack.Stack<Node.Node<K, V>> | undefined = undefined
|
|
1012
|
-
let done = false
|
|
1013
|
-
while (!done) {
|
|
1014
|
-
if (current !== undefined) {
|
|
1015
|
-
stack = Stack.make(current, stack)
|
|
1016
|
-
current = current.left
|
|
1017
|
-
} else if (stack !== undefined && ord(max, stack.value.key) > 0) {
|
|
1018
|
-
const value = visit(stack.value.key, stack.value.value)
|
|
1019
|
-
if (Option.isSome(value)) {
|
|
1020
|
-
return value
|
|
1021
|
-
}
|
|
1022
|
-
current = stack.value.right
|
|
1023
|
-
stack = stack.previous
|
|
1024
|
-
} else {
|
|
1025
|
-
done = true
|
|
1026
|
-
}
|
|
1027
|
-
}
|
|
1028
|
-
return Option.none()
|
|
1029
|
-
}
|
|
1030
|
-
|
|
1031
|
-
const visitBetween = <K, V, A>(
|
|
1032
|
-
node: Node.Node<K, V>,
|
|
1033
|
-
min: K,
|
|
1034
|
-
max: K,
|
|
1035
|
-
ord: Order.Order<K>,
|
|
1036
|
-
visit: (key: K, value: V) => Option.Option<A>
|
|
1037
|
-
): Option.Option<A> => {
|
|
1038
|
-
let current: Node.Node<K, V> | undefined = node
|
|
1039
|
-
let stack: Stack.Stack<Node.Node<K, V>> | undefined = undefined
|
|
1040
|
-
let done = false
|
|
1041
|
-
while (!done) {
|
|
1042
|
-
if (current !== undefined) {
|
|
1043
|
-
stack = Stack.make(current, stack)
|
|
1044
|
-
if (ord(min, current.key) <= 0) {
|
|
1045
|
-
current = current.left
|
|
1046
|
-
} else {
|
|
1047
|
-
current = undefined
|
|
1048
|
-
}
|
|
1049
|
-
} else if (stack !== undefined && ord(max, stack.value.key) > 0) {
|
|
1050
|
-
if (ord(min, stack.value.key) <= 0) {
|
|
1051
|
-
const value = visit(stack.value.key, stack.value.value)
|
|
1052
|
-
if (Option.isSome(value)) {
|
|
1053
|
-
return value
|
|
1054
|
-
}
|
|
1055
|
-
}
|
|
1056
|
-
current = stack.value.right
|
|
1057
|
-
stack = stack.previous
|
|
1058
|
-
} else {
|
|
1059
|
-
done = true
|
|
1060
|
-
}
|
|
1061
|
-
}
|
|
1062
|
-
return Option.none()
|
|
1063
|
-
}
|
|
1064
|
-
|
|
1065
|
-
/**
|
|
1066
|
-
* Fix up a double black node in a Red-Black Tree.
|
|
1067
|
-
*/
|
|
1068
|
-
const fixDoubleBlack = <K, V>(stack: Array<Node.Node<K, V>>) => {
|
|
1069
|
-
let n, p, s, z
|
|
1070
|
-
for (let i = stack.length - 1; i >= 0; --i) {
|
|
1071
|
-
n = stack[i]!
|
|
1072
|
-
if (i === 0) {
|
|
1073
|
-
n.color = Node.Color.Black
|
|
1074
|
-
return
|
|
1075
|
-
}
|
|
1076
|
-
p = stack[i - 1]!
|
|
1077
|
-
if (p.left === n) {
|
|
1078
|
-
s = p.right
|
|
1079
|
-
if (s !== undefined && s.right !== undefined && s.right.color === Node.Color.Red) {
|
|
1080
|
-
s = p.right = Node.clone(s)
|
|
1081
|
-
z = s.right = Node.clone(s.right!)
|
|
1082
|
-
p.right = s.left
|
|
1083
|
-
s.left = p
|
|
1084
|
-
s.right = z
|
|
1085
|
-
s.color = p.color
|
|
1086
|
-
n.color = Node.Color.Black
|
|
1087
|
-
p.color = Node.Color.Black
|
|
1088
|
-
z.color = Node.Color.Black
|
|
1089
|
-
Node.recount(p)
|
|
1090
|
-
Node.recount(s)
|
|
1091
|
-
if (i > 1) {
|
|
1092
|
-
const pp = stack[i - 2]!
|
|
1093
|
-
if (pp.left === p) {
|
|
1094
|
-
pp.left = s
|
|
1095
|
-
} else {
|
|
1096
|
-
pp.right = s
|
|
1097
|
-
}
|
|
1098
|
-
}
|
|
1099
|
-
stack[i - 1] = s
|
|
1100
|
-
return
|
|
1101
|
-
} else if (s !== undefined && s.left !== undefined && s.left.color === Node.Color.Red) {
|
|
1102
|
-
s = p.right = Node.clone(s)
|
|
1103
|
-
z = s.left = Node.clone(s.left!)
|
|
1104
|
-
p.right = z.left
|
|
1105
|
-
s.left = z.right
|
|
1106
|
-
z.left = p
|
|
1107
|
-
z.right = s
|
|
1108
|
-
z.color = p.color
|
|
1109
|
-
p.color = Node.Color.Black
|
|
1110
|
-
s.color = Node.Color.Black
|
|
1111
|
-
n.color = Node.Color.Black
|
|
1112
|
-
Node.recount(p)
|
|
1113
|
-
Node.recount(s)
|
|
1114
|
-
Node.recount(z)
|
|
1115
|
-
if (i > 1) {
|
|
1116
|
-
const pp = stack[i - 2]!
|
|
1117
|
-
if (pp.left === p) {
|
|
1118
|
-
pp.left = z
|
|
1119
|
-
} else {
|
|
1120
|
-
pp.right = z
|
|
1121
|
-
}
|
|
1122
|
-
}
|
|
1123
|
-
stack[i - 1] = z
|
|
1124
|
-
return
|
|
1125
|
-
}
|
|
1126
|
-
if (s !== undefined && s.color === Node.Color.Black) {
|
|
1127
|
-
if (p.color === Node.Color.Red) {
|
|
1128
|
-
p.color = Node.Color.Black
|
|
1129
|
-
p.right = Node.repaint(s, Node.Color.Red)
|
|
1130
|
-
return
|
|
1131
|
-
} else {
|
|
1132
|
-
p.right = Node.repaint(s, Node.Color.Red)
|
|
1133
|
-
continue
|
|
1134
|
-
}
|
|
1135
|
-
} else if (s !== undefined) {
|
|
1136
|
-
s = Node.clone(s)
|
|
1137
|
-
p.right = s.left
|
|
1138
|
-
s.left = p
|
|
1139
|
-
s.color = p.color
|
|
1140
|
-
p.color = Node.Color.Red
|
|
1141
|
-
Node.recount(p)
|
|
1142
|
-
Node.recount(s)
|
|
1143
|
-
if (i > 1) {
|
|
1144
|
-
const pp = stack[i - 2]!
|
|
1145
|
-
if (pp.left === p) {
|
|
1146
|
-
pp.left = s
|
|
1147
|
-
} else {
|
|
1148
|
-
pp.right = s
|
|
1149
|
-
}
|
|
1150
|
-
}
|
|
1151
|
-
stack[i - 1] = s
|
|
1152
|
-
stack[i] = p
|
|
1153
|
-
if (i + 1 < stack.length) {
|
|
1154
|
-
stack[i + 1] = n
|
|
1155
|
-
} else {
|
|
1156
|
-
stack.push(n)
|
|
1157
|
-
}
|
|
1158
|
-
i = i + 2
|
|
1159
|
-
}
|
|
1160
|
-
} else {
|
|
1161
|
-
s = p.left
|
|
1162
|
-
if (s !== undefined && s.left !== undefined && s.left.color === Node.Color.Red) {
|
|
1163
|
-
s = p.left = Node.clone(s)
|
|
1164
|
-
z = s.left = Node.clone(s.left!)
|
|
1165
|
-
p.left = s.right
|
|
1166
|
-
s.right = p
|
|
1167
|
-
s.left = z
|
|
1168
|
-
s.color = p.color
|
|
1169
|
-
n.color = Node.Color.Black
|
|
1170
|
-
p.color = Node.Color.Black
|
|
1171
|
-
z.color = Node.Color.Black
|
|
1172
|
-
Node.recount(p)
|
|
1173
|
-
Node.recount(s)
|
|
1174
|
-
if (i > 1) {
|
|
1175
|
-
const pp = stack[i - 2]!
|
|
1176
|
-
if (pp.right === p) {
|
|
1177
|
-
pp.right = s
|
|
1178
|
-
} else {
|
|
1179
|
-
pp.left = s
|
|
1180
|
-
}
|
|
1181
|
-
}
|
|
1182
|
-
stack[i - 1] = s
|
|
1183
|
-
return
|
|
1184
|
-
} else if (s !== undefined && s.right !== undefined && s.right.color === Node.Color.Red) {
|
|
1185
|
-
s = p.left = Node.clone(s)
|
|
1186
|
-
z = s.right = Node.clone(s.right!)
|
|
1187
|
-
p.left = z.right
|
|
1188
|
-
s.right = z.left
|
|
1189
|
-
z.right = p
|
|
1190
|
-
z.left = s
|
|
1191
|
-
z.color = p.color
|
|
1192
|
-
p.color = Node.Color.Black
|
|
1193
|
-
s.color = Node.Color.Black
|
|
1194
|
-
n.color = Node.Color.Black
|
|
1195
|
-
Node.recount(p)
|
|
1196
|
-
Node.recount(s)
|
|
1197
|
-
Node.recount(z)
|
|
1198
|
-
if (i > 1) {
|
|
1199
|
-
const pp = stack[i - 2]!
|
|
1200
|
-
if (pp.right === p) {
|
|
1201
|
-
pp.right = z
|
|
1202
|
-
} else {
|
|
1203
|
-
pp.left = z
|
|
1204
|
-
}
|
|
1205
|
-
}
|
|
1206
|
-
stack[i - 1] = z
|
|
1207
|
-
return
|
|
1208
|
-
}
|
|
1209
|
-
if (s !== undefined && s.color === Node.Color.Black) {
|
|
1210
|
-
if (p.color === Node.Color.Red) {
|
|
1211
|
-
p.color = Node.Color.Black
|
|
1212
|
-
p.left = Node.repaint(s, Node.Color.Red)
|
|
1213
|
-
return
|
|
1214
|
-
} else {
|
|
1215
|
-
p.left = Node.repaint(s, Node.Color.Red)
|
|
1216
|
-
continue
|
|
1217
|
-
}
|
|
1218
|
-
} else if (s !== undefined) {
|
|
1219
|
-
s = Node.clone(s)
|
|
1220
|
-
p.left = s.right
|
|
1221
|
-
s.right = p
|
|
1222
|
-
s.color = p.color
|
|
1223
|
-
p.color = Node.Color.Red
|
|
1224
|
-
Node.recount(p)
|
|
1225
|
-
Node.recount(s)
|
|
1226
|
-
if (i > 1) {
|
|
1227
|
-
const pp = stack[i - 2]!
|
|
1228
|
-
if (pp.right === p) {
|
|
1229
|
-
pp.right = s
|
|
1230
|
-
} else {
|
|
1231
|
-
pp.left = s
|
|
1232
|
-
}
|
|
1233
|
-
}
|
|
1234
|
-
stack[i - 1] = s
|
|
1235
|
-
stack[i] = p
|
|
1236
|
-
if (i + 1 < stack.length) {
|
|
1237
|
-
stack[i + 1] = n
|
|
1238
|
-
} else {
|
|
1239
|
-
stack.push(n)
|
|
1240
|
-
}
|
|
1241
|
-
i = i + 2
|
|
1242
|
-
}
|
|
1243
|
-
}
|
|
1244
|
-
}
|
|
1245
|
-
}
|