Optimize [Data].flatCount

This commit is contained in:
Davide De Rosa 2019-06-05 14:14:15 +02:00
parent a861c69f36
commit eb56a9a56c
2 changed files with 11 additions and 1 deletions

View File

@ -203,7 +203,7 @@ extension Data {
extension Array where Element == Data { extension Array where Element == Data {
var flatCount: Int { var flatCount: Int {
return map { $0.count }.reduce(0) { $0 + $1 } return reduce(0) { $0 + $1.count }
} }
} }

View File

@ -72,4 +72,14 @@ class DataManipulationTests: XCTestCase {
XCTAssertEqual(z2.toData(), Data(hex: "5678ab")) XCTAssertEqual(z2.toData(), Data(hex: "5678ab"))
XCTAssertEqual(z3.toData(), Data(hex: "5678abaaddcc")) XCTAssertEqual(z3.toData(), Data(hex: "5678abaaddcc"))
} }
func testFlatCount() {
var v: [Data] = []
v.append(Data(hex: "11223344"))
v.append(Data(hex: "1122"))
v.append(Data(hex: "1122334455"))
v.append(Data(hex: "11223344556677"))
v.append(Data(hex: "112233"))
XCTAssertEqual(v.flatCount, 21)
}
} }