@@ -79,9 +79,46 @@ type ConnDifference =
79
79
80
80
concatLines lines
81
81
82
+ type Defs =
83
+ { byScope: MMap < Scope , Def >
84
+ bySlug: MMap < ScopeSlug , Scope * Def > }
85
+
86
+ static member Empty = { byScope = MMap.empty; bySlug = MMap.empty }
87
+
88
+ member this.IsEmpty = MMap.isEmpty this.byScope
89
+
90
+ member this.AllSeq = MMap.toSeq this.byScope
91
+
92
+ member this.AllSetSeq = MMap.toSetSeq this.byScope
93
+
94
+ member this.Add ( scope : Scope , def : Def ) : Defs =
95
+ let byScope = this.byScope |> MMap.add scope def
96
+
97
+ let scopeSlug = ScopeSlug.ofScopedDef ( scope, def)
98
+
99
+ let bySlug =
100
+ scopeSlug
101
+ |> Option.map ( fun x -> this.bySlug |> MMap.add x ( scope, def))
102
+ |> Option.defaultValue this.bySlug
103
+
104
+ { byScope = byScope; bySlug = bySlug }
105
+
106
+ member this.Remove ( scope : Scope , def : Def ) : Defs =
107
+ let byScope = this.byScope |> MMap.removeValue scope def
108
+
109
+ let scopeSlug = ScopeSlug.ofScopedDef ( scope, def)
110
+
111
+ let bySlug =
112
+ scopeSlug
113
+ |> Option.map ( fun x -> this.bySlug |> MMap.removeValue x ( scope, def))
114
+ |> Option.defaultValue this.bySlug
115
+
116
+ { byScope = byScope; bySlug = bySlug }
117
+
118
+
82
119
type Conn =
83
120
{ refs: MMap < Scope , Ref >
84
- defs: MMap < Scope , Def >
121
+ defs: Defs
85
122
tags: MMap < Scope , Tag >
86
123
resolved: Graph < ScopedSym >
87
124
unresolved: Graph < Unresolved >
@@ -141,10 +178,10 @@ type Conn =
141
178
for ref in refs do
142
179
yield Indented( 4 , ref). ToString()
143
180
144
- if this.defs |> MMap.isEmpty |> not then
181
+ if not this.defs.IsEmpty then
145
182
yield " Defs:"
146
183
147
- for scope, defs in MMap.toSetSeq this.defs do
184
+ for scope, defs in this.defs.AllSetSeq do
148
185
yield $" {scope}:"
149
186
150
187
for def in defs do
@@ -182,7 +219,7 @@ module Conn =
182
219
183
220
let empty =
184
221
{ refs = MMap.empty
185
- defs = MMap.empty
222
+ defs = Defs.Empty
186
223
tags = MMap.empty
187
224
resolved = Graph.empty
188
225
unresolved = Graph.empty
@@ -191,7 +228,7 @@ module Conn =
191
228
192
229
let isSameStructure c1 c2 =
193
230
c1.refs = c2.refs
194
- && c1.defs = c2.defs
231
+ && c1.defs.byScope = c2.defs.byScope
195
232
&& c1.tags = c2.tags
196
233
&& c1.resolved = c2.resolved
197
234
&& c1.unresolved = c2.unresolved
@@ -296,7 +333,7 @@ module Conn =
296
333
| _, Sym.Def _
297
334
| _, Sym.Tag _ -> ()
298
335
299
- defs <- MMap.removeValue scope def defs
336
+ defs <- defs.Remove ( scope, def)
300
337
resolved <- Graph.removeVertexWithCallback cb scopedSym resolved
301
338
302
339
// When the doc is removed we need to remove all unresolved links within this doc's scope
@@ -326,15 +363,15 @@ module Conn =
326
363
| CrossRef( CrossDoc _)
327
364
| IntraRef _ -> ()
328
365
| Sym.Def def ->
329
- defs <- MMap.add scope def defs
366
+ defs <- defs.Add ( scope, def)
330
367
331
368
match def with
332
369
| Doc
333
370
| Header( 1 , _) ->
334
371
// Whenever a new title is added, links that were previously pointing at the Doc
335
372
// or the other titles need to be invalidated
336
373
let affectedDefs =
337
- defs
374
+ defs.byScope
338
375
|> MMap.tryFind scope
339
376
|> Option.defaultValue Set.empty
340
377
|> Seq.filter Def.isTitle
@@ -344,18 +381,11 @@ module Conn =
344
381
// Similarly, other doc/titles could resolve to the same scope group
345
382
let scopeSlug = ScopeSlug.ofScopedDef ( scope, def)
346
383
347
- // TODO: we could do this faster if the groups were maintained in a set
348
384
let affectedDefsInOtherScopes =
349
385
match scopeSlug with
350
- | None -> Seq .empty
386
+ | None -> Set .empty
351
387
| Some scopeSlug ->
352
- defs
353
- |> MMap.toSeq
354
- |> Seq.choose ( fun scopedDef ->
355
- ScopeSlug.ofScopedDef scopedDef
356
- |> Option.map ( fun slug -> scopedDef, slug))
357
- |> Seq.filter ( fun ( _ , slug ) -> slug = scopeSlug)
358
- |> Seq.map fst
388
+ MMap.tryFind scopeSlug defs.bySlug |> Option.defaultValue Set.empty
359
389
360
390
let affectedDefs =
361
391
affectedDefs |> Seq.append affectedDefsInOtherScopes |> Set.ofSeq
@@ -470,7 +500,7 @@ module Conn =
470
500
471
501
let difference c1 c2 : ConnDifference =
472
502
{ refsDifference = MMap.difference c1.refs c2.refs
473
- defsDifference = MMap.difference c1.defs c2.defs
503
+ defsDifference = MMap.difference c1.defs.byScope c2.defs.byScope
474
504
tagsDifference = MMap.difference c1.tags c2.tags
475
505
resolvedDifference = Graph.difference c1.resolved c2.resolved
476
506
unresolvedDifference = Graph.difference c1.unresolved c2.unresolved
0 commit comments