@clayroach/effect 3.19.14-source-capture.8 → 3.19.14-source-trace.1

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.
Files changed (452) hide show
  1. package/SourceLocation/package.json +6 -0
  2. package/dist/cjs/Effect.js +2 -28
  3. package/dist/cjs/Effect.js.map +1 -1
  4. package/dist/cjs/FiberRef.js +12 -1
  5. package/dist/cjs/FiberRef.js.map +1 -1
  6. package/dist/cjs/Layer.js +2 -24
  7. package/dist/cjs/Layer.js.map +1 -1
  8. package/dist/cjs/RuntimeFlags.js +1 -29
  9. package/dist/cjs/RuntimeFlags.js.map +1 -1
  10. package/dist/cjs/SourceLocation.js +60 -0
  11. package/dist/cjs/SourceLocation.js.map +1 -0
  12. package/dist/cjs/Tracer.js +1 -15
  13. package/dist/cjs/Tracer.js.map +1 -1
  14. package/dist/cjs/Utils.js +1 -1
  15. package/dist/cjs/Utils.js.map +1 -1
  16. package/dist/cjs/index.js +3 -1
  17. package/dist/cjs/index.js.map +1 -1
  18. package/dist/cjs/internal/clock.js +1 -1
  19. package/dist/cjs/internal/clock.js.map +1 -1
  20. package/dist/cjs/internal/core.js +17 -50
  21. package/dist/cjs/internal/core.js.map +1 -1
  22. package/dist/cjs/internal/effect/circular.js +18 -30
  23. package/dist/cjs/internal/effect/circular.js.map +1 -1
  24. package/dist/cjs/internal/fiberRuntime.js +16 -65
  25. package/dist/cjs/internal/fiberRuntime.js.map +1 -1
  26. package/dist/cjs/internal/layer/circular.js +1 -5
  27. package/dist/cjs/internal/layer/circular.js.map +1 -1
  28. package/dist/cjs/internal/layer.js +1 -3
  29. package/dist/cjs/internal/layer.js.map +1 -1
  30. package/dist/cjs/internal/logger.js +25 -2
  31. package/dist/cjs/internal/logger.js.map +1 -1
  32. package/dist/cjs/internal/runtimeFlags.js +2 -11
  33. package/dist/cjs/internal/runtimeFlags.js.map +1 -1
  34. package/dist/cjs/internal/tracer.js +1 -114
  35. package/dist/cjs/internal/tracer.js.map +1 -1
  36. package/dist/dts/Config.d.ts +2 -2
  37. package/dist/dts/Config.d.ts.map +1 -1
  38. package/dist/dts/Effect.d.ts +8 -29
  39. package/dist/dts/Effect.d.ts.map +1 -1
  40. package/dist/dts/FiberRef.d.ts +12 -0
  41. package/dist/dts/FiberRef.d.ts.map +1 -1
  42. package/dist/dts/Layer.d.ts +0 -22
  43. package/dist/dts/Layer.d.ts.map +1 -1
  44. package/dist/dts/RuntimeFlags.d.ts +0 -28
  45. package/dist/dts/RuntimeFlags.d.ts.map +1 -1
  46. package/dist/dts/SourceLocation.d.ts +88 -0
  47. package/dist/dts/SourceLocation.d.ts.map +1 -0
  48. package/dist/dts/Tracer.d.ts +0 -15
  49. package/dist/dts/Tracer.d.ts.map +1 -1
  50. package/dist/dts/index.d.ts +6 -0
  51. package/dist/dts/index.d.ts.map +1 -1
  52. package/dist/dts/internal/core.d.ts.map +1 -1
  53. package/dist/dts/internal/layer.d.ts.map +1 -1
  54. package/dist/dts/internal/runtimeFlags.d.ts.map +1 -1
  55. package/dist/esm/Effect.js +0 -26
  56. package/dist/esm/Effect.js.map +1 -1
  57. package/dist/esm/FiberRef.js +11 -0
  58. package/dist/esm/FiberRef.js.map +1 -1
  59. package/dist/esm/Layer.js +0 -22
  60. package/dist/esm/Layer.js.map +1 -1
  61. package/dist/esm/RuntimeFlags.js +0 -28
  62. package/dist/esm/RuntimeFlags.js.map +1 -1
  63. package/dist/esm/SourceLocation.js +51 -0
  64. package/dist/esm/SourceLocation.js.map +1 -0
  65. package/dist/esm/Tracer.js +0 -14
  66. package/dist/esm/Tracer.js.map +1 -1
  67. package/dist/esm/Utils.js +1 -1
  68. package/dist/esm/Utils.js.map +1 -1
  69. package/dist/esm/index.js +6 -0
  70. package/dist/esm/index.js.map +1 -1
  71. package/dist/esm/internal/clock.js +1 -1
  72. package/dist/esm/internal/clock.js.map +1 -1
  73. package/dist/esm/internal/core.js +12 -45
  74. package/dist/esm/internal/core.js.map +1 -1
  75. package/dist/esm/internal/effect/circular.js +18 -30
  76. package/dist/esm/internal/effect/circular.js.map +1 -1
  77. package/dist/esm/internal/fiberRuntime.js +13 -60
  78. package/dist/esm/internal/fiberRuntime.js.map +1 -1
  79. package/dist/esm/internal/layer/circular.js +0 -4
  80. package/dist/esm/internal/layer/circular.js.map +1 -1
  81. package/dist/esm/internal/layer.js +0 -2
  82. package/dist/esm/internal/layer.js.map +1 -1
  83. package/dist/esm/internal/logger.js +25 -2
  84. package/dist/esm/internal/logger.js.map +1 -1
  85. package/dist/esm/internal/runtimeFlags.js +1 -9
  86. package/dist/esm/internal/runtimeFlags.js.map +1 -1
  87. package/dist/esm/internal/tracer.js +0 -111
  88. package/dist/esm/internal/tracer.js.map +1 -1
  89. package/package.json +12 -1
  90. package/src/Arbitrary.ts +1101 -0
  91. package/src/Array.ts +3589 -0
  92. package/src/BigDecimal.ts +1349 -0
  93. package/src/BigInt.ts +643 -0
  94. package/src/Boolean.ts +287 -0
  95. package/src/Brand.ts +360 -0
  96. package/src/Cache.ts +281 -0
  97. package/src/Cause.ts +1555 -0
  98. package/src/Channel.ts +2355 -0
  99. package/src/ChildExecutorDecision.ts +146 -0
  100. package/src/Chunk.ts +1495 -0
  101. package/src/Clock.ts +111 -0
  102. package/src/Config.ts +542 -0
  103. package/src/ConfigError.ts +270 -0
  104. package/src/ConfigProvider.ts +333 -0
  105. package/src/ConfigProviderPathPatch.ts +100 -0
  106. package/src/Console.ts +226 -0
  107. package/src/Context.ts +585 -0
  108. package/src/Cron.ts +706 -0
  109. package/src/Data.ts +596 -0
  110. package/src/DateTime.ts +1686 -0
  111. package/src/DefaultServices.ts +34 -0
  112. package/src/Deferred.ts +301 -0
  113. package/src/Differ.ts +450 -0
  114. package/src/Duration.ts +1000 -0
  115. package/src/Effect.ts +14817 -0
  116. package/src/Effectable.ts +107 -0
  117. package/src/Either.ts +1040 -0
  118. package/src/Encoding.ts +195 -0
  119. package/src/Equal.ts +98 -0
  120. package/src/Equivalence.ts +235 -0
  121. package/src/ExecutionPlan.ts +308 -0
  122. package/src/ExecutionStrategy.ts +119 -0
  123. package/src/Exit.ts +467 -0
  124. package/src/FastCheck.ts +9 -0
  125. package/src/Fiber.ts +744 -0
  126. package/src/FiberHandle.ts +540 -0
  127. package/src/FiberId.ts +195 -0
  128. package/src/FiberMap.ts +656 -0
  129. package/src/FiberRef.ts +444 -0
  130. package/src/FiberRefs.ts +204 -0
  131. package/src/FiberRefsPatch.ts +105 -0
  132. package/src/FiberSet.ts +491 -0
  133. package/src/FiberStatus.ts +108 -0
  134. package/src/Function.ts +1222 -0
  135. package/src/GlobalValue.ts +53 -0
  136. package/src/Graph.ts +3732 -0
  137. package/src/GroupBy.ts +103 -0
  138. package/src/HKT.ts +45 -0
  139. package/src/Hash.ts +195 -0
  140. package/src/HashMap.ts +519 -0
  141. package/src/HashRing.ts +317 -0
  142. package/src/HashSet.ts +2346 -0
  143. package/src/Inspectable.ts +287 -0
  144. package/src/Iterable.ts +1119 -0
  145. package/src/JSONSchema.ts +1044 -0
  146. package/src/KeyedPool.ts +167 -0
  147. package/src/Layer.ts +1228 -0
  148. package/src/LayerMap.ts +436 -0
  149. package/src/List.ts +977 -0
  150. package/src/LogLevel.ts +285 -0
  151. package/src/LogSpan.ts +25 -0
  152. package/src/Logger.ts +702 -0
  153. package/src/Mailbox.ts +268 -0
  154. package/src/ManagedRuntime.ts +180 -0
  155. package/src/Match.ts +1477 -0
  156. package/src/MergeDecision.ts +95 -0
  157. package/src/MergeState.ts +172 -0
  158. package/src/MergeStrategy.ts +107 -0
  159. package/src/Metric.ts +780 -0
  160. package/src/MetricBoundaries.ts +69 -0
  161. package/src/MetricHook.ts +151 -0
  162. package/src/MetricKey.ts +224 -0
  163. package/src/MetricKeyType.ts +262 -0
  164. package/src/MetricLabel.ts +47 -0
  165. package/src/MetricPair.ts +71 -0
  166. package/src/MetricPolling.ts +148 -0
  167. package/src/MetricRegistry.ts +48 -0
  168. package/src/MetricState.ts +257 -0
  169. package/src/Micro.ts +4405 -0
  170. package/src/ModuleVersion.ts +18 -0
  171. package/src/MutableHashMap.ts +411 -0
  172. package/src/MutableHashSet.ts +706 -0
  173. package/src/MutableList.ts +297 -0
  174. package/src/MutableQueue.ts +227 -0
  175. package/src/MutableRef.ts +202 -0
  176. package/src/NonEmptyIterable.ts +32 -0
  177. package/src/Number.ts +1071 -0
  178. package/src/Option.ts +2170 -0
  179. package/src/Order.ts +373 -0
  180. package/src/Ordering.ts +111 -0
  181. package/src/ParseResult.ts +2031 -0
  182. package/src/PartitionedSemaphore.ts +200 -0
  183. package/src/Pipeable.ts +566 -0
  184. package/src/Pool.ts +204 -0
  185. package/src/Predicate.ts +1405 -0
  186. package/src/Pretty.ts +205 -0
  187. package/src/PrimaryKey.ts +23 -0
  188. package/src/PubSub.ts +182 -0
  189. package/src/Queue.ts +644 -0
  190. package/src/Random.ts +204 -0
  191. package/src/RateLimiter.ts +138 -0
  192. package/src/RcMap.ts +141 -0
  193. package/src/RcRef.ts +122 -0
  194. package/src/Readable.ts +93 -0
  195. package/src/Record.ts +1274 -0
  196. package/src/RedBlackTree.ts +421 -0
  197. package/src/Redacted.ts +144 -0
  198. package/src/Ref.ts +180 -0
  199. package/src/RegExp.ts +38 -0
  200. package/src/Reloadable.ts +127 -0
  201. package/src/Request.ts +347 -0
  202. package/src/RequestBlock.ts +118 -0
  203. package/src/RequestResolver.ts +366 -0
  204. package/src/Resource.ts +119 -0
  205. package/src/Runtime.ts +383 -0
  206. package/src/RuntimeFlags.ts +336 -0
  207. package/src/RuntimeFlagsPatch.ts +183 -0
  208. package/src/STM.ts +2045 -0
  209. package/src/Schedule.ts +2219 -0
  210. package/src/ScheduleDecision.ts +62 -0
  211. package/src/ScheduleInterval.ts +151 -0
  212. package/src/ScheduleIntervals.ts +122 -0
  213. package/src/Scheduler.ts +353 -0
  214. package/src/Schema.ts +10914 -0
  215. package/src/SchemaAST.ts +3043 -0
  216. package/src/Scope.ts +204 -0
  217. package/src/ScopedCache.ts +151 -0
  218. package/src/ScopedRef.ts +117 -0
  219. package/src/Secret.ts +88 -0
  220. package/src/SingleProducerAsyncInput.ts +67 -0
  221. package/src/Sink.ts +1461 -0
  222. package/src/SortedMap.ts +287 -0
  223. package/src/SortedSet.ts +390 -0
  224. package/src/SourceLocation.ts +108 -0
  225. package/src/Stream.ts +6468 -0
  226. package/src/StreamEmit.ts +136 -0
  227. package/src/StreamHaltStrategy.ts +123 -0
  228. package/src/Streamable.ts +45 -0
  229. package/src/String.ts +778 -0
  230. package/src/Struct.ts +243 -0
  231. package/src/Subscribable.ts +100 -0
  232. package/src/SubscriptionRef.ts +298 -0
  233. package/src/Supervisor.ts +240 -0
  234. package/src/Symbol.ts +29 -0
  235. package/src/SynchronizedRef.ts +270 -0
  236. package/src/TArray.ts +495 -0
  237. package/src/TDeferred.ts +100 -0
  238. package/src/TMap.ts +515 -0
  239. package/src/TPriorityQueue.ts +223 -0
  240. package/src/TPubSub.ts +200 -0
  241. package/src/TQueue.ts +432 -0
  242. package/src/TRandom.ts +129 -0
  243. package/src/TReentrantLock.ts +224 -0
  244. package/src/TRef.ts +178 -0
  245. package/src/TSemaphore.ts +129 -0
  246. package/src/TSet.ts +365 -0
  247. package/src/TSubscriptionRef.ts +192 -0
  248. package/src/Take.ts +258 -0
  249. package/src/TestAnnotation.ts +158 -0
  250. package/src/TestAnnotationMap.ts +119 -0
  251. package/src/TestAnnotations.ts +117 -0
  252. package/src/TestClock.ts +556 -0
  253. package/src/TestConfig.ts +47 -0
  254. package/src/TestContext.ts +36 -0
  255. package/src/TestLive.ts +53 -0
  256. package/src/TestServices.ts +390 -0
  257. package/src/TestSized.ts +55 -0
  258. package/src/Tracer.ts +182 -0
  259. package/src/Trie.ts +840 -0
  260. package/src/Tuple.ts +305 -0
  261. package/src/Types.ts +353 -0
  262. package/src/Unify.ts +113 -0
  263. package/src/UpstreamPullRequest.ts +117 -0
  264. package/src/UpstreamPullStrategy.ts +121 -0
  265. package/src/Utils.ts +809 -0
  266. package/src/index.ts +1568 -0
  267. package/src/internal/array.ts +8 -0
  268. package/src/internal/blockedRequests.ts +520 -0
  269. package/src/internal/cache.ts +733 -0
  270. package/src/internal/cause.ts +1050 -0
  271. package/src/internal/channel/channelExecutor.ts +1200 -0
  272. package/src/internal/channel/channelState.ts +134 -0
  273. package/src/internal/channel/childExecutorDecision.ts +96 -0
  274. package/src/internal/channel/continuation.ts +200 -0
  275. package/src/internal/channel/mergeDecision.ts +113 -0
  276. package/src/internal/channel/mergeState.ts +120 -0
  277. package/src/internal/channel/mergeStrategy.ts +72 -0
  278. package/src/internal/channel/singleProducerAsyncInput.ts +259 -0
  279. package/src/internal/channel/subexecutor.ts +229 -0
  280. package/src/internal/channel/upstreamPullRequest.ts +84 -0
  281. package/src/internal/channel/upstreamPullStrategy.ts +87 -0
  282. package/src/internal/channel.ts +2603 -0
  283. package/src/internal/clock.ts +95 -0
  284. package/src/internal/completedRequestMap.ts +9 -0
  285. package/src/internal/concurrency.ts +54 -0
  286. package/src/internal/config.ts +716 -0
  287. package/src/internal/configError.ts +304 -0
  288. package/src/internal/configProvider/pathPatch.ts +97 -0
  289. package/src/internal/configProvider.ts +799 -0
  290. package/src/internal/console.ts +153 -0
  291. package/src/internal/context.ts +337 -0
  292. package/src/internal/core-effect.ts +2293 -0
  293. package/src/internal/core-stream.ts +998 -0
  294. package/src/internal/core.ts +3189 -0
  295. package/src/internal/data.ts +36 -0
  296. package/src/internal/dataSource.ts +327 -0
  297. package/src/internal/dateTime.ts +1277 -0
  298. package/src/internal/defaultServices/console.ts +100 -0
  299. package/src/internal/defaultServices.ts +163 -0
  300. package/src/internal/deferred.ts +46 -0
  301. package/src/internal/differ/chunkPatch.ts +211 -0
  302. package/src/internal/differ/contextPatch.ts +232 -0
  303. package/src/internal/differ/hashMapPatch.ts +220 -0
  304. package/src/internal/differ/hashSetPatch.ts +176 -0
  305. package/src/internal/differ/orPatch.ts +311 -0
  306. package/src/internal/differ/readonlyArrayPatch.ts +210 -0
  307. package/src/internal/differ.ts +200 -0
  308. package/src/internal/doNotation.ts +80 -0
  309. package/src/internal/effect/circular.ts +895 -0
  310. package/src/internal/effectable.ts +131 -0
  311. package/src/internal/either.ts +110 -0
  312. package/src/internal/encoding/base64.ts +286 -0
  313. package/src/internal/encoding/base64Url.ts +29 -0
  314. package/src/internal/encoding/common.ts +51 -0
  315. package/src/internal/encoding/hex.ts +315 -0
  316. package/src/internal/errors.ts +7 -0
  317. package/src/internal/executionPlan.ts +114 -0
  318. package/src/internal/executionStrategy.ts +74 -0
  319. package/src/internal/fiber.ts +388 -0
  320. package/src/internal/fiberId.ts +267 -0
  321. package/src/internal/fiberMessage.ts +82 -0
  322. package/src/internal/fiberRefs/patch.ts +144 -0
  323. package/src/internal/fiberRefs.ts +297 -0
  324. package/src/internal/fiberRuntime.ts +3842 -0
  325. package/src/internal/fiberScope.ts +71 -0
  326. package/src/internal/fiberStatus.ts +119 -0
  327. package/src/internal/groupBy.ts +530 -0
  328. package/src/internal/hashMap/array.ts +49 -0
  329. package/src/internal/hashMap/bitwise.ts +32 -0
  330. package/src/internal/hashMap/config.ts +14 -0
  331. package/src/internal/hashMap/keySet.ts +8 -0
  332. package/src/internal/hashMap/node.ts +391 -0
  333. package/src/internal/hashMap.ts +586 -0
  334. package/src/internal/hashSet.ts +323 -0
  335. package/src/internal/keyedPool.ts +244 -0
  336. package/src/internal/layer/circular.ts +214 -0
  337. package/src/internal/layer.ts +1483 -0
  338. package/src/internal/logSpan.ts +20 -0
  339. package/src/internal/logger-circular.ts +24 -0
  340. package/src/internal/logger.ts +522 -0
  341. package/src/internal/mailbox.ts +561 -0
  342. package/src/internal/managedRuntime/circular.ts +6 -0
  343. package/src/internal/managedRuntime.ts +134 -0
  344. package/src/internal/matcher.ts +652 -0
  345. package/src/internal/metric/boundaries.ts +75 -0
  346. package/src/internal/metric/hook.ts +483 -0
  347. package/src/internal/metric/key.ts +167 -0
  348. package/src/internal/metric/keyType.ts +238 -0
  349. package/src/internal/metric/label.ts +41 -0
  350. package/src/internal/metric/pair.ts +48 -0
  351. package/src/internal/metric/polling.ts +149 -0
  352. package/src/internal/metric/registry.ts +187 -0
  353. package/src/internal/metric/state.ts +290 -0
  354. package/src/internal/metric.ts +577 -0
  355. package/src/internal/opCodes/cause.ts +35 -0
  356. package/src/internal/opCodes/channel.ts +83 -0
  357. package/src/internal/opCodes/channelChildExecutorDecision.ts +17 -0
  358. package/src/internal/opCodes/channelMergeDecision.ts +11 -0
  359. package/src/internal/opCodes/channelMergeState.ts +17 -0
  360. package/src/internal/opCodes/channelMergeStrategy.ts +11 -0
  361. package/src/internal/opCodes/channelState.ts +23 -0
  362. package/src/internal/opCodes/channelUpstreamPullRequest.ts +11 -0
  363. package/src/internal/opCodes/channelUpstreamPullStrategy.ts +11 -0
  364. package/src/internal/opCodes/config.ts +65 -0
  365. package/src/internal/opCodes/configError.ts +35 -0
  366. package/src/internal/opCodes/continuation.ts +11 -0
  367. package/src/internal/opCodes/deferred.ts +11 -0
  368. package/src/internal/opCodes/effect.ts +89 -0
  369. package/src/internal/opCodes/layer.ts +59 -0
  370. package/src/internal/opCodes/streamHaltStrategy.ts +23 -0
  371. package/src/internal/option.ts +80 -0
  372. package/src/internal/pool.ts +432 -0
  373. package/src/internal/pubsub.ts +1762 -0
  374. package/src/internal/query.ts +204 -0
  375. package/src/internal/queue.ts +766 -0
  376. package/src/internal/random.ts +161 -0
  377. package/src/internal/rateLimiter.ts +93 -0
  378. package/src/internal/rcMap.ts +285 -0
  379. package/src/internal/rcRef.ts +192 -0
  380. package/src/internal/redBlackTree/iterator.ts +200 -0
  381. package/src/internal/redBlackTree/node.ts +68 -0
  382. package/src/internal/redBlackTree.ts +1245 -0
  383. package/src/internal/redacted.ts +73 -0
  384. package/src/internal/ref.ts +171 -0
  385. package/src/internal/reloadable.ts +140 -0
  386. package/src/internal/request.ts +177 -0
  387. package/src/internal/resource.ts +76 -0
  388. package/src/internal/ringBuffer.ts +68 -0
  389. package/src/internal/runtime.ts +558 -0
  390. package/src/internal/runtimeFlags.ts +178 -0
  391. package/src/internal/runtimeFlagsPatch.ts +103 -0
  392. package/src/internal/schedule/decision.ts +47 -0
  393. package/src/internal/schedule/interval.ts +101 -0
  394. package/src/internal/schedule/intervals.ts +180 -0
  395. package/src/internal/schedule.ts +2199 -0
  396. package/src/internal/schema/errors.ts +191 -0
  397. package/src/internal/schema/schemaId.ts +106 -0
  398. package/src/internal/schema/util.ts +50 -0
  399. package/src/internal/scopedCache.ts +644 -0
  400. package/src/internal/scopedRef.ts +118 -0
  401. package/src/internal/secret.ts +89 -0
  402. package/src/internal/singleShotGen.ts +35 -0
  403. package/src/internal/sink.ts +2120 -0
  404. package/src/internal/stack.ts +10 -0
  405. package/src/internal/stm/core.ts +817 -0
  406. package/src/internal/stm/entry.ts +59 -0
  407. package/src/internal/stm/journal.ts +123 -0
  408. package/src/internal/stm/opCodes/stm.ts +71 -0
  409. package/src/internal/stm/opCodes/stmState.ts +17 -0
  410. package/src/internal/stm/opCodes/strategy.ts +17 -0
  411. package/src/internal/stm/opCodes/tExit.ts +29 -0
  412. package/src/internal/stm/opCodes/tryCommit.ts +11 -0
  413. package/src/internal/stm/stm.ts +1453 -0
  414. package/src/internal/stm/stmState.ts +136 -0
  415. package/src/internal/stm/tArray.ts +550 -0
  416. package/src/internal/stm/tDeferred.ts +81 -0
  417. package/src/internal/stm/tExit.ts +190 -0
  418. package/src/internal/stm/tMap.ts +824 -0
  419. package/src/internal/stm/tPriorityQueue.ts +267 -0
  420. package/src/internal/stm/tPubSub.ts +551 -0
  421. package/src/internal/stm/tQueue.ts +393 -0
  422. package/src/internal/stm/tRandom.ts +140 -0
  423. package/src/internal/stm/tReentrantLock.ts +352 -0
  424. package/src/internal/stm/tRef.ts +195 -0
  425. package/src/internal/stm/tSemaphore.ts +113 -0
  426. package/src/internal/stm/tSet.ts +259 -0
  427. package/src/internal/stm/tSubscriptionRef.ts +286 -0
  428. package/src/internal/stm/tryCommit.ts +34 -0
  429. package/src/internal/stm/txnId.ts +14 -0
  430. package/src/internal/stm/versioned.ts +4 -0
  431. package/src/internal/stream/debounceState.ts +57 -0
  432. package/src/internal/stream/emit.ts +123 -0
  433. package/src/internal/stream/haltStrategy.ts +94 -0
  434. package/src/internal/stream/handoff.ts +187 -0
  435. package/src/internal/stream/handoffSignal.ts +59 -0
  436. package/src/internal/stream/pull.ts +34 -0
  437. package/src/internal/stream/sinkEndReason.ts +30 -0
  438. package/src/internal/stream/zipAllState.ts +88 -0
  439. package/src/internal/stream/zipChunksState.ts +56 -0
  440. package/src/internal/stream.ts +8801 -0
  441. package/src/internal/string-utils.ts +107 -0
  442. package/src/internal/subscriptionRef.ts +138 -0
  443. package/src/internal/supervisor/patch.ts +190 -0
  444. package/src/internal/supervisor.ts +303 -0
  445. package/src/internal/synchronizedRef.ts +114 -0
  446. package/src/internal/take.ts +199 -0
  447. package/src/internal/testing/sleep.ts +27 -0
  448. package/src/internal/testing/suspendedWarningData.ts +85 -0
  449. package/src/internal/testing/warningData.ts +94 -0
  450. package/src/internal/tracer.ts +150 -0
  451. package/src/internal/trie.ts +722 -0
  452. package/src/internal/version.ts +7 -0
@@ -0,0 +1,1245 @@
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
+ }