-
Notifications
You must be signed in to change notification settings - Fork 20
/
CRDTCausalTreesWeave.swift
1321 lines (1143 loc) · 44 KB
/
CRDTCausalTreesWeave.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// CRDTCausalTrees.swift
// CRDTPlayground
//
// Created by Alexei Baboulevitch on 2017-8-28.
// Copyright © 2017 Alexei Baboulevitch. All rights reserved.
//
import Foundation
// AB: there are methods marked internal here which are in practice public since this class isn't packaged in a
// framework; unfortunately, using a framework comes with a performance penalty, so there seems to be no way around this
//////////////////
// MARK: -
// MARK: - Weave -
// MARK: -
//////////////////
// an ordered collection of atoms and their trees/yarns, for multiple sites
public final class Weave
<V: CausalTreeValueT> :
CvRDT, NSCopying, CustomDebugStringConvertible, ApproxSizeable
{
public typealias ValueT = V
public typealias AtomT = Atom<ValueT>
/////////////////
// MARK: - Data -
/////////////////
// TODO: make owner setter, to ensure that nothing breaks
public internal(set) var owner: SiteId
// CONDITION: this data must be the same locally as in the cloud, i.e. no object oriented cache layers etc.
private var atoms: ArrayType<AtomT> = [] //solid chunk of memory for optimal performance
// needed for sibling sorting
public private(set) var lamportTimestamp: CRDTCounter<YarnIndex>
///////////////////
// MARK: - Caches -
///////////////////
// these must be updated whenever the canonical data structures above are mutated; do not have to be the same on different sites
private var weft: LocalWeft = LocalWeft()
private var yarns: ArrayType<AtomT> = []
private var yarnsMap: [SiteId:CountableClosedRange<Int>] = [:]
//////////////////////
// MARK: - Lifecycle -
//////////////////////
private enum CodingKeys: String, CodingKey {
case owner
case atoms
case lamportTimestamp
}
// Complexity: O(N * log(N))
public init(owner: SiteId, weave: inout ArrayType<AtomT>, timestamp: YarnIndex)
{
self.owner = owner
self.atoms = weave //TODO: how is this weave copied?
self.lamportTimestamp = CRDTCounter<YarnIndex>(withValue: timestamp)
generateCacheBySortingAtoms()
}
public convenience init(from decoder: Decoder) throws
{
let values = try decoder.container(keyedBy: CodingKeys.self)
let owner = try values.decode(SiteId.self, forKey: .owner)
var atoms = try values.decode(ArrayType<AtomT>.self, forKey: .atoms)
let timestamp = try values.decode(CRDTCounter<YarnIndex>.self, forKey: .lamportTimestamp)
self.init(owner: owner, weave: &atoms, timestamp: timestamp.counter)
}
// starting from scratch
public init(owner: SiteId)
{
self.owner = owner
self.lamportTimestamp = CRDTCounter<YarnIndex>(withValue: 0)
addBaseYarn: do
{
let siteId = ControlSite
let startAtomId = AtomId(site: siteId, index: 0)
let startAtom = AtomT(id: startAtomId, cause: startAtomId, timestamp: lamportTimestamp.increment(), value: ValueT())
atoms.append(startAtom)
updateCaches(withAtom: startAtom)
assert(atomWeaveIndex(startAtomId) == startAtomId.index)
}
}
public func copy(with zone: NSZone? = nil) -> Any
{
let returnWeave = Weave(owner: self.owner)
// TODO: verify that these structs do copy as expected
returnWeave.owner = self.owner
returnWeave.atoms = self.atoms
returnWeave.weft = self.weft
returnWeave.yarns = self.yarns
returnWeave.yarnsMap = self.yarnsMap
returnWeave.lamportTimestamp = self.lamportTimestamp.copy() as! CRDTCounter<YarnIndex>
return returnWeave
}
/////////////////////
// MARK: - Mutation -
/////////////////////
public func addAtom(withValue value: ValueT, causedBy cause: AtomId) -> (AtomId, WeaveIndex)?
{
return _debugAddAtom(atSite: self.owner, withValue: value, causedBy: cause)
}
// TODO: rename, make moduleprivate
public func _debugAddAtom(atSite: SiteId, withValue value: ValueT, causedBy cause: AtomId) -> (AtomId, WeaveIndex)?
{
let atom = Atom(id: generateNextAtomId(forSite: atSite), cause: cause, timestamp: lamportTimestamp.increment(), value: value)
if let e = integrateAtom(atom)
{
return (atom.id, e)
}
else
{
return nil
}
}
// adds awareness atom, usually prior to another add to ensure convergent sibling conflict resolution
// AB: no-op because we use Lamports now
public func addCommit(fromSite: SiteId, toSite: SiteId, atTime time: Clock) -> (AtomId, WeaveIndex)? { return nil }
// Complexity: O(N)
private func updateCaches(withAtom atom: AtomT)
{
if let existingRange = yarnsMap[atom.site]
{
assert(existingRange.count == atom.id.index, "adding atom out of order")
let newUpperBound = existingRange.upperBound + 1
yarns.insert(atom, at: newUpperBound)
yarnsMap[atom.site] = existingRange.lowerBound...newUpperBound
for (site,range) in yarnsMap
{
if range.lowerBound >= newUpperBound
{
yarnsMap[site] = (range.lowerBound + 1)...(range.upperBound + 1)
}
}
weft.update(atom: atom.id)
}
else
{
assert(atom.id.index == 0, "adding atom out of order")
yarns.append(atom)
yarnsMap[atom.site] = (yarns.count - 1)...(yarns.count - 1)
weft.update(atom: atom.id)
}
assertCacheIntegrity()
}
// TODO: combine somehow with updateCaches
private func generateCacheBySortingAtoms()
{
generateYarns: do
{
var yarns = self.atoms
yarns.sort(by:
{ (a1: Atom, a2: Atom) -> Bool in
if a1.id.site < a2.id.site
{
return true
}
else if a1.id.site > a2.id.site
{
return false
}
else
{
return a1.id.index < a2.id.index
}
})
self.yarns = yarns
}
processYarns: do
{
timeMe({
var weft = LocalWeft()
var yarnsMap = [SiteId:CountableClosedRange<Int>]()
// PERF: we don't have to update each atom -- can simply detect change
for i in 0..<self.yarns.count
{
if let range = yarnsMap[self.yarns[i].site]
{
yarnsMap[self.yarns[i].site] = range.lowerBound...i
}
else
{
yarnsMap[self.yarns[i].site] = i...i
}
weft.update(atom: self.yarns[i].id)
}
self.weft = weft
self.yarnsMap = yarnsMap
}, "CacheGen")
}
assertCacheIntegrity()
}
// Complexity: O(1)
private func generateNextAtomId(forSite site: SiteId) -> AtomId
{
if let lastIndex = weft.mapping[site]
{
return AtomId(site: site, index: lastIndex + 1)
}
else
{
return AtomId(site: site, index: 0)
}
}
////////////////////////
// MARK: - Integration -
////////////////////////
// TODO: make a protocol that atom, value, etc. conform to
public func remapIndices(_ indices: [SiteId:SiteId])
{
func updateAtom(inArray array: inout ArrayType<AtomT>, atIndex i: Int)
{
array[i].remapIndices(indices)
}
if let newOwner = indices[self.owner]
{
self.owner = newOwner
}
for i in 0..<self.atoms.count
{
updateAtom(inArray: &self.atoms, atIndex: i)
}
weft: do
{
var newWeft = LocalWeft()
for v in self.weft.mapping
{
if let newOwner = indices[v.key]
{
newWeft.update(site: newOwner, index: v.value)
}
else
{
newWeft.update(site: v.key, index: v.value)
}
}
self.weft = newWeft
}
for i in 0..<self.yarns.count
{
updateAtom(inArray: &self.yarns, atIndex: i)
}
yarnsMap: do
{
var newYarnsMap = [SiteId:CountableClosedRange<Int>]()
for v in self.yarnsMap
{
if let newOwner = indices[v.key]
{
newYarnsMap[newOwner] = v.value
}
else
{
newYarnsMap[v.key] = v.value
}
}
self.yarnsMap = newYarnsMap
}
assertCacheIntegrity()
}
// adds atom as firstmost child of head atom, or appends to end if non-causal; lets us treat weave like an actual tree
// Complexity: O(N)
private func integrateAtom(_ atom: AtomT) -> WeaveIndex?
{
var headIndex: Int = -1
let causeAtom = atomForId(atom.cause)
if causeAtom != nil && causeAtom!.value.childless
{
assert(false, "appending atom to non-causal parent")
return nil
}
if let aIndex = atomWeaveIndex(atom.cause, searchInReverse: true)
{
headIndex = Int(aIndex)
// safety check 1
if headIndex < atoms.count
{
let prevAtom = atoms[headIndex]
assert(atom.cause == prevAtom.id, "atom is not attached to the correct parent")
}
// resolve priority ordering
if !(atom.value.priority != 0) && (headIndex + 1) < atoms.count
{
let nextAtom = atoms[headIndex + 1]
if nextAtom.cause == atom.cause && (nextAtom.value.priority != 0)
{
// PERF: an unusual case: if we add a child atom to an atom that has priority children (usually
// deletes), then we need to find the last priority child that we can insert our new atom after;
// unfortunately, unlike the default case, this requires some O(N) operations
guard let cb = causalBlock(forAtomIndexInWeave: WeaveIndex(headIndex)) else
{
assert(false, "sibling is priority but could not get causal block")
return nil
}
for i in (cb.lowerBound + 1)...cb.upperBound
{
let a = atoms[Int(i)]
if a.cause == atom.cause && !(a.value.priority != 0)
{
break
}
headIndex = Int(i)
}
}
}
// safety check 2
if headIndex + 1 < atoms.count
{
let nextAtom = atoms[headIndex + 1]
if nextAtom.cause == atom.cause //siblings
{
assert(Weave.atomSiblingOrder(a1: atom, a2: nextAtom), "atom is not ordered correctly")
}
}
}
else
{
assert(false, "could not determine location of causing atom")
return nil
}
// no awareness recalculation, just assume it belongs in front
atoms.insert(atom, at: headIndex + 1)
updateCaches(withAtom: atom)
return WeaveIndex(headIndex + 1)
}
public enum MergeError
{
case invalidAwareSiblingComparison
case invalidUnawareSiblingComparison
case unknownSiblingComparison
case unknownTypeComparison
}
// we assume that indices have been correctly remapped at this point
// we also assume that remote weave was correctly generated and isn't somehow corrupted
// IMPORTANT: this function should only be called with a validated weave, because we do not check consistency here
// PERF: don't need to generate entire weave + caches
// PERF: TODO: this is currently O(W * c) (or maybe not???) and requires trusted peers; with lamport, we can do it in O(W * log(W)) and simultaneously verify + simplify our yarn algorithm
// TODO: refactor, "basic" no longer needed since Lamport comparison is fast
public func integrate(_ v: inout Weave<ValueT>)
{
typealias Insertion = (localIndex: WeaveIndex, remoteRange: CountableClosedRange<Int>)
//#if DEBUG
// let debugCopy = self.copy() as! Weave
// let remoteCopy = v.copy() as! Weave
//#endif
// in order of traversal, so make sure to iterate backwards when actually mutating the weave to keep indices correct
var insertions: [Insertion] = []
var newAtoms: [AtomT] = []
newAtoms.reserveCapacity(self.atoms.capacity)
let local = weave()
let remote = v.weave()
let localWeft = currentWeft()
let remoteWeft = v.currentWeft()
var i = local.startIndex
var j = remote.startIndex
// instead of inserting atoms one-by-one -- an O(N) operation -- we accumulate change ranges and process
// them later; one of these functions is called with each atom
// TODO: get rid of this, no longer used
var currentInsertion: Insertion?
func insertAtom(atLocalIndex: WeaveIndex, fromRemoteIndex: WeaveIndex)
{
if let insertion = currentInsertion
{
assert(fromRemoteIndex == insertion.remoteRange.upperBound + 1, "skipped some atoms without committing")
currentInsertion = (insertion.localIndex, insertion.remoteRange.lowerBound...Int(fromRemoteIndex))
}
else
{
currentInsertion = (atLocalIndex, Int(fromRemoteIndex)...Int(fromRemoteIndex))
}
}
func commitInsertion()
{
if let insertion = currentInsertion
{
insertions.append(insertion)
currentInsertion = nil
}
}
func commitLocal()
{
//commitInsertion()
newAtoms.append(local[i])
i += 1
}
func commitRemote()
{
//insertAtom(atLocalIndex: WeaveIndex(i), fromRemoteIndex: WeaveIndex(j))
newAtoms.append(remote[j])
j += 1
}
func commitBoth()
{
//commitInsertion()
newAtoms.append(local[i])
i += 1
j += 1
}
// here be the actual merge algorithm
while (i < local.endIndex || j < remote.endIndex) {
var mergeError: MergeError? = nil
// past local bounds, so just append remote
if i >= local.endIndex
{
commitRemote()
}
else if j >= remote.endIndex
{
commitLocal()
}
else if let comparison = try? atomArbitraryOrder(a1: local[i], a2: remote[j], basicOnly: true)
{
if comparison == .orderedAscending
{
commitLocal()
}
else if comparison == .orderedDescending
{
commitRemote()
}
else
{
commitBoth()
}
}
// assuming local weave is valid, we can just insert our local changes; relies on trust
else if localWeft.included(remote[j].id)
{
// local < remote, fast forward through to the next matching sibling
// AB: this and the below block would be more "correct" with causal blocks, but those
// require O(weave) operations; this is functionally equivalent since we know
// that one is aware of the other, so we have to reach the other one eventually
// (barring corruption)
repeat {
commitLocal()
} while local[i].id != remote[j].id
}
// assuming remote weave is valid, we can just insert remote's changes; relies on trust
else if remoteWeft.included(local[i].id)
{
// remote < local, fast forward through to the next matching sibling
repeat {
commitRemote()
} while local[i].id != remote[j].id
}
// testing for unaware atoms merge
// PERF: causal block generation is O(N)... what happens if lots of concurrent changes?
// PERF: TODO: in the case of non-sibling priority atoms conflicting with non-priority atoms, perf will be O(N),
// can fix by precalculating weave indices for all atoms in O(N); this is only applicable in the edgiest of edge
// cases where the number of those types of conflicts is more than one or two in a merge (super rare)
else if
let comparison = try? atomArbitraryOrder(a1: local[i], a2: remote[j], basicOnly: false),
let localCausalBlock = causalBlock(forAtomIndexInWeave: WeaveIndex(i)),
let remoteCausalBlock = v.causalBlock(forAtomIndexInWeave: WeaveIndex(j))
{
if comparison == .orderedAscending
{
for _ in 0..<localCausalBlock.count
{
commitLocal()
}
}
else
{
for _ in 0..<remoteCausalBlock.count
{
commitRemote()
}
}
}
else
{
mergeError = .unknownTypeComparison
}
// this should never happen in theory, but in practice... let's not trust our algorithms too much
if let error = mergeError
{
//#if DEBUG
// print("Tree 1 \(debugCopy.atomsDescription)")
// print("Tree 2 \(remoteCopy.atomsDescription)")
// print("Stopped at \(i),\(j)")
//#endif
assert(false, "atoms unequal, unaware, and not comparable -- cannot merge (\(error))")
// TODO: return false here
}
}
commitInsertion() //TODO: maybe avoid commit and just start new range when disjoint interval?
process: do
{
//// we go in reverse to avoid having to update our indices
//for i in (0..<insertions.count).reversed()
//{
// let remoteContent = remote[insertions[i].remoteRange]
// atoms.insert(contentsOf: remoteContent, at: Int(insertions[i].localIndex))
//}
//updateCaches(afterMergeWithWeave: v)
self.atoms = newAtoms
generateCacheBySortingAtoms()
lamportTimestamp.integrate(&v.lamportTimestamp)
}
}
public enum ValidationError: Error
{
case noAtoms
case noSites
case causalityViolation
case atomUnawareOfParent
case atomUnawareOfReference
case childlessAtomHasChildren
case treeAtomIsUnparented
case incorrectTreeAtomOrder
case likelyCorruption
}
// a quick check of the invariants, so that (for example) malicious users couldn't corrupt our data
// prerequisite: we assume that the yarn cache was successfully generated
// assuming a reasonable (~log(N)) number of sites, O(N*log(N)) at worst, and O(N) for typical use
public func validate() throws -> Bool
{
func vassert(_ b: Bool, _ e: ValidationError) throws
{
if !b
{
throw e
}
}
// sanity check, since we rely on yarns being correct for the rest of this method
try vassert(atoms.count == yarns.count, .likelyCorruption)
let sitesCount = Int(yarnsMap.keys.max() ?? 0) + 1
let atomsCount = atoms.count
try vassert(atomsCount >= 1, .noAtoms)
try vassert(sitesCount >= 1, .noSites)
validate: do
{
var lastAtomChild = ContiguousArray<Int>(repeating: -1, count: atomsCount)
var i = 0
checkTree: do
{
while i < atoms.count
{
let atom = atoms[i]
guard let a = atomYarnsIndex(atom.id) else
{
try vassert(false, .likelyCorruption); return false
}
guard let c = atomYarnsIndex(atom.cause) else
{
try vassert(false, .treeAtomIsUnparented); return false
}
let cause = yarns[Int(c)]
let r = atomYarnsIndex((atom as? CRDTValueReference)?.reference ?? NullAtomId)
atomChecking: do
{
try vassert(!cause.value.childless, .childlessAtomHasChildren)
}
causalityProcessing: do
{
if a != 0
{
try vassert(atom.timestamp > yarns[Int(c)].timestamp, .atomUnawareOfParent)
}
if let aR = r
{
try vassert(atom.timestamp > yarns[Int(aR)].timestamp, .atomUnawareOfReference)
}
}
childrenOrderChecking: if a != 0
{
if lastAtomChild[Int(c)] == -1
{
lastAtomChild[Int(c)] = Int(a)
}
else
{
let lastChild = yarns[Int(lastAtomChild[Int(c)])]
let order = Weave.atomSiblingOrder(a1: lastChild, a2: atom)
try vassert(order, .incorrectTreeAtomOrder)
}
}
i += 1
}
}
return try lamportTimestamp.validate()
}
}
private func assertCacheIntegrity()
{
#if DEBUG
assert(atoms.count == yarns.count, "length mismatch between atoms and yarns")
assert(yarnsMap.count == weft.mapping.count, "length mismatch between yarns map count and weft site count")
verifyYarnMapCoverage: do
{
let sortedYarnMap = yarnsMap.sorted { v0,v1 -> Bool in return v0.value.upperBound < v1.value.lowerBound }
let totalCount = sortedYarnMap.last!.value.upperBound - sortedYarnMap.first!.value.lowerBound + 1
assert(totalCount == yarns.count, "yarns and yarns map count do not match")
for i in 0..<sortedYarnMap.count
{
if i != 0
{
assert(sortedYarnMap[i].value.lowerBound == sortedYarnMap[i - 1].value.upperBound + 1, "yarn map is not contiguous")
}
}
}
var visitedArray = Array<Bool>(repeating: false, count: atoms.count)
var visitedSites = Set<SiteId>()
for i in 0..<atoms.count
{
guard let index = atomYarnsIndex(atoms[i].id) else
{
assert(false, "atom not found in yarns")
}
assert(atoms[i].id == yarns[Int(index)].id, "weave atom does not match yarn atom")
visitedArray[Int(index)] = true
visitedSites.insert(atoms[i].id.site)
}
assert(visitedArray.reduce(true) { soFar,val in soFar && val }, "some atoms were not visited")
assert(Set<SiteId>(weft.mapping.keys) == visitedSites, "weft does not have same sites as yarns")
#endif
}
//////////////////////
// MARK: - Iteration -
//////////////////////
// A struct that lets us treat the weave, its yarns, or any past revisions as an array. In some cases, this
// object will generate a cache of indices in O(weave). If a weave is mutated from under a slice, the slice
// will become invalid and will have to be revalidated. The slice must not persist past the weave lifecycle.
// TODO: there should really be a way to initialize this with an absolute weft, while still allowing the
// weave to use this structure internally
// TODO: need to figure out how to treat local/absolute units in a properly functional/protocol-oriented way
public struct AtomsSlice: RandomAccessCollection
{
private enum Mode
{
case weave(weft: LocalWeft?)
case yarn(site: SiteId, weft: LocalWeft?)
var hasWeft: Bool {
switch self {
case .weave(let weft):
return weft != nil
case .yarn(_, let weft):
return weft != nil
}
}
var requiresGeneratedIndices: Bool
{
switch self {
case .weave(_):
return true
case .yarn(_, _):
return false
}
}
}
private unowned let fullWeave: Weave
private let mode: Mode
private var generatedIndices: ContiguousArray<Int>? = nil //only used for case weave with weft
private var startingWeft: LocalWeft? = nil //used for invalidation
// if a weft is present, indices will be generated
init(withWeave weave: Weave, weft: LocalWeft?)
{
self.fullWeave = weave
self.mode = .weave(weft: weft)
generateCache(force: true)
}
init(withWeave weave: Weave, site: SiteId, weft: LocalWeft?)
{
self.fullWeave = weave
self.mode = .yarn(site: site, weft: weft)
generateCache(force: true)
}
// (re)generate indices, if needed
// TODO: once we have absolute wefts, we can expose this for outside callers
mutating private func generateCache(force: Bool = false)
{
if !force && !invalid
{
return
}
if case .weave(let maybeWeft) = self.mode, let weft = maybeWeft
{
if self.generatedIndices != nil
{
self.generatedIndices!.removeAll()
}
else
{
self.generatedIndices = ContiguousArray<Int>()
}
for i in 0..<fullWeave.atoms.count
{
if weft.included(fullWeave.atoms[i].id)
{
self.generatedIndices!.append(i)
}
}
}
else
{
self.generatedIndices = nil
}
self.startingWeft = fullWeave.currentWeft()
}
// we can't regenerate the indices for a struct, but we can let users know when to scrap it
public var invalid: Bool
{
// AB: even though this weft does not contain 0-atom sites, we can still check for equality: if sites
// are shifted on merge, there is no way two wefts pre- and post- merge will be equal
if fullWeave.currentWeft() != self.startingWeft
{
return true
}
return false
}
public var startIndex: Int
{
assert(!invalid, "weave was mutated")
return 0
}
public var endIndex: Int
{
assert(!invalid, "weave was mutated")
switch self.mode
{
case .weave(let weft):
if weft != nil
{
return generatedIndices!.count
}
else
{
return fullWeave.atoms.count
}
case .yarn(let site, let weft):
let yarnIndex = fullWeave.currentWeft().mapping[site] ?? -1
if let targetWeft = weft
{
let targetIndex = targetWeft.mapping[site] ?? -1
return Int(Swift.min(yarnIndex, targetIndex) + 1)
}
else
{
return Int(yarnIndex + 1)
}
}
}
public func index(after i: Int) -> Int
{
assert(!invalid, "weave was mutated")
assert(i < self.endIndex, "index not less than end index")
return i + 1
}
public func index(before i: Int) -> Int
{
assert(!invalid, "weave was mutated")
assert(i > self.startIndex, "index not greater than start index")
return i - 1
}
public subscript(position: Int) -> AtomT
{
assert(!invalid, "weave was mutated")
assert(position >= self.startIndex && position < self.endIndex, "index out of range")
switch self.mode
{
case .weave(let weft):
if weft != nil
{
return fullWeave.atoms[generatedIndices![position]]
}
else
{
return fullWeave.atoms[position]
}
case .yarn(let site, _):
let yarnSlice = fullWeave.yarns[fullWeave.yarnsMap[site]!]
return yarnSlice[yarnSlice.startIndex + position]
}
}
}
public func weave(withWeft weft: LocalWeft? = nil) -> AtomsSlice
{
return AtomsSlice(withWeave: self, weft: weft)
}
public func yarn(forSite site:SiteId, withWeft weft: LocalWeft? = nil) -> AtomsSlice
{
return AtomsSlice(withWeave: self, site: site, weft: weft)
}
//////////////////////////
// MARK: - Basic Queries -
//////////////////////////
// Complexity: O(1)
public func atomForId(_ atomId: AtomId) -> AtomT?
{
if let index = atomYarnsIndex(atomId)
{
return yarns[Int(index)]
}
else
{
return nil
}
}
// Complexity: O(1)
public func atomYarnsIndex(_ atomId: AtomId) -> AllYarnsIndex?
{
if atomId == NullAtomId
{
return nil
}
if let range = yarnsMap[atomId.site]
{
let count = (range.upperBound - range.lowerBound) + 1
if atomId.index >= 0 && atomId.index < count
{
return AllYarnsIndex(range.lowerBound + Int(atomId.index))
}
else
{
return nil
}
}
else
{
return nil
}
}
// Complexity: O(N)
public func atomWeaveIndex(_ atomId: AtomId, searchInReverse: Bool = false) -> WeaveIndex?
{
if atomId == NullAtomId
{
return nil
}
if atoms.count == 0
{
return nil
}
var index: Int? = nil
for i in stride(from: (searchInReverse ? atoms.count - 1 : 0), through: (searchInReverse ? 0 : atoms.count - 1), by: (searchInReverse ? -1 : 1))
{
let atom = atoms[i]
if atom.id == atomId
{
index = i
break
}
}
return (index != nil ? WeaveIndex(index!) : nil)
}
// Complexity: O(1)
public func lastSiteAtomYarnsIndex(_ site: SiteId) -> AllYarnsIndex?
{
if let range = yarnsMap[site]
{
return AllYarnsIndex(range.upperBound)
}
else
{
return nil
}
}
// Complexity: O(N)
public func lastSiteAtomWeaveIndex(_ site: SiteId) -> WeaveIndex?
{
var maxIndex: Int? = nil
for i in 0..<atoms.count
{
let a = atoms[i]
if a.id.site == site
{
if let aMaxIndex = maxIndex
{