return buffer.startIndex } public var endIndex: Int { return buffer.endIndex } public func index(after i: Int) -> Int { return i + 1 } public subscript(position: Int) -> Element { return buffer[position].element } }
elements.startIndex } public var endIndex: Int { return elements.endIndex } public subscript(index: Int) -> Element { return elements[index] } public func makeIterator() -> ContiguousArray<Element>.Iterator { return elements.makeIterator() } } σϑΥϧτͰ࣮͞ΕΔIndexingIterator<Bag>෦Ͱɺؔݺͼग़͠ʹίε τ͕͔͔Δ߹͕͋ΔͨΊContiguousArray<Element>.IteratorΛެ։͢Δ @_versioned, @_inlineable͕privateଐੑͰͳ͘ͳΕΑΓదʹ࠷దԽ͞Ε Δ࣮͕Ͱ͖Δ͕... extension Bag0: RandomAccessCollection { public var startIndex: Int { return buffer.startIndex } public var endIndex: Int { return buffer.endIndex } public func index(after i: Int) -> Int { return i + 1 } public subscript(position: Int) -> Element { return buffer[position].element } }
let count = 1000_000 var bag = Bag<Object>() for _ in 0..<count { let object = Object() bag.add(object) } measure { var trash = [Object]() trash.reserveCapacity(count) for object in bag { trash.append(object) } XCTAssertEqual(trash.count, count) } }
= Bag0 let count = 10_000 measure { var bag = Bag<Object>() var keys = [Bag<Object>.Key]() for _ in 0..<count { let object = Object() let key = bag.add(object) keys.append(key) } var trash = [Object]() trash.reserveCapacity(count) for object in bag { trash.append(object) } for key in keys { bag.remove(for: key) } XCTAssertEqual(trash.count, count) XCTAssertEqual(bag.count, 0) } }