Skip to content

Implement a custom Data.Iterator #3849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ set(SWIFT_BENCH_MODULES
single-source/CaptureProp
single-source/Chars
single-source/ClassArrayGetter
single-source/Data
single-source/DeadArray
single-source/DictionaryBridge
single-source/DictionaryLiteral
Expand Down
34 changes: 34 additions & 0 deletions benchmark/single-source/Data.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===--- Data.swift -------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import TestsUtils
import Foundation

@inline(never)
func generateData() -> Data {
var data = Data(count: 16 * 1024)
data.withUnsafeMutableBytes { (ptr: UnsafeMutablePointer<UInt8>) -> () in
for i in 0..<data.count {
ptr[i] = UInt8(i % 23)
}
}
return data
}

@inline(never)
public func run_IterateData(_ N: Int) {
let data = generateData()

for _ in 0...10*N {
_ = data.reduce(0, +)
}
}
2 changes: 2 additions & 0 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Calculator
import CaptureProp
import Chars
import ClassArrayGetter
import Data
import DeadArray
import DictTest
import DictTest2
Expand Down Expand Up @@ -141,6 +142,7 @@ precommitTests = [
"HashTest": run_HashTest,
"Histogram": run_Histogram,
"Integrate": run_Integrate,
"IterateData": run_IterateData,
"Join": run_Join,
"LinkedList": run_LinkedList,
"MapReduce": run_MapReduce,
Expand Down
35 changes: 34 additions & 1 deletion stdlib/public/SDK/Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,40 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
///
/// The iterator will increment byte-by-byte.
public func makeIterator() -> Data.Iterator {
return IndexingIterator(_elements: self)
return Iterator(_data: self)
}

public struct Iterator : IteratorProtocol {
private let _data: Data
private var _buffer: (
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8,
UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8)
private var _idx: Data.Index
private let _endIdx: Data.Index

fileprivate init(_data: Data) {
self._data = _data
_buffer = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
_idx = 0
_endIdx = _data.endIndex
}

public mutating func next() -> UInt8? {
guard _idx < _endIdx else { return nil }
defer { _idx += 1 }
let bufferSize = sizeofValue(_buffer)
return withUnsafeMutablePointer(to: &_buffer) { ptr_ in
let ptr = UnsafeMutableRawPointer(ptr_).assumingMemoryBound(to: UInt8.self)
let bufferIdx = _idx % bufferSize
if bufferIdx == 0 {
// populate the buffer
_data.copyBytes(to: ptr, from: _idx..<(_endIdx - _idx > bufferSize ? _idx + bufferSize : _endIdx))
}
return ptr[bufferIdx]
}
}
}

// MARK: -
Expand Down
32 changes: 32 additions & 0 deletions validation-test/stdlib/Data.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
// REQUIRES: objc_interop

import StdlibUnittest
import StdlibCollectionUnittest
import Foundation

var DataTestSuite = TestSuite("Data")
DataTestSuite.test("Data.Iterator semantics") {
// Empty data
checkSequence([], Data())

// Small data
checkSequence([1,2,4,8,16], Data(bytes: [1,2,4,8,16]))

// Boundary conditions
checkSequence([5], Data(bytes: [5]))
checkSequence(1...31, Data(bytes: Array(1...31)))
checkSequence(1...32, Data(bytes: Array(1...32)))
checkSequence(1...33, Data(bytes: Array(1...33)))

// Large data
var data = Data(count: 65535)
data.withUnsafeMutableBytes { (ptr: UnsafeMutablePointer<UInt8>) -> () in
for i in 0..<data.count {
ptr[i] = UInt8(i % 23)
}
}
checkSequence((0..<65535).lazy.map({ UInt8($0 % 23) }), data)
}
runAllTests()