@@ -478,3 +478,64 @@ extension PA {
478
478
useP ( p. 1 ( 5 ) )
479
479
}
480
480
}
481
+
482
+ public struct Foo {
483
+ var id : Int = 0
484
+ var p : Int64 = 1
485
+ }
486
+
487
+ struct Test : RandomAccessCollection {
488
+ struct Index : Comparable , Hashable {
489
+ var identifier : AnyHashable ?
490
+ var offset : Int
491
+
492
+ static func < ( lhs: Index , rhs: Index ) -> Bool {
493
+ return lhs. offset < rhs. offset
494
+ }
495
+
496
+ func hash( into hasher: inout Hasher ) {
497
+ hasher. combine ( identifier)
498
+ hasher. combine ( offset)
499
+ }
500
+ }
501
+
502
+ let foos : [ Foo ]
503
+ let ids : [ AnyHashable ]
504
+
505
+ init ( foos: [ Foo ] ) {
506
+ self . foos = foos
507
+ self . ids = foos. map { $0. id }
508
+ }
509
+
510
+ func _index( atOffset n: Int ) -> Index {
511
+ return Index ( identifier: ids. isEmpty ? nil : ids [ n] , offset: n)
512
+ }
513
+
514
+ var startIndex : Index {
515
+ return _index ( atOffset: 0 )
516
+ }
517
+
518
+ var endIndex : Index {
519
+ return Index ( identifier: nil , offset: ids. endIndex)
520
+ }
521
+
522
+ func index( after i: Index ) -> Index {
523
+ return _index ( atOffset: i. offset + 1 )
524
+ }
525
+
526
+ func index( before i: Index ) -> Index {
527
+ return _index ( atOffset: i. offset - 1 )
528
+ }
529
+
530
+ func distance( from start: Index , to end: Index ) -> Int {
531
+ return end. offset - start. offset
532
+ }
533
+
534
+ func index( _ i: Index , offsetBy n: Int ) -> Index {
535
+ return _index ( atOffset: i. offset + n)
536
+ }
537
+
538
+ subscript( i: Index ) -> some P {
539
+ return foos [ i. offset] . p
540
+ }
541
+ }
0 commit comments